org.el 566 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297
  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-4
  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-4"
  87. "The version number of the file org.el.")
  88. (defun org-version (&optional here)
  89. "Show the org-mode version in the echo area.
  90. With prefix arg HERE, insert it at point."
  91. (interactive "P")
  92. (let ((version (format "Org-mode version %s" org-version)))
  93. (message version)
  94. (if here
  95. (insert version))))
  96. ;;; Compatibility constants
  97. ;;; The custom variables
  98. (defgroup org nil
  99. "Outline-based notes management and organizer."
  100. :tag "Org"
  101. :group 'outlines
  102. :group 'hypermedia
  103. :group 'calendar)
  104. (defcustom org-load-hook nil
  105. "Hook that is run after org.el has been loaded."
  106. :group 'org
  107. :type 'hook)
  108. (defvar org-modules) ; defined below
  109. (defvar org-modules-loaded nil
  110. "Have the modules been loaded already?")
  111. (defun org-load-modules-maybe (&optional force)
  112. "Load all extensions listed in `org-default-extensions'."
  113. (when (or force (not org-modules-loaded))
  114. (mapc (lambda (ext)
  115. (condition-case nil (require ext)
  116. (error (message "Problems while trying to load feature `%s'" ext))))
  117. org-modules)
  118. (setq org-modules-loaded t)))
  119. (defun org-set-modules (var value)
  120. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  121. (set var value)
  122. (when (featurep 'org)
  123. (org-load-modules-maybe 'force)))
  124. (defcustom org-modules '(org-bbdb org-bibtex org-gnus org-info org-infojs org-irc org-mew org-mhe org-rmail org-vm org-wl)
  125. "Modules that should always be loaded together with org.el.
  126. If a description starts with <C>, the file is not part of emacs
  127. and loading it will require that you have downloaded and properly installed
  128. the org-mode distribution.
  129. You can also use this system to load external packages (i.e. neither Org
  130. core modules, not modules from the CONTRIB directory). Just add symbols
  131. to the end of the list. If the package is called org-xyz.e, then you need
  132. to add the symbol `xyz', and the package must have a call to
  133. (provide 'org-xyz)"
  134. :group 'org
  135. :set 'org-set-modules
  136. :type
  137. '(set :greedy t
  138. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  139. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  140. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  141. (const :tag " info: Links to Info nodes" org-info)
  142. (const :tag " infojs: Set up Sebastian Rose's JavaScript org-info.js" org-infojs)
  143. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  144. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  145. (const :tag " mew Links to Mew folders/messages" org-mew)
  146. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  147. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  148. (const :tag " vm: Links to VM folders/messages" org-vm)
  149. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  150. (const :tag " mouse: Additional mouse support" org-mouse)
  151. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  152. (const :tag "C bookmark: Org links to bookmarks" org-bookmark)
  153. (const :tag "C depend: TODO dependencies for Org-mode" org-depend)
  154. (const :tag "C elisp-symbol: Org links to emacs-lisp symbols" org-elisp-symbol)
  155. (const :tag "C expiry: Expiry mechanism for Org entries" org-expiry)
  156. (const :tag "C id: Global id's for identifying entries" org-id)
  157. (const :tag "C interactive-query: Interactive modification of tags query" org-interactive-query)
  158. (const :tag "C mairix: Hook mairix search into Org for different MUAs" org-mairix)
  159. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  160. (const :tag "C mew: Support for links to messages in Mew" org-mew)
  161. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  162. (const :tag "C registry: A registry for Org links" org-registry)
  163. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  164. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  165. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  166. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  167. (defgroup org-startup nil
  168. "Options concerning startup of Org-mode."
  169. :tag "Org Startup"
  170. :group 'org)
  171. (defcustom org-startup-folded t
  172. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  173. This can also be configured on a per-file basis by adding one of
  174. the following lines anywhere in the buffer:
  175. #+STARTUP: fold
  176. #+STARTUP: nofold
  177. #+STARTUP: content"
  178. :group 'org-startup
  179. :type '(choice
  180. (const :tag "nofold: show all" nil)
  181. (const :tag "fold: overview" t)
  182. (const :tag "content: all headlines" content)))
  183. (defcustom org-startup-truncated t
  184. "Non-nil means, entering Org-mode will set `truncate-lines'.
  185. This is useful since some lines containing links can be very long and
  186. uninteresting. Also tables look terrible when wrapped."
  187. :group 'org-startup
  188. :type 'boolean)
  189. (defcustom org-startup-align-all-tables nil
  190. "Non-nil means, align all tables when visiting a file.
  191. This is useful when the column width in tables is forced with <N> cookies
  192. in table fields. Such tables will look correct only after the first re-align.
  193. This can also be configured on a per-file basis by adding one of
  194. the following lines anywhere in the buffer:
  195. #+STARTUP: align
  196. #+STARTUP: noalign"
  197. :group 'org-startup
  198. :type 'boolean)
  199. (defcustom org-insert-mode-line-in-empty-file nil
  200. "Non-nil means insert the first line setting Org-mode in empty files.
  201. When the function `org-mode' is called interactively in an empty file, this
  202. normally means that the file name does not automatically trigger Org-mode.
  203. To ensure that the file will always be in Org-mode in the future, a
  204. line enforcing Org-mode will be inserted into the buffer, if this option
  205. has been set."
  206. :group 'org-startup
  207. :type 'boolean)
  208. (defcustom org-replace-disputed-keys nil
  209. "Non-nil means use alternative key bindings for some keys.
  210. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  211. These keys are also used by other packages like `CUA-mode' or `windmove.el'.
  212. If you want to use Org-mode together with one of these other modes,
  213. or more generally if you would like to move some Org-mode commands to
  214. other keys, set this variable and configure the keys with the variable
  215. `org-disputed-keys'.
  216. This option is only relevant at load-time of Org-mode, and must be set
  217. *before* org.el is loaded. Changing it requires a restart of Emacs to
  218. become effective."
  219. :group 'org-startup
  220. :type 'boolean)
  221. (if (fboundp 'defvaralias)
  222. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  223. (defcustom org-disputed-keys
  224. '(([(shift up)] . [(meta p)])
  225. ([(shift down)] . [(meta n)])
  226. ([(shift left)] . [(meta -)])
  227. ([(shift right)] . [(meta +)])
  228. ([(control shift right)] . [(meta shift +)])
  229. ([(control shift left)] . [(meta shift -)]))
  230. "Keys for which Org-mode and other modes compete.
  231. This is an alist, cars are the default keys, second element specifies
  232. the alternative to use when `org-replace-disputed-keys' is t.
  233. Keys can be specified in any syntax supported by `define-key'.
  234. The value of this option takes effect only at Org-mode's startup,
  235. therefore you'll have to restart Emacs to apply it after changing."
  236. :group 'org-startup
  237. :type 'alist)
  238. (defun org-key (key)
  239. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  240. Or return the original if not disputed."
  241. (if org-replace-disputed-keys
  242. (let* ((nkey (key-description key))
  243. (x (org-find-if (lambda (x)
  244. (equal (key-description (car x)) nkey))
  245. org-disputed-keys)))
  246. (if x (cdr x) key))
  247. key))
  248. (defun org-find-if (predicate seq)
  249. (catch 'exit
  250. (while seq
  251. (if (funcall predicate (car seq))
  252. (throw 'exit (car seq))
  253. (pop seq)))))
  254. (defun org-defkey (keymap key def)
  255. "Define a key, possibly translated, as returned by `org-key'."
  256. (define-key keymap (org-key key) def))
  257. (defcustom org-ellipsis nil
  258. "The ellipsis to use in the Org-mode outline.
  259. When nil, just use the standard three dots. When a string, use that instead,
  260. When a face, use the standart 3 dots, but with the specified face.
  261. The change affects only Org-mode (which will then use its own display table).
  262. Changing this requires executing `M-x org-mode' in a buffer to become
  263. effective."
  264. :group 'org-startup
  265. :type '(choice (const :tag "Default" nil)
  266. (face :tag "Face" :value org-warning)
  267. (string :tag "String" :value "...#")))
  268. (defvar org-display-table nil
  269. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  270. (defgroup org-keywords nil
  271. "Keywords in Org-mode."
  272. :tag "Org Keywords"
  273. :group 'org)
  274. (defcustom org-deadline-string "DEADLINE:"
  275. "String to mark deadline entries.
  276. A deadline is this string, followed by a time stamp. Should be a word,
  277. terminated by a colon. You can insert a schedule keyword and
  278. a timestamp with \\[org-deadline].
  279. Changes become only effective after restarting Emacs."
  280. :group 'org-keywords
  281. :type 'string)
  282. (defcustom org-scheduled-string "SCHEDULED:"
  283. "String to mark scheduled TODO entries.
  284. A schedule is this string, followed by a time stamp. Should be a word,
  285. terminated by a colon. You can insert a schedule keyword and
  286. a timestamp with \\[org-schedule].
  287. Changes become only effective after restarting Emacs."
  288. :group 'org-keywords
  289. :type 'string)
  290. (defcustom org-closed-string "CLOSED:"
  291. "String used as the prefix for timestamps logging closing a TODO entry."
  292. :group 'org-keywords
  293. :type 'string)
  294. (defcustom org-clock-string "CLOCK:"
  295. "String used as prefix for timestamps clocking work hours on an item."
  296. :group 'org-keywords
  297. :type 'string)
  298. (defcustom org-comment-string "COMMENT"
  299. "Entries starting with this keyword will never be exported.
  300. An entry can be toggled between COMMENT and normal with
  301. \\[org-toggle-comment].
  302. Changes become only effective after restarting Emacs."
  303. :group 'org-keywords
  304. :type 'string)
  305. (defcustom org-quote-string "QUOTE"
  306. "Entries starting with this keyword will be exported in fixed-width font.
  307. Quoting applies only to the text in the entry following the headline, and does
  308. not extend beyond the next headline, even if that is lower level.
  309. An entry can be toggled between QUOTE and normal with
  310. \\[org-toggle-fixed-width-section]."
  311. :group 'org-keywords
  312. :type 'string)
  313. (defconst org-repeat-re
  314. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\([.+]?\\+[0-9]+[dwmy]\\)"
  315. "Regular expression for specifying repeated events.
  316. After a match, group 1 contains the repeat expression.")
  317. (defgroup org-structure nil
  318. "Options concerning the general structure of Org-mode files."
  319. :tag "Org Structure"
  320. :group 'org)
  321. (defgroup org-reveal-location nil
  322. "Options about how to make context of a location visible."
  323. :tag "Org Reveal Location"
  324. :group 'org-structure)
  325. (defconst org-context-choice
  326. '(choice
  327. (const :tag "Always" t)
  328. (const :tag "Never" nil)
  329. (repeat :greedy t :tag "Individual contexts"
  330. (cons
  331. (choice :tag "Context"
  332. (const agenda)
  333. (const org-goto)
  334. (const occur-tree)
  335. (const tags-tree)
  336. (const link-search)
  337. (const mark-goto)
  338. (const bookmark-jump)
  339. (const isearch)
  340. (const default))
  341. (boolean))))
  342. "Contexts for the reveal options.")
  343. (defcustom org-show-hierarchy-above '((default . t))
  344. "Non-nil means, show full hierarchy when revealing a location.
  345. Org-mode often shows locations in an org-mode file which might have
  346. been invisible before. When this is set, the hierarchy of headings
  347. above the exposed location is shown.
  348. Turning this off for example for sparse trees makes them very compact.
  349. Instead of t, this can also be an alist specifying this option for different
  350. contexts. Valid contexts are
  351. agenda when exposing an entry from the agenda
  352. org-goto when using the command `org-goto' on key C-c C-j
  353. occur-tree when using the command `org-occur' on key C-c /
  354. tags-tree when constructing a sparse tree based on tags matches
  355. link-search when exposing search matches associated with a link
  356. mark-goto when exposing the jump goal of a mark
  357. bookmark-jump when exposing a bookmark location
  358. isearch when exiting from an incremental search
  359. default default for all contexts not set explicitly"
  360. :group 'org-reveal-location
  361. :type org-context-choice)
  362. (defcustom org-show-following-heading '((default . nil))
  363. "Non-nil means, show following heading when revealing a location.
  364. Org-mode often shows locations in an org-mode file which might have
  365. been invisible before. When this is set, the heading following the
  366. match is shown.
  367. Turning this off for example for sparse trees makes them very compact,
  368. but makes it harder to edit the location of the match. In such a case,
  369. use the command \\[org-reveal] to show more context.
  370. Instead of t, this can also be an alist specifying this option for different
  371. contexts. See `org-show-hierarchy-above' for valid contexts."
  372. :group 'org-reveal-location
  373. :type org-context-choice)
  374. (defcustom org-show-siblings '((default . nil) (isearch t))
  375. "Non-nil means, show all sibling heading when revealing a location.
  376. Org-mode often shows locations in an org-mode file which might have
  377. been invisible before. When this is set, the sibling of the current entry
  378. heading are all made visible. If `org-show-hierarchy-above' is t,
  379. the same happens on each level of the hierarchy above the current entry.
  380. By default this is on for the isearch context, off for all other contexts.
  381. Turning this off for example for sparse trees makes them very compact,
  382. but makes it harder to edit the location of the match. In such a case,
  383. use the command \\[org-reveal] to show more context.
  384. Instead of t, this can also be an alist specifying this option for different
  385. contexts. See `org-show-hierarchy-above' for valid contexts."
  386. :group 'org-reveal-location
  387. :type org-context-choice)
  388. (defcustom org-show-entry-below '((default . nil))
  389. "Non-nil means, show the entry below a headline when revealing a location.
  390. Org-mode often shows locations in an org-mode file which might have
  391. been invisible before. When this is set, the text below the headline that is
  392. exposed is also shown.
  393. By default this is off for all contexts.
  394. Instead of t, this can also be an alist specifying this option for different
  395. contexts. See `org-show-hierarchy-above' for valid contexts."
  396. :group 'org-reveal-location
  397. :type org-context-choice)
  398. (defcustom org-indirect-buffer-display 'other-window
  399. "How should indirect tree buffers be displayed?
  400. This applies to indirect buffers created with the commands
  401. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  402. Valid values are:
  403. current-window Display in the current window
  404. other-window Just display in another window.
  405. dedicated-frame Create one new frame, and re-use it each time.
  406. new-frame Make a new frame each time. Note that in this case
  407. previously-made indirect buffers are kept, and you need to
  408. kill these buffers yourself."
  409. :group 'org-structure
  410. :group 'org-agenda-windows
  411. :type '(choice
  412. (const :tag "In current window" current-window)
  413. (const :tag "In current frame, other window" other-window)
  414. (const :tag "Each time a new frame" new-frame)
  415. (const :tag "One dedicated frame" dedicated-frame)))
  416. (defgroup org-cycle nil
  417. "Options concerning visibility cycling in Org-mode."
  418. :tag "Org Cycle"
  419. :group 'org-structure)
  420. (defcustom org-drawers '("PROPERTIES" "CLOCK")
  421. "Names of drawers. Drawers are not opened by cycling on the headline above.
  422. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  423. this:
  424. :DRAWERNAME:
  425. .....
  426. :END:
  427. The drawer \"PROPERTIES\" is special for capturing properties through
  428. the property API.
  429. Drawers can be defined on the per-file basis with a line like:
  430. #+DRAWERS: HIDDEN STATE PROPERTIES"
  431. :group 'org-structure
  432. :type '(repeat (string :tag "Drawer Name")))
  433. (defcustom org-cycle-global-at-bob nil
  434. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  435. This makes it possible to do global cycling without having to use S-TAB or
  436. C-u TAB. For this special case to work, the first line of the buffer
  437. must not be a headline - it may be empty ot some other text. When used in
  438. this way, `org-cycle-hook' is disables temporarily, to make sure the
  439. cursor stays at the beginning of the buffer.
  440. When this option is nil, don't do anything special at the beginning
  441. of the buffer."
  442. :group 'org-cycle
  443. :type 'boolean)
  444. (defcustom org-cycle-emulate-tab t
  445. "Where should `org-cycle' emulate TAB.
  446. nil Never
  447. white Only in completely white lines
  448. whitestart Only at the beginning of lines, before the first non-white char
  449. t Everywhere except in headlines
  450. exc-hl-bol Everywhere except at the start of a headline
  451. If TAB is used in a place where it does not emulate TAB, the current subtree
  452. visibility is cycled."
  453. :group 'org-cycle
  454. :type '(choice (const :tag "Never" nil)
  455. (const :tag "Only in completely white lines" white)
  456. (const :tag "Before first char in a line" whitestart)
  457. (const :tag "Everywhere except in headlines" t)
  458. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  459. ))
  460. (defcustom org-cycle-separator-lines 2
  461. "Number of empty lines needed to keep an empty line between collapsed trees.
  462. If you leave an empty line between the end of a subtree and the following
  463. headline, this empty line is hidden when the subtree is folded.
  464. Org-mode will leave (exactly) one empty line visible if the number of
  465. empty lines is equal or larger to the number given in this variable.
  466. So the default 2 means, at least 2 empty lines after the end of a subtree
  467. are needed to produce free space between a collapsed subtree and the
  468. following headline.
  469. Special case: when 0, never leave empty lines in collapsed view."
  470. :group 'org-cycle
  471. :type 'integer)
  472. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  473. org-cycle-hide-drawers
  474. org-cycle-show-empty-lines
  475. org-optimize-window-after-visibility-change)
  476. "Hook that is run after `org-cycle' has changed the buffer visibility.
  477. The function(s) in this hook must accept a single argument which indicates
  478. the new state that was set by the most recent `org-cycle' command. The
  479. argument is a symbol. After a global state change, it can have the values
  480. `overview', `content', or `all'. After a local state change, it can have
  481. the values `folded', `children', or `subtree'."
  482. :group 'org-cycle
  483. :type 'hook)
  484. (defgroup org-edit-structure nil
  485. "Options concerning structure editing in Org-mode."
  486. :tag "Org Edit Structure"
  487. :group 'org-structure)
  488. (defcustom org-odd-levels-only nil
  489. "Non-nil means, skip even levels and only use odd levels for the outline.
  490. This has the effect that two stars are being added/taken away in
  491. promotion/demotion commands. It also influences how levels are
  492. handled by the exporters.
  493. Changing it requires restart of `font-lock-mode' to become effective
  494. for fontification also in regions already fontified.
  495. You may also set this on a per-file basis by adding one of the following
  496. lines to the buffer:
  497. #+STARTUP: odd
  498. #+STARTUP: oddeven"
  499. :group 'org-edit-structure
  500. :group 'org-font-lock
  501. :type 'boolean)
  502. (defcustom org-adapt-indentation t
  503. "Non-nil means, adapt indentation when promoting and demoting.
  504. When this is set and the *entire* text in an entry is indented, the
  505. indentation is increased by one space in a demotion command, and
  506. decreased by one in a promotion command. If any line in the entry
  507. body starts at column 0, indentation is not changed at all."
  508. :group 'org-edit-structure
  509. :type 'boolean)
  510. (defcustom org-special-ctrl-a/e nil
  511. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  512. When t, `C-a' will bring back the cursor to the beginning of the
  513. headline text, i.e. after the stars and after a possible TODO keyword.
  514. In an item, this will be the position after the bullet.
  515. When the cursor is already at that position, another `C-a' will bring
  516. it to the beginning of the line.
  517. `C-e' will jump to the end of the headline, ignoring the presence of tags
  518. in the headline. A second `C-e' will then jump to the true end of the
  519. line, after any tags.
  520. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  521. and only a directly following, identical keypress will bring the cursor
  522. to the special positions."
  523. :group 'org-edit-structure
  524. :type '(choice
  525. (const :tag "off" nil)
  526. (const :tag "after bullet first" t)
  527. (const :tag "border first" reversed)))
  528. (if (fboundp 'defvaralias)
  529. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  530. (defcustom org-special-ctrl-k nil
  531. "Non-nil means `C-k' will behave specially in headlines.
  532. When nil, `C-k' will call the default `kill-line' command.
  533. When t, the following will happen while the cursor is in the headline:
  534. - When the cursor is at the beginning of a headline, kill the entire
  535. line and possible the folded subtree below the line.
  536. - When in the middle of the headline text, kill the headline up to the tags.
  537. - When after the headline text, kill the tags."
  538. :group 'org-edit-structure
  539. :type 'boolean)
  540. (defcustom org-M-RET-may-split-line '((default . t))
  541. "Non-nil means, M-RET will split the line at the cursor position.
  542. When nil, it will go to the end of the line before making a
  543. new line.
  544. You may also set this option in a different way for different
  545. contexts. Valid contexts are:
  546. headline when creating a new headline
  547. item when creating a new item
  548. table in a table field
  549. default the value to be used for all contexts not explicitly
  550. customized"
  551. :group 'org-structure
  552. :group 'org-table
  553. :type '(choice
  554. (const :tag "Always" t)
  555. (const :tag "Never" nil)
  556. (repeat :greedy t :tag "Individual contexts"
  557. (cons
  558. (choice :tag "Context"
  559. (const headline)
  560. (const item)
  561. (const table)
  562. (const default))
  563. (boolean)))))
  564. (defcustom org-blank-before-new-entry '((heading . nil)
  565. (plain-list-item . nil))
  566. "Should `org-insert-heading' leave a blank line before new heading/item?
  567. The value is an alist, with `heading' and `plain-list-item' as car,
  568. and a boolean flag as cdr."
  569. :group 'org-edit-structure
  570. :type '(list
  571. (cons (const heading) (boolean))
  572. (cons (const plain-list-item) (boolean))))
  573. (defcustom org-insert-heading-hook nil
  574. "Hook being run after inserting a new heading."
  575. :group 'org-edit-structure
  576. :type 'hook)
  577. (defcustom org-enable-fixed-width-editor t
  578. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  579. This currently only means, they are never auto-wrapped.
  580. When nil, such lines will be treated like ordinary lines.
  581. See also the QUOTE keyword."
  582. :group 'org-edit-structure
  583. :type 'boolean)
  584. (defcustom org-goto-auto-isearch t
  585. "Non-nil means, typing characters in org-goto starts incremental search."
  586. :group 'org-edit-structure
  587. :type 'boolean)
  588. (defgroup org-sparse-trees nil
  589. "Options concerning sparse trees in Org-mode."
  590. :tag "Org Sparse Trees"
  591. :group 'org-structure)
  592. (defcustom org-highlight-sparse-tree-matches t
  593. "Non-nil means, highlight all matches that define a sparse tree.
  594. The highlights will automatically disappear the next time the buffer is
  595. changed by an edit command."
  596. :group 'org-sparse-trees
  597. :type 'boolean)
  598. (defcustom org-remove-highlights-with-change t
  599. "Non-nil means, any change to the buffer will remove temporary highlights.
  600. Such highlights are created by `org-occur' and `org-clock-display'.
  601. When nil, `C-c C-c needs to be used to get rid of the highlights.
  602. The highlights created by `org-preview-latex-fragment' always need
  603. `C-c C-c' to be removed."
  604. :group 'org-sparse-trees
  605. :group 'org-time
  606. :type 'boolean)
  607. (defcustom org-occur-hook '(org-first-headline-recenter)
  608. "Hook that is run after `org-occur' has constructed a sparse tree.
  609. This can be used to recenter the window to show as much of the structure
  610. as possible."
  611. :group 'org-sparse-trees
  612. :type 'hook)
  613. (defgroup org-plain-lists nil
  614. "Options concerning plain lists in Org-mode."
  615. :tag "Org Plain lists"
  616. :group 'org-structure)
  617. (defcustom org-cycle-include-plain-lists nil
  618. "Non-nil means, include plain lists into visibility cycling.
  619. This means that during cycling, plain list items will *temporarily* be
  620. interpreted as outline headlines with a level given by 1000+i where i is the
  621. indentation of the bullet. In all other operations, plain list items are
  622. not seen as headlines. For example, you cannot assign a TODO keyword to
  623. such an item."
  624. :group 'org-plain-lists
  625. :type 'boolean)
  626. (defcustom org-plain-list-ordered-item-terminator t
  627. "The character that makes a line with leading number an ordered list item.
  628. Valid values are ?. and ?\). To get both terminators, use t. While
  629. ?. may look nicer, it creates the danger that a line with leading
  630. number may be incorrectly interpreted as an item. ?\) therefore is
  631. the safe choice."
  632. :group 'org-plain-lists
  633. :type '(choice (const :tag "dot like in \"2.\"" ?.)
  634. (const :tag "paren like in \"2)\"" ?\))
  635. (const :tab "both" t)))
  636. (defcustom org-auto-renumber-ordered-lists t
  637. "Non-nil means, automatically renumber ordered plain lists.
  638. Renumbering happens when the sequence have been changed with
  639. \\[org-shiftmetaup] or \\[org-shiftmetadown]. After other editing commands,
  640. use \\[org-ctrl-c-ctrl-c] to trigger renumbering."
  641. :group 'org-plain-lists
  642. :type 'boolean)
  643. (defcustom org-provide-checkbox-statistics t
  644. "Non-nil means, update checkbox statistics after insert and toggle.
  645. When this is set, checkbox statistics is updated each time you either insert
  646. a new checkbox with \\[org-insert-todo-heading] or toggle a checkbox
  647. with \\[org-ctrl-c-ctrl-c\\]."
  648. :group 'org-plain-lists
  649. :type 'boolean)
  650. (defgroup org-archive nil
  651. "Options concerning archiving in Org-mode."
  652. :tag "Org Archive"
  653. :group 'org-structure)
  654. (defcustom org-archive-tag "ARCHIVE"
  655. "The tag that marks a subtree as archived.
  656. An archived subtree does not open during visibility cycling, and does
  657. not contribute to the agenda listings.
  658. After changing this, font-lock must be restarted in the relevant buffers to
  659. get the proper fontification."
  660. :group 'org-archive
  661. :group 'org-keywords
  662. :type 'string)
  663. (defcustom org-attic-heading "Attic"
  664. "Name of the local attic sibling that is used to archive entries locally.
  665. Locally means: in the tree, under a sibling.
  666. See `org-archive-to-attic-sibling' for more information."
  667. :group 'org-archive
  668. :type 'string)
  669. (defcustom org-agenda-skip-archived-trees t
  670. "Non-nil means, the agenda will skip any items located in archived trees.
  671. An archived tree is a tree marked with the tag ARCHIVE."
  672. :group 'org-archive
  673. :group 'org-agenda-skip
  674. :type 'boolean)
  675. (defcustom org-cycle-open-archived-trees nil
  676. "Non-nil means, `org-cycle' will open archived trees.
  677. An archived tree is a tree marked with the tag ARCHIVE.
  678. When nil, archived trees will stay folded. You can still open them with
  679. normal outline commands like `show-all', but not with the cycling commands."
  680. :group 'org-archive
  681. :group 'org-cycle
  682. :type 'boolean)
  683. (defcustom org-sparse-tree-open-archived-trees nil
  684. "Non-nil means sparse tree construction shows matches in archived trees.
  685. When nil, matches in these trees are highlighted, but the trees are kept in
  686. collapsed state."
  687. :group 'org-archive
  688. :group 'org-sparse-trees
  689. :type 'boolean)
  690. (defcustom org-archive-location "%s_archive::"
  691. "The location where subtrees should be archived.
  692. Otherwise, the value of this variable is a string, consisting of two
  693. parts, separated by a double-colon.
  694. The first part is a file name - when omitted, archiving happens in the same
  695. file. %s will be replaced by the current file name (without directory part).
  696. Archiving to a different file is useful to keep archived entries from
  697. contributing to the Org-mode Agenda.
  698. The part after the double colon is a headline. The archived entries will be
  699. filed under that headline. When omitted, the subtrees are simply filed away
  700. at the end of the file, as top-level entries.
  701. Here are a few examples:
  702. \"%s_archive::\"
  703. If the current file is Projects.org, archive in file
  704. Projects.org_archive, as top-level trees. This is the default.
  705. \"::* Archived Tasks\"
  706. Archive in the current file, under the top-level headline
  707. \"* Archived Tasks\".
  708. \"~/org/archive.org::\"
  709. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  710. \"basement::** Finished Tasks\"
  711. Archive in file ./basement (relative path), as level 3 trees
  712. below the level 2 heading \"** Finished Tasks\".
  713. You may set this option on a per-file basis by adding to the buffer a
  714. line like
  715. #+ARCHIVE: basement::** Finished Tasks
  716. You may also define it locally for a subtree by setting an ARCHIVE property
  717. in the entry. If such a property is found in an entry, or anywhere up
  718. the hierarchy, it will be used."
  719. :group 'org-archive
  720. :type 'string)
  721. (defcustom org-archive-mark-done t
  722. "Non-nil means, mark entries as DONE when they are moved to the archive file.
  723. This can be a string to set the keyword to use. When t, Org-mode will
  724. use the first keyword in its list that means done."
  725. :group 'org-archive
  726. :type '(choice
  727. (const :tag "No" nil)
  728. (const :tag "Yes" t)
  729. (string :tag "Use this keyword")))
  730. (defcustom org-archive-stamp-time t
  731. "Non-nil means, add a time stamp to entries moved to an archive file.
  732. This variable is obsolete and has no effect anymore, instead add ot remove
  733. `time' from the variablle `org-archive-save-context-info'."
  734. :group 'org-archive
  735. :type 'boolean)
  736. (defcustom org-archive-save-context-info '(time file olpath category todo itags)
  737. "Parts of context info that should be stored as properties when archiving.
  738. When a subtree is moved to an archive file, it looses information given by
  739. context, like inherited tags, the category, and possibly also the TODO
  740. state (depending on the variable `org-archive-mark-done').
  741. This variable can be a list of any of the following symbols:
  742. time The time of archiving.
  743. file The file where the entry originates.
  744. itags The local tags, in the headline of the subtree.
  745. ltags The tags the subtree inherits from further up the hierarchy.
  746. todo The pre-archive TODO state.
  747. category The category, taken from file name or #+CATEGORY lines.
  748. olpath The outline path to the item. These are all headlines above
  749. the current item, separated by /, like a file path.
  750. For each symbol present in the list, a property will be created in
  751. the archived entry, with a prefix \"PRE_ARCHIVE_\", to remember this
  752. information."
  753. :group 'org-archive
  754. :type '(set :greedy t
  755. (const :tag "Time" time)
  756. (const :tag "File" file)
  757. (const :tag "Category" category)
  758. (const :tag "TODO state" todo)
  759. (const :tag "TODO state" priority)
  760. (const :tag "Inherited tags" itags)
  761. (const :tag "Outline path" olpath)
  762. (const :tag "Local tags" ltags)))
  763. (defgroup org-imenu-and-speedbar nil
  764. "Options concerning imenu and speedbar in Org-mode."
  765. :tag "Org Imenu and Speedbar"
  766. :group 'org-structure)
  767. (defcustom org-imenu-depth 2
  768. "The maximum level for Imenu access to Org-mode headlines.
  769. This also applied for speedbar access."
  770. :group 'org-imenu-and-speedbar
  771. :type 'number)
  772. (defgroup org-table nil
  773. "Options concerning tables in Org-mode."
  774. :tag "Org Table"
  775. :group 'org)
  776. (defcustom org-enable-table-editor 'optimized
  777. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  778. When nil, such lines will be treated like ordinary lines.
  779. When equal to the symbol `optimized', the table editor will be optimized to
  780. do the following:
  781. - Automatic overwrite mode in front of whitespace in table fields.
  782. This makes the structure of the table stay in tact as long as the edited
  783. field does not exceed the column width.
  784. - Minimize the number of realigns. Normally, the table is aligned each time
  785. TAB or RET are pressed to move to another field. With optimization this
  786. happens only if changes to a field might have changed the column width.
  787. Optimization requires replacing the functions `self-insert-command',
  788. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  789. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  790. very good at guessing when a re-align will be necessary, but you can always
  791. force one with \\[org-ctrl-c-ctrl-c].
  792. If you would like to use the optimized version in Org-mode, but the
  793. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  794. This variable can be used to turn on and off the table editor during a session,
  795. but in order to toggle optimization, a restart is required.
  796. See also the variable `org-table-auto-blank-field'."
  797. :group 'org-table
  798. :type '(choice
  799. (const :tag "off" nil)
  800. (const :tag "on" t)
  801. (const :tag "on, optimized" optimized)))
  802. (defcustom org-table-tab-recognizes-table.el t
  803. "Non-nil means, TAB will automatically notice a table.el table.
  804. When it sees such a table, it moves point into it and - if necessary -
  805. calls `table-recognize-table'."
  806. :group 'org-table-editing
  807. :type 'boolean)
  808. (defgroup org-link nil
  809. "Options concerning links in Org-mode."
  810. :tag "Org Link"
  811. :group 'org)
  812. (defvar org-link-abbrev-alist-local nil
  813. "Buffer-local version of `org-link-abbrev-alist', which see.
  814. The value of this is taken from the #+LINK lines.")
  815. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  816. (defcustom org-link-abbrev-alist nil
  817. "Alist of link abbreviations.
  818. The car of each element is a string, to be replaced at the start of a link.
  819. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  820. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  821. [[linkkey:tag][description]]
  822. If REPLACE is a string, the tag will simply be appended to create the link.
  823. If the string contains \"%s\", the tag will be inserted there.
  824. REPLACE may also be a function that will be called with the tag as the
  825. only argument to create the link, which should be returned as a string.
  826. See the manual for examples."
  827. :group 'org-link
  828. :type 'alist)
  829. (defcustom org-descriptive-links t
  830. "Non-nil means, hide link part and only show description of bracket links.
  831. Bracket links are like [[link][descritpion]]. This variable sets the initial
  832. state in new org-mode buffers. The setting can then be toggled on a
  833. per-buffer basis from the Org->Hyperlinks menu."
  834. :group 'org-link
  835. :type 'boolean)
  836. (defcustom org-link-file-path-type 'adaptive
  837. "How the path name in file links should be stored.
  838. Valid values are:
  839. relative Relative to the current directory, i.e. the directory of the file
  840. into which the link is being inserted.
  841. absolute Absolute path, if possible with ~ for home directory.
  842. noabbrev Absolute path, no abbreviation of home directory.
  843. adaptive Use relative path for files in the current directory and sub-
  844. directories of it. For other files, use an absolute path."
  845. :group 'org-link
  846. :type '(choice
  847. (const relative)
  848. (const absolute)
  849. (const noabbrev)
  850. (const adaptive)))
  851. (defcustom org-activate-links '(bracket angle plain radio tag date)
  852. "Types of links that should be activated in Org-mode files.
  853. This is a list of symbols, each leading to the activation of a certain link
  854. type. In principle, it does not hurt to turn on most link types - there may
  855. be a small gain when turning off unused link types. The types are:
  856. bracket The recommended [[link][description]] or [[link]] links with hiding.
  857. angular Links in angular brackes that may contain whitespace like
  858. <bbdb:Carsten Dominik>.
  859. plain Plain links in normal text, no whitespace, like http://google.com.
  860. radio Text that is matched by a radio target, see manual for details.
  861. tag Tag settings in a headline (link to tag search).
  862. date Time stamps (link to calendar).
  863. Changing this variable requires a restart of Emacs to become effective."
  864. :group 'org-link
  865. :type '(set (const :tag "Double bracket links (new style)" bracket)
  866. (const :tag "Angular bracket links (old style)" angular)
  867. (const :tag "Plain text links" plain)
  868. (const :tag "Radio target matches" radio)
  869. (const :tag "Tags" tag)
  870. (const :tag "Timestamps" date)))
  871. (defgroup org-link-store nil
  872. "Options concerning storing links in Org-mode."
  873. :tag "Org Store Link"
  874. :group 'org-link)
  875. (defcustom org-email-link-description-format "Email %c: %.30s"
  876. "Format of the description part of a link to an email or usenet message.
  877. The following %-excapes will be replaced by corresponding information:
  878. %F full \"From\" field
  879. %f name, taken from \"From\" field, address if no name
  880. %T full \"To\" field
  881. %t first name in \"To\" field, address if no name
  882. %c correspondent. Unually \"from NAME\", but if you sent it yourself, it
  883. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  884. %s subject
  885. %m message-id.
  886. You may use normal field width specification between the % and the letter.
  887. This is for example useful to limit the length of the subject.
  888. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  889. :group 'org-link-store
  890. :type 'string)
  891. (defcustom org-from-is-user-regexp
  892. (let (r1 r2)
  893. (when (and user-mail-address (not (string= user-mail-address "")))
  894. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  895. (when (and user-full-name (not (string= user-full-name "")))
  896. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  897. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  898. "Regexp mached against the \"From:\" header of an email or usenet message.
  899. It should match if the message is from the user him/herself."
  900. :group 'org-link-store
  901. :type 'regexp)
  902. (defcustom org-context-in-file-links t
  903. "Non-nil means, file links from `org-store-link' contain context.
  904. A search string will be added to the file name with :: as separator and
  905. used to find the context when the link is activated by the command
  906. `org-open-at-point'.
  907. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  908. negates this setting for the duration of the command."
  909. :group 'org-link-store
  910. :type 'boolean)
  911. (defcustom org-keep-stored-link-after-insertion nil
  912. "Non-nil means, keep link in list for entire session.
  913. The command `org-store-link' adds a link pointing to the current
  914. location to an internal list. These links accumulate during a session.
  915. The command `org-insert-link' can be used to insert links into any
  916. Org-mode file (offering completion for all stored links). When this
  917. option is nil, every link which has been inserted once using \\[org-insert-link]
  918. will be removed from the list, to make completing the unused links
  919. more efficient."
  920. :group 'org-link-store
  921. :type 'boolean)
  922. (defgroup org-link-follow nil
  923. "Options concerning following links in Org-mode."
  924. :tag "Org Follow Link"
  925. :group 'org-link)
  926. (defcustom org-follow-link-hook nil
  927. "Hook that is run after a link has been followed."
  928. :group 'org-link-follow
  929. :type 'hook)
  930. (defcustom org-tab-follows-link nil
  931. "Non-nil means, on links TAB will follow the link.
  932. Needs to be set before org.el is loaded."
  933. :group 'org-link-follow
  934. :type 'boolean)
  935. (defcustom org-return-follows-link nil
  936. "Non-nil means, on links RET will follow the link.
  937. Needs to be set before org.el is loaded."
  938. :group 'org-link-follow
  939. :type 'boolean)
  940. (defcustom org-mouse-1-follows-link
  941. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  942. "Non-nil means, mouse-1 on a link will follow the link.
  943. A longer mouse click will still set point. Does not work on XEmacs.
  944. Needs to be set before org.el is loaded."
  945. :group 'org-link-follow
  946. :type 'boolean)
  947. (defcustom org-mark-ring-length 4
  948. "Number of different positions to be recorded in the ring
  949. Changing this requires a restart of Emacs to work correctly."
  950. :group 'org-link-follow
  951. :type 'interger)
  952. (defcustom org-link-frame-setup
  953. '((vm . vm-visit-folder-other-frame)
  954. (gnus . gnus-other-frame)
  955. (file . find-file-other-window))
  956. "Setup the frame configuration for following links.
  957. When following a link with Emacs, it may often be useful to display
  958. this link in another window or frame. This variable can be used to
  959. set this up for the different types of links.
  960. For VM, use any of
  961. `vm-visit-folder'
  962. `vm-visit-folder-other-frame'
  963. For Gnus, use any of
  964. `gnus'
  965. `gnus-other-frame'
  966. For FILE, use any of
  967. `find-file'
  968. `find-file-other-window'
  969. `find-file-other-frame'
  970. For the calendar, use the variable `calendar-setup'.
  971. For BBDB, it is currently only possible to display the matches in
  972. another window."
  973. :group 'org-link-follow
  974. :type '(list
  975. (cons (const vm)
  976. (choice
  977. (const vm-visit-folder)
  978. (const vm-visit-folder-other-window)
  979. (const vm-visit-folder-other-frame)))
  980. (cons (const gnus)
  981. (choice
  982. (const gnus)
  983. (const gnus-other-frame)))
  984. (cons (const file)
  985. (choice
  986. (const find-file)
  987. (const find-file-other-window)
  988. (const find-file-other-frame)))))
  989. (defcustom org-display-internal-link-with-indirect-buffer nil
  990. "Non-nil means, use indirect buffer to display infile links.
  991. Activating internal links (from one location in a file to another location
  992. in the same file) normally just jumps to the location. When the link is
  993. activated with a C-u prefix (or with mouse-3), the link is displayed in
  994. another window. When this option is set, the other window actually displays
  995. an indirect buffer clone of the current buffer, to avoid any visibility
  996. changes to the current buffer."
  997. :group 'org-link-follow
  998. :type 'boolean)
  999. (defcustom org-open-non-existing-files nil
  1000. "Non-nil means, `org-open-file' will open non-existing files.
  1001. When nil, an error will be generated."
  1002. :group 'org-link-follow
  1003. :type 'boolean)
  1004. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1005. "Function and arguments to call for following mailto links.
  1006. This is a list with the first element being a lisp function, and the
  1007. remaining elements being arguments to the function. In string arguments,
  1008. %a will be replaced by the address, and %s will be replaced by the subject
  1009. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1010. :group 'org-link-follow
  1011. :type '(choice
  1012. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1013. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1014. (const :tag "message-mail" (message-mail "%a" "%s"))
  1015. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1016. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1017. "Non-nil means, ask for confirmation before executing shell links.
  1018. Shell links can be dangerous: just think about a link
  1019. [[shell:rm -rf ~/*][Google Search]]
  1020. This link would show up in your Org-mode document as \"Google Search\",
  1021. but really it would remove your entire home directory.
  1022. Therefore we advise against setting this variable to nil.
  1023. Just change it to `y-or-n-p' of you want to confirm with a
  1024. single keystroke rather than having to type \"yes\"."
  1025. :group 'org-link-follow
  1026. :type '(choice
  1027. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1028. (const :tag "with y-or-n (faster)" y-or-n-p)
  1029. (const :tag "no confirmation (dangerous)" nil)))
  1030. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1031. "Non-nil means, ask for confirmation before executing Emacs Lisp links.
  1032. Elisp links can be dangerous: just think about a link
  1033. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1034. This link would show up in your Org-mode document as \"Google Search\",
  1035. but really it would remove your entire home directory.
  1036. Therefore we advise against setting this variable to nil.
  1037. Just change it to `y-or-n-p' of you want to confirm with a
  1038. single keystroke rather than having to type \"yes\"."
  1039. :group 'org-link-follow
  1040. :type '(choice
  1041. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1042. (const :tag "with y-or-n (faster)" y-or-n-p)
  1043. (const :tag "no confirmation (dangerous)" nil)))
  1044. (defconst org-file-apps-defaults-gnu
  1045. '((remote . emacs)
  1046. (t . mailcap))
  1047. "Default file applications on a UNIX or GNU/Linux system.
  1048. See `org-file-apps'.")
  1049. (defconst org-file-apps-defaults-macosx
  1050. '((remote . emacs)
  1051. (t . "open %s")
  1052. ("ps" . "gv %s")
  1053. ("ps.gz" . "gv %s")
  1054. ("eps" . "gv %s")
  1055. ("eps.gz" . "gv %s")
  1056. ("dvi" . "xdvi %s")
  1057. ("fig" . "xfig %s"))
  1058. "Default file applications on a MacOS X system.
  1059. The system \"open\" is known as a default, but we use X11 applications
  1060. for some files for which the OS does not have a good default.
  1061. See `org-file-apps'.")
  1062. (defconst org-file-apps-defaults-windowsnt
  1063. (list
  1064. '(remote . emacs)
  1065. (cons t
  1066. (list (if (featurep 'xemacs)
  1067. 'mswindows-shell-execute
  1068. 'w32-shell-execute)
  1069. "open" 'file)))
  1070. "Default file applications on a Windows NT system.
  1071. The system \"open\" is used for most files.
  1072. See `org-file-apps'.")
  1073. (defcustom org-file-apps
  1074. '(
  1075. ("txt" . emacs)
  1076. ("tex" . emacs)
  1077. ("ltx" . emacs)
  1078. ("org" . emacs)
  1079. ("el" . emacs)
  1080. ("bib" . emacs)
  1081. )
  1082. "External applications for opening `file:path' items in a document.
  1083. Org-mode uses system defaults for different file types, but
  1084. you can use this variable to set the application for a given file
  1085. extension. The entries in this list are cons cells where the car identifies
  1086. files and the cdr the corresponding command. Possible values for the
  1087. file identifier are
  1088. \"ext\" A string identifying an extension
  1089. `directory' Matches a directory
  1090. `remote' Matches a remote file, accessible through tramp or efs.
  1091. Remote files most likely should be visited through Emacs
  1092. because external applications cannot handle such paths.
  1093. t Default for all remaining files
  1094. Possible values for the command are:
  1095. `emacs' The file will be visited by the current Emacs process.
  1096. `default' Use the default application for this file type.
  1097. string A command to be executed by a shell; %s will be replaced
  1098. by the path to the file.
  1099. sexp A Lisp form which will be evaluated. The file path will
  1100. be available in the Lisp variable `file'.
  1101. For more examples, see the system specific constants
  1102. `org-file-apps-defaults-macosx'
  1103. `org-file-apps-defaults-windowsnt'
  1104. `org-file-apps-defaults-gnu'."
  1105. :group 'org-link-follow
  1106. :type '(repeat
  1107. (cons (choice :value ""
  1108. (string :tag "Extension")
  1109. (const :tag "Default for unrecognized files" t)
  1110. (const :tag "Remote file" remote)
  1111. (const :tag "Links to a directory" directory))
  1112. (choice :value ""
  1113. (const :tag "Visit with Emacs" emacs)
  1114. (const :tag "Use system default" default)
  1115. (string :tag "Command")
  1116. (sexp :tag "Lisp form")))))
  1117. (defgroup org-refile nil
  1118. "Options concerning refiling entries in Org-mode."
  1119. :tag "Org Remember"
  1120. :group 'org)
  1121. (defcustom org-directory "~/org"
  1122. "Directory with org files.
  1123. This directory will be used as default to prompt for org files.
  1124. Used by the hooks for remember.el."
  1125. :group 'org-refile
  1126. :group 'org-remember
  1127. :type 'directory)
  1128. (defcustom org-default-notes-file "~/.notes"
  1129. "Default target for storing notes.
  1130. Used by the hooks for remember.el. This can be a string, or nil to mean
  1131. the value of `remember-data-file'.
  1132. You can set this on a per-template basis with the variable
  1133. `org-remember-templates'."
  1134. :group 'org-refile
  1135. :group 'org-remember
  1136. :type '(choice
  1137. (const :tag "Default from remember-data-file" nil)
  1138. file))
  1139. (defcustom org-goto-interface 'outline
  1140. "The default interface to be used for `org-goto'.
  1141. Allowed vaues are:
  1142. outline The interface shows an outline of the relevant file
  1143. and the correct heading is found by moving through
  1144. the outline or by searching with incremental search.
  1145. outline-path-completion Headlines in the current buffer are offered via
  1146. completion."
  1147. :group 'org-refile
  1148. :type '(choice
  1149. (const :tag "Outline" outline)
  1150. (const :tag "Outline-path-completion" outline-path-completion)))
  1151. (defcustom org-reverse-note-order nil
  1152. "Non-nil means, store new notes at the beginning of a file or entry.
  1153. When nil, new notes will be filed to the end of a file or entry.
  1154. This can also be a list with cons cells of regular expressions that
  1155. are matched against file names, and values."
  1156. :group 'org-remember
  1157. :type '(choice
  1158. (const :tag "Reverse always" t)
  1159. (const :tag "Reverse never" nil)
  1160. (repeat :tag "By file name regexp"
  1161. (cons regexp boolean))))
  1162. (defcustom org-refile-targets nil
  1163. "Targets for refiling entries with \\[org-refile].
  1164. This is list of cons cells. Each cell contains:
  1165. - a specification of the files to be considered, either a list of files,
  1166. or a symbol whose function or variable value will be used to retrieve
  1167. a file name or a list of file names. Nil means, refile to a different
  1168. heading in the current buffer.
  1169. - A specification of how to find candidate refile targets. This may be
  1170. any of
  1171. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1172. This tag has to be present in all target headlines, inheritance will
  1173. not be considered.
  1174. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1175. todo keyword.
  1176. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1177. headlines that are refiling targets.
  1178. - a cons cell (:level . N). Any headline of level N is considered a target.
  1179. - a cons cell (:maxlevel . N). Any headline with level <= N is a target."
  1180. :group 'org-remember
  1181. :type '(repeat
  1182. (cons
  1183. (choice :value org-agenda-files
  1184. (const :tag "All agenda files" org-agenda-files)
  1185. (const :tag "Current buffer" nil)
  1186. (function) (variable) (file))
  1187. (choice :tag "Identify target headline by"
  1188. (cons :tag "Specific tag" (const :tag) (string))
  1189. (cons :tag "TODO keyword" (const :todo) (string))
  1190. (cons :tag "Regular expression" (const :regexp) (regexp))
  1191. (cons :tag "Level number" (const :level) (integer))
  1192. (cons :tag "Max Level number" (const :maxlevel) (integer))))))
  1193. (defcustom org-refile-use-outline-path nil
  1194. "Non-nil means, provide refile targets as paths.
  1195. So a level 3 headline will be available as level1/level2/level3.
  1196. When the value is `file', also include the file name (without directory)
  1197. into the path. When `full-file-path', include the full file path."
  1198. :group 'org-remember
  1199. :type '(choice
  1200. (const :tag "Not" nil)
  1201. (const :tag "Yes" t)
  1202. (const :tag "Start with file name" file)
  1203. (const :tag "Start with full file path" full-file-path)))
  1204. (defgroup org-todo nil
  1205. "Options concerning TODO items in Org-mode."
  1206. :tag "Org TODO"
  1207. :group 'org)
  1208. (defgroup org-progress nil
  1209. "Options concerning Progress logging in Org-mode."
  1210. :tag "Org Progress"
  1211. :group 'org-time)
  1212. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1213. "List of TODO entry keyword sequences and their interpretation.
  1214. \\<org-mode-map>This is a list of sequences.
  1215. Each sequence starts with a symbol, either `sequence' or `type',
  1216. indicating if the keywords should be interpreted as a sequence of
  1217. action steps, or as different types of TODO items. The first
  1218. keywords are states requiring action - these states will select a headline
  1219. for inclusion into the global TODO list Org-mode produces. If one of
  1220. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1221. signify that no further action is necessary. If \"|\" is not found,
  1222. the last keyword is treated as the only DONE state of the sequence.
  1223. The command \\[org-todo] cycles an entry through these states, and one
  1224. additional state where no keyword is present. For details about this
  1225. cycling, see the manual.
  1226. TODO keywords and interpretation can also be set on a per-file basis with
  1227. the special #+SEQ_TODO and #+TYP_TODO lines.
  1228. Each keyword can optionally specify a character for fast state selection
  1229. \(in combination with the variable `org-use-fast-todo-selection')
  1230. and specifiers for state change logging, using the same syntax
  1231. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1232. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1233. indicates to record a time stamp each time this state is selected.
  1234. Each keyword may also specify if a timestamp or a note should be
  1235. recorded when entering or leaving the state, by adding additional
  1236. characters in the parenthesis after the keyword. This looks like this:
  1237. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1238. record only the time of the state change. With X and Y being either
  1239. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1240. Y when leaving the state if and only if the *target* state does not
  1241. define X. You may omit any of the fast-selection key or X or /Y,
  1242. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1243. For backward compatibility, this variable may also be just a list
  1244. of keywords - in this case the interptetation (sequence or type) will be
  1245. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1246. :group 'org-todo
  1247. :group 'org-keywords
  1248. :type '(choice
  1249. (repeat :tag "Old syntax, just keywords"
  1250. (string :tag "Keyword"))
  1251. (repeat :tag "New syntax"
  1252. (cons
  1253. (choice
  1254. :tag "Interpretation"
  1255. (const :tag "Sequence (cycling hits every state)" sequence)
  1256. (const :tag "Type (cycling directly to DONE)" type))
  1257. (repeat
  1258. (string :tag "Keyword"))))))
  1259. (defvar org-todo-keywords-1 nil
  1260. "All TODO and DONE keywords active in a buffer.")
  1261. (make-variable-buffer-local 'org-todo-keywords-1)
  1262. (defvar org-todo-keywords-for-agenda nil)
  1263. (defvar org-done-keywords-for-agenda nil)
  1264. (defvar org-not-done-keywords nil)
  1265. (make-variable-buffer-local 'org-not-done-keywords)
  1266. (defvar org-done-keywords nil)
  1267. (make-variable-buffer-local 'org-done-keywords)
  1268. (defvar org-todo-heads nil)
  1269. (make-variable-buffer-local 'org-todo-heads)
  1270. (defvar org-todo-sets nil)
  1271. (make-variable-buffer-local 'org-todo-sets)
  1272. (defvar org-todo-log-states nil)
  1273. (make-variable-buffer-local 'org-todo-log-states)
  1274. (defvar org-todo-kwd-alist nil)
  1275. (make-variable-buffer-local 'org-todo-kwd-alist)
  1276. (defvar org-todo-key-alist nil)
  1277. (make-variable-buffer-local 'org-todo-key-alist)
  1278. (defvar org-todo-key-trigger nil)
  1279. (make-variable-buffer-local 'org-todo-key-trigger)
  1280. (defcustom org-todo-interpretation 'sequence
  1281. "Controls how TODO keywords are interpreted.
  1282. This variable is in principle obsolete and is only used for
  1283. backward compatibility, if the interpretation of todo keywords is
  1284. not given already in `org-todo-keywords'. See that variable for
  1285. more information."
  1286. :group 'org-todo
  1287. :group 'org-keywords
  1288. :type '(choice (const sequence)
  1289. (const type)))
  1290. (defcustom org-use-fast-todo-selection 'prefix
  1291. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1292. This variable describes if and under what circumstances the cycling
  1293. mechanism for TODO keywords will be replaced by a single-key, direct
  1294. selection scheme.
  1295. When nil, fast selection is never used.
  1296. When the symbol `prefix', it will be used when `org-todo' is called with
  1297. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1298. in an agenda buffer.
  1299. When t, fast selection is used by default. In this case, the prefix
  1300. argument forces cycling instead.
  1301. In all cases, the special interface is only used if access keys have actually
  1302. been assigned by the user, i.e. if keywords in the configuration are followed
  1303. by a letter in parenthesis, like TODO(t)."
  1304. :group 'org-todo
  1305. :type '(choice
  1306. (const :tag "Never" nil)
  1307. (const :tag "By default" t)
  1308. (const :tag "Only with C-u C-c C-t" prefix)))
  1309. (defcustom org-after-todo-state-change-hook nil
  1310. "Hook which is run after the state of a TODO item was changed.
  1311. The new state (a string with a TODO keyword, or nil) is available in the
  1312. Lisp variable `state'."
  1313. :group 'org-todo
  1314. :type 'hook)
  1315. (defcustom org-log-done nil
  1316. "Non-nil means, record a CLOSED timestamp when moving an entry to DONE.
  1317. When equal to the list (done), also prompt for a closing note.
  1318. This can also be configured on a per-file basis by adding one of
  1319. the following lines anywhere in the buffer:
  1320. #+STARTUP: logdone
  1321. #+STARTUP: lognotedone
  1322. #+STARTUP: nologdone"
  1323. :group 'org-todo
  1324. :group 'org-progress
  1325. :type '(choice
  1326. (const :tag "No logging" nil)
  1327. (const :tag "Record CLOSED timestamp" time)
  1328. (const :tag "Record CLOSED timestamp with closing note." note)))
  1329. ;; Normalize old uses of org-log-done.
  1330. (cond
  1331. ((eq org-log-done t) (setq org-log-done 'time))
  1332. ((and (listp org-log-done) (memq 'done org-log-done))
  1333. (setq org-log-done 'note)))
  1334. (defcustom org-log-note-clock-out nil
  1335. "Non-nil means, recored a note when clocking out of an item.
  1336. This can also be configured on a per-file basis by adding one of
  1337. the following lines anywhere in the buffer:
  1338. #+STARTUP: lognoteclock-out
  1339. #+STARTUP: nolognoteclock-out"
  1340. :group 'org-todo
  1341. :group 'org-progress
  1342. :type 'boolean)
  1343. (defcustom org-log-done-with-time t
  1344. "Non-nil means, the CLOSED time stamp will contain date and time.
  1345. When nil, only the date will be recorded."
  1346. :group 'org-progress
  1347. :type 'boolean)
  1348. (defcustom org-log-note-headings
  1349. '((done . "CLOSING NOTE %t")
  1350. (state . "State %-12s %t")
  1351. (note . "Note taken on %t")
  1352. (clock-out . ""))
  1353. "Headings for notes added to entries.
  1354. The value is an alist, with the car being a symbol indicating the note
  1355. context, and the cdr is the heading to be used. The heading may also be the
  1356. empty string.
  1357. %t in the heading will be replaced by a time stamp.
  1358. %s will be replaced by the new TODO state, in double quotes.
  1359. %u will be replaced by the user name.
  1360. %U will be replaced by the full user name."
  1361. :group 'org-todo
  1362. :group 'org-progress
  1363. :type '(list :greedy t
  1364. (cons (const :tag "Heading when closing an item" done) string)
  1365. (cons (const :tag
  1366. "Heading when changing todo state (todo sequence only)"
  1367. state) string)
  1368. (cons (const :tag "Heading when just taking a note" note) string)
  1369. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1370. (unless (assq 'note org-log-note-headings)
  1371. (push '(note . "%t") org-log-note-headings))
  1372. (defcustom org-log-states-order-reversed t
  1373. "Non-nil means, the latest state change note will be directly after heading.
  1374. When nil, the notes will be orderer according to time."
  1375. :group 'org-todo
  1376. :group 'org-progress
  1377. :type 'boolean)
  1378. (defcustom org-log-repeat 'time
  1379. "Non-nil means, record moving through the DONE state when triggering repeat.
  1380. An auto-repeating tasks is immediately switched back to TODO when marked
  1381. done. If you are not logging state changes (by adding \"@\" or \"!\" to
  1382. the TODO keyword definition, or recording a cloing note by setting
  1383. `org-log-done', there will be no record of the task moving trhough DONE.
  1384. This variable forces taking a note anyway. Possible values are:
  1385. nil Don't force a record
  1386. time Record a time stamp
  1387. note Record a note
  1388. This option can also be set with on a per-file-basis with
  1389. #+STARTUP: logrepeat
  1390. #+STARTUP: lognoterepeat
  1391. #+STARTUP: nologrepeat
  1392. You can have local logging settings for a subtree by setting the LOGGING
  1393. property to one or more of these keywords."
  1394. :group 'org-todo
  1395. :group 'org-progress
  1396. :type '(choice
  1397. (const :tag "Don't force a record" nil)
  1398. (const :tag "Force recording the DONE state" time)
  1399. (const :tag "Force recording a note with the DONE state" note)))
  1400. (defgroup org-priorities nil
  1401. "Priorities in Org-mode."
  1402. :tag "Org Priorities"
  1403. :group 'org-todo)
  1404. (defcustom org-highest-priority ?A
  1405. "The highest priority of TODO items. A character like ?A, ?B etc.
  1406. Must have a smaller ASCII number than `org-lowest-priority'."
  1407. :group 'org-priorities
  1408. :type 'character)
  1409. (defcustom org-lowest-priority ?C
  1410. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1411. Must have a larger ASCII number than `org-highest-priority'."
  1412. :group 'org-priorities
  1413. :type 'character)
  1414. (defcustom org-default-priority ?B
  1415. "The default priority of TODO items.
  1416. This is the priority an item get if no explicit priority is given."
  1417. :group 'org-priorities
  1418. :type 'character)
  1419. (defcustom org-priority-start-cycle-with-default t
  1420. "Non-nil means, start with default priority when starting to cycle.
  1421. When this is nil, the first step in the cycle will be (depending on the
  1422. command used) one higher or lower that the default priority."
  1423. :group 'org-priorities
  1424. :type 'boolean)
  1425. (defgroup org-time nil
  1426. "Options concerning time stamps and deadlines in Org-mode."
  1427. :tag "Org Time"
  1428. :group 'org)
  1429. (defcustom org-insert-labeled-timestamps-at-point nil
  1430. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1431. When nil, these labeled time stamps are forces into the second line of an
  1432. entry, just after the headline. When scheduling from the global TODO list,
  1433. the time stamp will always be forced into the second line."
  1434. :group 'org-time
  1435. :type 'boolean)
  1436. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1437. "Formats for `format-time-string' which are used for time stamps.
  1438. It is not recommended to change this constant.")
  1439. (defcustom org-time-stamp-rounding-minutes '(0 5)
  1440. "Number of minutes to round time stamps to.
  1441. These are two values, the first applies when first creating a time stamp.
  1442. The second applies when changing it with the commands `S-up' and `S-down'.
  1443. When changing the time stamp, this means that it will change in steps
  1444. of N minutes, as given by the second value.
  1445. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  1446. numbers should be factors of 60, so for example 5, 10, 15.
  1447. When this is larger than 1, you can still force an exact time-stamp by using
  1448. a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
  1449. and by using a prefix arg to `S-up/down' to specify the exact number
  1450. of minutes to shift."
  1451. :group 'org-time
  1452. :get '(lambda (var) ; Make sure all entries have 5 elements
  1453. (if (integerp (default-value var))
  1454. (list (default-value var) 5)
  1455. (default-value var)))
  1456. :type '(list
  1457. (integer :tag "when inserting times")
  1458. (integer :tag "when modifying times")))
  1459. ;; Normalize old customizations of this variable.
  1460. (when (integerp org-time-stamp-rounding-minutes)
  1461. (setq org-time-stamp-rounding-minutes
  1462. (list org-time-stamp-rounding-minutes
  1463. org-time-stamp-rounding-minutes)))
  1464. (defcustom org-display-custom-times nil
  1465. "Non-nil means, overlay custom formats over all time stamps.
  1466. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1467. To turn this on on a per-file basis, insert anywhere in the file:
  1468. #+STARTUP: customtime"
  1469. :group 'org-time
  1470. :set 'set-default
  1471. :type 'sexp)
  1472. (make-variable-buffer-local 'org-display-custom-times)
  1473. (defcustom org-time-stamp-custom-formats
  1474. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1475. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1476. These are overlayed over the default ISO format if the variable
  1477. `org-display-custom-times' is set. Time like %H:%M should be at the
  1478. end of the second format."
  1479. :group 'org-time
  1480. :type 'sexp)
  1481. (defun org-time-stamp-format (&optional long inactive)
  1482. "Get the right format for a time string."
  1483. (let ((f (if long (cdr org-time-stamp-formats)
  1484. (car org-time-stamp-formats))))
  1485. (if inactive
  1486. (concat "[" (substring f 1 -1) "]")
  1487. f)))
  1488. (defcustom org-deadline-warning-days 14
  1489. "No. of days before expiration during which a deadline becomes active.
  1490. This variable governs the display in sparse trees and in the agenda.
  1491. When 0 or negative, it means use this number (the absolute value of it)
  1492. even if a deadline has a different individual lead time specified."
  1493. :group 'org-time
  1494. :group 'org-agenda-daily/weekly
  1495. :type 'number)
  1496. (defcustom org-read-date-prefer-future t
  1497. "Non-nil means, assume future for incomplete date input from user.
  1498. This affects the following situations:
  1499. 1. The user gives a day, but no month.
  1500. For example, if today is the 15th, and you enter \"3\", Org-mode will
  1501. read this as the third of *next* month. However, if you enter \"17\",
  1502. it will be considered as *this* month.
  1503. 2. The user gives a month but not a year.
  1504. For example, if it is april and you enter \"feb 2\", this will be read
  1505. as feb 2, *next* year. \"May 5\", however, will be this year.
  1506. Currently this does not work for ISO week specifications.
  1507. When this option is nil, the current month and year will always be used
  1508. as defaults."
  1509. :group 'org-time
  1510. :type 'boolean)
  1511. (defcustom org-read-date-display-live t
  1512. "Non-nil means, display current interpretation of date prompt live.
  1513. This display will be in an overlay, in the minibuffer."
  1514. :group 'org-time
  1515. :type 'boolean)
  1516. (defcustom org-read-date-popup-calendar t
  1517. "Non-nil means, pop up a calendar when prompting for a date.
  1518. In the calendar, the date can be selected with mouse-1. However, the
  1519. minibuffer will also be active, and you can simply enter the date as well.
  1520. When nil, only the minibuffer will be available."
  1521. :group 'org-time
  1522. :type 'boolean)
  1523. (if (fboundp 'defvaralias)
  1524. (defvaralias 'org-popup-calendar-for-date-prompt
  1525. 'org-read-date-popup-calendar))
  1526. (defcustom org-extend-today-until 0
  1527. "The hour when your day really ends.
  1528. This has influence for the following applications:
  1529. - When switching the agenda to \"today\". It it is still earlier than
  1530. the time given here, the day recognized as TODAY is actually yesterday.
  1531. - When a date is read from the user and it is still before the time given
  1532. here, the current date and time will be assumed to be yesterday, 23:59.
  1533. FIXME:
  1534. IMPORTANT: This is still a very experimental feature, it may disappear
  1535. again or it may be extended to mean more things."
  1536. :group 'org-time
  1537. :type 'number)
  1538. (defcustom org-edit-timestamp-down-means-later nil
  1539. "Non-nil means, S-down will increase the time in a time stamp.
  1540. When nil, S-up will increase."
  1541. :group 'org-time
  1542. :type 'boolean)
  1543. (defcustom org-calendar-follow-timestamp-change t
  1544. "Non-nil means, make the calendar window follow timestamp changes.
  1545. When a timestamp is modified and the calendar window is visible, it will be
  1546. moved to the new date."
  1547. :group 'org-time
  1548. :type 'boolean)
  1549. (defgroup org-tags nil
  1550. "Options concerning tags in Org-mode."
  1551. :tag "Org Tags"
  1552. :group 'org)
  1553. (defcustom org-tag-alist nil
  1554. "List of tags allowed in Org-mode files.
  1555. When this list is nil, Org-mode will base TAG input on what is already in the
  1556. buffer.
  1557. The value of this variable is an alist, the car of each entry must be a
  1558. keyword as a string, the cdr may be a character that is used to select
  1559. that tag through the fast-tag-selection interface.
  1560. See the manual for details."
  1561. :group 'org-tags
  1562. :type '(repeat
  1563. (choice
  1564. (cons (string :tag "Tag name")
  1565. (character :tag "Access char"))
  1566. (const :tag "Start radio group" (:startgroup))
  1567. (const :tag "End radio group" (:endgroup)))))
  1568. (defcustom org-use-fast-tag-selection 'auto
  1569. "Non-nil means, use fast tag selection scheme.
  1570. This is a special interface to select and deselect tags with single keys.
  1571. When nil, fast selection is never used.
  1572. When the symbol `auto', fast selection is used if and only if selection
  1573. characters for tags have been configured, either through the variable
  1574. `org-tag-alist' or through a #+TAGS line in the buffer.
  1575. When t, fast selection is always used and selection keys are assigned
  1576. automatically if necessary."
  1577. :group 'org-tags
  1578. :type '(choice
  1579. (const :tag "Always" t)
  1580. (const :tag "Never" nil)
  1581. (const :tag "When selection characters are configured" 'auto)))
  1582. (defcustom org-fast-tag-selection-single-key nil
  1583. "Non-nil means, fast tag selection exits after first change.
  1584. When nil, you have to press RET to exit it.
  1585. During fast tag selection, you can toggle this flag with `C-c'.
  1586. This variable can also have the value `expert'. In this case, the window
  1587. displaying the tags menu is not even shown, until you press C-c again."
  1588. :group 'org-tags
  1589. :type '(choice
  1590. (const :tag "No" nil)
  1591. (const :tag "Yes" t)
  1592. (const :tag "Expert" expert)))
  1593. (defvar org-fast-tag-selection-include-todo nil
  1594. "Non-nil means, fast tags selection interface will also offer TODO states.
  1595. This is an undocumented feature, you should not rely on it.")
  1596. (defcustom org-tags-column -80
  1597. "The column to which tags should be indented in a headline.
  1598. If this number is positive, it specifies the column. If it is negative,
  1599. it means that the tags should be flushright to that column. For example,
  1600. -80 works well for a normal 80 character screen."
  1601. :group 'org-tags
  1602. :type 'integer)
  1603. (defcustom org-auto-align-tags t
  1604. "Non-nil means, realign tags after pro/demotion of TODO state change.
  1605. These operations change the length of a headline and therefore shift
  1606. the tags around. With this options turned on, after each such operation
  1607. the tags are again aligned to `org-tags-column'."
  1608. :group 'org-tags
  1609. :type 'boolean)
  1610. (defcustom org-use-tag-inheritance t
  1611. "Non-nil means, tags in levels apply also for sublevels.
  1612. When nil, only the tags directly given in a specific line apply there.
  1613. If you turn off this option, you very likely want to turn on the
  1614. companion option `org-tags-match-list-sublevels'.
  1615. This may also be a list of tags that should be inherited, or a regexp that
  1616. matches tags that should be inherited."
  1617. :group 'org-tags
  1618. :type '(choice
  1619. (const :tag "Not" nil)
  1620. (const :tag "Always" t)
  1621. (repeat :tag "Specific tags" (string :tag "Tag"))
  1622. (regexp :tag "Tags matched by regexp")))
  1623. (defun org-tag-inherit-p (tag)
  1624. "Check if TAG is one that should be inherited."
  1625. (cond
  1626. ((eq org-use-tag-inheritance t) t)
  1627. ((not org-use-tag-inheritance) nil)
  1628. ((stringp org-use-tag-inheritance)
  1629. (string-match org-use-tag-inheritance tag))
  1630. ((listp org-use-tag-inheritance)
  1631. (member tag org-use-tag-inheritance))
  1632. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  1633. (defcustom org-tags-match-list-sublevels nil
  1634. "Non-nil means list also sublevels of headlines matching tag search.
  1635. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  1636. the sublevels of a headline matching a tag search often also match
  1637. the same search. Listing all of them can create very long lists.
  1638. Setting this variable to nil causes subtrees of a match to be skipped.
  1639. This option is off by default, because inheritance in on. If you turn
  1640. inheritance off, you very likely want to turn this option on.
  1641. As a special case, if the tag search is restricted to TODO items, the
  1642. value of this variable is ignored and sublevels are always checked, to
  1643. make sure all corresponding TODO items find their way into the list."
  1644. :group 'org-tags
  1645. :type 'boolean)
  1646. (defvar org-tags-history nil
  1647. "History of minibuffer reads for tags.")
  1648. (defvar org-last-tags-completion-table nil
  1649. "The last used completion table for tags.")
  1650. (defvar org-after-tags-change-hook nil
  1651. "Hook that is run after the tags in a line have changed.")
  1652. (defgroup org-properties nil
  1653. "Options concerning properties in Org-mode."
  1654. :tag "Org Properties"
  1655. :group 'org)
  1656. (defcustom org-property-format "%-10s %s"
  1657. "How property key/value pairs should be formatted by `indent-line'.
  1658. When `indent-line' hits a property definition, it will format the line
  1659. according to this format, mainly to make sure that the values are
  1660. lined-up with respect to each other."
  1661. :group 'org-properties
  1662. :type 'string)
  1663. (defcustom org-use-property-inheritance nil
  1664. "Non-nil means, properties apply also for sublevels.
  1665. This setting is chiefly used during property searches. Turning it on can
  1666. cause significant overhead when doing a search, which is why it is not
  1667. on by default.
  1668. When nil, only the properties directly given in the current entry count.
  1669. When t, every property is inherited. The value may also be a list of
  1670. properties that should have inheritance, or a regular expression matching
  1671. properties that should be inherited.
  1672. However, note that some special properties use inheritance under special
  1673. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  1674. and the properties ending in \"_ALL\" when they are used as descriptor
  1675. for valid values of a property.
  1676. Note for programmers:
  1677. When querying an entry with `org-entry-get', you can control if inheritance
  1678. should be used. By default, `org-entry-get' looks only at the local
  1679. properties. You can request inheritance by setting the inherit argument
  1680. to t (to force inheritance) or to `selective' (to respect the setting
  1681. in this variable)."
  1682. :group 'org-properties
  1683. :type '(choice
  1684. (const :tag "Not" nil)
  1685. (const :tag "Always" t)
  1686. (repeat :tag "Specific properties" (string :tag "Property"))
  1687. (regexp :tag "Properties matched by regexp")))
  1688. (defun org-property-inherit-p (property)
  1689. "Check if PROPERTY is one that should be inherited."
  1690. (cond
  1691. ((eq org-use-property-inheritance t) t)
  1692. ((not org-use-property-inheritance) nil)
  1693. ((stringp org-use-property-inheritance)
  1694. (string-match org-use-property-inheritance property))
  1695. ((listp org-use-property-inheritance)
  1696. (member property org-use-property-inheritance))
  1697. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  1698. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  1699. "The default column format, if no other format has been defined.
  1700. This variable can be set on the per-file basis by inserting a line
  1701. #+COLUMNS: %25ITEM ....."
  1702. :group 'org-properties
  1703. :type 'string)
  1704. (defcustom org-global-properties nil
  1705. "List of property/value pairs that can be inherited by any entry.
  1706. You can set buffer-local values for this by adding lines like
  1707. #+PROPERTY: NAME VALUE"
  1708. :group 'org-properties
  1709. :type '(repeat
  1710. (cons (string :tag "Property")
  1711. (string :tag "Value"))))
  1712. (defvar org-local-properties nil
  1713. "List of property/value pairs that can be inherited by any entry.
  1714. Valid for the current buffer.
  1715. This variable is populated from #+PROPERTY lines.")
  1716. (defgroup org-agenda nil
  1717. "Options concerning agenda views in Org-mode."
  1718. :tag "Org Agenda"
  1719. :group 'org)
  1720. (defvar org-category nil
  1721. "Variable used by org files to set a category for agenda display.
  1722. Such files should use a file variable to set it, for example
  1723. # -*- mode: org; org-category: \"ELisp\"
  1724. or contain a special line
  1725. #+CATEGORY: ELisp
  1726. If the file does not specify a category, then file's base name
  1727. is used instead.")
  1728. (make-variable-buffer-local 'org-category)
  1729. (defcustom org-agenda-files nil
  1730. "The files to be used for agenda display.
  1731. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  1732. \\[org-remove-file]. You can also use customize to edit the list.
  1733. If an entry is a directory, all files in that directory that are matched by
  1734. `org-agenda-file-regexp' will be part of the file list.
  1735. If the value of the variable is not a list but a single file name, then
  1736. the list of agenda files is actually stored and maintained in that file, one
  1737. agenda file per line."
  1738. :group 'org-agenda
  1739. :type '(choice
  1740. (repeat :tag "List of files and directories" file)
  1741. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  1742. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  1743. "Regular expression to match files for `org-agenda-files'.
  1744. If any element in the list in that variable contains a directory instead
  1745. of a normal file, all files in that directory that are matched by this
  1746. regular expression will be included."
  1747. :group 'org-agenda
  1748. :type 'regexp)
  1749. (defcustom org-agenda-text-search-extra-files nil
  1750. "List of extra files to be searched by text search commands.
  1751. These files will be search in addition to the agenda files bu the
  1752. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  1753. Note that these files will only be searched for text search commands,
  1754. not for the other agenda views like todo lists, tag earches or the weekly
  1755. agenda. This variable is intended to list notes and possibly archive files
  1756. that should also be searched by these two commands."
  1757. :group 'org-agenda
  1758. :type '(repeat file))
  1759. (if (fboundp 'defvaralias)
  1760. (defvaralias 'org-agenda-multi-occur-extra-files
  1761. 'org-agenda-text-search-extra-files))
  1762. (defcustom org-agenda-skip-unavailable-files nil
  1763. "t means to just skip non-reachable files in `org-agenda-files'.
  1764. Nil means to remove them, after a query, from the list."
  1765. :group 'org-agenda
  1766. :type 'boolean)
  1767. (defcustom org-calendar-to-agenda-key [?c]
  1768. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  1769. The command `org-calendar-goto-agenda' will be bound to this key. The
  1770. default is the character `c' because then `c' can be used to switch back and
  1771. forth between agenda and calendar."
  1772. :group 'org-agenda
  1773. :type 'sexp)
  1774. (eval-after-load "calendar"
  1775. '(org-defkey calendar-mode-map org-calendar-to-agenda-key
  1776. 'org-calendar-goto-agenda))
  1777. (defgroup org-latex nil
  1778. "Options for embedding LaTeX code into Org-mode."
  1779. :tag "Org LaTeX"
  1780. :group 'org)
  1781. (defcustom org-format-latex-options
  1782. '(:foreground default :background default :scale 1.0
  1783. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  1784. :matchers ("begin" "$" "$$" "\\(" "\\["))
  1785. "Options for creating images from LaTeX fragments.
  1786. This is a property list with the following properties:
  1787. :foreground the foreground color for images embedded in emacs, e.g. \"Black\".
  1788. `default' means use the forground of the default face.
  1789. :background the background color, or \"Transparent\".
  1790. `default' means use the background of the default face.
  1791. :scale a scaling factor for the size of the images
  1792. :html-foreground, :html-background, :html-scale
  1793. The same numbers for HTML export.
  1794. :matchers a list indicating which matchers should be used to
  1795. find LaTeX fragments. Valid members of this list are:
  1796. \"begin\" find environments
  1797. \"$\" find math expressions surrounded by $...$
  1798. \"$$\" find math expressions surrounded by $$....$$
  1799. \"\\(\" find math expressions surrounded by \\(...\\)
  1800. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  1801. :group 'org-latex
  1802. :type 'plist)
  1803. (defcustom org-format-latex-header "\\documentclass{article}
  1804. \\usepackage{fullpage} % do not remove
  1805. \\usepackage{amssymb}
  1806. \\usepackage[usenames]{color}
  1807. \\usepackage{amsmath}
  1808. \\usepackage{latexsym}
  1809. \\usepackage[mathscr]{eucal}
  1810. \\pagestyle{empty} % do not remove"
  1811. "The document header used for processing LaTeX fragments."
  1812. :group 'org-latex
  1813. :type 'string)
  1814. (defgroup org-font-lock nil
  1815. "Font-lock settings for highlighting in Org-mode."
  1816. :tag "Org Font Lock"
  1817. :group 'org)
  1818. (defcustom org-level-color-stars-only nil
  1819. "Non-nil means fontify only the stars in each headline.
  1820. When nil, the entire headline is fontified.
  1821. Changing it requires restart of `font-lock-mode' to become effective
  1822. also in regions already fontified."
  1823. :group 'org-font-lock
  1824. :type 'boolean)
  1825. (defcustom org-hide-leading-stars nil
  1826. "Non-nil means, hide the first N-1 stars in a headline.
  1827. This works by using the face `org-hide' for these stars. This
  1828. face is white for a light background, and black for a dark
  1829. background. You may have to customize the face `org-hide' to
  1830. make this work.
  1831. Changing it requires restart of `font-lock-mode' to become effective
  1832. also in regions already fontified.
  1833. You may also set this on a per-file basis by adding one of the following
  1834. lines to the buffer:
  1835. #+STARTUP: hidestars
  1836. #+STARTUP: showstars"
  1837. :group 'org-font-lock
  1838. :type 'boolean)
  1839. (defcustom org-fontify-done-headline nil
  1840. "Non-nil means, change the face of a headline if it is marked DONE.
  1841. Normally, only the TODO/DONE keyword indicates the state of a headline.
  1842. When this is non-nil, the headline after the keyword is set to the
  1843. `org-headline-done' as an additional indication."
  1844. :group 'org-font-lock
  1845. :type 'boolean)
  1846. (defcustom org-fontify-emphasized-text t
  1847. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  1848. Changing this variable requires a restart of Emacs to take effect."
  1849. :group 'org-font-lock
  1850. :type 'boolean)
  1851. (defcustom org-highlight-latex-fragments-and-specials nil
  1852. "Non-nil means, fontify what is treated specially by the exporters."
  1853. :group 'org-font-lock
  1854. :type 'boolean)
  1855. (defcustom org-hide-emphasis-markers nil
  1856. "Non-nil mean font-lock should hide the emphasis marker characters."
  1857. :group 'org-font-lock
  1858. :type 'boolean)
  1859. (defvar org-emph-re nil
  1860. "Regular expression for matching emphasis.")
  1861. (defvar org-verbatim-re nil
  1862. "Regular expression for matching verbatim text.")
  1863. (defvar org-emphasis-regexp-components) ; defined just below
  1864. (defvar org-emphasis-alist) ; defined just below
  1865. (defun org-set-emph-re (var val)
  1866. "Set variable and compute the emphasis regular expression."
  1867. (set var val)
  1868. (when (and (boundp 'org-emphasis-alist)
  1869. (boundp 'org-emphasis-regexp-components)
  1870. org-emphasis-alist org-emphasis-regexp-components)
  1871. (let* ((e org-emphasis-regexp-components)
  1872. (pre (car e))
  1873. (post (nth 1 e))
  1874. (border (nth 2 e))
  1875. (body (nth 3 e))
  1876. (nl (nth 4 e))
  1877. (stacked (and nil (nth 5 e))) ; stacked is no longer allowed, forced to nil
  1878. (body1 (concat body "*?"))
  1879. (markers (mapconcat 'car org-emphasis-alist ""))
  1880. (vmarkers (mapconcat
  1881. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  1882. org-emphasis-alist "")))
  1883. ;; make sure special characters appear at the right position in the class
  1884. (if (string-match "\\^" markers)
  1885. (setq markers (concat (replace-match "" t t markers) "^")))
  1886. (if (string-match "-" markers)
  1887. (setq markers (concat (replace-match "" t t markers) "-")))
  1888. (if (string-match "\\^" vmarkers)
  1889. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  1890. (if (string-match "-" vmarkers)
  1891. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  1892. (if (> nl 0)
  1893. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  1894. (int-to-string nl) "\\}")))
  1895. ;; Make the regexp
  1896. (setq org-emph-re
  1897. (concat "\\([" pre (if (and nil stacked) markers) "]\\|^\\)"
  1898. "\\("
  1899. "\\([" markers "]\\)"
  1900. "\\("
  1901. "[^" border "]\\|"
  1902. "[^" border (if (and nil stacked) markers) "]"
  1903. body1
  1904. "[^" border (if (and nil stacked) markers) "]"
  1905. "\\)"
  1906. "\\3\\)"
  1907. "\\([" post (if (and nil stacked) markers) "]\\|$\\)"))
  1908. (setq org-verbatim-re
  1909. (concat "\\([" pre "]\\|^\\)"
  1910. "\\("
  1911. "\\([" vmarkers "]\\)"
  1912. "\\("
  1913. "[^" border "]\\|"
  1914. "[^" border "]"
  1915. body1
  1916. "[^" border "]"
  1917. "\\)"
  1918. "\\3\\)"
  1919. "\\([" post "]\\|$\\)")))))
  1920. (defcustom org-emphasis-regexp-components
  1921. '(" \t('\"" "- \t.,:?;'\")" " \t\r\n,\"'" "." 1)
  1922. "Components used to build the regular expression for emphasis.
  1923. This is a list with 6 entries. Terminology: In an emphasis string
  1924. like \" *strong word* \", we call the initial space PREMATCH, the final
  1925. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  1926. and \"trong wor\" is the body. The different components in this variable
  1927. specify what is allowed/forbidden in each part:
  1928. pre Chars allowed as prematch. Beginning of line will be allowed too.
  1929. post Chars allowed as postmatch. End of line will be allowed too.
  1930. border The chars *forbidden* as border characters.
  1931. body-regexp A regexp like \".\" to match a body character. Don't use
  1932. non-shy groups here, and don't allow newline here.
  1933. newline The maximum number of newlines allowed in an emphasis exp.
  1934. Use customize to modify this, or restart Emacs after changing it."
  1935. :group 'org-font-lock
  1936. :set 'org-set-emph-re
  1937. :type '(list
  1938. (sexp :tag "Allowed chars in pre ")
  1939. (sexp :tag "Allowed chars in post ")
  1940. (sexp :tag "Forbidden chars in border ")
  1941. (sexp :tag "Regexp for body ")
  1942. (integer :tag "number of newlines allowed")
  1943. (option (boolean :tag "Stacking (DISABLED) "))))
  1944. (defcustom org-emphasis-alist
  1945. '(("*" bold "<b>" "</b>")
  1946. ("/" italic "<i>" "</i>")
  1947. ("_" underline "<u>" "</u>")
  1948. ("=" org-code "<code>" "</code>" verbatim)
  1949. ("~" org-verbatim "" "" verbatim)
  1950. ("+" (:strike-through t) "<del>" "</del>")
  1951. )
  1952. "Special syntax for emphasized text.
  1953. Text starting and ending with a special character will be emphasized, for
  1954. example *bold*, _underlined_ and /italic/. This variable sets the marker
  1955. characters, the face to be used by font-lock for highlighting in Org-mode
  1956. Emacs buffers, and the HTML tags to be used for this.
  1957. Use customize to modify this, or restart Emacs after changing it."
  1958. :group 'org-font-lock
  1959. :set 'org-set-emph-re
  1960. :type '(repeat
  1961. (list
  1962. (string :tag "Marker character")
  1963. (choice
  1964. (face :tag "Font-lock-face")
  1965. (plist :tag "Face property list"))
  1966. (string :tag "HTML start tag")
  1967. (string :tag "HTML end tag")
  1968. (option (const verbatim)))))
  1969. ;;; Miscellaneous options
  1970. (defgroup org-completion nil
  1971. "Completion in Org-mode."
  1972. :tag "Org Completion"
  1973. :group 'org)
  1974. (defcustom org-completion-fallback-command 'hippie-expand
  1975. "The expansion command called by \\[org-complete] in normal context.
  1976. Normal means, no org-mode-specific context."
  1977. :group 'org-completion
  1978. :type 'function)
  1979. ;;; The faces
  1980. (defgroup org-faces nil
  1981. "Faces in Org-mode."
  1982. :tag "Org Faces"
  1983. :group 'org-font-lock)
  1984. (defface org-hide
  1985. '((((background light)) (:foreground "white"))
  1986. (((background dark)) (:foreground "black")))
  1987. "Face used to hide leading stars in headlines.
  1988. The forground color of this face should be equal to the background
  1989. color of the frame."
  1990. :group 'org-faces)
  1991. (defface org-level-1 ;; font-lock-function-name-face
  1992. (org-compatible-face 'outline-1
  1993. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  1994. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  1995. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  1996. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  1997. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  1998. (t (:bold t))))
  1999. "Face used for level 1 headlines."
  2000. :group 'org-faces)
  2001. (defface org-level-2 ;; font-lock-variable-name-face
  2002. (org-compatible-face 'outline-2
  2003. '((((class color) (min-colors 16) (background light)) (:foreground "DarkGoldenrod"))
  2004. (((class color) (min-colors 16) (background dark)) (:foreground "LightGoldenrod"))
  2005. (((class color) (min-colors 8) (background light)) (:foreground "yellow"))
  2006. (((class color) (min-colors 8) (background dark)) (:foreground "yellow" :bold t))
  2007. (t (:bold t))))
  2008. "Face used for level 2 headlines."
  2009. :group 'org-faces)
  2010. (defface org-level-3 ;; font-lock-keyword-face
  2011. (org-compatible-face 'outline-3
  2012. '((((class color) (min-colors 88) (background light)) (:foreground "Purple"))
  2013. (((class color) (min-colors 88) (background dark)) (:foreground "Cyan1"))
  2014. (((class color) (min-colors 16) (background light)) (:foreground "Purple"))
  2015. (((class color) (min-colors 16) (background dark)) (:foreground "Cyan"))
  2016. (((class color) (min-colors 8) (background light)) (:foreground "purple" :bold t))
  2017. (((class color) (min-colors 8) (background dark)) (:foreground "cyan" :bold t))
  2018. (t (:bold t))))
  2019. "Face used for level 3 headlines."
  2020. :group 'org-faces)
  2021. (defface org-level-4 ;; font-lock-comment-face
  2022. (org-compatible-face 'outline-4
  2023. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  2024. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  2025. (((class color) (min-colors 16) (background light)) (:foreground "red"))
  2026. (((class color) (min-colors 16) (background dark)) (:foreground "red1"))
  2027. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  2028. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  2029. (t (:bold t))))
  2030. "Face used for level 4 headlines."
  2031. :group 'org-faces)
  2032. (defface org-level-5 ;; font-lock-type-face
  2033. (org-compatible-face 'outline-5
  2034. '((((class color) (min-colors 16) (background light)) (:foreground "ForestGreen"))
  2035. (((class color) (min-colors 16) (background dark)) (:foreground "PaleGreen"))
  2036. (((class color) (min-colors 8)) (:foreground "green"))))
  2037. "Face used for level 5 headlines."
  2038. :group 'org-faces)
  2039. (defface org-level-6 ;; font-lock-constant-face
  2040. (org-compatible-face 'outline-6
  2041. '((((class color) (min-colors 16) (background light)) (:foreground "CadetBlue"))
  2042. (((class color) (min-colors 16) (background dark)) (:foreground "Aquamarine"))
  2043. (((class color) (min-colors 8)) (:foreground "magenta"))))
  2044. "Face used for level 6 headlines."
  2045. :group 'org-faces)
  2046. (defface org-level-7 ;; font-lock-builtin-face
  2047. (org-compatible-face 'outline-7
  2048. '((((class color) (min-colors 16) (background light)) (:foreground "Orchid"))
  2049. (((class color) (min-colors 16) (background dark)) (:foreground "LightSteelBlue"))
  2050. (((class color) (min-colors 8)) (:foreground "blue"))))
  2051. "Face used for level 7 headlines."
  2052. :group 'org-faces)
  2053. (defface org-level-8 ;; font-lock-string-face
  2054. (org-compatible-face 'outline-8
  2055. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  2056. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  2057. (((class color) (min-colors 8)) (:foreground "green"))))
  2058. "Face used for level 8 headlines."
  2059. :group 'org-faces)
  2060. (defface org-special-keyword ;; font-lock-string-face
  2061. (org-compatible-face nil
  2062. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  2063. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  2064. (t (:italic t))))
  2065. "Face used for special keywords."
  2066. :group 'org-faces)
  2067. (defface org-drawer ;; font-lock-function-name-face
  2068. (org-compatible-face nil
  2069. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  2070. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  2071. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  2072. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  2073. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  2074. (t (:bold t))))
  2075. "Face used for drawers."
  2076. :group 'org-faces)
  2077. (defface org-property-value nil
  2078. "Face used for the value of a property."
  2079. :group 'org-faces)
  2080. (defface org-column
  2081. (org-compatible-face nil
  2082. '((((class color) (min-colors 16) (background light))
  2083. (:background "grey90"))
  2084. (((class color) (min-colors 16) (background dark))
  2085. (:background "grey30"))
  2086. (((class color) (min-colors 8))
  2087. (:background "cyan" :foreground "black"))
  2088. (t (:inverse-video t))))
  2089. "Face for column display of entry properties."
  2090. :group 'org-faces)
  2091. (when (fboundp 'set-face-attribute)
  2092. ;; Make sure that a fixed-width face is used when we have a column table.
  2093. (set-face-attribute 'org-column nil
  2094. :height (face-attribute 'default :height)
  2095. :family (face-attribute 'default :family)))
  2096. (defface org-warning
  2097. (org-compatible-face 'font-lock-warning-face
  2098. '((((class color) (min-colors 16) (background light)) (:foreground "Red1" :bold t))
  2099. (((class color) (min-colors 16) (background dark)) (:foreground "Pink" :bold t))
  2100. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  2101. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  2102. (t (:bold t))))
  2103. "Face for deadlines and TODO keywords."
  2104. :group 'org-faces)
  2105. (defface org-archived ; similar to shadow
  2106. (org-compatible-face 'shadow
  2107. '((((class color grayscale) (min-colors 88) (background light))
  2108. (:foreground "grey50"))
  2109. (((class color grayscale) (min-colors 88) (background dark))
  2110. (:foreground "grey70"))
  2111. (((class color) (min-colors 8) (background light))
  2112. (:foreground "green"))
  2113. (((class color) (min-colors 8) (background dark))
  2114. (:foreground "yellow"))))
  2115. "Face for headline with the ARCHIVE tag."
  2116. :group 'org-faces)
  2117. (defface org-link
  2118. '((((class color) (background light)) (:foreground "Purple" :underline t))
  2119. (((class color) (background dark)) (:foreground "Cyan" :underline t))
  2120. (t (:underline t)))
  2121. "Face for links."
  2122. :group 'org-faces)
  2123. (defface org-ellipsis
  2124. '((((class color) (background light)) (:foreground "DarkGoldenrod" :underline t))
  2125. (((class color) (background dark)) (:foreground "LightGoldenrod" :underline t))
  2126. (t (:strike-through t)))
  2127. "Face for the ellipsis in folded text."
  2128. :group 'org-faces)
  2129. (defface org-target
  2130. '((((class color) (background light)) (:underline t))
  2131. (((class color) (background dark)) (:underline t))
  2132. (t (:underline t)))
  2133. "Face for links."
  2134. :group 'org-faces)
  2135. (defface org-date
  2136. '((((class color) (background light)) (:foreground "Purple" :underline t))
  2137. (((class color) (background dark)) (:foreground "Cyan" :underline t))
  2138. (t (:underline t)))
  2139. "Face for links."
  2140. :group 'org-faces)
  2141. (defface org-sexp-date
  2142. '((((class color) (background light)) (:foreground "Purple"))
  2143. (((class color) (background dark)) (:foreground "Cyan"))
  2144. (t (:underline t)))
  2145. "Face for links."
  2146. :group 'org-faces)
  2147. (defface org-tag
  2148. '((t (:bold t)))
  2149. "Face for tags."
  2150. :group 'org-faces)
  2151. (defface org-todo ; font-lock-warning-face
  2152. (org-compatible-face nil
  2153. '((((class color) (min-colors 16) (background light)) (:foreground "Red1" :bold t))
  2154. (((class color) (min-colors 16) (background dark)) (:foreground "Pink" :bold t))
  2155. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  2156. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  2157. (t (:inverse-video t :bold t))))
  2158. "Face for TODO keywords."
  2159. :group 'org-faces)
  2160. (defface org-done ;; font-lock-type-face
  2161. (org-compatible-face nil
  2162. '((((class color) (min-colors 16) (background light)) (:foreground "ForestGreen" :bold t))
  2163. (((class color) (min-colors 16) (background dark)) (:foreground "PaleGreen" :bold t))
  2164. (((class color) (min-colors 8)) (:foreground "green"))
  2165. (t (:bold t))))
  2166. "Face used for todo keywords that indicate DONE items."
  2167. :group 'org-faces)
  2168. (defface org-headline-done ;; font-lock-string-face
  2169. (org-compatible-face nil
  2170. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  2171. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  2172. (((class color) (min-colors 8) (background light)) (:bold nil))))
  2173. "Face used to indicate that a headline is DONE.
  2174. This face is only used if `org-fontify-done-headline' is set. If applies
  2175. to the part of the headline after the DONE keyword."
  2176. :group 'org-faces)
  2177. (defcustom org-todo-keyword-faces nil
  2178. "Faces for specific TODO keywords.
  2179. This is a list of cons cells, with TODO keywords in the car
  2180. and faces in the cdr. The face can be a symbol, or a property
  2181. list of attributes, like (:foreground \"blue\" :weight bold :underline t)."
  2182. :group 'org-faces
  2183. :group 'org-todo
  2184. :type '(repeat
  2185. (cons
  2186. (string :tag "keyword")
  2187. (sexp :tag "face"))))
  2188. (defface org-table ;; font-lock-function-name-face
  2189. (org-compatible-face nil
  2190. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  2191. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  2192. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  2193. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  2194. (((class color) (min-colors 8) (background light)) (:foreground "blue"))
  2195. (((class color) (min-colors 8) (background dark)))))
  2196. "Face used for tables."
  2197. :group 'org-faces)
  2198. (defface org-formula
  2199. (org-compatible-face nil
  2200. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  2201. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  2202. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  2203. (((class color) (min-colors 8) (background dark)) (:foreground "red"))
  2204. (t (:bold t :italic t))))
  2205. "Face for formulas."
  2206. :group 'org-faces)
  2207. (defface org-code
  2208. (org-compatible-face nil
  2209. '((((class color grayscale) (min-colors 88) (background light))
  2210. (:foreground "grey50"))
  2211. (((class color grayscale) (min-colors 88) (background dark))
  2212. (:foreground "grey70"))
  2213. (((class color) (min-colors 8) (background light))
  2214. (:foreground "green"))
  2215. (((class color) (min-colors 8) (background dark))
  2216. (:foreground "yellow"))))
  2217. "Face for fixed-with text like code snippets."
  2218. :group 'org-faces
  2219. :version "22.1")
  2220. (defface org-verbatim
  2221. (org-compatible-face nil
  2222. '((((class color grayscale) (min-colors 88) (background light))
  2223. (:foreground "grey50" :underline t))
  2224. (((class color grayscale) (min-colors 88) (background dark))
  2225. (:foreground "grey70" :underline t))
  2226. (((class color) (min-colors 8) (background light))
  2227. (:foreground "green" :underline t))
  2228. (((class color) (min-colors 8) (background dark))
  2229. (:foreground "yellow" :underline t))))
  2230. "Face for fixed-with text like code snippets."
  2231. :group 'org-faces
  2232. :version "22.1")
  2233. (defface org-agenda-structure ;; font-lock-function-name-face
  2234. (org-compatible-face nil
  2235. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  2236. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  2237. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  2238. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  2239. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  2240. (t (:bold t))))
  2241. "Face used in agenda for captions and dates."
  2242. :group 'org-faces)
  2243. (defface org-agenda-date
  2244. (org-compatible-face nil
  2245. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  2246. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  2247. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  2248. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  2249. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  2250. (t (:bold t))))
  2251. "Face used in agenda for captions and dates."
  2252. :group 'org-faces)
  2253. (defface org-agenda-date-weekend
  2254. (org-compatible-face nil
  2255. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1" :weight bold))
  2256. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  2257. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  2258. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  2259. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  2260. (t (:bold t))))
  2261. "Face used in agenda for captions and dates."
  2262. :group 'org-faces)
  2263. (defface org-scheduled-today
  2264. (org-compatible-face nil
  2265. '((((class color) (min-colors 88) (background light)) (:foreground "DarkGreen"))
  2266. (((class color) (min-colors 88) (background dark)) (:foreground "PaleGreen"))
  2267. (((class color) (min-colors 8)) (:foreground "green"))
  2268. (t (:bold t :italic t))))
  2269. "Face for items scheduled for a certain day."
  2270. :group 'org-faces)
  2271. (defface org-scheduled-previously
  2272. (org-compatible-face nil
  2273. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  2274. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  2275. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  2276. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  2277. (t (:bold t))))
  2278. "Face for items scheduled previously, and not yet done."
  2279. :group 'org-faces)
  2280. (defface org-upcoming-deadline
  2281. (org-compatible-face nil
  2282. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  2283. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  2284. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  2285. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  2286. (t (:bold t))))
  2287. "Face for items scheduled previously, and not yet done."
  2288. :group 'org-faces)
  2289. (defcustom org-agenda-deadline-faces
  2290. '((1.0 . org-warning)
  2291. (0.5 . org-upcoming-deadline)
  2292. (0.0 . default))
  2293. "Faces for showing deadlines in the agenda.
  2294. This is a list of cons cells. The cdr of each cell is a face to be used,
  2295. and it can also just be like '(:foreground \"yellow\").
  2296. Each car is a fraction of the head-warning time that must have passed for
  2297. this the face in the cdr to be used for display. The numbers must be
  2298. given in descending order. The head-warning time is normally taken
  2299. from `org-deadline-warning-days', but can also be specified in the deadline
  2300. timestamp itself, like this:
  2301. DEADLINE: <2007-08-13 Mon -8d>
  2302. You may use d for days, w for weeks, m for months and y for years. Months
  2303. and years will only be treated in an approximate fashion (30.4 days for a
  2304. month and 365.24 days for a year)."
  2305. :group 'org-faces
  2306. :group 'org-agenda-daily/weekly
  2307. :type '(repeat
  2308. (cons
  2309. (number :tag "Fraction of head-warning time passed")
  2310. (sexp :tag "Face"))))
  2311. (defface org-agenda-restriction-lock
  2312. (org-compatible-face nil
  2313. '((((class color) (min-colors 88) (background light)) (:background "yellow1"))
  2314. (((class color) (min-colors 88) (background dark)) (:background "skyblue4"))
  2315. (((class color) (min-colors 16) (background light)) (:background "yellow1"))
  2316. (((class color) (min-colors 16) (background dark)) (:background "skyblue4"))
  2317. (((class color) (min-colors 8)) (:background "cyan" :foreground "black"))
  2318. (t (:inverse-video t))))
  2319. "Face for showing the agenda restriction lock."
  2320. :group 'org-faces)
  2321. (defface org-time-grid ;; font-lock-variable-name-face
  2322. (org-compatible-face nil
  2323. '((((class color) (min-colors 16) (background light)) (:foreground "DarkGoldenrod"))
  2324. (((class color) (min-colors 16) (background dark)) (:foreground "LightGoldenrod"))
  2325. (((class color) (min-colors 8)) (:foreground "yellow" :weight light))))
  2326. "Face used for time grids."
  2327. :group 'org-faces)
  2328. (defconst org-level-faces
  2329. '(org-level-1 org-level-2 org-level-3 org-level-4
  2330. org-level-5 org-level-6 org-level-7 org-level-8
  2331. ))
  2332. (defcustom org-n-level-faces (length org-level-faces)
  2333. "The number of different faces to be used for headlines.
  2334. Org-mode defines 8 different headline faces, so this can be at most 8.
  2335. If it is less than 8, the level-1 face gets re-used for level N+1 etc."
  2336. :type 'number
  2337. :group 'org-faces)
  2338. ;;; Functions and variables from ther packages
  2339. ;; Declared here to avoid compiler warnings
  2340. ;; XEmacs only
  2341. (defvar outline-mode-menu-heading)
  2342. (defvar outline-mode-menu-show)
  2343. (defvar outline-mode-menu-hide)
  2344. (defvar zmacs-regions) ; XEmacs regions
  2345. ;; Emacs only
  2346. (defvar mark-active)
  2347. ;; Various packages
  2348. (declare-function calendar-absolute-from-iso "cal-iso" (&optional date))
  2349. (declare-function calendar-forward-day "cal-move" (arg))
  2350. (declare-function calendar-goto-date "cal-move" (date))
  2351. (declare-function calendar-goto-today "cal-move" ())
  2352. (declare-function calendar-iso-from-absolute "cal-iso" (&optional date))
  2353. (defvar calc-embedded-close-formula)
  2354. (defvar calc-embedded-open-formula)
  2355. (declare-function cdlatex-tab "ext:cdlatex" ())
  2356. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  2357. (defvar font-lock-unfontify-region-function)
  2358. (declare-function iswitchb-mode "iswitchb" (&optional arg))
  2359. (declare-function iswitchb-read-buffer (prompt &optional default require-match start matches-set))
  2360. (defvar iswitchb-temp-buflist)
  2361. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  2362. (declare-function org-agenda-skip "org-agenda" ())
  2363. (declare-function org-format-agenda-item "org-agenda"
  2364. (extra txt &optional category tags dotime noprefix remove-re))
  2365. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  2366. (declare-function org-agenda-change-all-lines "org-agenda"
  2367. (newhead hdmarker &optional fixface))
  2368. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  2369. (declare-function org-agenda-maybe-redo "org-agenda" ())
  2370. (declare-function parse-time-string "parse-time" (string))
  2371. (declare-function remember "remember" (&optional initial))
  2372. (declare-function remember-buffer-desc "remember" ())
  2373. (declare-function remember-finalize "remember" ())
  2374. (defvar remember-save-after-remembering)
  2375. (defvar remember-data-file)
  2376. (defvar remember-register)
  2377. (defvar remember-buffer)
  2378. (defvar remember-handler-functions)
  2379. (defvar remember-annotation-functions)
  2380. (defvar texmathp-why)
  2381. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  2382. (declare-function table--at-cell-p "table" (position &optional object at-column))
  2383. (defvar w3m-current-url)
  2384. (defvar w3m-current-title)
  2385. (defvar org-latex-regexps)
  2386. ;;; Autoload and prepare some org modules
  2387. ;; Some table stuff that needs to be defined here, because it is used
  2388. ;; by the functions setting up org-mode or checking for table context.
  2389. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  2390. "Detects an org-type or table-type table.")
  2391. (defconst org-table-line-regexp "^[ \t]*|"
  2392. "Detects an org-type table line.")
  2393. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  2394. "Detects an org-type table line.")
  2395. (defconst org-table-hline-regexp "^[ \t]*|-"
  2396. "Detects an org-type table hline.")
  2397. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  2398. "Detects a table-type table hline.")
  2399. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  2400. "Searching from within a table (any type) this finds the first line
  2401. outside the table.")
  2402. ;; Autoload the functions in org-table.el that are needed by functions here.
  2403. (eval-and-compile
  2404. (org-autoload "org-table"
  2405. '(org-table-align org-table-begin org-table-blank-field
  2406. org-table-convert org-table-convert-region org-table-copy-down
  2407. org-table-copy-region org-table-create
  2408. org-table-create-or-convert-from-region
  2409. org-table-create-with-table.el org-table-current-dline
  2410. org-table-cut-region org-table-delete-column org-table-edit-field
  2411. org-table-edit-formulas org-table-end org-table-eval-formula
  2412. org-table-export org-table-field-info
  2413. org-table-get-stored-formulas org-table-goto-column
  2414. org-table-hline-and-move org-table-import org-table-insert-column
  2415. org-table-insert-hline org-table-insert-row org-table-iterate
  2416. org-table-justify-field-maybe org-table-kill-row
  2417. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  2418. org-table-move-column org-table-move-column-left
  2419. org-table-move-column-right org-table-move-row
  2420. org-table-move-row-down org-table-move-row-up
  2421. org-table-next-field org-table-next-row org-table-paste-rectangle
  2422. org-table-previous-field org-table-recalculate
  2423. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  2424. org-table-toggle-coordinate-overlays
  2425. org-table-toggle-formula-debugger org-table-wrap-region
  2426. orgtbl-mode turn-on-orgtbl)))
  2427. (defun org-at-table-p (&optional table-type)
  2428. "Return t if the cursor is inside an org-type table.
  2429. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  2430. (if org-enable-table-editor
  2431. (save-excursion
  2432. (beginning-of-line 1)
  2433. (looking-at (if table-type org-table-any-line-regexp
  2434. org-table-line-regexp)))
  2435. nil))
  2436. (defsubst org-table-p () (org-at-table-p))
  2437. (defun org-at-table.el-p ()
  2438. "Return t if and only if we are at a table.el table."
  2439. (and (org-at-table-p 'any)
  2440. (save-excursion
  2441. (goto-char (org-table-begin 'any))
  2442. (looking-at org-table1-hline-regexp))))
  2443. (defun org-table-recognize-table.el ()
  2444. "If there is a table.el table nearby, recognize it and move into it."
  2445. (if org-table-tab-recognizes-table.el
  2446. (if (org-at-table.el-p)
  2447. (progn
  2448. (beginning-of-line 1)
  2449. (if (looking-at org-table-dataline-regexp)
  2450. nil
  2451. (if (looking-at org-table1-hline-regexp)
  2452. (progn
  2453. (beginning-of-line 2)
  2454. (if (looking-at org-table-any-border-regexp)
  2455. (beginning-of-line -1)))))
  2456. (if (re-search-forward "|" (org-table-end t) t)
  2457. (progn
  2458. (require 'table)
  2459. (if (table--at-cell-p (point))
  2460. t
  2461. (message "recognizing table.el table...")
  2462. (table-recognize-table)
  2463. (message "recognizing table.el table...done")))
  2464. (error "This should not happen..."))
  2465. t)
  2466. nil)
  2467. nil))
  2468. (defun org-at-table-hline-p ()
  2469. "Return t if the cursor is inside a hline in a table."
  2470. (if org-enable-table-editor
  2471. (save-excursion
  2472. (beginning-of-line 1)
  2473. (looking-at org-table-hline-regexp))
  2474. nil))
  2475. (defvar org-table-clean-did-remove-column nil)
  2476. (defun org-table-map-tables (function)
  2477. "Apply FUNCTION to the start of all tables in the buffer."
  2478. (save-excursion
  2479. (save-restriction
  2480. (widen)
  2481. (goto-char (point-min))
  2482. (while (re-search-forward org-table-any-line-regexp nil t)
  2483. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  2484. (beginning-of-line 1)
  2485. (if (looking-at org-table-line-regexp)
  2486. (save-excursion (funcall function)))
  2487. (re-search-forward org-table-any-border-regexp nil 1))))
  2488. (message "Mapping tables: done"))
  2489. ;; Declare and autoload functions from org-exp.el
  2490. (declare-function org-default-export-plist "org-exp")
  2491. (declare-function org-infile-export-plist "org-exp")
  2492. (declare-function org-get-current-options "org-exp")
  2493. (eval-and-compile
  2494. (org-autoload "org-exp"
  2495. '(org-export org-export-as-ascii org-export-visible
  2496. org-insert-export-options-template org-export-as-html-and-open
  2497. org-export-as-html-batch org-export-as-html-to-buffer
  2498. org-replace-region-by-html org-export-region-as-html
  2499. org-export-as-html org-export-icalendar-this-file
  2500. org-export-icalendar-all-agenda-files
  2501. org-export-icalendar-combine-agenda-files org-export-as-xoxo)))
  2502. ;; Declare and autoload functions from org-exp.el
  2503. (eval-and-compile
  2504. (org-autoload "org-exp"
  2505. '(org-agenda org-agenda-list org-search-view
  2506. org-todo-list org-tags-view org-agenda-list-stuck-projects
  2507. org-diary org-agenda-to-appt)))
  2508. ;; Autoload org-remember
  2509. (eval-and-compile
  2510. (org-autoload "org-remember"
  2511. '(org-remember-insinuate org-remember-annotation
  2512. org-remember-apply-template org-remember org-remember-handler)))
  2513. ;; Autoload org-clock.el
  2514. (defvar org-clock-marker (make-marker)
  2515. "Marker recording the last clock-in.")
  2516. (eval-and-compile
  2517. (org-autoload
  2518. "org-clock"
  2519. '(org-clock-in org-clock-out org-clock-cancel
  2520. org-clock-goto org-clock-sum org-clock-display
  2521. org-remove-clock-overlays org-clock-report
  2522. org-clocktable-shift org-dblock-write:clocktable
  2523. org-get-clocktable)))
  2524. (defun org-clock-update-time-maybe ()
  2525. "If this is a CLOCK line, update it and return t.
  2526. Otherwise, return nil."
  2527. (interactive)
  2528. (save-excursion
  2529. (beginning-of-line 1)
  2530. (skip-chars-forward " \t")
  2531. (when (looking-at org-clock-string)
  2532. (let ((re (concat "[ \t]*" org-clock-string
  2533. " *[[<]\\([^]>]+\\)[]>]-+[[<]\\([^]>]+\\)[]>]"
  2534. "\\([ \t]*=>.*\\)?"))
  2535. ts te h m s)
  2536. (if (not (looking-at re))
  2537. nil
  2538. (and (match-end 3) (delete-region (match-beginning 3) (match-end 3)))
  2539. (end-of-line 1)
  2540. (setq ts (match-string 1)
  2541. te (match-string 2))
  2542. (setq s (- (time-to-seconds
  2543. (apply 'encode-time (org-parse-time-string te)))
  2544. (time-to-seconds
  2545. (apply 'encode-time (org-parse-time-string ts))))
  2546. h (floor (/ s 3600))
  2547. s (- s (* 3600 h))
  2548. m (floor (/ s 60))
  2549. s (- s (* 60 s)))
  2550. (insert " => " (format "%2d:%02d" h m))
  2551. t)))))
  2552. (defun org-check-running-clock ()
  2553. "Check if the current buffer contains the running clock.
  2554. If yes, offer to stop it and to save the buffer with the changes."
  2555. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2556. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  2557. (buffer-name))))
  2558. (org-clock-out)
  2559. (when (y-or-n-p "Save changed buffer?")
  2560. (save-buffer))))
  2561. (defun org-clocktable-try-shift (dir n)
  2562. "Check if this line starts a clock table, if yes, shift the time block."
  2563. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  2564. (org-clocktable-shift dir n)))
  2565. ;;; Variables for pre-computed regular expressions, all buffer local
  2566. (defvar org-drawer-regexp nil
  2567. "Matches first line of a hidden block.")
  2568. (make-variable-buffer-local 'org-drawer-regexp)
  2569. (defvar org-todo-regexp nil
  2570. "Matches any of the TODO state keywords.")
  2571. (make-variable-buffer-local 'org-todo-regexp)
  2572. (defvar org-not-done-regexp nil
  2573. "Matches any of the TODO state keywords except the last one.")
  2574. (make-variable-buffer-local 'org-not-done-regexp)
  2575. (defvar org-todo-line-regexp nil
  2576. "Matches a headline and puts TODO state into group 2 if present.")
  2577. (make-variable-buffer-local 'org-todo-line-regexp)
  2578. (defvar org-complex-heading-regexp nil
  2579. "Matches a headline and puts everything into groups:
  2580. group 1: the stars
  2581. group 2: The todo keyword, maybe
  2582. group 3: Priority cookie
  2583. group 4: True headline
  2584. group 5: Tags")
  2585. (make-variable-buffer-local 'org-complex-heading-regexp)
  2586. (defvar org-todo-line-tags-regexp nil
  2587. "Matches a headline and puts TODO state into group 2 if present.
  2588. Also put tags into group 4 if tags are present.")
  2589. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  2590. (defvar org-nl-done-regexp nil
  2591. "Matches newline followed by a headline with the DONE keyword.")
  2592. (make-variable-buffer-local 'org-nl-done-regexp)
  2593. (defvar org-looking-at-done-regexp nil
  2594. "Matches the DONE keyword a point.")
  2595. (make-variable-buffer-local 'org-looking-at-done-regexp)
  2596. (defvar org-ds-keyword-length 12
  2597. "Maximum length of the Deadline and SCHEDULED keywords.")
  2598. (make-variable-buffer-local 'org-ds-keyword-length)
  2599. (defvar org-deadline-regexp nil
  2600. "Matches the DEADLINE keyword.")
  2601. (make-variable-buffer-local 'org-deadline-regexp)
  2602. (defvar org-deadline-time-regexp nil
  2603. "Matches the DEADLINE keyword together with a time stamp.")
  2604. (make-variable-buffer-local 'org-deadline-time-regexp)
  2605. (defvar org-deadline-line-regexp nil
  2606. "Matches the DEADLINE keyword and the rest of the line.")
  2607. (make-variable-buffer-local 'org-deadline-line-regexp)
  2608. (defvar org-scheduled-regexp nil
  2609. "Matches the SCHEDULED keyword.")
  2610. (make-variable-buffer-local 'org-scheduled-regexp)
  2611. (defvar org-scheduled-time-regexp nil
  2612. "Matches the SCHEDULED keyword together with a time stamp.")
  2613. (make-variable-buffer-local 'org-scheduled-time-regexp)
  2614. (defvar org-closed-time-regexp nil
  2615. "Matches the CLOSED keyword together with a time stamp.")
  2616. (make-variable-buffer-local 'org-closed-time-regexp)
  2617. (defvar org-keyword-time-regexp nil
  2618. "Matches any of the 4 keywords, together with the time stamp.")
  2619. (make-variable-buffer-local 'org-keyword-time-regexp)
  2620. (defvar org-keyword-time-not-clock-regexp nil
  2621. "Matches any of the 3 keywords, together with the time stamp.")
  2622. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  2623. (defvar org-maybe-keyword-time-regexp nil
  2624. "Matches a timestamp, possibly preceeded by a keyword.")
  2625. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  2626. (defvar org-planning-or-clock-line-re nil
  2627. "Matches a line with planning or clock info.")
  2628. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  2629. (defconst org-plain-time-of-day-regexp
  2630. (concat
  2631. "\\(\\<[012]?[0-9]"
  2632. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2633. "\\(--?"
  2634. "\\(\\<[012]?[0-9]"
  2635. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2636. "\\)?")
  2637. "Regular expression to match a plain time or time range.
  2638. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2639. groups carry important information:
  2640. 0 the full match
  2641. 1 the first time, range or not
  2642. 8 the second time, if it is a range.")
  2643. (defconst org-plain-time-extension-regexp
  2644. (concat
  2645. "\\(\\<[012]?[0-9]"
  2646. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2647. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  2648. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  2649. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2650. groups carry important information:
  2651. 0 the full match
  2652. 7 hours of duration
  2653. 9 minutes of duration")
  2654. (defconst org-stamp-time-of-day-regexp
  2655. (concat
  2656. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  2657. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  2658. "\\(--?"
  2659. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  2660. "Regular expression to match a timestamp time or time range.
  2661. After a match, the following groups carry important information:
  2662. 0 the full match
  2663. 1 date plus weekday, for backreferencing to make sure both times on same day
  2664. 2 the first time, range or not
  2665. 4 the second time, if it is a range.")
  2666. (defconst org-startup-options
  2667. '(("fold" org-startup-folded t)
  2668. ("overview" org-startup-folded t)
  2669. ("nofold" org-startup-folded nil)
  2670. ("showall" org-startup-folded nil)
  2671. ("content" org-startup-folded content)
  2672. ("hidestars" org-hide-leading-stars t)
  2673. ("showstars" org-hide-leading-stars nil)
  2674. ("odd" org-odd-levels-only t)
  2675. ("oddeven" org-odd-levels-only nil)
  2676. ("align" org-startup-align-all-tables t)
  2677. ("noalign" org-startup-align-all-tables nil)
  2678. ("customtime" org-display-custom-times t)
  2679. ("logdone" org-log-done time)
  2680. ("lognotedone" org-log-done note)
  2681. ("nologdone" org-log-done nil)
  2682. ("lognoteclock-out" org-log-note-clock-out t)
  2683. ("nolognoteclock-out" org-log-note-clock-out nil)
  2684. ("logrepeat" org-log-repeat state)
  2685. ("lognoterepeat" org-log-repeat note)
  2686. ("nologrepeat" org-log-repeat nil)
  2687. ("constcgs" constants-unit-system cgs)
  2688. ("constSI" constants-unit-system SI))
  2689. "Variable associated with STARTUP options for org-mode.
  2690. Each element is a list of three items: The startup options as written
  2691. in the #+STARTUP line, the corresponding variable, and the value to
  2692. set this variable to if the option is found. An optional forth element PUSH
  2693. means to push this value onto the list in the variable.")
  2694. (defun org-set-regexps-and-options ()
  2695. "Precompute regular expressions for current buffer."
  2696. (when (org-mode-p)
  2697. (org-set-local 'org-todo-kwd-alist nil)
  2698. (org-set-local 'org-todo-key-alist nil)
  2699. (org-set-local 'org-todo-key-trigger nil)
  2700. (org-set-local 'org-todo-keywords-1 nil)
  2701. (org-set-local 'org-done-keywords nil)
  2702. (org-set-local 'org-todo-heads nil)
  2703. (org-set-local 'org-todo-sets nil)
  2704. (org-set-local 'org-todo-log-states nil)
  2705. (let ((re (org-make-options-regexp
  2706. '("CATEGORY" "SEQ_TODO" "TYP_TODO" "TODO" "COLUMNS"
  2707. "STARTUP" "ARCHIVE" "TAGS" "LINK" "PRIORITIES"
  2708. "CONSTANTS" "PROPERTY" "DRAWERS")))
  2709. (splitre "[ \t]+")
  2710. kwds kws0 kwsa key log value cat arch tags const links hw dws
  2711. tail sep kws1 prio props drawers)
  2712. (save-excursion
  2713. (save-restriction
  2714. (widen)
  2715. (goto-char (point-min))
  2716. (while (re-search-forward re nil t)
  2717. (setq key (match-string 1) value (org-match-string-no-properties 2))
  2718. (cond
  2719. ((equal key "CATEGORY")
  2720. (if (string-match "[ \t]+$" value)
  2721. (setq value (replace-match "" t t value)))
  2722. (setq cat value))
  2723. ((member key '("SEQ_TODO" "TODO"))
  2724. (push (cons 'sequence (org-split-string value splitre)) kwds))
  2725. ((equal key "TYP_TODO")
  2726. (push (cons 'type (org-split-string value splitre)) kwds))
  2727. ((equal key "TAGS")
  2728. (setq tags (append tags (org-split-string value splitre))))
  2729. ((equal key "COLUMNS")
  2730. (org-set-local 'org-columns-default-format value))
  2731. ((equal key "LINK")
  2732. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  2733. (push (cons (match-string 1 value)
  2734. (org-trim (match-string 2 value)))
  2735. links)))
  2736. ((equal key "PRIORITIES")
  2737. (setq prio (org-split-string value " +")))
  2738. ((equal key "PROPERTY")
  2739. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  2740. (push (cons (match-string 1 value) (match-string 2 value))
  2741. props)))
  2742. ((equal key "DRAWERS")
  2743. (setq drawers (org-split-string value splitre)))
  2744. ((equal key "CONSTANTS")
  2745. (setq const (append const (org-split-string value splitre))))
  2746. ((equal key "STARTUP")
  2747. (let ((opts (org-split-string value splitre))
  2748. l var val)
  2749. (while (setq l (pop opts))
  2750. (when (setq l (assoc l org-startup-options))
  2751. (setq var (nth 1 l) val (nth 2 l))
  2752. (if (not (nth 3 l))
  2753. (set (make-local-variable var) val)
  2754. (if (not (listp (symbol-value var)))
  2755. (set (make-local-variable var) nil))
  2756. (set (make-local-variable var) (symbol-value var))
  2757. (add-to-list var val))))))
  2758. ((equal key "ARCHIVE")
  2759. (string-match " *$" value)
  2760. (setq arch (replace-match "" t t value))
  2761. (remove-text-properties 0 (length arch)
  2762. '(face t fontified t) arch)))
  2763. )))
  2764. (when cat
  2765. (org-set-local 'org-category (intern cat))
  2766. (push (cons "CATEGORY" cat) props))
  2767. (when prio
  2768. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  2769. (setq prio (mapcar 'string-to-char prio))
  2770. (org-set-local 'org-highest-priority (nth 0 prio))
  2771. (org-set-local 'org-lowest-priority (nth 1 prio))
  2772. (org-set-local 'org-default-priority (nth 2 prio)))
  2773. (and props (org-set-local 'org-local-properties (nreverse props)))
  2774. (and drawers (org-set-local 'org-drawers drawers))
  2775. (and arch (org-set-local 'org-archive-location arch))
  2776. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  2777. ;; Process the TODO keywords
  2778. (unless kwds
  2779. ;; Use the global values as if they had been given locally.
  2780. (setq kwds (default-value 'org-todo-keywords))
  2781. (if (stringp (car kwds))
  2782. (setq kwds (list (cons org-todo-interpretation
  2783. (default-value 'org-todo-keywords)))))
  2784. (setq kwds (reverse kwds)))
  2785. (setq kwds (nreverse kwds))
  2786. (let (inter kws kw)
  2787. (while (setq kws (pop kwds))
  2788. (setq inter (pop kws) sep (member "|" kws)
  2789. kws0 (delete "|" (copy-sequence kws))
  2790. kwsa nil
  2791. kws1 (mapcar
  2792. (lambda (x)
  2793. ;; 1 2
  2794. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  2795. (progn
  2796. (setq kw (match-string 1 x)
  2797. key (and (match-end 2) (match-string 2 x))
  2798. log (org-extract-log-state-settings x))
  2799. (push (cons kw (and key (string-to-char key))) kwsa)
  2800. (and log (push log org-todo-log-states))
  2801. kw)
  2802. (error "Invalid TODO keyword %s" x)))
  2803. kws0)
  2804. kwsa (if kwsa (append '((:startgroup))
  2805. (nreverse kwsa)
  2806. '((:endgroup))))
  2807. hw (car kws1)
  2808. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  2809. tail (list inter hw (car dws) (org-last dws)))
  2810. (add-to-list 'org-todo-heads hw 'append)
  2811. (push kws1 org-todo-sets)
  2812. (setq org-done-keywords (append org-done-keywords dws nil))
  2813. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  2814. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  2815. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  2816. (setq org-todo-sets (nreverse org-todo-sets)
  2817. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  2818. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  2819. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  2820. ;; Process the constants
  2821. (when const
  2822. (let (e cst)
  2823. (while (setq e (pop const))
  2824. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  2825. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  2826. (setq org-table-formula-constants-local cst)))
  2827. ;; Process the tags.
  2828. (when tags
  2829. (let (e tgs)
  2830. (while (setq e (pop tags))
  2831. (cond
  2832. ((equal e "{") (push '(:startgroup) tgs))
  2833. ((equal e "}") (push '(:endgroup) tgs))
  2834. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  2835. (push (cons (match-string 1 e)
  2836. (string-to-char (match-string 2 e)))
  2837. tgs))
  2838. (t (push (list e) tgs))))
  2839. (org-set-local 'org-tag-alist nil)
  2840. (while (setq e (pop tgs))
  2841. (or (and (stringp (car e))
  2842. (assoc (car e) org-tag-alist))
  2843. (push e org-tag-alist))))))
  2844. ;; Compute the regular expressions and other local variables
  2845. (if (not org-done-keywords)
  2846. (setq org-done-keywords (list (org-last org-todo-keywords-1))))
  2847. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  2848. (length org-scheduled-string)))
  2849. org-drawer-regexp
  2850. (concat "^[ \t]*:\\("
  2851. (mapconcat 'regexp-quote org-drawers "\\|")
  2852. "\\):[ \t]*$")
  2853. org-not-done-keywords
  2854. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  2855. org-todo-regexp
  2856. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  2857. "\\|") "\\)\\>")
  2858. org-not-done-regexp
  2859. (concat "\\<\\("
  2860. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  2861. "\\)\\>")
  2862. org-todo-line-regexp
  2863. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  2864. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2865. "\\)\\>\\)?[ \t]*\\(.*\\)")
  2866. org-complex-heading-regexp
  2867. (concat "^\\(\\*+\\)\\(?:[ \t]+\\("
  2868. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2869. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  2870. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  2871. org-nl-done-regexp
  2872. (concat "\n\\*+[ \t]+"
  2873. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  2874. "\\)" "\\>")
  2875. org-todo-line-tags-regexp
  2876. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  2877. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2878. (org-re
  2879. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  2880. org-looking-at-done-regexp
  2881. (concat "^" "\\(?:"
  2882. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  2883. "\\>")
  2884. org-deadline-regexp (concat "\\<" org-deadline-string)
  2885. org-deadline-time-regexp
  2886. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  2887. org-deadline-line-regexp
  2888. (concat "\\<\\(" org-deadline-string "\\).*")
  2889. org-scheduled-regexp
  2890. (concat "\\<" org-scheduled-string)
  2891. org-scheduled-time-regexp
  2892. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  2893. org-closed-time-regexp
  2894. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  2895. org-keyword-time-regexp
  2896. (concat "\\<\\(" org-scheduled-string
  2897. "\\|" org-deadline-string
  2898. "\\|" org-closed-string
  2899. "\\|" org-clock-string "\\)"
  2900. " *[[<]\\([^]>]+\\)[]>]")
  2901. org-keyword-time-not-clock-regexp
  2902. (concat "\\<\\(" org-scheduled-string
  2903. "\\|" org-deadline-string
  2904. "\\|" org-closed-string
  2905. "\\)"
  2906. " *[[<]\\([^]>]+\\)[]>]")
  2907. org-maybe-keyword-time-regexp
  2908. (concat "\\(\\<\\(" org-scheduled-string
  2909. "\\|" org-deadline-string
  2910. "\\|" org-closed-string
  2911. "\\|" org-clock-string "\\)\\)?"
  2912. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  2913. org-planning-or-clock-line-re
  2914. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  2915. "\\|" org-deadline-string
  2916. "\\|" org-closed-string "\\|" org-clock-string
  2917. "\\)\\>\\)")
  2918. )
  2919. (org-compute-latex-and-specials-regexp)
  2920. (org-set-font-lock-defaults)))
  2921. (defun org-extract-log-state-settings (x)
  2922. "Extract the log state setting from a TODO keyword string.
  2923. This will extract info from a string like \"WAIT(w@/!)\"."
  2924. (let (kw key log1 log2)
  2925. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  2926. (setq kw (match-string 1 x)
  2927. key (and (match-end 2) (match-string 2 x))
  2928. log1 (and (match-end 3) (match-string 3 x))
  2929. log2 (and (match-end 4) (match-string 4 x)))
  2930. (and (or log1 log2)
  2931. (list kw
  2932. (and log1 (if (equal log1 "!") 'time 'note))
  2933. (and log2 (if (equal log2 "!") 'time 'note)))))))
  2934. (defun org-remove-keyword-keys (list)
  2935. "Remove a pair of parenthesis at the end of each string in LIST."
  2936. (mapcar (lambda (x)
  2937. (if (string-match "(.*)$" x)
  2938. (substring x 0 (match-beginning 0))
  2939. x))
  2940. list))
  2941. ;; FIXME: this could be done much better, using second characters etc.
  2942. (defun org-assign-fast-keys (alist)
  2943. "Assign fast keys to a keyword-key alist.
  2944. Respect keys that are already there."
  2945. (let (new e k c c1 c2 (char ?a))
  2946. (while (setq e (pop alist))
  2947. (cond
  2948. ((equal e '(:startgroup)) (push e new))
  2949. ((equal e '(:endgroup)) (push e new))
  2950. (t
  2951. (setq k (car e) c2 nil)
  2952. (if (cdr e)
  2953. (setq c (cdr e))
  2954. ;; automatically assign a character.
  2955. (setq c1 (string-to-char
  2956. (downcase (substring
  2957. k (if (= (string-to-char k) ?@) 1 0)))))
  2958. (if (or (rassoc c1 new) (rassoc c1 alist))
  2959. (while (or (rassoc char new) (rassoc char alist))
  2960. (setq char (1+ char)))
  2961. (setq c2 c1))
  2962. (setq c (or c2 char)))
  2963. (push (cons k c) new))))
  2964. (nreverse new)))
  2965. ;;; Some variables used in various places
  2966. (defvar org-window-configuration nil
  2967. "Used in various places to store a window configuration.")
  2968. (defvar org-finish-function nil
  2969. "Function to be called when `C-c C-c' is used.
  2970. This is for getting out of special buffers like remember.")
  2971. ;; FIXME: Occasionally check by commenting these, to make sure
  2972. ;; no other functions uses these, forgetting to let-bind them.
  2973. (defvar entry)
  2974. (defvar state)
  2975. (defvar last-state)
  2976. (defvar date)
  2977. (defvar description)
  2978. ;; Defined somewhere in this file, but used before definition.
  2979. (defvar org-html-entities)
  2980. (defvar org-struct-menu)
  2981. (defvar org-org-menu)
  2982. (defvar org-tbl-menu)
  2983. (defvar org-agenda-keymap)
  2984. ;;;; Define the Org-mode
  2985. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  2986. (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."))
  2987. ;; We use a before-change function to check if a table might need
  2988. ;; an update.
  2989. (defvar org-table-may-need-update t
  2990. "Indicates that a table might need an update.
  2991. This variable is set by `org-before-change-function'.
  2992. `org-table-align' sets it back to nil.")
  2993. (defun org-before-change-function (beg end)
  2994. "Every change indicates that a table might need an update."
  2995. (setq org-table-may-need-update t))
  2996. (defvar org-mode-map)
  2997. (defvar org-mode-hook nil)
  2998. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  2999. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  3000. (defvar org-table-buffer-is-an nil)
  3001. (defconst org-outline-regexp "\\*+ ")
  3002. ;;;###autoload
  3003. (define-derived-mode org-mode outline-mode "Org"
  3004. "Outline-based notes management and organizer, alias
  3005. \"Carsten's outline-mode for keeping track of everything.\"
  3006. Org-mode develops organizational tasks around a NOTES file which
  3007. contains information about projects as plain text. Org-mode is
  3008. implemented on top of outline-mode, which is ideal to keep the content
  3009. of large files well structured. It supports ToDo items, deadlines and
  3010. time stamps, which magically appear in the diary listing of the Emacs
  3011. calendar. Tables are easily created with a built-in table editor.
  3012. Plain text URL-like links connect to websites, emails (VM), Usenet
  3013. messages (Gnus), BBDB entries, and any files related to the project.
  3014. For printing and sharing of notes, an Org-mode file (or a part of it)
  3015. can be exported as a structured ASCII or HTML file.
  3016. The following commands are available:
  3017. \\{org-mode-map}"
  3018. ;; Get rid of Outline menus, they are not needed
  3019. ;; Need to do this here because define-derived-mode sets up
  3020. ;; the keymap so late. Still, it is a waste to call this each time
  3021. ;; we switch another buffer into org-mode.
  3022. (if (featurep 'xemacs)
  3023. (when (boundp 'outline-mode-menu-heading)
  3024. ;; Assume this is Greg's port, it used easymenu
  3025. (easy-menu-remove outline-mode-menu-heading)
  3026. (easy-menu-remove outline-mode-menu-show)
  3027. (easy-menu-remove outline-mode-menu-hide))
  3028. (define-key org-mode-map [menu-bar headings] 'undefined)
  3029. (define-key org-mode-map [menu-bar hide] 'undefined)
  3030. (define-key org-mode-map [menu-bar show] 'undefined))
  3031. (org-load-modules-maybe)
  3032. (easy-menu-add org-org-menu)
  3033. (easy-menu-add org-tbl-menu)
  3034. (org-install-agenda-files-menu)
  3035. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  3036. (org-add-to-invisibility-spec '(org-cwidth))
  3037. (when (featurep 'xemacs)
  3038. (org-set-local 'line-move-ignore-invisible t))
  3039. (org-set-local 'outline-regexp org-outline-regexp)
  3040. (org-set-local 'outline-level 'org-outline-level)
  3041. (when (and org-ellipsis
  3042. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  3043. (fboundp 'make-glyph-code))
  3044. (unless org-display-table
  3045. (setq org-display-table (make-display-table)))
  3046. (set-display-table-slot
  3047. org-display-table 4
  3048. (vconcat (mapcar
  3049. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  3050. org-ellipsis)))
  3051. (if (stringp org-ellipsis) org-ellipsis "..."))))
  3052. (setq buffer-display-table org-display-table))
  3053. (org-set-regexps-and-options)
  3054. ;; Calc embedded
  3055. (org-set-local 'calc-embedded-open-mode "# ")
  3056. (modify-syntax-entry ?# "<")
  3057. (modify-syntax-entry ?@ "w")
  3058. (if org-startup-truncated (setq truncate-lines t))
  3059. (org-set-local 'font-lock-unfontify-region-function
  3060. 'org-unfontify-region)
  3061. ;; Activate before-change-function
  3062. (org-set-local 'org-table-may-need-update t)
  3063. (org-add-hook 'before-change-functions 'org-before-change-function nil
  3064. 'local)
  3065. ;; Check for running clock before killing a buffer
  3066. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  3067. ;; Paragraphs and auto-filling
  3068. (org-set-autofill-regexps)
  3069. (setq indent-line-function 'org-indent-line-function)
  3070. (org-update-radio-target-regexp)
  3071. ;; Comment characters
  3072. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  3073. (org-set-local 'comment-padding " ")
  3074. ;; Align options lines
  3075. (org-set-local
  3076. 'align-mode-rules-list
  3077. '((org-in-buffer-settings
  3078. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  3079. (modes . '(org-mode)))))
  3080. ;; Imenu
  3081. (org-set-local 'imenu-create-index-function
  3082. 'org-imenu-get-tree)
  3083. ;; Make isearch reveal context
  3084. (if (or (featurep 'xemacs)
  3085. (not (boundp 'outline-isearch-open-invisible-function)))
  3086. ;; Emacs 21 and XEmacs make use of the hook
  3087. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  3088. ;; Emacs 22 deals with this through a special variable
  3089. (org-set-local 'outline-isearch-open-invisible-function
  3090. (lambda (&rest ignore) (org-show-context 'isearch))))
  3091. ;; If empty file that did not turn on org-mode automatically, make it to.
  3092. (if (and org-insert-mode-line-in-empty-file
  3093. (interactive-p)
  3094. (= (point-min) (point-max)))
  3095. (insert "# -*- mode: org -*-\n\n"))
  3096. (unless org-inhibit-startup
  3097. (when org-startup-align-all-tables
  3098. (let ((bmp (buffer-modified-p)))
  3099. (org-table-map-tables 'org-table-align)
  3100. (set-buffer-modified-p bmp)))
  3101. (org-cycle-hide-drawers 'all)
  3102. (cond
  3103. ((eq org-startup-folded t)
  3104. (org-cycle '(4)))
  3105. ((eq org-startup-folded 'content)
  3106. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  3107. (org-cycle '(4)) (org-cycle '(4)))))))
  3108. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  3109. (defun org-current-time ()
  3110. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  3111. (if (> (car org-time-stamp-rounding-minutes) 1)
  3112. (let ((r (car org-time-stamp-rounding-minutes))
  3113. (time (decode-time)))
  3114. (apply 'encode-time
  3115. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  3116. (nthcdr 2 time))))
  3117. (current-time)))
  3118. ;;;; Font-Lock stuff, including the activators
  3119. (defvar org-mouse-map (make-sparse-keymap))
  3120. (org-defkey org-mouse-map
  3121. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  3122. (org-defkey org-mouse-map
  3123. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  3124. (when org-mouse-1-follows-link
  3125. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  3126. (when org-tab-follows-link
  3127. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  3128. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  3129. (when org-return-follows-link
  3130. (org-defkey org-mouse-map [(return)] 'org-open-at-point)
  3131. (org-defkey org-mouse-map "\C-m" 'org-open-at-point))
  3132. (require 'font-lock)
  3133. (defconst org-non-link-chars "]\t\n\r<>")
  3134. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  3135. "shell" "elisp"))
  3136. (defvar org-link-re-with-space nil
  3137. "Matches a link with spaces, optional angular brackets around it.")
  3138. (defvar org-link-re-with-space2 nil
  3139. "Matches a link with spaces, optional angular brackets around it.")
  3140. (defvar org-angle-link-re nil
  3141. "Matches link with angular brackets, spaces are allowed.")
  3142. (defvar org-plain-link-re nil
  3143. "Matches plain link, without spaces.")
  3144. (defvar org-bracket-link-regexp nil
  3145. "Matches a link in double brackets.")
  3146. (defvar org-bracket-link-analytic-regexp nil
  3147. "Regular expression used to analyze links.
  3148. Here is what the match groups contain after a match:
  3149. 1: http:
  3150. 2: http
  3151. 3: path
  3152. 4: [desc]
  3153. 5: desc")
  3154. (defvar org-any-link-re nil
  3155. "Regular expression matching any link.")
  3156. (defun org-make-link-regexps ()
  3157. "Update the link regular expressions.
  3158. This should be called after the variable `org-link-types' has changed."
  3159. (setq org-link-re-with-space
  3160. (concat
  3161. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3162. "\\([^" org-non-link-chars " ]"
  3163. "[^" org-non-link-chars "]*"
  3164. "[^" org-non-link-chars " ]\\)>?")
  3165. org-link-re-with-space2
  3166. (concat
  3167. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3168. "\\([^" org-non-link-chars " ]"
  3169. "[^]\t\n\r]*"
  3170. "[^" org-non-link-chars " ]\\)>?")
  3171. org-angle-link-re
  3172. (concat
  3173. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3174. "\\([^" org-non-link-chars " ]"
  3175. "[^" org-non-link-chars "]*"
  3176. "\\)>")
  3177. org-plain-link-re
  3178. (concat
  3179. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3180. "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  3181. org-bracket-link-regexp
  3182. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  3183. org-bracket-link-analytic-regexp
  3184. (concat
  3185. "\\[\\["
  3186. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  3187. "\\([^]]+\\)"
  3188. "\\]"
  3189. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3190. "\\]")
  3191. org-any-link-re
  3192. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  3193. org-angle-link-re "\\)\\|\\("
  3194. org-plain-link-re "\\)")))
  3195. (org-make-link-regexps)
  3196. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  3197. "Regular expression for fast time stamp matching.")
  3198. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  3199. "Regular expression for fast time stamp matching.")
  3200. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3201. "Regular expression matching time strings for analysis.
  3202. This one does not require the space after the date, so it can be used
  3203. on a string that terminates immediately after the date.")
  3204. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3205. "Regular expression matching time strings for analysis.")
  3206. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  3207. "Regular expression matching time stamps, with groups.")
  3208. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  3209. "Regular expression matching time stamps (also [..]), with groups.")
  3210. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  3211. "Regular expression matching a time stamp range.")
  3212. (defconst org-tr-regexp-both
  3213. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  3214. "Regular expression matching a time stamp range.")
  3215. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  3216. org-ts-regexp "\\)?")
  3217. "Regular expression matching a time stamp or time stamp range.")
  3218. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  3219. org-ts-regexp-both "\\)?")
  3220. "Regular expression matching a time stamp or time stamp range.
  3221. The time stamps may be either active or inactive.")
  3222. (defvar org-emph-face nil)
  3223. (defun org-do-emphasis-faces (limit)
  3224. "Run through the buffer and add overlays to links."
  3225. (let (rtn)
  3226. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  3227. (if (not (= (char-after (match-beginning 3))
  3228. (char-after (match-beginning 4))))
  3229. (progn
  3230. (setq rtn t)
  3231. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  3232. 'face
  3233. (nth 1 (assoc (match-string 3)
  3234. org-emphasis-alist)))
  3235. (add-text-properties (match-beginning 2) (match-end 2)
  3236. '(font-lock-multiline t))
  3237. (when org-hide-emphasis-markers
  3238. (add-text-properties (match-end 4) (match-beginning 5)
  3239. '(invisible org-link))
  3240. (add-text-properties (match-beginning 3) (match-end 3)
  3241. '(invisible org-link)))))
  3242. (backward-char 1))
  3243. rtn))
  3244. (defun org-emphasize (&optional char)
  3245. "Insert or change an emphasis, i.e. a font like bold or italic.
  3246. If there is an active region, change that region to a new emphasis.
  3247. If there is no region, just insert the marker characters and position
  3248. the cursor between them.
  3249. CHAR should be either the marker character, or the first character of the
  3250. HTML tag associated with that emphasis. If CHAR is a space, the means
  3251. to remove the emphasis of the selected region.
  3252. If char is not given (for example in an interactive call) it
  3253. will be prompted for."
  3254. (interactive)
  3255. (let ((eal org-emphasis-alist) e det
  3256. (erc org-emphasis-regexp-components)
  3257. (prompt "")
  3258. (string "") beg end move tag c s)
  3259. (if (org-region-active-p)
  3260. (setq beg (region-beginning) end (region-end)
  3261. string (buffer-substring beg end))
  3262. (setq move t))
  3263. (while (setq e (pop eal))
  3264. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  3265. c (aref tag 0))
  3266. (push (cons c (string-to-char (car e))) det)
  3267. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  3268. (substring tag 1)))))
  3269. (unless char
  3270. (message "%s" (concat "Emphasis marker or tag:" prompt))
  3271. (setq char (read-char-exclusive)))
  3272. (setq char (or (cdr (assoc char det)) char))
  3273. (if (equal char ?\ )
  3274. (setq s "" move nil)
  3275. (unless (assoc (char-to-string char) org-emphasis-alist)
  3276. (error "No such emphasis marker: \"%c\"" char))
  3277. (setq s (char-to-string char)))
  3278. (while (and (> (length string) 1)
  3279. (equal (substring string 0 1) (substring string -1))
  3280. (assoc (substring string 0 1) org-emphasis-alist))
  3281. (setq string (substring string 1 -1)))
  3282. (setq string (concat s string s))
  3283. (if beg (delete-region beg end))
  3284. (unless (or (bolp)
  3285. (string-match (concat "[" (nth 0 erc) "\n]")
  3286. (char-to-string (char-before (point)))))
  3287. (insert " "))
  3288. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  3289. (char-to-string (char-after (point))))
  3290. (insert " ") (backward-char 1))
  3291. (insert string)
  3292. (and move (backward-char 1))))
  3293. (defconst org-nonsticky-props
  3294. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  3295. (defun org-activate-plain-links (limit)
  3296. "Run through the buffer and add overlays to links."
  3297. (catch 'exit
  3298. (let (f)
  3299. (while (re-search-forward org-plain-link-re limit t)
  3300. (setq f (get-text-property (match-beginning 0) 'face))
  3301. (if (or (eq f 'org-tag)
  3302. (and (listp f) (memq 'org-tag f)))
  3303. nil
  3304. (add-text-properties (match-beginning 0) (match-end 0)
  3305. (list 'mouse-face 'highlight
  3306. 'rear-nonsticky org-nonsticky-props
  3307. 'keymap org-mouse-map
  3308. ))
  3309. (throw 'exit t))))))
  3310. (defun org-activate-code (limit)
  3311. (if (re-search-forward "^[ \t]*\\(:.*\\)" limit t)
  3312. (unless (get-text-property (match-beginning 1) 'face)
  3313. (remove-text-properties (match-beginning 0) (match-end 0)
  3314. '(display t invisible t intangible t))
  3315. t)))
  3316. (defun org-activate-angle-links (limit)
  3317. "Run through the buffer and add overlays to links."
  3318. (if (re-search-forward org-angle-link-re limit t)
  3319. (progn
  3320. (add-text-properties (match-beginning 0) (match-end 0)
  3321. (list 'mouse-face 'highlight
  3322. 'rear-nonsticky org-nonsticky-props
  3323. 'keymap org-mouse-map
  3324. ))
  3325. t)))
  3326. (defun org-activate-bracket-links (limit)
  3327. "Run through the buffer and add overlays to bracketed links."
  3328. (if (re-search-forward org-bracket-link-regexp limit t)
  3329. (let* ((help (concat "LINK: "
  3330. (org-match-string-no-properties 1)))
  3331. ;; FIXME: above we should remove the escapes.
  3332. ;; but that requires another match, protecting match data,
  3333. ;; a lot of overhead for font-lock.
  3334. (ip (org-maybe-intangible
  3335. (list 'invisible 'org-link 'rear-nonsticky org-nonsticky-props
  3336. 'keymap org-mouse-map 'mouse-face 'highlight
  3337. 'font-lock-multiline t 'help-echo help)))
  3338. (vp (list 'rear-nonsticky org-nonsticky-props
  3339. 'keymap org-mouse-map 'mouse-face 'highlight
  3340. ' font-lock-multiline t 'help-echo help)))
  3341. ;; We need to remove the invisible property here. Table narrowing
  3342. ;; may have made some of this invisible.
  3343. (remove-text-properties (match-beginning 0) (match-end 0)
  3344. '(invisible nil))
  3345. (if (match-end 3)
  3346. (progn
  3347. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  3348. (add-text-properties (match-beginning 3) (match-end 3) vp)
  3349. (add-text-properties (match-end 3) (match-end 0) ip))
  3350. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  3351. (add-text-properties (match-beginning 1) (match-end 1) vp)
  3352. (add-text-properties (match-end 1) (match-end 0) ip))
  3353. t)))
  3354. (defun org-activate-dates (limit)
  3355. "Run through the buffer and add overlays to dates."
  3356. (if (re-search-forward org-tsr-regexp-both limit t)
  3357. (progn
  3358. (add-text-properties (match-beginning 0) (match-end 0)
  3359. (list 'mouse-face 'highlight
  3360. 'rear-nonsticky org-nonsticky-props
  3361. 'keymap org-mouse-map))
  3362. (when org-display-custom-times
  3363. (if (match-end 3)
  3364. (org-display-custom-time (match-beginning 3) (match-end 3)))
  3365. (org-display-custom-time (match-beginning 1) (match-end 1)))
  3366. t)))
  3367. (defvar org-target-link-regexp nil
  3368. "Regular expression matching radio targets in plain text.")
  3369. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  3370. "Regular expression matching a link target.")
  3371. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  3372. "Regular expression matching a radio target.")
  3373. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  3374. "Regular expression matching any target.")
  3375. (defun org-activate-target-links (limit)
  3376. "Run through the buffer and add overlays to target matches."
  3377. (when org-target-link-regexp
  3378. (let ((case-fold-search t))
  3379. (if (re-search-forward org-target-link-regexp limit t)
  3380. (progn
  3381. (add-text-properties (match-beginning 0) (match-end 0)
  3382. (list 'mouse-face 'highlight
  3383. 'rear-nonsticky org-nonsticky-props
  3384. 'keymap org-mouse-map
  3385. 'help-echo "Radio target link"
  3386. 'org-linked-text t))
  3387. t)))))
  3388. (defun org-update-radio-target-regexp ()
  3389. "Find all radio targets in this file and update the regular expression."
  3390. (interactive)
  3391. (when (memq 'radio org-activate-links)
  3392. (setq org-target-link-regexp
  3393. (org-make-target-link-regexp (org-all-targets 'radio)))
  3394. (org-restart-font-lock)))
  3395. (defun org-hide-wide-columns (limit)
  3396. (let (s e)
  3397. (setq s (text-property-any (point) (or limit (point-max))
  3398. 'org-cwidth t))
  3399. (when s
  3400. (setq e (next-single-property-change s 'org-cwidth))
  3401. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  3402. (goto-char e)
  3403. t)))
  3404. (defvar org-latex-and-specials-regexp nil
  3405. "Regular expression for highlighting export special stuff.")
  3406. (defvar org-match-substring-regexp)
  3407. (defvar org-match-substring-with-braces-regexp)
  3408. (defvar org-export-html-special-string-regexps)
  3409. (defun org-compute-latex-and-specials-regexp ()
  3410. "Compute regular expression for stuff treated specially by exporters."
  3411. (if (not org-highlight-latex-fragments-and-specials)
  3412. (org-set-local 'org-latex-and-specials-regexp nil)
  3413. (require 'org-exp)
  3414. (let*
  3415. ((matchers (plist-get org-format-latex-options :matchers))
  3416. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  3417. org-latex-regexps)))
  3418. (options (org-combine-plists (org-default-export-plist)
  3419. (org-infile-export-plist)))
  3420. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  3421. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  3422. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  3423. (org-export-html-expand (plist-get options :expand-quoted-html))
  3424. (org-export-with-special-strings (plist-get options :special-strings))
  3425. (re-sub
  3426. (cond
  3427. ((equal org-export-with-sub-superscripts '{})
  3428. (list org-match-substring-with-braces-regexp))
  3429. (org-export-with-sub-superscripts
  3430. (list org-match-substring-regexp))
  3431. (t nil)))
  3432. (re-latex
  3433. (if org-export-with-LaTeX-fragments
  3434. (mapcar (lambda (x) (nth 1 x)) latexs)))
  3435. (re-macros
  3436. (if org-export-with-TeX-macros
  3437. (list (concat "\\\\"
  3438. (regexp-opt
  3439. (append (mapcar 'car org-html-entities)
  3440. (if (boundp 'org-latex-entities)
  3441. org-latex-entities nil))
  3442. 'words))) ; FIXME
  3443. ))
  3444. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  3445. (re-special (if org-export-with-special-strings
  3446. (mapcar (lambda (x) (car x))
  3447. org-export-html-special-string-regexps)))
  3448. (re-rest
  3449. (delq nil
  3450. (list
  3451. (if org-export-html-expand "@<[^>\n]+>")
  3452. ))))
  3453. (org-set-local
  3454. 'org-latex-and-specials-regexp
  3455. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  3456. re-rest) "\\|")))))
  3457. (defface org-latex-and-export-specials
  3458. (let ((font (cond ((assq :inherit custom-face-attributes)
  3459. '(:inherit underline))
  3460. (t '(:underline t)))))
  3461. `((((class grayscale) (background light))
  3462. (:foreground "DimGray" ,@font))
  3463. (((class grayscale) (background dark))
  3464. (:foreground "LightGray" ,@font))
  3465. (((class color) (background light))
  3466. (:foreground "SaddleBrown"))
  3467. (((class color) (background dark))
  3468. (:foreground "burlywood"))
  3469. (t (,@font))))
  3470. "Face used to highlight math latex and other special exporter stuff."
  3471. :group 'org-faces)
  3472. (defun org-do-latex-and-special-faces (limit)
  3473. "Run through the buffer and add overlays to links."
  3474. (when org-latex-and-specials-regexp
  3475. (let (rtn d)
  3476. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  3477. limit t))
  3478. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  3479. 'face))
  3480. '(org-code org-verbatim underline)))
  3481. (progn
  3482. (setq rtn t
  3483. d (cond ((member (char-after (1+ (match-beginning 0)))
  3484. '(?_ ?^)) 1)
  3485. (t 0)))
  3486. (font-lock-prepend-text-property
  3487. (+ d (match-beginning 0)) (match-end 0)
  3488. 'face 'org-latex-and-export-specials)
  3489. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  3490. '(font-lock-multiline t)))))
  3491. rtn)))
  3492. (defun org-restart-font-lock ()
  3493. "Restart font-lock-mode, to force refontification."
  3494. (when (and (boundp 'font-lock-mode) font-lock-mode)
  3495. (font-lock-mode -1)
  3496. (font-lock-mode 1)))
  3497. (defun org-all-targets (&optional radio)
  3498. "Return a list of all targets in this file.
  3499. With optional argument RADIO, only find radio targets."
  3500. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  3501. rtn)
  3502. (save-excursion
  3503. (goto-char (point-min))
  3504. (while (re-search-forward re nil t)
  3505. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  3506. rtn)))
  3507. (defun org-make-target-link-regexp (targets)
  3508. "Make regular expression matching all strings in TARGETS.
  3509. The regular expression finds the targets also if there is a line break
  3510. between words."
  3511. (and targets
  3512. (concat
  3513. "\\<\\("
  3514. (mapconcat
  3515. (lambda (x)
  3516. (while (string-match " +" x)
  3517. (setq x (replace-match "\\s-+" t t x)))
  3518. x)
  3519. targets
  3520. "\\|")
  3521. "\\)\\>")))
  3522. (defun org-activate-tags (limit)
  3523. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  3524. (progn
  3525. (add-text-properties (match-beginning 1) (match-end 1)
  3526. (list 'mouse-face 'highlight
  3527. 'rear-nonsticky org-nonsticky-props
  3528. 'keymap org-mouse-map))
  3529. t)))
  3530. (defun org-outline-level ()
  3531. (save-excursion
  3532. (looking-at outline-regexp)
  3533. (if (match-beginning 1)
  3534. (+ (org-get-string-indentation (match-string 1)) 1000)
  3535. (1- (- (match-end 0) (match-beginning 0))))))
  3536. (defvar org-font-lock-keywords nil)
  3537. (defconst org-property-re (org-re "^[ \t]*\\(:\\([[:alnum:]_]+\\):\\)[ \t]*\\(\\S-.*\\)")
  3538. "Regular expression matching a property line.")
  3539. (defun org-set-font-lock-defaults ()
  3540. (let* ((em org-fontify-emphasized-text)
  3541. (lk org-activate-links)
  3542. (org-font-lock-extra-keywords
  3543. (list
  3544. ;; Headlines
  3545. '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
  3546. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  3547. ;; Table lines
  3548. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  3549. (1 'org-table t))
  3550. ;; Table internals
  3551. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  3552. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  3553. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  3554. ;; Drawers
  3555. (list org-drawer-regexp '(0 'org-special-keyword t))
  3556. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  3557. ;; Properties
  3558. (list org-property-re
  3559. '(1 'org-special-keyword t)
  3560. '(3 'org-property-value t))
  3561. (if org-format-transports-properties-p
  3562. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  3563. ;; Links
  3564. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  3565. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  3566. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  3567. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  3568. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  3569. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  3570. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  3571. '(org-hide-wide-columns (0 nil append))
  3572. ;; TODO lines
  3573. (list (concat "^\\*+[ \t]+" org-todo-regexp)
  3574. '(1 (org-get-todo-face 1) t))
  3575. ;; DONE
  3576. (if org-fontify-done-headline
  3577. (list (concat "^[*]+ +\\<\\("
  3578. (mapconcat 'regexp-quote org-done-keywords "\\|")
  3579. "\\)\\(.*\\)")
  3580. '(2 'org-headline-done t))
  3581. nil)
  3582. ;; Priorities
  3583. (list (concat "\\[#[A-Z0-9]\\]") '(0 'org-special-keyword t))
  3584. ;; Special keywords
  3585. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  3586. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  3587. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  3588. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  3589. ;; Emphasis
  3590. (if em
  3591. (if (featurep 'xemacs)
  3592. '(org-do-emphasis-faces (0 nil append))
  3593. '(org-do-emphasis-faces)))
  3594. ;; Checkboxes
  3595. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  3596. 2 'bold prepend)
  3597. (if org-provide-checkbox-statistics
  3598. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  3599. (0 (org-get-checkbox-statistics-face) t)))
  3600. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  3601. '(1 'org-archived prepend))
  3602. ;; Specials
  3603. '(org-do-latex-and-special-faces)
  3604. ;; Code
  3605. '(org-activate-code (1 'org-code t))
  3606. ;; COMMENT
  3607. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  3608. "\\|" org-quote-string "\\)\\>")
  3609. '(1 'org-special-keyword t))
  3610. '("^#.*" (0 'font-lock-comment-face t))
  3611. )))
  3612. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  3613. ;; Now set the full font-lock-keywords
  3614. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  3615. (org-set-local 'font-lock-defaults
  3616. '(org-font-lock-keywords t nil nil backward-paragraph))
  3617. (kill-local-variable 'font-lock-keywords) nil))
  3618. (defvar org-m nil)
  3619. (defvar org-l nil)
  3620. (defvar org-f nil)
  3621. (defun org-get-level-face (n)
  3622. "Get the right face for match N in font-lock matching of healdines."
  3623. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  3624. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  3625. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  3626. (cond
  3627. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  3628. ((eq n 2) org-f)
  3629. (t (if org-level-color-stars-only nil org-f))))
  3630. (defun org-get-todo-face (kwd)
  3631. "Get the right face for a TODO keyword KWD.
  3632. If KWD is a number, get the corresponding match group."
  3633. (if (numberp kwd) (setq kwd (match-string kwd)))
  3634. (or (cdr (assoc kwd org-todo-keyword-faces))
  3635. (and (member kwd org-done-keywords) 'org-done)
  3636. 'org-todo))
  3637. (defun org-unfontify-region (beg end &optional maybe_loudly)
  3638. "Remove fontification and activation overlays from links."
  3639. (font-lock-default-unfontify-region beg end)
  3640. (let* ((buffer-undo-list t)
  3641. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  3642. (inhibit-modification-hooks t)
  3643. deactivate-mark buffer-file-name buffer-file-truename)
  3644. (remove-text-properties beg end
  3645. '(mouse-face t keymap t org-linked-text t
  3646. invisible t intangible t))))
  3647. ;;;; Visibility cycling, including org-goto and indirect buffer
  3648. ;;; Cycling
  3649. (defvar org-cycle-global-status nil)
  3650. (make-variable-buffer-local 'org-cycle-global-status)
  3651. (defvar org-cycle-subtree-status nil)
  3652. (make-variable-buffer-local 'org-cycle-subtree-status)
  3653. ;;;###autoload
  3654. (defun org-cycle (&optional arg)
  3655. "Visibility cycling for Org-mode.
  3656. - When this function is called with a prefix argument, rotate the entire
  3657. buffer through 3 states (global cycling)
  3658. 1. OVERVIEW: Show only top-level headlines.
  3659. 2. CONTENTS: Show all headlines of all levels, but no body text.
  3660. 3. SHOW ALL: Show everything.
  3661. - When point is at the beginning of a headline, rotate the subtree started
  3662. by this line through 3 different states (local cycling)
  3663. 1. FOLDED: Only the main headline is shown.
  3664. 2. CHILDREN: The main headline and the direct children are shown.
  3665. From this state, you can move to one of the children
  3666. and zoom in further.
  3667. 3. SUBTREE: Show the entire subtree, including body text.
  3668. - When there is a numeric prefix, go up to a heading with level ARG, do
  3669. a `show-subtree' and return to the previous cursor position. If ARG
  3670. is negative, go up that many levels.
  3671. - When point is not at the beginning of a headline, execute
  3672. `indent-relative', like TAB normally does. See the option
  3673. `org-cycle-emulate-tab' for details.
  3674. - Special case: if point is at the beginning of the buffer and there is
  3675. no headline in line 1, this function will act as if called with prefix arg.
  3676. But only if also the variable `org-cycle-global-at-bob' is t."
  3677. (interactive "P")
  3678. (org-load-modules-maybe)
  3679. (let* ((outline-regexp
  3680. (if (and (org-mode-p) org-cycle-include-plain-lists)
  3681. "\\(?:\\*+ \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"
  3682. outline-regexp))
  3683. (bob-special (and org-cycle-global-at-bob (bobp)
  3684. (not (looking-at outline-regexp))))
  3685. (org-cycle-hook
  3686. (if bob-special
  3687. (delq 'org-optimize-window-after-visibility-change
  3688. (copy-sequence org-cycle-hook))
  3689. org-cycle-hook))
  3690. (pos (point)))
  3691. (if (or bob-special (equal arg '(4)))
  3692. ;; special case: use global cycling
  3693. (setq arg t))
  3694. (cond
  3695. ((org-at-table-p 'any)
  3696. ;; Enter the table or move to the next field in the table
  3697. (or (org-table-recognize-table.el)
  3698. (progn
  3699. (if arg (org-table-edit-field t)
  3700. (org-table-justify-field-maybe)
  3701. (call-interactively 'org-table-next-field)))))
  3702. ((eq arg t) ;; Global cycling
  3703. (cond
  3704. ((and (eq last-command this-command)
  3705. (eq org-cycle-global-status 'overview))
  3706. ;; We just created the overview - now do table of contents
  3707. ;; This can be slow in very large buffers, so indicate action
  3708. (message "CONTENTS...")
  3709. (org-content)
  3710. (message "CONTENTS...done")
  3711. (setq org-cycle-global-status 'contents)
  3712. (run-hook-with-args 'org-cycle-hook 'contents))
  3713. ((and (eq last-command this-command)
  3714. (eq org-cycle-global-status 'contents))
  3715. ;; We just showed the table of contents - now show everything
  3716. (show-all)
  3717. (message "SHOW ALL")
  3718. (setq org-cycle-global-status 'all)
  3719. (run-hook-with-args 'org-cycle-hook 'all))
  3720. (t
  3721. ;; Default action: go to overview
  3722. (org-overview)
  3723. (message "OVERVIEW")
  3724. (setq org-cycle-global-status 'overview)
  3725. (run-hook-with-args 'org-cycle-hook 'overview))))
  3726. ((and org-drawers org-drawer-regexp
  3727. (save-excursion
  3728. (beginning-of-line 1)
  3729. (looking-at org-drawer-regexp)))
  3730. ;; Toggle block visibility
  3731. (org-flag-drawer
  3732. (not (get-char-property (match-end 0) 'invisible))))
  3733. ((integerp arg)
  3734. ;; Show-subtree, ARG levels up from here.
  3735. (save-excursion
  3736. (org-back-to-heading)
  3737. (outline-up-heading (if (< arg 0) (- arg)
  3738. (- (funcall outline-level) arg)))
  3739. (org-show-subtree)))
  3740. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  3741. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  3742. ;; At a heading: rotate between three different views
  3743. (org-back-to-heading)
  3744. (let ((goal-column 0) eoh eol eos)
  3745. ;; First, some boundaries
  3746. (save-excursion
  3747. (org-back-to-heading)
  3748. (save-excursion
  3749. (beginning-of-line 2)
  3750. (while (and (not (eobp)) ;; this is like `next-line'
  3751. (get-char-property (1- (point)) 'invisible))
  3752. (beginning-of-line 2)) (setq eol (point)))
  3753. (outline-end-of-heading) (setq eoh (point))
  3754. (org-end-of-subtree t)
  3755. (unless (eobp)
  3756. (skip-chars-forward " \t\n")
  3757. (beginning-of-line 1) ; in case this is an item
  3758. )
  3759. (setq eos (1- (point))))
  3760. ;; Find out what to do next and set `this-command'
  3761. (cond
  3762. ((= eos eoh)
  3763. ;; Nothing is hidden behind this heading
  3764. (message "EMPTY ENTRY")
  3765. (setq org-cycle-subtree-status nil)
  3766. (save-excursion
  3767. (goto-char eos)
  3768. (outline-next-heading)
  3769. (if (org-invisible-p) (org-flag-heading nil))))
  3770. ((or (>= eol eos)
  3771. (not (string-match "\\S-" (buffer-substring eol eos))))
  3772. ;; Entire subtree is hidden in one line: open it
  3773. (org-show-entry)
  3774. (show-children)
  3775. (message "CHILDREN")
  3776. (save-excursion
  3777. (goto-char eos)
  3778. (outline-next-heading)
  3779. (if (org-invisible-p) (org-flag-heading nil)))
  3780. (setq org-cycle-subtree-status 'children)
  3781. (run-hook-with-args 'org-cycle-hook 'children))
  3782. ((and (eq last-command this-command)
  3783. (eq org-cycle-subtree-status 'children))
  3784. ;; We just showed the children, now show everything.
  3785. (org-show-subtree)
  3786. (message "SUBTREE")
  3787. (setq org-cycle-subtree-status 'subtree)
  3788. (run-hook-with-args 'org-cycle-hook 'subtree))
  3789. (t
  3790. ;; Default action: hide the subtree.
  3791. (hide-subtree)
  3792. (message "FOLDED")
  3793. (setq org-cycle-subtree-status 'folded)
  3794. (run-hook-with-args 'org-cycle-hook 'folded)))))
  3795. ;; TAB emulation
  3796. (buffer-read-only (org-back-to-heading))
  3797. ((org-try-cdlatex-tab))
  3798. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  3799. (or (not (bolp))
  3800. (not (looking-at outline-regexp))))
  3801. (call-interactively (global-key-binding "\t")))
  3802. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  3803. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  3804. (or (and (eq org-cycle-emulate-tab 'white)
  3805. (= (match-end 0) (point-at-eol)))
  3806. (and (eq org-cycle-emulate-tab 'whitestart)
  3807. (>= (match-end 0) pos))))
  3808. t
  3809. (eq org-cycle-emulate-tab t))
  3810. (call-interactively (global-key-binding "\t")))
  3811. (t (save-excursion
  3812. (org-back-to-heading)
  3813. (org-cycle))))))
  3814. ;;;###autoload
  3815. (defun org-global-cycle (&optional arg)
  3816. "Cycle the global visibility. For details see `org-cycle'."
  3817. (interactive "P")
  3818. (let ((org-cycle-include-plain-lists
  3819. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  3820. (if (integerp arg)
  3821. (progn
  3822. (show-all)
  3823. (hide-sublevels arg)
  3824. (setq org-cycle-global-status 'contents))
  3825. (org-cycle '(4)))))
  3826. (defun org-overview ()
  3827. "Switch to overview mode, shoing only top-level headlines.
  3828. Really, this shows all headlines with level equal or greater than the level
  3829. of the first headline in the buffer. This is important, because if the
  3830. first headline is not level one, then (hide-sublevels 1) gives confusing
  3831. results."
  3832. (interactive)
  3833. (let ((level (save-excursion
  3834. (goto-char (point-min))
  3835. (if (re-search-forward (concat "^" outline-regexp) nil t)
  3836. (progn
  3837. (goto-char (match-beginning 0))
  3838. (funcall outline-level))))))
  3839. (and level (hide-sublevels level))))
  3840. (defun org-content (&optional arg)
  3841. "Show all headlines in the buffer, like a table of contents.
  3842. With numerical argument N, show content up to level N."
  3843. (interactive "P")
  3844. (save-excursion
  3845. ;; Visit all headings and show their offspring
  3846. (and (integerp arg) (org-overview))
  3847. (goto-char (point-max))
  3848. (catch 'exit
  3849. (while (and (progn (condition-case nil
  3850. (outline-previous-visible-heading 1)
  3851. (error (goto-char (point-min))))
  3852. t)
  3853. (looking-at outline-regexp))
  3854. (if (integerp arg)
  3855. (show-children (1- arg))
  3856. (show-branches))
  3857. (if (bobp) (throw 'exit nil))))))
  3858. (defun org-optimize-window-after-visibility-change (state)
  3859. "Adjust the window after a change in outline visibility.
  3860. This function is the default value of the hook `org-cycle-hook'."
  3861. (when (get-buffer-window (current-buffer))
  3862. (cond
  3863. ; ((eq state 'overview) (org-first-headline-recenter 1))
  3864. ; ((eq state 'overview) (org-beginning-of-line))
  3865. ((eq state 'content) nil)
  3866. ((eq state 'all) nil)
  3867. ((eq state 'folded) nil)
  3868. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  3869. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  3870. (defun org-compact-display-after-subtree-move ()
  3871. (let (beg end)
  3872. (save-excursion
  3873. (if (org-up-heading-safe)
  3874. (progn
  3875. (hide-subtree)
  3876. (show-entry)
  3877. (show-children)
  3878. (org-cycle-show-empty-lines 'children)
  3879. (org-cycle-hide-drawers 'children))
  3880. (org-overview)))))
  3881. (defun org-cycle-show-empty-lines (state)
  3882. "Show empty lines above all visible headlines.
  3883. The region to be covered depends on STATE when called through
  3884. `org-cycle-hook'. Lisp program can use t for STATE to get the
  3885. entire buffer covered. Note that an empty line is only shown if there
  3886. are at least `org-cycle-separator-lines' empty lines before the headeline."
  3887. (when (> org-cycle-separator-lines 0)
  3888. (save-excursion
  3889. (let* ((n org-cycle-separator-lines)
  3890. (re (cond
  3891. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  3892. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  3893. (t (let ((ns (number-to-string (- n 2))))
  3894. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  3895. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  3896. beg end)
  3897. (cond
  3898. ((memq state '(overview contents t))
  3899. (setq beg (point-min) end (point-max)))
  3900. ((memq state '(children folded))
  3901. (setq beg (point) end (progn (org-end-of-subtree t t)
  3902. (beginning-of-line 2)
  3903. (point)))))
  3904. (when beg
  3905. (goto-char beg)
  3906. (while (re-search-forward re end t)
  3907. (if (not (get-char-property (match-end 1) 'invisible))
  3908. (outline-flag-region
  3909. (match-beginning 1) (match-end 1) nil)))))))
  3910. ;; Never hide empty lines at the end of the file.
  3911. (save-excursion
  3912. (goto-char (point-max))
  3913. (outline-previous-heading)
  3914. (outline-end-of-heading)
  3915. (if (and (looking-at "[ \t\n]+")
  3916. (= (match-end 0) (point-max)))
  3917. (outline-flag-region (point) (match-end 0) nil))))
  3918. (defun org-subtree-end-visible-p ()
  3919. "Is the end of the current subtree visible?"
  3920. (pos-visible-in-window-p
  3921. (save-excursion (org-end-of-subtree t) (point))))
  3922. (defun org-first-headline-recenter (&optional N)
  3923. "Move cursor to the first headline and recenter the headline.
  3924. Optional argument N means, put the headline into the Nth line of the window."
  3925. (goto-char (point-min))
  3926. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  3927. (beginning-of-line)
  3928. (recenter (prefix-numeric-value N))))
  3929. ;;; Org-goto
  3930. (defvar org-goto-window-configuration nil)
  3931. (defvar org-goto-marker nil)
  3932. (defvar org-goto-map
  3933. (let ((map (make-sparse-keymap)))
  3934. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  3935. (while (setq cmd (pop cmds))
  3936. (substitute-key-definition cmd cmd map global-map)))
  3937. (suppress-keymap map)
  3938. (org-defkey map "\C-m" 'org-goto-ret)
  3939. (org-defkey map [(return)] 'org-goto-ret)
  3940. (org-defkey map [(left)] 'org-goto-left)
  3941. (org-defkey map [(right)] 'org-goto-right)
  3942. (org-defkey map [(control ?g)] 'org-goto-quit)
  3943. (org-defkey map "\C-i" 'org-cycle)
  3944. (org-defkey map [(tab)] 'org-cycle)
  3945. (org-defkey map [(down)] 'outline-next-visible-heading)
  3946. (org-defkey map [(up)] 'outline-previous-visible-heading)
  3947. (if org-goto-auto-isearch
  3948. (if (fboundp 'define-key-after)
  3949. (define-key-after map [t] 'org-goto-local-auto-isearch)
  3950. nil)
  3951. (org-defkey map "q" 'org-goto-quit)
  3952. (org-defkey map "n" 'outline-next-visible-heading)
  3953. (org-defkey map "p" 'outline-previous-visible-heading)
  3954. (org-defkey map "f" 'outline-forward-same-level)
  3955. (org-defkey map "b" 'outline-backward-same-level)
  3956. (org-defkey map "u" 'outline-up-heading))
  3957. (org-defkey map "/" 'org-occur)
  3958. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  3959. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  3960. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  3961. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  3962. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  3963. map))
  3964. (defconst org-goto-help
  3965. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  3966. RET=jump to location [Q]uit and return to previous location
  3967. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  3968. (defvar org-goto-start-pos) ; dynamically scoped parameter
  3969. (defun org-goto (&optional alternative-interface)
  3970. "Look up a different location in the current file, keeping current visibility.
  3971. When you want look-up or go to a different location in a document, the
  3972. fastest way is often to fold the entire buffer and then dive into the tree.
  3973. This method has the disadvantage, that the previous location will be folded,
  3974. which may not be what you want.
  3975. This command works around this by showing a copy of the current buffer
  3976. in an indirect buffer, in overview mode. You can dive into the tree in
  3977. that copy, use org-occur and incremental search to find a location.
  3978. When pressing RET or `Q', the command returns to the original buffer in
  3979. which the visibility is still unchanged. After RET is will also jump to
  3980. the location selected in the indirect buffer and expose the
  3981. the headline hierarchy above."
  3982. (interactive "P")
  3983. (let* ((org-refile-targets '((nil . (:maxlevel . 10))))
  3984. (org-refile-use-outline-path t)
  3985. (interface
  3986. (if (not alternative-interface)
  3987. org-goto-interface
  3988. (if (eq org-goto-interface 'outline)
  3989. 'outline-path-completion
  3990. 'outline)))
  3991. (org-goto-start-pos (point))
  3992. (selected-point
  3993. (if (eq interface 'outline)
  3994. (car (org-get-location (current-buffer) org-goto-help))
  3995. (nth 3 (org-refile-get-location "Goto: ")))))
  3996. (if selected-point
  3997. (progn
  3998. (org-mark-ring-push org-goto-start-pos)
  3999. (goto-char selected-point)
  4000. (if (or (org-invisible-p) (org-invisible-p2))
  4001. (org-show-context 'org-goto)))
  4002. (message "Quit"))))
  4003. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  4004. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  4005. (defvar org-goto-local-auto-isearch-map) ; defined below
  4006. (defun org-get-location (buf help)
  4007. "Let the user select a location in the Org-mode buffer BUF.
  4008. This function uses a recursive edit. It returns the selected position
  4009. or nil."
  4010. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  4011. (isearch-hide-immediately nil)
  4012. (isearch-search-fun-function
  4013. (lambda () 'org-goto-local-search-forward-headings))
  4014. (org-goto-selected-point org-goto-exit-command))
  4015. (save-excursion
  4016. (save-window-excursion
  4017. (delete-other-windows)
  4018. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  4019. (switch-to-buffer
  4020. (condition-case nil
  4021. (make-indirect-buffer (current-buffer) "*org-goto*")
  4022. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  4023. (with-output-to-temp-buffer "*Help*"
  4024. (princ help))
  4025. (shrink-window-if-larger-than-buffer (get-buffer-window "*Help*"))
  4026. (setq buffer-read-only nil)
  4027. (let ((org-startup-truncated t)
  4028. (org-startup-folded nil)
  4029. (org-startup-align-all-tables nil))
  4030. (org-mode)
  4031. (org-overview))
  4032. (setq buffer-read-only t)
  4033. (if (and (boundp 'org-goto-start-pos)
  4034. (integer-or-marker-p org-goto-start-pos))
  4035. (let ((org-show-hierarchy-above t)
  4036. (org-show-siblings t)
  4037. (org-show-following-heading t))
  4038. (goto-char org-goto-start-pos)
  4039. (and (org-invisible-p) (org-show-context)))
  4040. (goto-char (point-min)))
  4041. (org-beginning-of-line)
  4042. (message "Select location and press RET")
  4043. (use-local-map org-goto-map)
  4044. (recursive-edit)
  4045. ))
  4046. (kill-buffer "*org-goto*")
  4047. (cons org-goto-selected-point org-goto-exit-command)))
  4048. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  4049. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  4050. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  4051. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  4052. (defun org-goto-local-search-forward-headings (string bound noerror)
  4053. "Search and make sure that anu matches are in headlines."
  4054. (catch 'return
  4055. (while (search-forward string bound noerror)
  4056. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  4057. (and (member :headline context)
  4058. (not (member :tags context))))
  4059. (throw 'return (point))))))
  4060. (defun org-goto-local-auto-isearch ()
  4061. "Start isearch."
  4062. (interactive)
  4063. (goto-char (point-min))
  4064. (let ((keys (this-command-keys)))
  4065. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  4066. (isearch-mode t)
  4067. (isearch-process-search-char (string-to-char keys)))))
  4068. (defun org-goto-ret (&optional arg)
  4069. "Finish `org-goto' by going to the new location."
  4070. (interactive "P")
  4071. (setq org-goto-selected-point (point)
  4072. org-goto-exit-command 'return)
  4073. (throw 'exit nil))
  4074. (defun org-goto-left ()
  4075. "Finish `org-goto' by going to the new location."
  4076. (interactive)
  4077. (if (org-on-heading-p)
  4078. (progn
  4079. (beginning-of-line 1)
  4080. (setq org-goto-selected-point (point)
  4081. org-goto-exit-command 'left)
  4082. (throw 'exit nil))
  4083. (error "Not on a heading")))
  4084. (defun org-goto-right ()
  4085. "Finish `org-goto' by going to the new location."
  4086. (interactive)
  4087. (if (org-on-heading-p)
  4088. (progn
  4089. (setq org-goto-selected-point (point)
  4090. org-goto-exit-command 'right)
  4091. (throw 'exit nil))
  4092. (error "Not on a heading")))
  4093. (defun org-goto-quit ()
  4094. "Finish `org-goto' without cursor motion."
  4095. (interactive)
  4096. (setq org-goto-selected-point nil)
  4097. (setq org-goto-exit-command 'quit)
  4098. (throw 'exit nil))
  4099. ;;; Indirect buffer display of subtrees
  4100. (defvar org-indirect-dedicated-frame nil
  4101. "This is the frame being used for indirect tree display.")
  4102. (defvar org-last-indirect-buffer nil)
  4103. (defun org-tree-to-indirect-buffer (&optional arg)
  4104. "Create indirect buffer and narrow it to current subtree.
  4105. With numerical prefix ARG, go up to this level and then take that tree.
  4106. If ARG is negative, go up that many levels.
  4107. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  4108. indirect buffer previously made with this command, to avoid proliferation of
  4109. indirect buffers. However, when you call the command with a `C-u' prefix, or
  4110. when `org-indirect-buffer-display' is `new-frame', the last buffer
  4111. is kept so that you can work with several indirect buffers at the same time.
  4112. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  4113. requests that a new frame be made for the new buffer, so that the dedicated
  4114. frame is not changed."
  4115. (interactive "P")
  4116. (let ((cbuf (current-buffer))
  4117. (cwin (selected-window))
  4118. (pos (point))
  4119. beg end level heading ibuf)
  4120. (save-excursion
  4121. (org-back-to-heading t)
  4122. (when (numberp arg)
  4123. (setq level (org-outline-level))
  4124. (if (< arg 0) (setq arg (+ level arg)))
  4125. (while (> (setq level (org-outline-level)) arg)
  4126. (outline-up-heading 1 t)))
  4127. (setq beg (point)
  4128. heading (org-get-heading))
  4129. (org-end-of-subtree t) (setq end (point)))
  4130. (if (and (buffer-live-p org-last-indirect-buffer)
  4131. (not (eq org-indirect-buffer-display 'new-frame))
  4132. (not arg))
  4133. (kill-buffer org-last-indirect-buffer))
  4134. (setq ibuf (org-get-indirect-buffer cbuf)
  4135. org-last-indirect-buffer ibuf)
  4136. (cond
  4137. ((or (eq org-indirect-buffer-display 'new-frame)
  4138. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  4139. (select-frame (make-frame))
  4140. (delete-other-windows)
  4141. (switch-to-buffer ibuf)
  4142. (org-set-frame-title heading))
  4143. ((eq org-indirect-buffer-display 'dedicated-frame)
  4144. (raise-frame
  4145. (select-frame (or (and org-indirect-dedicated-frame
  4146. (frame-live-p org-indirect-dedicated-frame)
  4147. org-indirect-dedicated-frame)
  4148. (setq org-indirect-dedicated-frame (make-frame)))))
  4149. (delete-other-windows)
  4150. (switch-to-buffer ibuf)
  4151. (org-set-frame-title (concat "Indirect: " heading)))
  4152. ((eq org-indirect-buffer-display 'current-window)
  4153. (switch-to-buffer ibuf))
  4154. ((eq org-indirect-buffer-display 'other-window)
  4155. (pop-to-buffer ibuf))
  4156. (t (error "Invalid value.")))
  4157. (if (featurep 'xemacs)
  4158. (save-excursion (org-mode) (turn-on-font-lock)))
  4159. (narrow-to-region beg end)
  4160. (show-all)
  4161. (goto-char pos)
  4162. (and (window-live-p cwin) (select-window cwin))))
  4163. (defun org-get-indirect-buffer (&optional buffer)
  4164. (setq buffer (or buffer (current-buffer)))
  4165. (let ((n 1) (base (buffer-name buffer)) bname)
  4166. (while (buffer-live-p
  4167. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  4168. (setq n (1+ n)))
  4169. (condition-case nil
  4170. (make-indirect-buffer buffer bname 'clone)
  4171. (error (make-indirect-buffer buffer bname)))))
  4172. (defun org-set-frame-title (title)
  4173. "Set the title of the current frame to the string TITLE."
  4174. ;; FIXME: how to name a single frame in XEmacs???
  4175. (unless (featurep 'xemacs)
  4176. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  4177. ;;;; Structure editing
  4178. ;;; Inserting headlines
  4179. (defun org-insert-heading (&optional force-heading)
  4180. "Insert a new heading or item with same depth at point.
  4181. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  4182. If point is at the beginning of a headline, insert a sibling before the
  4183. current headline. If point is not at the beginning, do not split the line,
  4184. but create the new hedline after the current line."
  4185. (interactive "P")
  4186. (if (= (buffer-size) 0)
  4187. (insert "\n* ")
  4188. (when (or force-heading (not (org-insert-item)))
  4189. (let* ((head (save-excursion
  4190. (condition-case nil
  4191. (progn
  4192. (org-back-to-heading)
  4193. (match-string 0))
  4194. (error "*"))))
  4195. (blank (cdr (assq 'heading org-blank-before-new-entry)))
  4196. pos)
  4197. (cond
  4198. ((and (org-on-heading-p) (bolp)
  4199. (or (bobp)
  4200. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  4201. ;; insert before the current line
  4202. (open-line (if blank 2 1)))
  4203. ((and (bolp)
  4204. (or (bobp)
  4205. (save-excursion
  4206. (backward-char 1) (not (org-invisible-p)))))
  4207. ;; insert right here
  4208. nil)
  4209. (t
  4210. ;; in the middle of the line
  4211. (org-show-entry)
  4212. (let ((split
  4213. (org-get-alist-option org-M-RET-may-split-line 'headline))
  4214. tags pos)
  4215. (if (org-on-heading-p)
  4216. (progn
  4217. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  4218. (setq tags (and (match-end 2) (match-string 2)))
  4219. (and (match-end 1)
  4220. (delete-region (match-beginning 1) (match-end 1)))
  4221. (setq pos (point-at-bol))
  4222. (or split (end-of-line 1))
  4223. (delete-horizontal-space)
  4224. (newline (if blank 2 1))
  4225. (when tags
  4226. (save-excursion
  4227. (goto-char pos)
  4228. (end-of-line 1)
  4229. (insert " " tags)
  4230. (org-set-tags nil 'align))))
  4231. (or split (end-of-line 1))
  4232. (newline (if blank 2 1))))))
  4233. (insert head) (just-one-space)
  4234. (setq pos (point))
  4235. (end-of-line 1)
  4236. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  4237. (run-hooks 'org-insert-heading-hook)))))
  4238. (defun org-get-heading (&optional no-tags)
  4239. "Return the heading of the current entry, without the stars."
  4240. (save-excursion
  4241. (org-back-to-heading t)
  4242. (if (looking-at
  4243. (if no-tags
  4244. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  4245. "\\*+[ \t]+\\([^\r\n]*\\)"))
  4246. (match-string 1) "")))
  4247. (defun org-insert-heading-after-current ()
  4248. "Insert a new heading with same level as current, after current subtree."
  4249. (interactive)
  4250. (org-back-to-heading)
  4251. (org-insert-heading)
  4252. (org-move-subtree-down)
  4253. (end-of-line 1))
  4254. (defun org-insert-todo-heading (arg)
  4255. "Insert a new heading with the same level and TODO state as current heading.
  4256. If the heading has no TODO state, or if the state is DONE, use the first
  4257. state (TODO by default). Also with prefix arg, force first state."
  4258. (interactive "P")
  4259. (when (not (org-insert-item 'checkbox))
  4260. (org-insert-heading)
  4261. (save-excursion
  4262. (org-back-to-heading)
  4263. (outline-previous-heading)
  4264. (looking-at org-todo-line-regexp))
  4265. (if (or arg
  4266. (not (match-beginning 2))
  4267. (member (match-string 2) org-done-keywords))
  4268. (insert (car org-todo-keywords-1) " ")
  4269. (insert (match-string 2) " "))))
  4270. (defun org-insert-subheading (arg)
  4271. "Insert a new subheading and demote it.
  4272. Works for outline headings and for plain lists alike."
  4273. (interactive "P")
  4274. (org-insert-heading arg)
  4275. (cond
  4276. ((org-on-heading-p) (org-do-demote))
  4277. ((org-at-item-p) (org-indent-item 1))))
  4278. (defun org-insert-todo-subheading (arg)
  4279. "Insert a new subheading with TODO keyword or checkbox and demote it.
  4280. Works for outline headings and for plain lists alike."
  4281. (interactive "P")
  4282. (org-insert-todo-heading arg)
  4283. (cond
  4284. ((org-on-heading-p) (org-do-demote))
  4285. ((org-at-item-p) (org-indent-item 1))))
  4286. ;;; Promotion and Demotion
  4287. (defun org-promote-subtree ()
  4288. "Promote the entire subtree.
  4289. See also `org-promote'."
  4290. (interactive)
  4291. (save-excursion
  4292. (org-map-tree 'org-promote))
  4293. (org-fix-position-after-promote))
  4294. (defun org-demote-subtree ()
  4295. "Demote the entire subtree. See `org-demote'.
  4296. See also `org-promote'."
  4297. (interactive)
  4298. (save-excursion
  4299. (org-map-tree 'org-demote))
  4300. (org-fix-position-after-promote))
  4301. (defun org-do-promote ()
  4302. "Promote the current heading higher up the tree.
  4303. If the region is active in `transient-mark-mode', promote all headings
  4304. in the region."
  4305. (interactive)
  4306. (save-excursion
  4307. (if (org-region-active-p)
  4308. (org-map-region 'org-promote (region-beginning) (region-end))
  4309. (org-promote)))
  4310. (org-fix-position-after-promote))
  4311. (defun org-do-demote ()
  4312. "Demote the current heading lower down the tree.
  4313. If the region is active in `transient-mark-mode', demote all headings
  4314. in the region."
  4315. (interactive)
  4316. (save-excursion
  4317. (if (org-region-active-p)
  4318. (org-map-region 'org-demote (region-beginning) (region-end))
  4319. (org-demote)))
  4320. (org-fix-position-after-promote))
  4321. (defun org-fix-position-after-promote ()
  4322. "Make sure that after pro/demotion cursor position is right."
  4323. (let ((pos (point)))
  4324. (when (save-excursion
  4325. (beginning-of-line 1)
  4326. (looking-at org-todo-line-regexp)
  4327. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  4328. (cond ((eobp) (insert " "))
  4329. ((eolp) (insert " "))
  4330. ((equal (char-after) ?\ ) (forward-char 1))))))
  4331. (defun org-reduced-level (l)
  4332. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  4333. (defun org-get-valid-level (level &optional change)
  4334. "Rectify a level change under the influence of `org-odd-levels-only'
  4335. LEVEL is a current level, CHANGE is by how much the level should be
  4336. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  4337. even level numbers will become the next higher odd number."
  4338. (if org-odd-levels-only
  4339. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  4340. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  4341. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  4342. (max 1 (+ level change))))
  4343. (if (boundp 'define-obsolete-function-alias)
  4344. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  4345. (define-obsolete-function-alias 'org-get-legal-level
  4346. 'org-get-valid-level)
  4347. (define-obsolete-function-alias 'org-get-legal-level
  4348. 'org-get-valid-level "23.1")))
  4349. (defun org-promote ()
  4350. "Promote the current heading higher up the tree.
  4351. If the region is active in `transient-mark-mode', promote all headings
  4352. in the region."
  4353. (org-back-to-heading t)
  4354. (let* ((level (save-match-data (funcall outline-level)))
  4355. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  4356. (diff (abs (- level (length up-head) -1))))
  4357. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  4358. (replace-match up-head nil t)
  4359. ;; Fixup tag positioning
  4360. (and org-auto-align-tags (org-set-tags nil t))
  4361. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  4362. (defun org-demote ()
  4363. "Demote the current heading lower down the tree.
  4364. If the region is active in `transient-mark-mode', demote all headings
  4365. in the region."
  4366. (org-back-to-heading t)
  4367. (let* ((level (save-match-data (funcall outline-level)))
  4368. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  4369. (diff (abs (- level (length down-head) -1))))
  4370. (replace-match down-head nil t)
  4371. ;; Fixup tag positioning
  4372. (and org-auto-align-tags (org-set-tags nil t))
  4373. (if org-adapt-indentation (org-fixup-indentation diff))))
  4374. (defun org-map-tree (fun)
  4375. "Call FUN for every heading underneath the current one."
  4376. (org-back-to-heading)
  4377. (let ((level (funcall outline-level)))
  4378. (save-excursion
  4379. (funcall fun)
  4380. (while (and (progn
  4381. (outline-next-heading)
  4382. (> (funcall outline-level) level))
  4383. (not (eobp)))
  4384. (funcall fun)))))
  4385. (defun org-map-region (fun beg end)
  4386. "Call FUN for every heading between BEG and END."
  4387. (let ((org-ignore-region t))
  4388. (save-excursion
  4389. (setq end (copy-marker end))
  4390. (goto-char beg)
  4391. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  4392. (< (point) end))
  4393. (funcall fun))
  4394. (while (and (progn
  4395. (outline-next-heading)
  4396. (< (point) end))
  4397. (not (eobp)))
  4398. (funcall fun)))))
  4399. (defun org-fixup-indentation (diff)
  4400. "Change the indentation in the current entry by DIFF
  4401. However, if any line in the current entry has no indentation, or if it
  4402. would end up with no indentation after the change, nothing at all is done."
  4403. (save-excursion
  4404. (let ((end (save-excursion (outline-next-heading)
  4405. (point-marker)))
  4406. (prohibit (if (> diff 0)
  4407. "^\\S-"
  4408. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  4409. col)
  4410. (unless (save-excursion (end-of-line 1)
  4411. (re-search-forward prohibit end t))
  4412. (while (and (< (point) end)
  4413. (re-search-forward "^[ \t]+" end t))
  4414. (goto-char (match-end 0))
  4415. (setq col (current-column))
  4416. (if (< diff 0) (replace-match ""))
  4417. (indent-to (+ diff col))))
  4418. (move-marker end nil))))
  4419. (defun org-convert-to-odd-levels ()
  4420. "Convert an org-mode file with all levels allowed to one with odd levels.
  4421. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  4422. level 5 etc."
  4423. (interactive)
  4424. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  4425. (let ((org-odd-levels-only nil) n)
  4426. (save-excursion
  4427. (goto-char (point-min))
  4428. (while (re-search-forward "^\\*\\*+ " nil t)
  4429. (setq n (- (length (match-string 0)) 2))
  4430. (while (>= (setq n (1- n)) 0)
  4431. (org-demote))
  4432. (end-of-line 1))))))
  4433. (defun org-convert-to-oddeven-levels ()
  4434. "Convert an org-mode file with only odd levels to one with odd and even levels.
  4435. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  4436. section with an even level, conversion would destroy the structure of the file. An error
  4437. is signaled in this case."
  4438. (interactive)
  4439. (goto-char (point-min))
  4440. ;; First check if there are no even levels
  4441. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  4442. (org-show-context t)
  4443. (error "Not all levels are odd in this file. Conversion not possible."))
  4444. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  4445. (let ((org-odd-levels-only nil) n)
  4446. (save-excursion
  4447. (goto-char (point-min))
  4448. (while (re-search-forward "^\\*\\*+ " nil t)
  4449. (setq n (/ (1- (length (match-string 0))) 2))
  4450. (while (>= (setq n (1- n)) 0)
  4451. (org-promote))
  4452. (end-of-line 1))))))
  4453. (defun org-tr-level (n)
  4454. "Make N odd if required."
  4455. (if org-odd-levels-only (1+ (/ n 2)) n))
  4456. ;;; Vertical tree motion, cutting and pasting of subtrees
  4457. (defun org-move-subtree-up (&optional arg)
  4458. "Move the current subtree up past ARG headlines of the same level."
  4459. (interactive "p")
  4460. (org-move-subtree-down (- (prefix-numeric-value arg))))
  4461. (defun org-move-subtree-down (&optional arg)
  4462. "Move the current subtree down past ARG headlines of the same level."
  4463. (interactive "p")
  4464. (setq arg (prefix-numeric-value arg))
  4465. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  4466. 'outline-get-last-sibling))
  4467. (ins-point (make-marker))
  4468. (cnt (abs arg))
  4469. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  4470. ;; Select the tree
  4471. (org-back-to-heading)
  4472. (setq beg0 (point))
  4473. (save-excursion
  4474. (setq ne-beg (org-back-over-empty-lines))
  4475. (setq beg (point)))
  4476. (save-match-data
  4477. (save-excursion (outline-end-of-heading)
  4478. (setq folded (org-invisible-p)))
  4479. (outline-end-of-subtree))
  4480. (outline-next-heading)
  4481. (setq ne-end (org-back-over-empty-lines))
  4482. (setq end (point))
  4483. (goto-char beg0)
  4484. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  4485. ;; include less whitespace
  4486. (save-excursion
  4487. (goto-char beg)
  4488. (forward-line (- ne-beg ne-end))
  4489. (setq beg (point))))
  4490. ;; Find insertion point, with error handling
  4491. (while (> cnt 0)
  4492. (or (and (funcall movfunc) (looking-at outline-regexp))
  4493. (progn (goto-char beg0)
  4494. (error "Cannot move past superior level or buffer limit")))
  4495. (setq cnt (1- cnt)))
  4496. (if (> arg 0)
  4497. ;; Moving forward - still need to move over subtree
  4498. (progn (org-end-of-subtree t t)
  4499. (save-excursion
  4500. (org-back-over-empty-lines)
  4501. (or (bolp) (newline)))))
  4502. (setq ne-ins (org-back-over-empty-lines))
  4503. (move-marker ins-point (point))
  4504. (setq txt (buffer-substring beg end))
  4505. (delete-region beg end)
  4506. (outline-flag-region (1- beg) beg nil)
  4507. (outline-flag-region (1- (point)) (point) nil)
  4508. (insert txt)
  4509. (or (bolp) (insert "\n"))
  4510. (setq ins-end (point))
  4511. (goto-char ins-point)
  4512. (org-skip-whitespace)
  4513. (when (and (< arg 0)
  4514. (org-first-sibling-p)
  4515. (> ne-ins ne-beg))
  4516. ;; Move whitespace back to beginning
  4517. (save-excursion
  4518. (goto-char ins-end)
  4519. (let ((kill-whole-line t))
  4520. (kill-line (- ne-ins ne-beg)) (point)))
  4521. (insert (make-string (- ne-ins ne-beg) ?\n)))
  4522. (move-marker ins-point nil)
  4523. (org-compact-display-after-subtree-move)
  4524. (unless folded
  4525. (org-show-entry)
  4526. (show-children)
  4527. (org-cycle-hide-drawers 'children))))
  4528. (defvar org-subtree-clip ""
  4529. "Clipboard for cut and paste of subtrees.
  4530. This is actually only a copy of the kill, because we use the normal kill
  4531. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  4532. (defvar org-subtree-clip-folded nil
  4533. "Was the last copied subtree folded?
  4534. This is used to fold the tree back after pasting.")
  4535. (defun org-cut-subtree (&optional n)
  4536. "Cut the current subtree into the clipboard.
  4537. With prefix arg N, cut this many sequential subtrees.
  4538. This is a short-hand for marking the subtree and then cutting it."
  4539. (interactive "p")
  4540. (org-copy-subtree n 'cut))
  4541. (defun org-copy-subtree (&optional n cut)
  4542. "Cut the current subtree into the clipboard.
  4543. With prefix arg N, cut this many sequential subtrees.
  4544. This is a short-hand for marking the subtree and then copying it.
  4545. If CUT is non-nil, actually cut the subtree."
  4546. (interactive "p")
  4547. (let (beg end folded (beg0 (point)))
  4548. (if (interactive-p)
  4549. (org-back-to-heading nil) ; take what looks like a subtree
  4550. (org-back-to-heading t)) ; take what is really there
  4551. (org-back-over-empty-lines)
  4552. (setq beg (point))
  4553. (skip-chars-forward " \t\r\n")
  4554. (save-match-data
  4555. (save-excursion (outline-end-of-heading)
  4556. (setq folded (org-invisible-p)))
  4557. (condition-case nil
  4558. (outline-forward-same-level (1- n))
  4559. (error nil))
  4560. (org-end-of-subtree t t))
  4561. (org-back-over-empty-lines)
  4562. (setq end (point))
  4563. (goto-char beg0)
  4564. (when (> end beg)
  4565. (setq org-subtree-clip-folded folded)
  4566. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  4567. (setq org-subtree-clip (current-kill 0))
  4568. (message "%s: Subtree(s) with %d characters"
  4569. (if cut "Cut" "Copied")
  4570. (length org-subtree-clip)))))
  4571. (defun org-paste-subtree (&optional level tree)
  4572. "Paste the clipboard as a subtree, with modification of headline level.
  4573. The entire subtree is promoted or demoted in order to match a new headline
  4574. level. By default, the new level is derived from the visible headings
  4575. before and after the insertion point, and taken to be the inferior headline
  4576. level of the two. So if the previous visible heading is level 3 and the
  4577. next is level 4 (or vice versa), level 4 will be used for insertion.
  4578. This makes sure that the subtree remains an independent subtree and does
  4579. not swallow low level entries.
  4580. You can also force a different level, either by using a numeric prefix
  4581. argument, or by inserting the heading marker by hand. For example, if the
  4582. cursor is after \"*****\", then the tree will be shifted to level 5.
  4583. If you want to insert the tree as is, just use \\[yank].
  4584. If optional TREE is given, use this text instead of the kill ring."
  4585. (interactive "P")
  4586. (unless (org-kill-is-subtree-p tree)
  4587. (error "%s"
  4588. (substitute-command-keys
  4589. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  4590. (let* ((txt (or tree (and kill-ring (current-kill 0))))
  4591. (^re (concat "^\\(" outline-regexp "\\)"))
  4592. (re (concat "\\(" outline-regexp "\\)"))
  4593. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  4594. (old-level (if (string-match ^re txt)
  4595. (- (match-end 0) (match-beginning 0) 1)
  4596. -1))
  4597. (force-level (cond (level (prefix-numeric-value level))
  4598. ((string-match
  4599. ^re_ (buffer-substring (point-at-bol) (point)))
  4600. (- (match-end 1) (match-beginning 1)))
  4601. (t nil)))
  4602. (previous-level (save-excursion
  4603. (condition-case nil
  4604. (progn
  4605. (outline-previous-visible-heading 1)
  4606. (if (looking-at re)
  4607. (- (match-end 0) (match-beginning 0) 1)
  4608. 1))
  4609. (error 1))))
  4610. (next-level (save-excursion
  4611. (condition-case nil
  4612. (progn
  4613. (or (looking-at outline-regexp)
  4614. (outline-next-visible-heading 1))
  4615. (if (looking-at re)
  4616. (- (match-end 0) (match-beginning 0) 1)
  4617. 1))
  4618. (error 1))))
  4619. (new-level (or force-level (max previous-level next-level)))
  4620. (shift (if (or (= old-level -1)
  4621. (= new-level -1)
  4622. (= old-level new-level))
  4623. 0
  4624. (- new-level old-level)))
  4625. (delta (if (> shift 0) -1 1))
  4626. (func (if (> shift 0) 'org-demote 'org-promote))
  4627. (org-odd-levels-only nil)
  4628. beg end)
  4629. ;; Remove the forced level indicator
  4630. (if force-level
  4631. (delete-region (point-at-bol) (point)))
  4632. ;; Paste
  4633. (beginning-of-line 1)
  4634. (org-back-over-empty-lines)
  4635. (setq beg (point))
  4636. (insert-before-markers txt)
  4637. (unless (string-match "\n\\'" txt) (insert "\n"))
  4638. (setq end (point))
  4639. (goto-char beg)
  4640. (skip-chars-forward " \t\n\r")
  4641. (setq beg (point))
  4642. ;; Shift if necessary
  4643. (unless (= shift 0)
  4644. (save-restriction
  4645. (narrow-to-region beg end)
  4646. (while (not (= shift 0))
  4647. (org-map-region func (point-min) (point-max))
  4648. (setq shift (+ delta shift)))
  4649. (goto-char (point-min))))
  4650. (when (interactive-p)
  4651. (message "Clipboard pasted as level %d subtree" new-level))
  4652. (if (and kill-ring
  4653. (eq org-subtree-clip (current-kill 0))
  4654. org-subtree-clip-folded)
  4655. ;; The tree was folded before it was killed/copied
  4656. (hide-subtree))))
  4657. (defun org-kill-is-subtree-p (&optional txt)
  4658. "Check if the current kill is an outline subtree, or a set of trees.
  4659. Returns nil if kill does not start with a headline, or if the first
  4660. headline level is not the largest headline level in the tree.
  4661. So this will actually accept several entries of equal levels as well,
  4662. which is OK for `org-paste-subtree'.
  4663. If optional TXT is given, check this string instead of the current kill."
  4664. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  4665. (start-level (and kill
  4666. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  4667. org-outline-regexp "\\)")
  4668. kill)
  4669. (- (match-end 2) (match-beginning 2) 1)))
  4670. (re (concat "^" org-outline-regexp))
  4671. (start (1+ (match-beginning 2))))
  4672. (if (not start-level)
  4673. (progn
  4674. nil) ;; does not even start with a heading
  4675. (catch 'exit
  4676. (while (setq start (string-match re kill (1+ start)))
  4677. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  4678. (throw 'exit nil)))
  4679. t))))
  4680. (defun org-narrow-to-subtree ()
  4681. "Narrow buffer to the current subtree."
  4682. (interactive)
  4683. (save-excursion
  4684. (save-match-data
  4685. (narrow-to-region
  4686. (progn (org-back-to-heading) (point))
  4687. (progn (org-end-of-subtree t t) (point))))))
  4688. ;;; Outline Sorting
  4689. (defun org-sort (with-case)
  4690. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  4691. Optional argument WITH-CASE means sort case-sensitively."
  4692. (interactive "P")
  4693. (if (org-at-table-p)
  4694. (org-call-with-arg 'org-table-sort-lines with-case)
  4695. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  4696. (defun org-sort-remove-invisible (s)
  4697. (remove-text-properties 0 (length s) org-rm-props s)
  4698. (while (string-match org-bracket-link-regexp s)
  4699. (setq s (replace-match (if (match-end 2)
  4700. (match-string 3 s)
  4701. (match-string 1 s)) t t s)))
  4702. s)
  4703. (defvar org-priority-regexp) ; defined later in the file
  4704. (defun org-sort-entries-or-items (&optional with-case sorting-type getkey-func property)
  4705. "Sort entries on a certain level of an outline tree.
  4706. If there is an active region, the entries in the region are sorted.
  4707. Else, if the cursor is before the first entry, sort the top-level items.
  4708. Else, the children of the entry at point are sorted.
  4709. Sorting can be alphabetically, numerically, and by date/time as given by
  4710. the first time stamp in the entry. The command prompts for the sorting
  4711. type unless it has been given to the function through the SORTING-TYPE
  4712. argument, which needs to a character, any of (?n ?N ?a ?A ?t ?T ?p ?P ?f ?F).
  4713. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  4714. called with point at the beginning of the record. It must return either
  4715. a string or a number that should serve as the sorting key for that record.
  4716. Comparing entries ignores case by default. However, with an optional argument
  4717. WITH-CASE, the sorting considers case as well."
  4718. (interactive "P")
  4719. (let ((case-func (if with-case 'identity 'downcase))
  4720. start beg end stars re re2
  4721. txt what tmp plain-list-p)
  4722. ;; Find beginning and end of region to sort
  4723. (cond
  4724. ((org-region-active-p)
  4725. ;; we will sort the region
  4726. (setq end (region-end)
  4727. what "region")
  4728. (goto-char (region-beginning))
  4729. (if (not (org-on-heading-p)) (outline-next-heading))
  4730. (setq start (point)))
  4731. ((org-at-item-p)
  4732. ;; we will sort this plain list
  4733. (org-beginning-of-item-list) (setq start (point))
  4734. (org-end-of-item-list) (setq end (point))
  4735. (goto-char start)
  4736. (setq plain-list-p t
  4737. what "plain list"))
  4738. ((or (org-on-heading-p)
  4739. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  4740. ;; we will sort the children of the current headline
  4741. (org-back-to-heading)
  4742. (setq start (point)
  4743. end (progn (org-end-of-subtree t t)
  4744. (org-back-over-empty-lines)
  4745. (point))
  4746. what "children")
  4747. (goto-char start)
  4748. (show-subtree)
  4749. (outline-next-heading))
  4750. (t
  4751. ;; we will sort the top-level entries in this file
  4752. (goto-char (point-min))
  4753. (or (org-on-heading-p) (outline-next-heading))
  4754. (setq start (point) end (point-max) what "top-level")
  4755. (goto-char start)
  4756. (show-all)))
  4757. (setq beg (point))
  4758. (if (>= beg end) (error "Nothing to sort"))
  4759. (unless plain-list-p
  4760. (looking-at "\\(\\*+\\)")
  4761. (setq stars (match-string 1)
  4762. re (concat "^" (regexp-quote stars) " +")
  4763. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  4764. txt (buffer-substring beg end))
  4765. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  4766. (if (and (not (equal stars "*")) (string-match re2 txt))
  4767. (error "Region to sort contains a level above the first entry")))
  4768. (unless sorting-type
  4769. (message
  4770. (if plain-list-p
  4771. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  4772. "Sort %s: [a]lpha [n]umeric [t]ime [p]riority p[r]operty [f]unc A/N/T/P/F means reversed:")
  4773. what)
  4774. (setq sorting-type (read-char-exclusive))
  4775. (and (= (downcase sorting-type) ?f)
  4776. (setq getkey-func
  4777. (completing-read "Sort using function: "
  4778. obarray 'fboundp t nil nil))
  4779. (setq getkey-func (intern getkey-func)))
  4780. (and (= (downcase sorting-type) ?r)
  4781. (setq property
  4782. (completing-read "Property: "
  4783. (mapcar 'list (org-buffer-property-keys t))
  4784. nil t))))
  4785. (message "Sorting entries...")
  4786. (save-restriction
  4787. (narrow-to-region start end)
  4788. (let ((dcst (downcase sorting-type))
  4789. (now (current-time)))
  4790. (sort-subr
  4791. (/= dcst sorting-type)
  4792. ;; This function moves to the beginning character of the "record" to
  4793. ;; be sorted.
  4794. (if plain-list-p
  4795. (lambda nil
  4796. (if (org-at-item-p) t (goto-char (point-max))))
  4797. (lambda nil
  4798. (if (re-search-forward re nil t)
  4799. (goto-char (match-beginning 0))
  4800. (goto-char (point-max)))))
  4801. ;; This function moves to the last character of the "record" being
  4802. ;; sorted.
  4803. (if plain-list-p
  4804. 'org-end-of-item
  4805. (lambda nil
  4806. (save-match-data
  4807. (condition-case nil
  4808. (outline-forward-same-level 1)
  4809. (error
  4810. (goto-char (point-max)))))))
  4811. ;; This function returns the value that gets sorted against.
  4812. (if plain-list-p
  4813. (lambda nil
  4814. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  4815. (cond
  4816. ((= dcst ?n)
  4817. (string-to-number (buffer-substring (match-end 0)
  4818. (point-at-eol))))
  4819. ((= dcst ?a)
  4820. (buffer-substring (match-end 0) (point-at-eol)))
  4821. ((= dcst ?t)
  4822. (if (re-search-forward org-ts-regexp
  4823. (point-at-eol) t)
  4824. (org-time-string-to-time (match-string 0))
  4825. now))
  4826. ((= dcst ?f)
  4827. (if getkey-func
  4828. (progn
  4829. (setq tmp (funcall getkey-func))
  4830. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  4831. tmp)
  4832. (error "Invalid key function `%s'" getkey-func)))
  4833. (t (error "Invalid sorting type `%c'" sorting-type)))))
  4834. (lambda nil
  4835. (cond
  4836. ((= dcst ?n)
  4837. (if (looking-at outline-regexp)
  4838. (string-to-number (buffer-substring (match-end 0)
  4839. (point-at-eol)))
  4840. nil))
  4841. ((= dcst ?a)
  4842. (funcall case-func (buffer-substring (point-at-bol)
  4843. (point-at-eol))))
  4844. ((= dcst ?t)
  4845. (if (re-search-forward org-ts-regexp
  4846. (save-excursion
  4847. (forward-line 2)
  4848. (point)) t)
  4849. (org-time-string-to-time (match-string 0))
  4850. now))
  4851. ((= dcst ?p)
  4852. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  4853. (string-to-char (match-string 2))
  4854. org-default-priority))
  4855. ((= dcst ?r)
  4856. (or (org-entry-get nil property) ""))
  4857. ((= dcst ?f)
  4858. (if getkey-func
  4859. (progn
  4860. (setq tmp (funcall getkey-func))
  4861. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  4862. tmp)
  4863. (error "Invalid key function `%s'" getkey-func)))
  4864. (t (error "Invalid sorting type `%c'" sorting-type)))))
  4865. nil
  4866. (cond
  4867. ((= dcst ?a) 'string<)
  4868. ((= dcst ?t) 'time-less-p)
  4869. (t nil)))))
  4870. (message "Sorting entries...done")))
  4871. (defun org-do-sort (table what &optional with-case sorting-type)
  4872. "Sort TABLE of WHAT according to SORTING-TYPE.
  4873. The user will be prompted for the SORTING-TYPE if the call to this
  4874. function does not specify it. WHAT is only for the prompt, to indicate
  4875. what is being sorted. The sorting key will be extracted from
  4876. the car of the elements of the table.
  4877. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  4878. (unless sorting-type
  4879. (message
  4880. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  4881. what)
  4882. (setq sorting-type (read-char-exclusive)))
  4883. (let ((dcst (downcase sorting-type))
  4884. extractfun comparefun)
  4885. ;; Define the appropriate functions
  4886. (cond
  4887. ((= dcst ?n)
  4888. (setq extractfun 'string-to-number
  4889. comparefun (if (= dcst sorting-type) '< '>)))
  4890. ((= dcst ?a)
  4891. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  4892. (lambda(x) (downcase (org-sort-remove-invisible x))))
  4893. comparefun (if (= dcst sorting-type)
  4894. 'string<
  4895. (lambda (a b) (and (not (string< a b))
  4896. (not (string= a b)))))))
  4897. ((= dcst ?t)
  4898. (setq extractfun
  4899. (lambda (x)
  4900. (if (string-match org-ts-regexp x)
  4901. (time-to-seconds
  4902. (org-time-string-to-time (match-string 0 x)))
  4903. 0))
  4904. comparefun (if (= dcst sorting-type) '< '>)))
  4905. (t (error "Invalid sorting type `%c'" sorting-type)))
  4906. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  4907. table)
  4908. (lambda (a b) (funcall comparefun (car a) (car b))))))
  4909. ;;;; Plain list items, including checkboxes
  4910. ;;; Plain list items
  4911. (defun org-at-item-p ()
  4912. "Is point in a line starting a hand-formatted item?"
  4913. (let ((llt org-plain-list-ordered-item-terminator))
  4914. (save-excursion
  4915. (goto-char (point-at-bol))
  4916. (looking-at
  4917. (cond
  4918. ((eq llt t) "\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  4919. ((= llt ?.) "\\([ \t]*\\([-+]\\|\\([0-9]+\\.\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  4920. ((= llt ?\)) "\\([ \t]*\\([-+]\\|\\([0-9]+))\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  4921. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))))))
  4922. (defun org-in-item-p ()
  4923. "It the cursor inside a plain list item.
  4924. Does not have to be the first line."
  4925. (save-excursion
  4926. (condition-case nil
  4927. (progn
  4928. (org-beginning-of-item)
  4929. (org-at-item-p)
  4930. t)
  4931. (error nil))))
  4932. (defun org-insert-item (&optional checkbox)
  4933. "Insert a new item at the current level.
  4934. Return t when things worked, nil when we are not in an item."
  4935. (when (save-excursion
  4936. (condition-case nil
  4937. (progn
  4938. (org-beginning-of-item)
  4939. (org-at-item-p)
  4940. (if (org-invisible-p) (error "Invisible item"))
  4941. t)
  4942. (error nil)))
  4943. (let* ((bul (match-string 0))
  4944. (eow (save-excursion (beginning-of-line 1) (looking-at "[ \t]*")
  4945. (match-end 0)))
  4946. (blank (cdr (assq 'plain-list-item org-blank-before-new-entry)))
  4947. pos)
  4948. (cond
  4949. ((and (org-at-item-p) (<= (point) eow))
  4950. ;; before the bullet
  4951. (beginning-of-line 1)
  4952. (open-line (if blank 2 1)))
  4953. ((<= (point) eow)
  4954. (beginning-of-line 1))
  4955. (t
  4956. (unless (org-get-alist-option org-M-RET-may-split-line 'item)
  4957. (end-of-line 1)
  4958. (delete-horizontal-space))
  4959. (newline (if blank 2 1))))
  4960. (insert bul (if checkbox "[ ]" ""))
  4961. (just-one-space)
  4962. (setq pos (point))
  4963. (end-of-line 1)
  4964. (unless (= (point) pos) (just-one-space) (backward-delete-char 1)))
  4965. (org-maybe-renumber-ordered-list)
  4966. (and checkbox (org-update-checkbox-count-maybe))
  4967. t))
  4968. ;;; Checkboxes
  4969. (defun org-at-item-checkbox-p ()
  4970. "Is point at a line starting a plain-list item with a checklet?"
  4971. (and (org-at-item-p)
  4972. (save-excursion
  4973. (goto-char (match-end 0))
  4974. (skip-chars-forward " \t")
  4975. (looking-at "\\[[- X]\\]"))))
  4976. (defun org-toggle-checkbox (&optional arg)
  4977. "Toggle the checkbox in the current line."
  4978. (interactive "P")
  4979. (catch 'exit
  4980. (let (beg end status (firstnew 'unknown))
  4981. (cond
  4982. ((org-region-active-p)
  4983. (setq beg (region-beginning) end (region-end)))
  4984. ((org-on-heading-p)
  4985. (setq beg (point) end (save-excursion (outline-next-heading) (point))))
  4986. ((org-at-item-checkbox-p)
  4987. (let ((pos (point)))
  4988. (replace-match
  4989. (cond (arg "[-]")
  4990. ((member (match-string 0) '("[ ]" "[-]")) "[X]")
  4991. (t "[ ]"))
  4992. t t)
  4993. (goto-char pos))
  4994. (throw 'exit t))
  4995. (t (error "Not at a checkbox or heading, and no active region")))
  4996. (save-excursion
  4997. (goto-char beg)
  4998. (while (< (point) end)
  4999. (when (org-at-item-checkbox-p)
  5000. (setq status (equal (match-string 0) "[X]"))
  5001. (when (eq firstnew 'unknown)
  5002. (setq firstnew (not status)))
  5003. (replace-match
  5004. (if (if arg (not status) firstnew) "[X]" "[ ]") t t))
  5005. (beginning-of-line 2)))))
  5006. (org-update-checkbox-count-maybe))
  5007. (defun org-update-checkbox-count-maybe ()
  5008. "Update checkbox statistics unless turned off by user."
  5009. (when org-provide-checkbox-statistics
  5010. (org-update-checkbox-count)))
  5011. (defun org-update-checkbox-count (&optional all)
  5012. "Update the checkbox statistics in the current section.
  5013. This will find all statistic cookies like [57%] and [6/12] and update them
  5014. with the current numbers. With optional prefix argument ALL, do this for
  5015. the whole buffer."
  5016. (interactive "P")
  5017. (save-excursion
  5018. (let* ((buffer-invisibility-spec (org-inhibit-invisibility)) ; Emacs 21
  5019. (beg (condition-case nil
  5020. (progn (outline-back-to-heading) (point))
  5021. (error (point-min))))
  5022. (end (move-marker (make-marker)
  5023. (progn (outline-next-heading) (point))))
  5024. (re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  5025. (re-box "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)")
  5026. (re-find (concat re "\\|" re-box))
  5027. beg-cookie end-cookie is-percent c-on c-off lim
  5028. eline curr-ind next-ind continue-from startsearch
  5029. (cstat 0)
  5030. )
  5031. (when all
  5032. (goto-char (point-min))
  5033. (outline-next-heading)
  5034. (setq beg (point) end (point-max)))
  5035. (goto-char end)
  5036. ;; find each statistic cookie
  5037. (while (re-search-backward re-find beg t)
  5038. (setq beg-cookie (match-beginning 1)
  5039. end-cookie (match-end 1)
  5040. cstat (+ cstat (if end-cookie 1 0))
  5041. startsearch (point-at-eol)
  5042. continue-from (point-at-bol)
  5043. is-percent (match-beginning 2)
  5044. lim (cond
  5045. ((org-on-heading-p) (outline-next-heading) (point))
  5046. ((org-at-item-p) (org-end-of-item) (point))
  5047. (t nil))
  5048. c-on 0
  5049. c-off 0)
  5050. (when lim
  5051. ;; find first checkbox for this cookie and gather
  5052. ;; statistics from all that are at this indentation level
  5053. (goto-char startsearch)
  5054. (if (re-search-forward re-box lim t)
  5055. (progn
  5056. (org-beginning-of-item)
  5057. (setq curr-ind (org-get-indentation))
  5058. (setq next-ind curr-ind)
  5059. (while (and (bolp) (org-at-item-p) (= curr-ind next-ind))
  5060. (save-excursion (end-of-line) (setq eline (point)))
  5061. (if (re-search-forward re-box eline t)
  5062. (if (member (match-string 2) '("[ ]" "[-]"))
  5063. (setq c-off (1+ c-off))
  5064. (setq c-on (1+ c-on))
  5065. )
  5066. )
  5067. (org-end-of-item)
  5068. (setq next-ind (org-get-indentation))
  5069. )))
  5070. (goto-char continue-from)
  5071. ;; update cookie
  5072. (when end-cookie
  5073. (delete-region beg-cookie end-cookie)
  5074. (goto-char beg-cookie)
  5075. (insert
  5076. (if is-percent
  5077. (format "[%d%%]" (/ (* 100 c-on) (max 1 (+ c-on c-off))))
  5078. (format "[%d/%d]" c-on (+ c-on c-off)))))
  5079. ;; update items checkbox if it has one
  5080. (when (org-at-item-p)
  5081. (org-beginning-of-item)
  5082. (when (and (> (+ c-on c-off) 0)
  5083. (re-search-forward re-box (point-at-eol) t))
  5084. (setq beg-cookie (match-beginning 2)
  5085. end-cookie (match-end 2))
  5086. (delete-region beg-cookie end-cookie)
  5087. (goto-char beg-cookie)
  5088. (cond ((= c-off 0) (insert "[X]"))
  5089. ((= c-on 0) (insert "[ ]"))
  5090. (t (insert "[-]")))
  5091. )))
  5092. (goto-char continue-from))
  5093. (when (interactive-p)
  5094. (message "Checkbox satistics updated %s (%d places)"
  5095. (if all "in entire file" "in current outline entry") cstat)))))
  5096. (defun org-get-checkbox-statistics-face ()
  5097. "Select the face for checkbox statistics.
  5098. The face will be `org-done' when all relevant boxes are checked. Otherwise
  5099. it will be `org-todo'."
  5100. (if (match-end 1)
  5101. (if (equal (match-string 1) "100%") 'org-done 'org-todo)
  5102. (if (and (> (match-end 2) (match-beginning 2))
  5103. (equal (match-string 2) (match-string 3)))
  5104. 'org-done
  5105. 'org-todo)))
  5106. (defun org-get-indentation (&optional line)
  5107. "Get the indentation of the current line, interpreting tabs.
  5108. When LINE is given, assume it represents a line and compute its indentation."
  5109. (if line
  5110. (if (string-match "^ *" (org-remove-tabs line))
  5111. (match-end 0))
  5112. (save-excursion
  5113. (beginning-of-line 1)
  5114. (skip-chars-forward " \t")
  5115. (current-column))))
  5116. (defun org-remove-tabs (s &optional width)
  5117. "Replace tabulators in S with spaces.
  5118. Assumes that s is a single line, starting in column 0."
  5119. (setq width (or width tab-width))
  5120. (while (string-match "\t" s)
  5121. (setq s (replace-match
  5122. (make-string
  5123. (- (* width (/ (+ (match-beginning 0) width) width))
  5124. (match-beginning 0)) ?\ )
  5125. t t s)))
  5126. s)
  5127. (defun org-fix-indentation (line ind)
  5128. "Fix indentation in LINE.
  5129. IND is a cons cell with target and minimum indentation.
  5130. If the current indenation in LINE is smaller than the minimum,
  5131. leave it alone. If it is larger than ind, set it to the target."
  5132. (let* ((l (org-remove-tabs line))
  5133. (i (org-get-indentation l))
  5134. (i1 (car ind)) (i2 (cdr ind)))
  5135. (if (>= i i2) (setq l (substring line i2)))
  5136. (if (> i1 0)
  5137. (concat (make-string i1 ?\ ) l)
  5138. l)))
  5139. (defcustom org-empty-line-terminates-plain-lists nil
  5140. "Non-nil means, an empty line ends all plain list levels.
  5141. When nil, empty lines are part of the preceeding item."
  5142. :group 'org-plain-lists
  5143. :type 'boolean)
  5144. (defun org-beginning-of-item ()
  5145. "Go to the beginning of the current hand-formatted item.
  5146. If the cursor is not in an item, throw an error."
  5147. (interactive)
  5148. (let ((pos (point))
  5149. (limit (save-excursion
  5150. (condition-case nil
  5151. (progn
  5152. (org-back-to-heading)
  5153. (beginning-of-line 2) (point))
  5154. (error (point-min)))))
  5155. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  5156. ind ind1)
  5157. (if (org-at-item-p)
  5158. (beginning-of-line 1)
  5159. (beginning-of-line 1)
  5160. (skip-chars-forward " \t")
  5161. (setq ind (current-column))
  5162. (if (catch 'exit
  5163. (while t
  5164. (beginning-of-line 0)
  5165. (if (or (bobp) (< (point) limit)) (throw 'exit nil))
  5166. (if (looking-at "[ \t]*$")
  5167. (setq ind1 ind-empty)
  5168. (skip-chars-forward " \t")
  5169. (setq ind1 (current-column)))
  5170. (if (< ind1 ind)
  5171. (progn (beginning-of-line 1) (throw 'exit (org-at-item-p))))))
  5172. nil
  5173. (goto-char pos)
  5174. (error "Not in an item")))))
  5175. (defun org-end-of-item ()
  5176. "Go to the end of the current hand-formatted item.
  5177. If the cursor is not in an item, throw an error."
  5178. (interactive)
  5179. (let* ((pos (point))
  5180. ind1
  5181. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  5182. (limit (save-excursion (outline-next-heading) (point)))
  5183. (ind (save-excursion
  5184. (org-beginning-of-item)
  5185. (skip-chars-forward " \t")
  5186. (current-column)))
  5187. (end (catch 'exit
  5188. (while t
  5189. (beginning-of-line 2)
  5190. (if (eobp) (throw 'exit (point)))
  5191. (if (>= (point) limit) (throw 'exit (point-at-bol)))
  5192. (if (looking-at "[ \t]*$")
  5193. (setq ind1 ind-empty)
  5194. (skip-chars-forward " \t")
  5195. (setq ind1 (current-column)))
  5196. (if (<= ind1 ind)
  5197. (throw 'exit (point-at-bol)))))))
  5198. (if end
  5199. (goto-char end)
  5200. (goto-char pos)
  5201. (error "Not in an item"))))
  5202. (defun org-next-item ()
  5203. "Move to the beginning of the next item in the current plain list.
  5204. Error if not at a plain list, or if this is the last item in the list."
  5205. (interactive)
  5206. (let (ind ind1 (pos (point)))
  5207. (org-beginning-of-item)
  5208. (setq ind (org-get-indentation))
  5209. (org-end-of-item)
  5210. (setq ind1 (org-get-indentation))
  5211. (unless (and (org-at-item-p) (= ind ind1))
  5212. (goto-char pos)
  5213. (error "On last item"))))
  5214. (defun org-previous-item ()
  5215. "Move to the beginning of the previous item in the current plain list.
  5216. Error if not at a plain list, or if this is the first item in the list."
  5217. (interactive)
  5218. (let (beg ind ind1 (pos (point)))
  5219. (org-beginning-of-item)
  5220. (setq beg (point))
  5221. (setq ind (org-get-indentation))
  5222. (goto-char beg)
  5223. (catch 'exit
  5224. (while t
  5225. (beginning-of-line 0)
  5226. (if (looking-at "[ \t]*$")
  5227. nil
  5228. (if (<= (setq ind1 (org-get-indentation)) ind)
  5229. (throw 'exit t)))))
  5230. (condition-case nil
  5231. (if (or (not (org-at-item-p))
  5232. (< ind1 (1- ind)))
  5233. (error "")
  5234. (org-beginning-of-item))
  5235. (error (goto-char pos)
  5236. (error "On first item")))))
  5237. (defun org-first-list-item-p ()
  5238. "Is this heading the item in a plain list?"
  5239. (unless (org-at-item-p)
  5240. (error "Not at a plain list item"))
  5241. (org-beginning-of-item)
  5242. (= (point) (save-excursion (org-beginning-of-item-list))))
  5243. (defun org-move-item-down ()
  5244. "Move the plain list item at point down, i.e. swap with following item.
  5245. Subitems (items with larger indentation) are considered part of the item,
  5246. so this really moves item trees."
  5247. (interactive)
  5248. (let (beg beg0 end end0 ind ind1 (pos (point)) txt ne-end ne-beg)
  5249. (org-beginning-of-item)
  5250. (setq beg0 (point))
  5251. (save-excursion
  5252. (setq ne-beg (org-back-over-empty-lines))
  5253. (setq beg (point)))
  5254. (goto-char beg0)
  5255. (setq ind (org-get-indentation))
  5256. (org-end-of-item)
  5257. (setq end0 (point))
  5258. (setq ind1 (org-get-indentation))
  5259. (setq ne-end (org-back-over-empty-lines))
  5260. (setq end (point))
  5261. (goto-char beg0)
  5262. (when (and (org-first-list-item-p) (< ne-end ne-beg))
  5263. ;; include less whitespace
  5264. (save-excursion
  5265. (goto-char beg)
  5266. (forward-line (- ne-beg ne-end))
  5267. (setq beg (point))))
  5268. (goto-char end0)
  5269. (if (and (org-at-item-p) (= ind ind1))
  5270. (progn
  5271. (org-end-of-item)
  5272. (org-back-over-empty-lines)
  5273. (setq txt (buffer-substring beg end))
  5274. (save-excursion
  5275. (delete-region beg end))
  5276. (setq pos (point))
  5277. (insert txt)
  5278. (goto-char pos) (org-skip-whitespace)
  5279. (org-maybe-renumber-ordered-list))
  5280. (goto-char pos)
  5281. (error "Cannot move this item further down"))))
  5282. (defun org-move-item-up (arg)
  5283. "Move the plain list item at point up, i.e. swap with previous item.
  5284. Subitems (items with larger indentation) are considered part of the item,
  5285. so this really moves item trees."
  5286. (interactive "p")
  5287. (let (beg beg0 end ind ind1 (pos (point)) txt
  5288. ne-beg ne-ins ins-end)
  5289. (org-beginning-of-item)
  5290. (setq beg0 (point))
  5291. (setq ind (org-get-indentation))
  5292. (save-excursion
  5293. (setq ne-beg (org-back-over-empty-lines))
  5294. (setq beg (point)))
  5295. (goto-char beg0)
  5296. (org-end-of-item)
  5297. (setq end (point))
  5298. (goto-char beg0)
  5299. (catch 'exit
  5300. (while t
  5301. (beginning-of-line 0)
  5302. (if (looking-at "[ \t]*$")
  5303. (if org-empty-line-terminates-plain-lists
  5304. (progn
  5305. (goto-char pos)
  5306. (error "Cannot move this item further up"))
  5307. nil)
  5308. (if (<= (setq ind1 (org-get-indentation)) ind)
  5309. (throw 'exit t)))))
  5310. (condition-case nil
  5311. (org-beginning-of-item)
  5312. (error (goto-char beg)
  5313. (error "Cannot move this item further up")))
  5314. (setq ind1 (org-get-indentation))
  5315. (if (and (org-at-item-p) (= ind ind1))
  5316. (progn
  5317. (setq ne-ins (org-back-over-empty-lines))
  5318. (setq txt (buffer-substring beg end))
  5319. (save-excursion
  5320. (delete-region beg end))
  5321. (setq pos (point))
  5322. (insert txt)
  5323. (setq ins-end (point))
  5324. (goto-char pos) (org-skip-whitespace)
  5325. (when (and (org-first-list-item-p) (> ne-ins ne-beg))
  5326. ;; Move whitespace back to beginning
  5327. (save-excursion
  5328. (goto-char ins-end)
  5329. (let ((kill-whole-line t))
  5330. (kill-line (- ne-ins ne-beg)) (point)))
  5331. (insert (make-string (- ne-ins ne-beg) ?\n)))
  5332. (org-maybe-renumber-ordered-list))
  5333. (goto-char pos)
  5334. (error "Cannot move this item further up"))))
  5335. (defun org-maybe-renumber-ordered-list ()
  5336. "Renumber the ordered list at point if setup allows it.
  5337. This tests the user option `org-auto-renumber-ordered-lists' before
  5338. doing the renumbering."
  5339. (interactive)
  5340. (when (and org-auto-renumber-ordered-lists
  5341. (org-at-item-p))
  5342. (if (match-beginning 3)
  5343. (org-renumber-ordered-list 1)
  5344. (org-fix-bullet-type))))
  5345. (defun org-maybe-renumber-ordered-list-safe ()
  5346. (condition-case nil
  5347. (save-excursion
  5348. (org-maybe-renumber-ordered-list))
  5349. (error nil)))
  5350. (defun org-cycle-list-bullet (&optional which)
  5351. "Cycle through the different itemize/enumerate bullets.
  5352. This cycle the entire list level through the sequence:
  5353. `-' -> `+' -> `*' -> `1.' -> `1)'
  5354. If WHICH is a string, use that as the new bullet. If WHICH is an integer,
  5355. 0 meand `-', 1 means `+' etc."
  5356. (interactive "P")
  5357. (org-preserve-lc
  5358. (org-beginning-of-item-list)
  5359. (org-at-item-p)
  5360. (beginning-of-line 1)
  5361. (let ((current (match-string 0))
  5362. (prevp (eq which 'previous))
  5363. new)
  5364. (setq new (cond
  5365. ((and (numberp which)
  5366. (nth (1- which) '("-" "+" "*" "1." "1)"))))
  5367. ((string-match "-" current) (if prevp "1)" "+"))
  5368. ((string-match "\\+" current)
  5369. (if prevp "-" (if (looking-at "\\S-") "1." "*")))
  5370. ((string-match "\\*" current) (if prevp "+" "1."))
  5371. ((string-match "\\." current) (if prevp "*" "1)"))
  5372. ((string-match ")" current) (if prevp "1." "-"))
  5373. (t (error "This should not happen"))))
  5374. (and (looking-at "\\([ \t]*\\)\\S-+") (replace-match (concat "\\1" new)))
  5375. (org-fix-bullet-type)
  5376. (org-maybe-renumber-ordered-list))))
  5377. (defun org-get-string-indentation (s)
  5378. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  5379. (let ((n -1) (i 0) (w tab-width) c)
  5380. (catch 'exit
  5381. (while (< (setq n (1+ n)) (length s))
  5382. (setq c (aref s n))
  5383. (cond ((= c ?\ ) (setq i (1+ i)))
  5384. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  5385. (t (throw 'exit t)))))
  5386. i))
  5387. (defun org-renumber-ordered-list (arg)
  5388. "Renumber an ordered plain list.
  5389. Cursor needs to be in the first line of an item, the line that starts
  5390. with something like \"1.\" or \"2)\"."
  5391. (interactive "p")
  5392. (unless (and (org-at-item-p)
  5393. (match-beginning 3))
  5394. (error "This is not an ordered list"))
  5395. (let ((line (org-current-line))
  5396. (col (current-column))
  5397. (ind (org-get-string-indentation
  5398. (buffer-substring (point-at-bol) (match-beginning 3))))
  5399. ;; (term (substring (match-string 3) -1))
  5400. ind1 (n (1- arg))
  5401. fmt)
  5402. ;; find where this list begins
  5403. (org-beginning-of-item-list)
  5404. (looking-at "[ \t]*[0-9]+\\([.)]\\)")
  5405. (setq fmt (concat "%d" (match-string 1)))
  5406. (beginning-of-line 0)
  5407. ;; walk forward and replace these numbers
  5408. (catch 'exit
  5409. (while t
  5410. (catch 'next
  5411. (beginning-of-line 2)
  5412. (if (eobp) (throw 'exit nil))
  5413. (if (looking-at "[ \t]*$") (throw 'next nil))
  5414. (skip-chars-forward " \t") (setq ind1 (current-column))
  5415. (if (> ind1 ind) (throw 'next t))
  5416. (if (< ind1 ind) (throw 'exit t))
  5417. (if (not (org-at-item-p)) (throw 'exit nil))
  5418. (delete-region (match-beginning 2) (match-end 2))
  5419. (goto-char (match-beginning 2))
  5420. (insert (format fmt (setq n (1+ n)))))))
  5421. (goto-line line)
  5422. (move-to-column col)))
  5423. (defun org-fix-bullet-type ()
  5424. "Make sure all items in this list have the same bullet as the firsst item."
  5425. (interactive)
  5426. (unless (org-at-item-p) (error "This is not a list"))
  5427. (let ((line (org-current-line))
  5428. (col (current-column))
  5429. (ind (current-indentation))
  5430. ind1 bullet)
  5431. ;; find where this list begins
  5432. (org-beginning-of-item-list)
  5433. (beginning-of-line 1)
  5434. ;; find out what the bullet type is
  5435. (looking-at "[ \t]*\\(\\S-+\\)")
  5436. (setq bullet (match-string 1))
  5437. ;; walk forward and replace these numbers
  5438. (beginning-of-line 0)
  5439. (catch 'exit
  5440. (while t
  5441. (catch 'next
  5442. (beginning-of-line 2)
  5443. (if (eobp) (throw 'exit nil))
  5444. (if (looking-at "[ \t]*$") (throw 'next nil))
  5445. (skip-chars-forward " \t") (setq ind1 (current-column))
  5446. (if (> ind1 ind) (throw 'next t))
  5447. (if (< ind1 ind) (throw 'exit t))
  5448. (if (not (org-at-item-p)) (throw 'exit nil))
  5449. (skip-chars-forward " \t")
  5450. (looking-at "\\S-+")
  5451. (replace-match bullet))))
  5452. (goto-line line)
  5453. (move-to-column col)
  5454. (if (string-match "[0-9]" bullet)
  5455. (org-renumber-ordered-list 1))))
  5456. (defun org-beginning-of-item-list ()
  5457. "Go to the beginning of the current item list.
  5458. I.e. to the first item in this list."
  5459. (interactive)
  5460. (org-beginning-of-item)
  5461. (let ((pos (point-at-bol))
  5462. (ind (org-get-indentation))
  5463. ind1)
  5464. ;; find where this list begins
  5465. (catch 'exit
  5466. (while t
  5467. (catch 'next
  5468. (beginning-of-line 0)
  5469. (if (looking-at "[ \t]*$")
  5470. (throw (if (bobp) 'exit 'next) t))
  5471. (skip-chars-forward " \t") (setq ind1 (current-column))
  5472. (if (or (< ind1 ind)
  5473. (and (= ind1 ind)
  5474. (not (org-at-item-p)))
  5475. (bobp))
  5476. (throw 'exit t)
  5477. (when (org-at-item-p) (setq pos (point-at-bol)))))))
  5478. (goto-char pos)))
  5479. (defun org-end-of-item-list ()
  5480. "Go to the end of the current item list.
  5481. I.e. to the text after the last item."
  5482. (interactive)
  5483. (org-beginning-of-item)
  5484. (let ((pos (point-at-bol))
  5485. (ind (org-get-indentation))
  5486. ind1)
  5487. ;; find where this list begins
  5488. (catch 'exit
  5489. (while t
  5490. (catch 'next
  5491. (beginning-of-line 2)
  5492. (if (looking-at "[ \t]*$")
  5493. (throw (if (eobp) 'exit 'next) t))
  5494. (skip-chars-forward " \t") (setq ind1 (current-column))
  5495. (if (or (< ind1 ind)
  5496. (and (= ind1 ind)
  5497. (not (org-at-item-p)))
  5498. (eobp))
  5499. (progn
  5500. (setq pos (point-at-bol))
  5501. (throw 'exit t))))))
  5502. (goto-char pos)))
  5503. (defvar org-last-indent-begin-marker (make-marker))
  5504. (defvar org-last-indent-end-marker (make-marker))
  5505. (defun org-outdent-item (arg)
  5506. "Outdent a local list item."
  5507. (interactive "p")
  5508. (org-indent-item (- arg)))
  5509. (defun org-indent-item (arg)
  5510. "Indent a local list item."
  5511. (interactive "p")
  5512. (unless (org-at-item-p)
  5513. (error "Not on an item"))
  5514. (save-excursion
  5515. (let (beg end ind ind1 tmp delta ind-down ind-up)
  5516. (if (memq last-command '(org-shiftmetaright org-shiftmetaleft))
  5517. (setq beg org-last-indent-begin-marker
  5518. end org-last-indent-end-marker)
  5519. (org-beginning-of-item)
  5520. (setq beg (move-marker org-last-indent-begin-marker (point)))
  5521. (org-end-of-item)
  5522. (setq end (move-marker org-last-indent-end-marker (point))))
  5523. (goto-char beg)
  5524. (setq tmp (org-item-indent-positions)
  5525. ind (car tmp)
  5526. ind-down (nth 2 tmp)
  5527. ind-up (nth 1 tmp)
  5528. delta (if (> arg 0)
  5529. (if ind-down (- ind-down ind) 2)
  5530. (if ind-up (- ind-up ind) -2)))
  5531. (if (< (+ delta ind) 0) (error "Cannot outdent beyond margin"))
  5532. (while (< (point) end)
  5533. (beginning-of-line 1)
  5534. (skip-chars-forward " \t") (setq ind1 (current-column))
  5535. (delete-region (point-at-bol) (point))
  5536. (or (eolp) (indent-to-column (+ ind1 delta)))
  5537. (beginning-of-line 2))))
  5538. (org-fix-bullet-type)
  5539. (org-maybe-renumber-ordered-list-safe)
  5540. (save-excursion
  5541. (beginning-of-line 0)
  5542. (condition-case nil (org-beginning-of-item) (error nil))
  5543. (org-maybe-renumber-ordered-list-safe)))
  5544. (defun org-item-indent-positions ()
  5545. "Return indentation for plain list items.
  5546. This returns a list with three values: The current indentation, the
  5547. parent indentation and the indentation a child should habe.
  5548. Assumes cursor in item line."
  5549. (let* ((bolpos (point-at-bol))
  5550. (ind (org-get-indentation))
  5551. ind-down ind-up pos)
  5552. (save-excursion
  5553. (org-beginning-of-item-list)
  5554. (skip-chars-backward "\n\r \t")
  5555. (when (org-in-item-p)
  5556. (org-beginning-of-item)
  5557. (setq ind-up (org-get-indentation))))
  5558. (setq pos (point))
  5559. (save-excursion
  5560. (cond
  5561. ((and (condition-case nil (progn (org-previous-item) t)
  5562. (error nil))
  5563. (or (forward-char 1) t)
  5564. (re-search-forward "^\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)" bolpos t))
  5565. (setq ind-down (org-get-indentation)))
  5566. ((and (goto-char pos)
  5567. (org-at-item-p))
  5568. (goto-char (match-end 0))
  5569. (skip-chars-forward " \t")
  5570. (setq ind-down (current-column)))))
  5571. (list ind ind-up ind-down)))
  5572. ;;; The orgstruct minor mode
  5573. ;; Define a minor mode which can be used in other modes in order to
  5574. ;; integrate the org-mode structure editing commands.
  5575. ;; This is really a hack, because the org-mode structure commands use
  5576. ;; keys which normally belong to the major mode. Here is how it
  5577. ;; works: The minor mode defines all the keys necessary to operate the
  5578. ;; structure commands, but wraps the commands into a function which
  5579. ;; tests if the cursor is currently at a headline or a plain list
  5580. ;; item. If that is the case, the structure command is used,
  5581. ;; temporarily setting many Org-mode variables like regular
  5582. ;; expressions for filling etc. However, when any of those keys is
  5583. ;; used at a different location, function uses `key-binding' to look
  5584. ;; up if the key has an associated command in another currently active
  5585. ;; keymap (minor modes, major mode, global), and executes that
  5586. ;; command. There might be problems if any of the keys is otherwise
  5587. ;; used as a prefix key.
  5588. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  5589. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  5590. ;; addresses this by checking explicitly for both bindings.
  5591. (defvar orgstruct-mode-map (make-sparse-keymap)
  5592. "Keymap for the minor `orgstruct-mode'.")
  5593. (defvar org-local-vars nil
  5594. "List of local variables, for use by `orgstruct-mode'")
  5595. ;;;###autoload
  5596. (define-minor-mode orgstruct-mode
  5597. "Toggle the minor more `orgstruct-mode'.
  5598. This mode is for using Org-mode structure commands in other modes.
  5599. The following key behave as if Org-mode was active, if the cursor
  5600. is on a headline, or on a plain list item (both in the definition
  5601. of Org-mode).
  5602. M-up Move entry/item up
  5603. M-down Move entry/item down
  5604. M-left Promote
  5605. M-right Demote
  5606. M-S-up Move entry/item up
  5607. M-S-down Move entry/item down
  5608. M-S-left Promote subtree
  5609. M-S-right Demote subtree
  5610. M-q Fill paragraph and items like in Org-mode
  5611. C-c ^ Sort entries
  5612. C-c - Cycle list bullet
  5613. TAB Cycle item visibility
  5614. M-RET Insert new heading/item
  5615. S-M-RET Insert new TODO heading / Chekbox item
  5616. C-c C-c Set tags / toggle checkbox"
  5617. nil " OrgStruct" nil
  5618. (org-load-modules-maybe)
  5619. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  5620. ;;;###autoload
  5621. (defun turn-on-orgstruct ()
  5622. "Unconditionally turn on `orgstruct-mode'."
  5623. (orgstruct-mode 1))
  5624. ;;;###autoload
  5625. (defun turn-on-orgstruct++ ()
  5626. "Unconditionally turn on `orgstruct-mode', and force org-mode indentations.
  5627. In addition to setting orgstruct-mode, this also exports all indentation and
  5628. autofilling variables from org-mode into the buffer. Note that turning
  5629. off orgstruct-mode will *not* remove these additional settings."
  5630. (orgstruct-mode 1)
  5631. (let (var val)
  5632. (mapc
  5633. (lambda (x)
  5634. (when (string-match
  5635. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5636. (symbol-name (car x)))
  5637. (setq var (car x) val (nth 1 x))
  5638. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  5639. org-local-vars)))
  5640. (defun orgstruct-error ()
  5641. "Error when there is no default binding for a structure key."
  5642. (interactive)
  5643. (error "This key has no function outside structure elements"))
  5644. (defun orgstruct-setup ()
  5645. "Setup orgstruct keymaps."
  5646. (let ((nfunc 0)
  5647. (bindings
  5648. (list
  5649. '([(meta up)] org-metaup)
  5650. '([(meta down)] org-metadown)
  5651. '([(meta left)] org-metaleft)
  5652. '([(meta right)] org-metaright)
  5653. '([(meta shift up)] org-shiftmetaup)
  5654. '([(meta shift down)] org-shiftmetadown)
  5655. '([(meta shift left)] org-shiftmetaleft)
  5656. '([(meta shift right)] org-shiftmetaright)
  5657. '([(shift up)] org-shiftup)
  5658. '([(shift down)] org-shiftdown)
  5659. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  5660. '("\M-q" fill-paragraph)
  5661. '("\C-c^" org-sort)
  5662. '("\C-c-" org-cycle-list-bullet)))
  5663. elt key fun cmd)
  5664. (while (setq elt (pop bindings))
  5665. (setq nfunc (1+ nfunc))
  5666. (setq key (org-key (car elt))
  5667. fun (nth 1 elt)
  5668. cmd (orgstruct-make-binding fun nfunc key))
  5669. (org-defkey orgstruct-mode-map key cmd))
  5670. ;; Special treatment needed for TAB and RET
  5671. (org-defkey orgstruct-mode-map [(tab)]
  5672. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  5673. (org-defkey orgstruct-mode-map "\C-i"
  5674. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  5675. (org-defkey orgstruct-mode-map "\M-\C-m"
  5676. (orgstruct-make-binding 'org-insert-heading 105
  5677. "\M-\C-m" [(meta return)]))
  5678. (org-defkey orgstruct-mode-map [(meta return)]
  5679. (orgstruct-make-binding 'org-insert-heading 106
  5680. [(meta return)] "\M-\C-m"))
  5681. (org-defkey orgstruct-mode-map [(shift meta return)]
  5682. (orgstruct-make-binding 'org-insert-todo-heading 107
  5683. [(meta return)] "\M-\C-m"))
  5684. (unless org-local-vars
  5685. (setq org-local-vars (org-get-local-variables)))
  5686. t))
  5687. (defun orgstruct-make-binding (fun n &rest keys)
  5688. "Create a function for binding in the structure minor mode.
  5689. FUN is the command to call inside a table. N is used to create a unique
  5690. command name. KEYS are keys that should be checked in for a command
  5691. to execute outside of tables."
  5692. (eval
  5693. (list 'defun
  5694. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  5695. '(arg)
  5696. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  5697. "Outside of structure, run the binding of `"
  5698. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  5699. "'.")
  5700. '(interactive "p")
  5701. (list 'if
  5702. '(org-context-p 'headline 'item)
  5703. (list 'org-run-like-in-org-mode (list 'quote fun))
  5704. (list 'let '(orgstruct-mode)
  5705. (list 'call-interactively
  5706. (append '(or)
  5707. (mapcar (lambda (k)
  5708. (list 'key-binding k))
  5709. keys)
  5710. '('orgstruct-error))))))))
  5711. (defun org-context-p (&rest contexts)
  5712. "Check if local context is and of CONTEXTS.
  5713. Possible values in the list of contexts are `table', `headline', and `item'."
  5714. (let ((pos (point)))
  5715. (goto-char (point-at-bol))
  5716. (prog1 (or (and (memq 'table contexts)
  5717. (looking-at "[ \t]*|"))
  5718. (and (memq 'headline contexts)
  5719. (looking-at "\\*+"))
  5720. (and (memq 'item contexts)
  5721. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)")))
  5722. (goto-char pos))))
  5723. (defun org-get-local-variables ()
  5724. "Return a list of all local variables in an org-mode buffer."
  5725. (let (varlist)
  5726. (with-current-buffer (get-buffer-create "*Org tmp*")
  5727. (erase-buffer)
  5728. (org-mode)
  5729. (setq varlist (buffer-local-variables)))
  5730. (kill-buffer "*Org tmp*")
  5731. (delq nil
  5732. (mapcar
  5733. (lambda (x)
  5734. (setq x
  5735. (if (symbolp x)
  5736. (list x)
  5737. (list (car x) (list 'quote (cdr x)))))
  5738. (if (string-match
  5739. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5740. (symbol-name (car x)))
  5741. x nil))
  5742. varlist))))
  5743. ;;;###autoload
  5744. (defun org-run-like-in-org-mode (cmd)
  5745. (org-load-modules-maybe)
  5746. (unless org-local-vars
  5747. (setq org-local-vars (org-get-local-variables)))
  5748. (eval (list 'let org-local-vars
  5749. (list 'call-interactively (list 'quote cmd)))))
  5750. ;;;; Archiving
  5751. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  5752. (defun org-archive-subtree (&optional find-done)
  5753. "Move the current subtree to the archive.
  5754. The archive can be a certain top-level heading in the current file, or in
  5755. a different file. The tree will be moved to that location, the subtree
  5756. heading be marked DONE, and the current time will be added.
  5757. When called with prefix argument FIND-DONE, find whole trees without any
  5758. open TODO items and archive them (after getting confirmation from the user).
  5759. If the cursor is not at a headline when this comand is called, try all level
  5760. 1 trees. If the cursor is on a headline, only try the direct children of
  5761. this heading."
  5762. (interactive "P")
  5763. (if find-done
  5764. (org-archive-all-done)
  5765. ;; Save all relevant TODO keyword-relatex variables
  5766. (let ((tr-org-todo-line-regexp org-todo-line-regexp) ; keep despite compiler
  5767. (tr-org-todo-keywords-1 org-todo-keywords-1)
  5768. (tr-org-todo-kwd-alist org-todo-kwd-alist)
  5769. (tr-org-done-keywords org-done-keywords)
  5770. (tr-org-todo-regexp org-todo-regexp)
  5771. (tr-org-todo-line-regexp org-todo-line-regexp)
  5772. (tr-org-odd-levels-only org-odd-levels-only)
  5773. (this-buffer (current-buffer))
  5774. (org-archive-location org-archive-location)
  5775. (re "^#\\+ARCHIVE:[ \t]+\\(\\S-.*\\S-\\)[ \t]*$")
  5776. ;; start of variables that will be used for saving context
  5777. ;; The compiler complains about them - keep them anyway!
  5778. (file (abbreviate-file-name (buffer-file-name)))
  5779. (olpath (mapconcat 'identity (org-get-outline-path) "/"))
  5780. (time (format-time-string
  5781. (substring (cdr org-time-stamp-formats) 1 -1)
  5782. (current-time)))
  5783. afile heading buffer level newfile-p
  5784. category todo priority
  5785. ;; start of variables that will be used for savind context
  5786. ltags itags prop)
  5787. ;; Try to find a local archive location
  5788. (save-excursion
  5789. (save-restriction
  5790. (widen)
  5791. (setq prop (org-entry-get nil "ARCHIVE" 'inherit))
  5792. (if (and prop (string-match "\\S-" prop))
  5793. (setq org-archive-location prop)
  5794. (if (or (re-search-backward re nil t)
  5795. (re-search-forward re nil t))
  5796. (setq org-archive-location (match-string 1))))))
  5797. (if (string-match "\\(.*\\)::\\(.*\\)" org-archive-location)
  5798. (progn
  5799. (setq afile (format (match-string 1 org-archive-location)
  5800. (file-name-nondirectory buffer-file-name))
  5801. heading (match-string 2 org-archive-location)))
  5802. (error "Invalid `org-archive-location'"))
  5803. (if (> (length afile) 0)
  5804. (setq newfile-p (not (file-exists-p afile))
  5805. buffer (find-file-noselect afile))
  5806. (setq buffer (current-buffer)))
  5807. (unless buffer
  5808. (error "Cannot access file \"%s\"" afile))
  5809. (if (and (> (length heading) 0)
  5810. (string-match "^\\*+" heading))
  5811. (setq level (match-end 0))
  5812. (setq heading nil level 0))
  5813. (save-excursion
  5814. (org-back-to-heading t)
  5815. ;; Get context information that will be lost by moving the tree
  5816. (org-refresh-category-properties)
  5817. (setq category (org-get-category)
  5818. todo (and (looking-at org-todo-line-regexp)
  5819. (match-string 2))
  5820. priority (org-get-priority
  5821. (if (match-end 3) (match-string 3) ""))
  5822. ltags (org-get-tags)
  5823. itags (org-delete-all ltags (org-get-tags-at)))
  5824. (setq ltags (mapconcat 'identity ltags " ")
  5825. itags (mapconcat 'identity itags " "))
  5826. ;; We first only copy, in case something goes wrong
  5827. ;; we need to protect this-command, to avoid kill-region sets it,
  5828. ;; which would lead to duplication of subtrees
  5829. (let (this-command) (org-copy-subtree))
  5830. (set-buffer buffer)
  5831. ;; Enforce org-mode for the archive buffer
  5832. (if (not (org-mode-p))
  5833. ;; Force the mode for future visits.
  5834. (let ((org-insert-mode-line-in-empty-file t)
  5835. (org-inhibit-startup t))
  5836. (call-interactively 'org-mode)))
  5837. (when newfile-p
  5838. (goto-char (point-max))
  5839. (insert (format "\nArchived entries from file %s\n\n"
  5840. (buffer-file-name this-buffer))))
  5841. ;; Force the TODO keywords of the original buffer
  5842. (let ((org-todo-line-regexp tr-org-todo-line-regexp)
  5843. (org-todo-keywords-1 tr-org-todo-keywords-1)
  5844. (org-todo-kwd-alist tr-org-todo-kwd-alist)
  5845. (org-done-keywords tr-org-done-keywords)
  5846. (org-todo-regexp tr-org-todo-regexp)
  5847. (org-todo-line-regexp tr-org-todo-line-regexp)
  5848. (org-odd-levels-only
  5849. (if (local-variable-p 'org-odd-levels-only (current-buffer))
  5850. org-odd-levels-only
  5851. tr-org-odd-levels-only)))
  5852. (goto-char (point-min))
  5853. (show-all)
  5854. (if heading
  5855. (progn
  5856. (if (re-search-forward
  5857. (concat "^" (regexp-quote heading)
  5858. (org-re "[ \t]*\\(:[[:alnum:]_@:]+:\\)?[ \t]*\\($\\|\r\\)"))
  5859. nil t)
  5860. (goto-char (match-end 0))
  5861. ;; Heading not found, just insert it at the end
  5862. (goto-char (point-max))
  5863. (or (bolp) (insert "\n"))
  5864. (insert "\n" heading "\n")
  5865. (end-of-line 0))
  5866. ;; Make the subtree visible
  5867. (show-subtree)
  5868. (org-end-of-subtree t)
  5869. (skip-chars-backward " \t\r\n")
  5870. (and (looking-at "[ \t\r\n]*")
  5871. (replace-match "\n\n")))
  5872. ;; No specific heading, just go to end of file.
  5873. (goto-char (point-max)) (insert "\n"))
  5874. ;; Paste
  5875. (org-paste-subtree (org-get-valid-level level 1))
  5876. ;; Mark the entry as done
  5877. (when (and org-archive-mark-done
  5878. (looking-at org-todo-line-regexp)
  5879. (or (not (match-end 2))
  5880. (not (member (match-string 2) org-done-keywords))))
  5881. (let (org-log-done org-todo-log-states)
  5882. (org-todo
  5883. (car (or (member org-archive-mark-done org-done-keywords)
  5884. org-done-keywords)))))
  5885. ;; Add the context info
  5886. (when org-archive-save-context-info
  5887. (let ((l org-archive-save-context-info) e n v)
  5888. (while (setq e (pop l))
  5889. (when (and (setq v (symbol-value e))
  5890. (stringp v) (string-match "\\S-" v))
  5891. (setq n (concat "ARCHIVE_" (upcase (symbol-name e))))
  5892. (org-entry-put (point) n v)))))
  5893. ;; Save and kill the buffer, if it is not the same buffer.
  5894. (if (not (eq this-buffer buffer))
  5895. (progn (save-buffer) (kill-buffer buffer)))))
  5896. ;; Here we are back in the original buffer. Everything seems to have
  5897. ;; worked. So now cut the tree and finish up.
  5898. (let (this-command) (org-cut-subtree))
  5899. (if (and (not (eobp)) (looking-at "[ \t]*$")) (kill-line))
  5900. (message "Subtree archived %s"
  5901. (if (eq this-buffer buffer)
  5902. (concat "under heading: " heading)
  5903. (concat "in file: " (abbreviate-file-name afile)))))))
  5904. (defun org-archive-to-attic-sibling ()
  5905. "Archive the current heading by moving it under the attic sibling.
  5906. The attic sibling is a sibling of the heading with the heading name
  5907. `org-attic-heading and an `org-archive-tag' tag. If this sibling does
  5908. not exist, it will be created at the end of the subtree."
  5909. (interactive)
  5910. (save-restriction
  5911. (widen)
  5912. (let (b e pos leader level)
  5913. (org-back-to-heading t)
  5914. (looking-at outline-regexp)
  5915. (setq leader (match-string 0)
  5916. level (funcall outline-level))
  5917. (setq pos (point))
  5918. (condition-case nil
  5919. (outline-up-heading 1 t)
  5920. (error (goto-char (point-min))))
  5921. (setq b (point))
  5922. (condition-case nil
  5923. (org-end-of-subtree t t)
  5924. (error (goto-char (point-max))))
  5925. (setq e (point))
  5926. (goto-char b)
  5927. (unless (re-search-forward
  5928. (concat "^" (regexp-quote leader)
  5929. "[ \t]*"
  5930. org-attic-heading
  5931. "[ \t]*:"
  5932. org-archive-tag ":") e t)
  5933. (goto-char e)
  5934. (or (bolp) (newline))
  5935. (insert leader org-attic-heading "\n")
  5936. (beginning-of-line 0)
  5937. (org-toggle-tag org-archive-tag 'on))
  5938. (beginning-of-line 1)
  5939. (org-end-of-subtree t t)
  5940. (save-excursion
  5941. (goto-char pos)
  5942. (org-cut-subtree))
  5943. (org-paste-subtree (org-get-valid-level level 1))
  5944. (org-set-property
  5945. "ARCHIVE_TIME"
  5946. (format-time-string
  5947. (substring (cdr org-time-stamp-formats) 1 -1)
  5948. (current-time)))
  5949. (outline-up-heading 1 t)
  5950. (hide-subtree)
  5951. (goto-char pos))))
  5952. (defun org-get-category (&optional pos)
  5953. "Get the category applying to position POS."
  5954. (get-text-property (or pos (point)) 'org-category))
  5955. (defun org-refresh-category-properties ()
  5956. "Refresh category text properties in the buffer."
  5957. (let ((def-cat (cond
  5958. ((null org-category)
  5959. (if buffer-file-name
  5960. (file-name-sans-extension
  5961. (file-name-nondirectory buffer-file-name))
  5962. "???"))
  5963. ((symbolp org-category) (symbol-name org-category))
  5964. (t org-category)))
  5965. beg end cat pos optionp)
  5966. (org-unmodified
  5967. (save-excursion
  5968. (save-restriction
  5969. (widen)
  5970. (goto-char (point-min))
  5971. (put-text-property (point) (point-max) 'org-category def-cat)
  5972. (while (re-search-forward
  5973. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  5974. (setq pos (match-end 0)
  5975. optionp (equal (char-after (match-beginning 0)) ?#)
  5976. cat (org-trim (match-string 2)))
  5977. (if optionp
  5978. (setq beg (point-at-bol) end (point-max))
  5979. (org-back-to-heading t)
  5980. (setq beg (point) end (org-end-of-subtree t t)))
  5981. (put-text-property beg end 'org-category cat)
  5982. (goto-char pos)))))))
  5983. (defun org-archive-all-done (&optional tag)
  5984. "Archive sublevels of the current tree without open TODO items.
  5985. If the cursor is not on a headline, try all level 1 trees. If
  5986. it is on a headline, try all direct children.
  5987. When TAG is non-nil, don't move trees, but mark them with the ARCHIVE tag."
  5988. (let ((re (concat "^\\*+ +" org-not-done-regexp)) re1
  5989. (rea (concat ".*:" org-archive-tag ":"))
  5990. (begm (make-marker))
  5991. (endm (make-marker))
  5992. (question (if tag "Set ARCHIVE tag (no open TODO items)? "
  5993. "Move subtree to archive (no open TODO items)? "))
  5994. beg end (cntarch 0))
  5995. (if (org-on-heading-p)
  5996. (progn
  5997. (setq re1 (concat "^" (regexp-quote
  5998. (make-string
  5999. (1+ (- (match-end 0) (match-beginning 0) 1))
  6000. ?*))
  6001. " "))
  6002. (move-marker begm (point))
  6003. (move-marker endm (org-end-of-subtree t)))
  6004. (setq re1 "^* ")
  6005. (move-marker begm (point-min))
  6006. (move-marker endm (point-max)))
  6007. (save-excursion
  6008. (goto-char begm)
  6009. (while (re-search-forward re1 endm t)
  6010. (setq beg (match-beginning 0)
  6011. end (save-excursion (org-end-of-subtree t) (point)))
  6012. (goto-char beg)
  6013. (if (re-search-forward re end t)
  6014. (goto-char end)
  6015. (goto-char beg)
  6016. (if (and (or (not tag) (not (looking-at rea)))
  6017. (y-or-n-p question))
  6018. (progn
  6019. (if tag
  6020. (org-toggle-tag org-archive-tag 'on)
  6021. (org-archive-subtree))
  6022. (setq cntarch (1+ cntarch)))
  6023. (goto-char end)))))
  6024. (message "%d trees archived" cntarch)))
  6025. (defun org-cycle-hide-drawers (state)
  6026. "Re-hide all drawers after a visibility state change."
  6027. (when (and (org-mode-p)
  6028. (not (memq state '(overview folded))))
  6029. (save-excursion
  6030. (let* ((globalp (memq state '(contents all)))
  6031. (beg (if globalp (point-min) (point)))
  6032. (end (if globalp (point-max) (org-end-of-subtree t))))
  6033. (goto-char beg)
  6034. (while (re-search-forward org-drawer-regexp end t)
  6035. (org-flag-drawer t))))))
  6036. (defun org-flag-drawer (flag)
  6037. (save-excursion
  6038. (beginning-of-line 1)
  6039. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  6040. (let ((b (match-end 0))
  6041. (outline-regexp org-outline-regexp))
  6042. (if (re-search-forward
  6043. "^[ \t]*:END:"
  6044. (save-excursion (outline-next-heading) (point)) t)
  6045. (outline-flag-region b (point-at-eol) flag)
  6046. (error ":END: line missing"))))))
  6047. (defun org-cycle-hide-archived-subtrees (state)
  6048. "Re-hide all archived subtrees after a visibility state change."
  6049. (when (and (not org-cycle-open-archived-trees)
  6050. (not (memq state '(overview folded))))
  6051. (save-excursion
  6052. (let* ((globalp (memq state '(contents all)))
  6053. (beg (if globalp (point-min) (point)))
  6054. (end (if globalp (point-max) (org-end-of-subtree t))))
  6055. (org-hide-archived-subtrees beg end)
  6056. (goto-char beg)
  6057. (if (looking-at (concat ".*:" org-archive-tag ":"))
  6058. (message "%s" (substitute-command-keys
  6059. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  6060. (defun org-force-cycle-archived ()
  6061. "Cycle subtree even if it is archived."
  6062. (interactive)
  6063. (setq this-command 'org-cycle)
  6064. (let ((org-cycle-open-archived-trees t))
  6065. (call-interactively 'org-cycle)))
  6066. (defun org-hide-archived-subtrees (beg end)
  6067. "Re-hide all archived subtrees after a visibility state change."
  6068. (save-excursion
  6069. (let* ((re (concat ":" org-archive-tag ":")))
  6070. (goto-char beg)
  6071. (while (re-search-forward re end t)
  6072. (and (org-on-heading-p) (hide-subtree))
  6073. (org-end-of-subtree t)))))
  6074. (defun org-toggle-tag (tag &optional onoff)
  6075. "Toggle the tag TAG for the current line.
  6076. If ONOFF is `on' or `off', don't toggle but set to this state."
  6077. (unless (org-on-heading-p t) (error "Not on headling"))
  6078. (let (res current)
  6079. (save-excursion
  6080. (beginning-of-line)
  6081. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  6082. (point-at-eol) t)
  6083. (progn
  6084. (setq current (match-string 1))
  6085. (replace-match ""))
  6086. (setq current ""))
  6087. (setq current (nreverse (org-split-string current ":")))
  6088. (cond
  6089. ((eq onoff 'on)
  6090. (setq res t)
  6091. (or (member tag current) (push tag current)))
  6092. ((eq onoff 'off)
  6093. (or (not (member tag current)) (setq current (delete tag current))))
  6094. (t (if (member tag current)
  6095. (setq current (delete tag current))
  6096. (setq res t)
  6097. (push tag current))))
  6098. (end-of-line 1)
  6099. (if current
  6100. (progn
  6101. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  6102. (org-set-tags nil t))
  6103. (delete-horizontal-space))
  6104. (run-hooks 'org-after-tags-change-hook))
  6105. res))
  6106. (defun org-toggle-archive-tag (&optional arg)
  6107. "Toggle the archive tag for the current headline.
  6108. With prefix ARG, check all children of current headline and offer tagging
  6109. the children that do not contain any open TODO items."
  6110. (interactive "P")
  6111. (if arg
  6112. (org-archive-all-done 'tag)
  6113. (let (set)
  6114. (save-excursion
  6115. (org-back-to-heading t)
  6116. (setq set (org-toggle-tag org-archive-tag))
  6117. (when set (hide-subtree)))
  6118. (and set (beginning-of-line 1))
  6119. (message "Subtree %s" (if set "archived" "unarchived")))))
  6120. ;;;; Link Stuff
  6121. ;;; Link abbreviations
  6122. (defun org-link-expand-abbrev (link)
  6123. "Apply replacements as defined in `org-link-abbrev-alist."
  6124. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  6125. (let* ((key (match-string 1 link))
  6126. (as (or (assoc key org-link-abbrev-alist-local)
  6127. (assoc key org-link-abbrev-alist)))
  6128. (tag (and (match-end 2) (match-string 3 link)))
  6129. rpl)
  6130. (if (not as)
  6131. link
  6132. (setq rpl (cdr as))
  6133. (cond
  6134. ((symbolp rpl) (funcall rpl tag))
  6135. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  6136. (t (concat rpl tag)))))
  6137. link))
  6138. ;;; Storing and inserting links
  6139. (defvar org-insert-link-history nil
  6140. "Minibuffer history for links inserted with `org-insert-link'.")
  6141. (defvar org-stored-links nil
  6142. "Contains the links stored with `org-store-link'.")
  6143. (defvar org-store-link-plist nil
  6144. "Plist with info about the most recently link created with `org-store-link'.")
  6145. (defvar org-link-protocols nil
  6146. "Link protocols added to Org-mode using `org-add-link-type'.")
  6147. (defvar org-store-link-functions nil
  6148. "List of functions that are called to create and store a link.
  6149. Each function will be called in turn until one returns a non-nil
  6150. value. Each function should check if it is responsible for creating
  6151. this link (for example by looking at the major mode).
  6152. If not, it must exit and return nil.
  6153. If yes, it should return a non-nil value after a calling
  6154. `org-store-link-props' with a list of properties and values.
  6155. Special properties are:
  6156. :type The link prefix. like \"http\". This must be given.
  6157. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  6158. This is obligatory as well.
  6159. :description Optional default description for the second pair
  6160. of brackets in an Org-mode link. The user can still change
  6161. this when inserting this link into an Org-mode buffer.
  6162. In addition to these, any additional properties can be specified
  6163. and then used in remember templates.")
  6164. (defun org-add-link-type (type &optional follow export)
  6165. "Add TYPE to the list of `org-link-types'.
  6166. Re-compute all regular expressions depending on `org-link-types'
  6167. FOLLOW and EXPORT are two functions.
  6168. FOLLOW should take the link path as the single argument and do whatever
  6169. is necessary to follow the link, for example find a file or display
  6170. a mail message.
  6171. EXPORT should format the link path for export to one of the export formats.
  6172. It should be a function accepting three arguments:
  6173. path the path of the link, the text after the prefix (like \"http:\")
  6174. desc the description of the link, if any, nil if there was no descripton
  6175. format the export format, a symbol like `html' or `latex'.
  6176. The function may use the FORMAT information to return different values
  6177. depending on the format. The return value will be put literally into
  6178. the exported file.
  6179. Org-mode has a built-in default for exporting links. If you are happy with
  6180. this default, there is no need to define an export function for the link
  6181. type. For a simple example of an export function, see `org-bbdb.el'."
  6182. (add-to-list 'org-link-types type t)
  6183. (org-make-link-regexps)
  6184. (if (assoc type org-link-protocols)
  6185. (setcdr (assoc type org-link-protocols) (list follow export))
  6186. (push (list type follow export) org-link-protocols)))
  6187. ;;;###autoload
  6188. (defun org-store-link (arg)
  6189. "\\<org-mode-map>Store an org-link to the current location.
  6190. This link is added to `org-stored-links' and can later be inserted
  6191. into an org-buffer with \\[org-insert-link].
  6192. For some link types, a prefix arg is interpreted:
  6193. For links to usenet articles, arg negates `org-usenet-links-prefer-google'.
  6194. For file links, arg negates `org-context-in-file-links'."
  6195. (interactive "P")
  6196. (org-load-modules-maybe)
  6197. (setq org-store-link-plist nil) ; reset
  6198. (let (link cpltxt desc description search txt)
  6199. (cond
  6200. ((run-hook-with-args-until-success 'org-store-link-functions)
  6201. (setq link (plist-get org-store-link-plist :link)
  6202. desc (or (plist-get org-store-link-plist :description) link)))
  6203. ((eq major-mode 'calendar-mode)
  6204. (let ((cd (calendar-cursor-to-date)))
  6205. (setq link
  6206. (format-time-string
  6207. (car org-time-stamp-formats)
  6208. (apply 'encode-time
  6209. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  6210. nil nil nil))))
  6211. (org-store-link-props :type "calendar" :date cd)))
  6212. ((eq major-mode 'w3-mode)
  6213. (setq cpltxt (url-view-url t)
  6214. link (org-make-link cpltxt))
  6215. (org-store-link-props :type "w3" :url (url-view-url t)))
  6216. ((eq major-mode 'w3m-mode)
  6217. (setq cpltxt (or w3m-current-title w3m-current-url)
  6218. link (org-make-link w3m-current-url))
  6219. (org-store-link-props :type "w3m" :url (url-view-url t)))
  6220. ((setq search (run-hook-with-args-until-success
  6221. 'org-create-file-search-functions))
  6222. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  6223. "::" search))
  6224. (setq cpltxt (or description link)))
  6225. ((eq major-mode 'image-mode)
  6226. (setq cpltxt (concat "file:"
  6227. (abbreviate-file-name buffer-file-name))
  6228. link (org-make-link cpltxt))
  6229. (org-store-link-props :type "image" :file buffer-file-name))
  6230. ((eq major-mode 'dired-mode)
  6231. ;; link to the file in the current line
  6232. (setq cpltxt (concat "file:"
  6233. (abbreviate-file-name
  6234. (expand-file-name
  6235. (dired-get-filename nil t))))
  6236. link (org-make-link cpltxt)))
  6237. ((and buffer-file-name (org-mode-p))
  6238. ;; Just link to current headline
  6239. (setq cpltxt (concat "file:"
  6240. (abbreviate-file-name buffer-file-name)))
  6241. ;; Add a context search string
  6242. (when (org-xor org-context-in-file-links arg)
  6243. ;; Check if we are on a target
  6244. (if (org-in-regexp "<<\\(.*?\\)>>")
  6245. (setq cpltxt (concat cpltxt "::" (match-string 1)))
  6246. (setq txt (cond
  6247. ((org-on-heading-p) nil)
  6248. ((org-region-active-p)
  6249. (buffer-substring (region-beginning) (region-end)))
  6250. (t (buffer-substring (point-at-bol) (point-at-eol)))))
  6251. (when (or (null txt) (string-match "\\S-" txt))
  6252. (setq cpltxt
  6253. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  6254. desc "NONE"))))
  6255. (if (string-match "::\\'" cpltxt)
  6256. (setq cpltxt (substring cpltxt 0 -2)))
  6257. (setq link (org-make-link cpltxt)))
  6258. ((buffer-file-name (buffer-base-buffer))
  6259. ;; Just link to this file here.
  6260. (setq cpltxt (concat "file:"
  6261. (abbreviate-file-name
  6262. (buffer-file-name (buffer-base-buffer)))))
  6263. ;; Add a context string
  6264. (when (org-xor org-context-in-file-links arg)
  6265. (setq txt (if (org-region-active-p)
  6266. (buffer-substring (region-beginning) (region-end))
  6267. (buffer-substring (point-at-bol) (point-at-eol))))
  6268. ;; Only use search option if there is some text.
  6269. (when (string-match "\\S-" txt)
  6270. (setq cpltxt
  6271. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  6272. desc "NONE")))
  6273. (setq link (org-make-link cpltxt)))
  6274. ((interactive-p)
  6275. (error "Cannot link to a buffer which is not visiting a file"))
  6276. (t (setq link nil)))
  6277. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  6278. (setq link (or link cpltxt)
  6279. desc (or desc cpltxt))
  6280. (if (equal desc "NONE") (setq desc nil))
  6281. (if (and (interactive-p) link)
  6282. (progn
  6283. (setq org-stored-links
  6284. (cons (list link desc) org-stored-links))
  6285. (message "Stored: %s" (or desc link)))
  6286. (and link (org-make-link-string link desc)))))
  6287. (defun org-store-link-props (&rest plist)
  6288. "Store link properties, extract names and addresses."
  6289. (let (x adr)
  6290. (when (setq x (plist-get plist :from))
  6291. (setq adr (mail-extract-address-components x))
  6292. (plist-put plist :fromname (car adr))
  6293. (plist-put plist :fromaddress (nth 1 adr)))
  6294. (when (setq x (plist-get plist :to))
  6295. (setq adr (mail-extract-address-components x))
  6296. (plist-put plist :toname (car adr))
  6297. (plist-put plist :toaddress (nth 1 adr))))
  6298. (let ((from (plist-get plist :from))
  6299. (to (plist-get plist :to)))
  6300. (when (and from to org-from-is-user-regexp)
  6301. (plist-put plist :fromto
  6302. (if (string-match org-from-is-user-regexp from)
  6303. (concat "to %t")
  6304. (concat "from %f")))))
  6305. (setq org-store-link-plist plist))
  6306. (defun org-add-link-props (&rest plist)
  6307. "Add these properties to the link property list."
  6308. (let (key value)
  6309. (while plist
  6310. (setq key (pop plist) value (pop plist))
  6311. (setq org-store-link-plist
  6312. (plist-put org-store-link-plist key value)))))
  6313. (defun org-email-link-description (&optional fmt)
  6314. "Return the description part of an email link.
  6315. This takes information from `org-store-link-plist' and formats it
  6316. according to FMT (default from `org-email-link-description-format')."
  6317. (setq fmt (or fmt org-email-link-description-format))
  6318. (let* ((p org-store-link-plist)
  6319. (to (plist-get p :toaddress))
  6320. (from (plist-get p :fromaddress))
  6321. (table
  6322. (list
  6323. (cons "%c" (plist-get p :fromto))
  6324. (cons "%F" (plist-get p :from))
  6325. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  6326. (cons "%T" (plist-get p :to))
  6327. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  6328. (cons "%s" (plist-get p :subject))
  6329. (cons "%m" (plist-get p :message-id)))))
  6330. (when (string-match "%c" fmt)
  6331. ;; Check if the user wrote this message
  6332. (if (and org-from-is-user-regexp from to
  6333. (save-match-data (string-match org-from-is-user-regexp from)))
  6334. (setq fmt (replace-match "to %t" t t fmt))
  6335. (setq fmt (replace-match "from %f" t t fmt))))
  6336. (org-replace-escapes fmt table)))
  6337. (defun org-make-org-heading-search-string (&optional string heading)
  6338. "Make search string for STRING or current headline."
  6339. (interactive)
  6340. (let ((s (or string (org-get-heading))))
  6341. (unless (and string (not heading))
  6342. ;; We are using a headline, clean up garbage in there.
  6343. (if (string-match org-todo-regexp s)
  6344. (setq s (replace-match "" t t s)))
  6345. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  6346. (setq s (replace-match "" t t s)))
  6347. (setq s (org-trim s))
  6348. (if (string-match (concat "^\\(" org-quote-string "\\|"
  6349. org-comment-string "\\)") s)
  6350. (setq s (replace-match "" t t s)))
  6351. (while (string-match org-ts-regexp s)
  6352. (setq s (replace-match "" t t s))))
  6353. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  6354. (setq s (replace-match " " t t s)))
  6355. (or string (setq s (concat "*" s))) ; Add * for headlines
  6356. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  6357. (defun org-make-link (&rest strings)
  6358. "Concatenate STRINGS."
  6359. (apply 'concat strings))
  6360. (defun org-make-link-string (link &optional description)
  6361. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  6362. (unless (string-match "\\S-" link)
  6363. (error "Empty link"))
  6364. (when (stringp description)
  6365. ;; Remove brackets from the description, they are fatal.
  6366. (while (string-match "\\[" description)
  6367. (setq description (replace-match "{" t t description)))
  6368. (while (string-match "\\]" description)
  6369. (setq description (replace-match "}" t t description))))
  6370. (when (equal (org-link-escape link) description)
  6371. ;; No description needed, it is identical
  6372. (setq description nil))
  6373. (when (and (not description)
  6374. (not (equal link (org-link-escape link))))
  6375. (setq description link))
  6376. (concat "[[" (org-link-escape link) "]"
  6377. (if description (concat "[" description "]") "")
  6378. "]"))
  6379. (defconst org-link-escape-chars
  6380. '((?\ . "%20")
  6381. (?\[ . "%5B")
  6382. (?\] . "%5D")
  6383. (?\340 . "%E0") ; `a
  6384. (?\342 . "%E2") ; ^a
  6385. (?\347 . "%E7") ; ,c
  6386. (?\350 . "%E8") ; `e
  6387. (?\351 . "%E9") ; 'e
  6388. (?\352 . "%EA") ; ^e
  6389. (?\356 . "%EE") ; ^i
  6390. (?\364 . "%F4") ; ^o
  6391. (?\371 . "%F9") ; `u
  6392. (?\373 . "%FB") ; ^u
  6393. (?\; . "%3B")
  6394. (?? . "%3F")
  6395. (?= . "%3D")
  6396. (?+ . "%2B")
  6397. )
  6398. "Association list of escapes for some characters problematic in links.
  6399. This is the list that is used for internal purposes.")
  6400. (defconst org-link-escape-chars-browser
  6401. '((?\ . "%20")) ; 32 for the SPC char
  6402. "Association list of escapes for some characters problematic in links.
  6403. This is the list that is used before handing over to the browser.")
  6404. (defun org-link-escape (text &optional table)
  6405. "Escape charaters in TEXT that are problematic for links."
  6406. (setq table (or table org-link-escape-chars))
  6407. (when text
  6408. (let ((re (mapconcat (lambda (x) (regexp-quote
  6409. (char-to-string (car x))))
  6410. table "\\|")))
  6411. (while (string-match re text)
  6412. (setq text
  6413. (replace-match
  6414. (cdr (assoc (string-to-char (match-string 0 text))
  6415. table))
  6416. t t text)))
  6417. text)))
  6418. (defun org-link-unescape (text &optional table)
  6419. "Reverse the action of `org-link-escape'."
  6420. (setq table (or table org-link-escape-chars))
  6421. (when text
  6422. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  6423. table "\\|")))
  6424. (while (string-match re text)
  6425. (setq text
  6426. (replace-match
  6427. (char-to-string (car (rassoc (match-string 0 text) table)))
  6428. t t text)))
  6429. text)))
  6430. (defun org-xor (a b)
  6431. "Exclusive or."
  6432. (if a (not b) b))
  6433. (defun org-get-header (header)
  6434. "Find a header field in the current buffer."
  6435. (save-excursion
  6436. (goto-char (point-min))
  6437. (let ((case-fold-search t) s)
  6438. (cond
  6439. ((eq header 'from)
  6440. (if (re-search-forward "^From:\\s-+\\(.*\\)" nil t)
  6441. (setq s (match-string 1)))
  6442. (while (string-match "\"" s)
  6443. (setq s (replace-match "" t t s)))
  6444. (if (string-match "[<(].*" s)
  6445. (setq s (replace-match "" t t s))))
  6446. ((eq header 'message-id)
  6447. (if (re-search-forward "^message-id:\\s-+\\(.*\\)" nil t)
  6448. (setq s (match-string 1))))
  6449. ((eq header 'subject)
  6450. (if (re-search-forward "^subject:\\s-+\\(.*\\)" nil t)
  6451. (setq s (match-string 1)))))
  6452. (if (string-match "\\`[ \t\]+" s) (setq s (replace-match "" t t s)))
  6453. (if (string-match "[ \t\]+\\'" s) (setq s (replace-match "" t t s)))
  6454. s)))
  6455. (defun org-fixup-message-id-for-http (s)
  6456. "Replace special characters in a message id, so it can be used in an http query."
  6457. (while (string-match "<" s)
  6458. (setq s (replace-match "%3C" t t s)))
  6459. (while (string-match ">" s)
  6460. (setq s (replace-match "%3E" t t s)))
  6461. (while (string-match "@" s)
  6462. (setq s (replace-match "%40" t t s)))
  6463. s)
  6464. ;;;###autoload
  6465. (defun org-insert-link-global ()
  6466. "Insert a link like Org-mode does.
  6467. This command can be called in any mode to insert a link in Org-mode syntax."
  6468. (interactive)
  6469. (org-load-modules-maybe)
  6470. (org-run-like-in-org-mode 'org-insert-link))
  6471. (defun org-insert-link (&optional complete-file)
  6472. "Insert a link. At the prompt, enter the link.
  6473. Completion can be used to select a link previously stored with
  6474. `org-store-link'. When the empty string is entered (i.e. if you just
  6475. press RET at the prompt), the link defaults to the most recently
  6476. stored link. As SPC triggers completion in the minibuffer, you need to
  6477. use M-SPC or C-q SPC to force the insertion of a space character.
  6478. You will also be prompted for a description, and if one is given, it will
  6479. be displayed in the buffer instead of the link.
  6480. If there is already a link at point, this command will allow you to edit link
  6481. and description parts.
  6482. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can be
  6483. selected using completion. The path to the file will be relative to
  6484. the current directory if the file is in the current directory or a
  6485. subdirectory. Otherwise, the link will be the absolute path as
  6486. completed in the minibuffer (i.e. normally ~/path/to/file).
  6487. With two \\[universal-argument] prefixes, enforce an absolute path even if the file
  6488. is in the current directory or below.
  6489. With three \\[universal-argument] prefixes, negate the meaning of
  6490. `org-keep-stored-link-after-insertion'."
  6491. (interactive "P")
  6492. (let* ((wcf (current-window-configuration))
  6493. (region (if (org-region-active-p)
  6494. (buffer-substring (region-beginning) (region-end))))
  6495. (remove (and region (list (region-beginning) (region-end))))
  6496. (desc region)
  6497. tmphist ; byte-compile incorrectly complains about this
  6498. link entry file)
  6499. (cond
  6500. ((org-in-regexp org-bracket-link-regexp 1)
  6501. ;; We do have a link at point, and we are going to edit it.
  6502. (setq remove (list (match-beginning 0) (match-end 0)))
  6503. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  6504. (setq link (read-string "Link: "
  6505. (org-link-unescape
  6506. (org-match-string-no-properties 1)))))
  6507. ((or (org-in-regexp org-angle-link-re)
  6508. (org-in-regexp org-plain-link-re))
  6509. ;; Convert to bracket link
  6510. (setq remove (list (match-beginning 0) (match-end 0))
  6511. link (read-string "Link: "
  6512. (org-remove-angle-brackets (match-string 0)))))
  6513. ((equal complete-file '(4))
  6514. ;; Completing read for file names.
  6515. (setq file (read-file-name "File: "))
  6516. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  6517. (pwd1 (file-name-as-directory (abbreviate-file-name
  6518. (expand-file-name ".")))))
  6519. (cond
  6520. ((equal complete-file '(16))
  6521. (setq link (org-make-link
  6522. "file:"
  6523. (abbreviate-file-name (expand-file-name file)))))
  6524. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  6525. (setq link (org-make-link "file:" (match-string 1 file))))
  6526. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  6527. (expand-file-name file))
  6528. (setq link (org-make-link
  6529. "file:" (match-string 1 (expand-file-name file)))))
  6530. (t (setq link (org-make-link "file:" file))))))
  6531. (t
  6532. ;; Read link, with completion for stored links.
  6533. (with-output-to-temp-buffer "*Org Links*"
  6534. (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
  6535. (when org-stored-links
  6536. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  6537. (princ (mapconcat
  6538. (lambda (x)
  6539. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  6540. (reverse org-stored-links) "\n"))))
  6541. (let ((cw (selected-window)))
  6542. (select-window (get-buffer-window "*Org Links*"))
  6543. (shrink-window-if-larger-than-buffer)
  6544. (setq truncate-lines t)
  6545. (select-window cw))
  6546. ;; Fake a link history, containing the stored links.
  6547. (setq tmphist (append (mapcar 'car org-stored-links)
  6548. org-insert-link-history))
  6549. (unwind-protect
  6550. (setq link (org-completing-read
  6551. "Link: "
  6552. (append
  6553. (mapcar (lambda (x) (list (concat (car x) ":")))
  6554. (append org-link-abbrev-alist-local org-link-abbrev-alist))
  6555. (mapcar (lambda (x) (list (concat x ":")))
  6556. org-link-types))
  6557. nil nil nil
  6558. 'tmphist
  6559. (or (car (car org-stored-links)))))
  6560. (set-window-configuration wcf)
  6561. (kill-buffer "*Org Links*"))
  6562. (setq entry (assoc link org-stored-links))
  6563. (or entry (push link org-insert-link-history))
  6564. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  6565. (not org-keep-stored-link-after-insertion))
  6566. (setq org-stored-links (delq (assoc link org-stored-links)
  6567. org-stored-links)))
  6568. (setq desc (or desc (nth 1 entry)))))
  6569. (if (string-match org-plain-link-re link)
  6570. ;; URL-like link, normalize the use of angular brackets.
  6571. (setq link (org-make-link (org-remove-angle-brackets link))))
  6572. ;; Check if we are linking to the current file with a search option
  6573. ;; If yes, simplify the link by using only the search option.
  6574. (when (and buffer-file-name
  6575. (string-match "\\<file:\\(.+?\\)::\\([^>]+\\)" link))
  6576. (let* ((path (match-string 1 link))
  6577. (case-fold-search nil)
  6578. (search (match-string 2 link)))
  6579. (save-match-data
  6580. (if (equal (file-truename buffer-file-name) (file-truename path))
  6581. ;; We are linking to this same file, with a search option
  6582. (setq link search)))))
  6583. ;; Check if we can/should use a relative path. If yes, simplify the link
  6584. (when (string-match "\\<file:\\(.*\\)" link)
  6585. (let* ((path (match-string 1 link))
  6586. (origpath path)
  6587. (case-fold-search nil))
  6588. (cond
  6589. ((eq org-link-file-path-type 'absolute)
  6590. (setq path (abbreviate-file-name (expand-file-name path))))
  6591. ((eq org-link-file-path-type 'noabbrev)
  6592. (setq path (expand-file-name path)))
  6593. ((eq org-link-file-path-type 'relative)
  6594. (setq path (file-relative-name path)))
  6595. (t
  6596. (save-match-data
  6597. (if (string-match (concat "^" (regexp-quote
  6598. (file-name-as-directory
  6599. (expand-file-name "."))))
  6600. (expand-file-name path))
  6601. ;; We are linking a file with relative path name.
  6602. (setq path (substring (expand-file-name path)
  6603. (match-end 0)))))))
  6604. (setq link (concat "file:" path))
  6605. (if (equal desc origpath)
  6606. (setq desc path))))
  6607. (setq desc (read-string "Description: " desc))
  6608. (unless (string-match "\\S-" desc) (setq desc nil))
  6609. (if remove (apply 'delete-region remove))
  6610. (insert (org-make-link-string link desc))))
  6611. (defun org-completing-read (&rest args)
  6612. (let ((minibuffer-local-completion-map
  6613. (copy-keymap minibuffer-local-completion-map)))
  6614. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  6615. (apply 'completing-read args)))
  6616. ;;; Opening/following a link
  6617. (defvar org-link-search-failed nil)
  6618. (defun org-next-link ()
  6619. "Move forward to the next link.
  6620. If the link is in hidden text, expose it."
  6621. (interactive)
  6622. (when (and org-link-search-failed (eq this-command last-command))
  6623. (goto-char (point-min))
  6624. (message "Link search wrapped back to beginning of buffer"))
  6625. (setq org-link-search-failed nil)
  6626. (let* ((pos (point))
  6627. (ct (org-context))
  6628. (a (assoc :link ct)))
  6629. (if a (goto-char (nth 2 a)))
  6630. (if (re-search-forward org-any-link-re nil t)
  6631. (progn
  6632. (goto-char (match-beginning 0))
  6633. (if (org-invisible-p) (org-show-context)))
  6634. (goto-char pos)
  6635. (setq org-link-search-failed t)
  6636. (error "No further link found"))))
  6637. (defun org-previous-link ()
  6638. "Move backward to the previous link.
  6639. If the link is in hidden text, expose it."
  6640. (interactive)
  6641. (when (and org-link-search-failed (eq this-command last-command))
  6642. (goto-char (point-max))
  6643. (message "Link search wrapped back to end of buffer"))
  6644. (setq org-link-search-failed nil)
  6645. (let* ((pos (point))
  6646. (ct (org-context))
  6647. (a (assoc :link ct)))
  6648. (if a (goto-char (nth 1 a)))
  6649. (if (re-search-backward org-any-link-re nil t)
  6650. (progn
  6651. (goto-char (match-beginning 0))
  6652. (if (org-invisible-p) (org-show-context)))
  6653. (goto-char pos)
  6654. (setq org-link-search-failed t)
  6655. (error "No further link found"))))
  6656. (defun org-find-file-at-mouse (ev)
  6657. "Open file link or URL at mouse."
  6658. (interactive "e")
  6659. (mouse-set-point ev)
  6660. (org-open-at-point 'in-emacs))
  6661. (defun org-open-at-mouse (ev)
  6662. "Open file link or URL at mouse."
  6663. (interactive "e")
  6664. (mouse-set-point ev)
  6665. (org-open-at-point))
  6666. (defvar org-window-config-before-follow-link nil
  6667. "The window configuration before following a link.
  6668. This is saved in case the need arises to restore it.")
  6669. (defvar org-open-link-marker (make-marker)
  6670. "Marker pointing to the location where `org-open-at-point; was called.")
  6671. ;;;###autoload
  6672. (defun org-open-at-point-global ()
  6673. "Follow a link like Org-mode does.
  6674. This command can be called in any mode to follow a link that has
  6675. Org-mode syntax."
  6676. (interactive)
  6677. (org-run-like-in-org-mode 'org-open-at-point))
  6678. (defun org-open-at-point (&optional in-emacs)
  6679. "Open link at or after point.
  6680. If there is no link at point, this function will search forward up to
  6681. the end of the current subtree.
  6682. Normally, files will be opened by an appropriate application. If the
  6683. optional argument IN-EMACS is non-nil, Emacs will visit the file."
  6684. (interactive "P")
  6685. (org-load-modules-maybe)
  6686. (move-marker org-open-link-marker (point))
  6687. (setq org-window-config-before-follow-link (current-window-configuration))
  6688. (org-remove-occur-highlights nil nil t)
  6689. (if (org-at-timestamp-p t)
  6690. (org-follow-timestamp-link)
  6691. (let (type path link line search (pos (point)))
  6692. (catch 'match
  6693. (save-excursion
  6694. (skip-chars-forward "^]\n\r")
  6695. (when (org-in-regexp org-bracket-link-regexp)
  6696. (setq link (org-link-unescape (org-match-string-no-properties 1)))
  6697. (while (string-match " *\n *" link)
  6698. (setq link (replace-match " " t t link)))
  6699. (setq link (org-link-expand-abbrev link))
  6700. (if (string-match org-link-re-with-space2 link)
  6701. (setq type (match-string 1 link) path (match-string 2 link))
  6702. (setq type "thisfile" path link))
  6703. (throw 'match t)))
  6704. (when (get-text-property (point) 'org-linked-text)
  6705. (setq type "thisfile"
  6706. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  6707. (1+ (point)) (point))
  6708. path (buffer-substring
  6709. (previous-single-property-change pos 'org-linked-text)
  6710. (next-single-property-change pos 'org-linked-text)))
  6711. (throw 'match t))
  6712. (save-excursion
  6713. (when (or (org-in-regexp org-angle-link-re)
  6714. (org-in-regexp org-plain-link-re))
  6715. (setq type (match-string 1) path (match-string 2))
  6716. (throw 'match t)))
  6717. (when (org-in-regexp "\\<\\([^><\n]+\\)\\>")
  6718. (setq type "tree-match"
  6719. path (match-string 1))
  6720. (throw 'match t))
  6721. (save-excursion
  6722. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  6723. (setq type "tags"
  6724. path (match-string 1))
  6725. (while (string-match ":" path)
  6726. (setq path (replace-match "+" t t path)))
  6727. (throw 'match t))))
  6728. (unless path
  6729. (error "No link found"))
  6730. ;; Remove any trailing spaces in path
  6731. (if (string-match " +\\'" path)
  6732. (setq path (replace-match "" t t path)))
  6733. (cond
  6734. ((assoc type org-link-protocols)
  6735. (funcall (nth 1 (assoc type org-link-protocols)) path))
  6736. ((equal type "mailto")
  6737. (let ((cmd (car org-link-mailto-program))
  6738. (args (cdr org-link-mailto-program)) args1
  6739. (address path) (subject "") a)
  6740. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  6741. (setq address (match-string 1 path)
  6742. subject (org-link-escape (match-string 2 path))))
  6743. (while args
  6744. (cond
  6745. ((not (stringp (car args))) (push (pop args) args1))
  6746. (t (setq a (pop args))
  6747. (if (string-match "%a" a)
  6748. (setq a (replace-match address t t a)))
  6749. (if (string-match "%s" a)
  6750. (setq a (replace-match subject t t a)))
  6751. (push a args1))))
  6752. (apply cmd (nreverse args1))))
  6753. ((member type '("http" "https" "ftp" "news"))
  6754. (browse-url (concat type ":" (org-link-escape
  6755. path org-link-escape-chars-browser))))
  6756. ((member type '("message"))
  6757. (browse-url (concat type ":" path)))
  6758. ((string= type "tags")
  6759. (org-tags-view in-emacs path))
  6760. ((string= type "thisfile")
  6761. (if in-emacs
  6762. (switch-to-buffer-other-window
  6763. (org-get-buffer-for-internal-link (current-buffer)))
  6764. (org-mark-ring-push))
  6765. (let ((cmd `(org-link-search
  6766. ,path
  6767. ,(cond ((equal in-emacs '(4)) 'occur)
  6768. ((equal in-emacs '(16)) 'org-occur)
  6769. (t nil))
  6770. ,pos)))
  6771. (condition-case nil (eval cmd)
  6772. (error (progn (widen) (eval cmd))))))
  6773. ((string= type "tree-match")
  6774. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  6775. ((string= type "file")
  6776. (if (string-match "::\\([0-9]+\\)\\'" path)
  6777. (setq line (string-to-number (match-string 1 path))
  6778. path (substring path 0 (match-beginning 0)))
  6779. (if (string-match "::\\(.+\\)\\'" path)
  6780. (setq search (match-string 1 path)
  6781. path (substring path 0 (match-beginning 0)))))
  6782. (if (string-match "[*?{]" (file-name-nondirectory path))
  6783. (dired path)
  6784. (org-open-file path in-emacs line search)))
  6785. ((string= type "news")
  6786. (require 'org-gnus)
  6787. (org-gnus-follow-link path))
  6788. ((string= type "shell")
  6789. (let ((cmd path))
  6790. (if (or (not org-confirm-shell-link-function)
  6791. (funcall org-confirm-shell-link-function
  6792. (format "Execute \"%s\" in shell? "
  6793. (org-add-props cmd nil
  6794. 'face 'org-warning))))
  6795. (progn
  6796. (message "Executing %s" cmd)
  6797. (shell-command cmd))
  6798. (error "Abort"))))
  6799. ((string= type "elisp")
  6800. (let ((cmd path))
  6801. (if (or (not org-confirm-elisp-link-function)
  6802. (funcall org-confirm-elisp-link-function
  6803. (format "Execute \"%s\" as elisp? "
  6804. (org-add-props cmd nil
  6805. 'face 'org-warning))))
  6806. (message "%s => %s" cmd (eval (read cmd)))
  6807. (error "Abort"))))
  6808. (t
  6809. (browse-url-at-point)))))
  6810. (move-marker org-open-link-marker nil)
  6811. (run-hook-with-args 'org-follow-link-hook))
  6812. ;;; File search
  6813. (defvar org-create-file-search-functions nil
  6814. "List of functions to construct the right search string for a file link.
  6815. These functions are called in turn with point at the location to
  6816. which the link should point.
  6817. A function in the hook should first test if it would like to
  6818. handle this file type, for example by checking the major-mode or
  6819. the file extension. If it decides not to handle this file, it
  6820. should just return nil to give other functions a chance. If it
  6821. does handle the file, it must return the search string to be used
  6822. when following the link. The search string will be part of the
  6823. file link, given after a double colon, and `org-open-at-point'
  6824. will automatically search for it. If special measures must be
  6825. taken to make the search successful, another function should be
  6826. added to the companion hook `org-execute-file-search-functions',
  6827. which see.
  6828. A function in this hook may also use `setq' to set the variable
  6829. `description' to provide a suggestion for the descriptive text to
  6830. be used for this link when it gets inserted into an Org-mode
  6831. buffer with \\[org-insert-link].")
  6832. (defvar org-execute-file-search-functions nil
  6833. "List of functions to execute a file search triggered by a link.
  6834. Functions added to this hook must accept a single argument, the
  6835. search string that was part of the file link, the part after the
  6836. double colon. The function must first check if it would like to
  6837. handle this search, for example by checking the major-mode or the
  6838. file extension. If it decides not to handle this search, it
  6839. should just return nil to give other functions a chance. If it
  6840. does handle the search, it must return a non-nil value to keep
  6841. other functions from trying.
  6842. Each function can access the current prefix argument through the
  6843. variable `current-prefix-argument'. Note that a single prefix is
  6844. used to force opening a link in Emacs, so it may be good to only
  6845. use a numeric or double prefix to guide the search function.
  6846. In case this is needed, a function in this hook can also restore
  6847. the window configuration before `org-open-at-point' was called using:
  6848. (set-window-configuration org-window-config-before-follow-link)")
  6849. (defun org-link-search (s &optional type avoid-pos)
  6850. "Search for a link search option.
  6851. If S is surrounded by forward slashes, it is interpreted as a
  6852. regular expression. In org-mode files, this will create an `org-occur'
  6853. sparse tree. In ordinary files, `occur' will be used to list matches.
  6854. If the current buffer is in `dired-mode', grep will be used to search
  6855. in all files. If AVOID-POS is given, ignore matches near that position."
  6856. (let ((case-fold-search t)
  6857. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  6858. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  6859. (append '(("") (" ") ("\t") ("\n"))
  6860. org-emphasis-alist)
  6861. "\\|") "\\)"))
  6862. (pos (point))
  6863. (pre "") (post "")
  6864. words re0 re1 re2 re3 re4 re5 re2a reall)
  6865. (cond
  6866. ;; First check if there are any special
  6867. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  6868. ;; Now try the builtin stuff
  6869. ((save-excursion
  6870. (goto-char (point-min))
  6871. (and
  6872. (re-search-forward
  6873. (concat "<<" (regexp-quote s0) ">>") nil t)
  6874. (setq pos (match-beginning 0))))
  6875. ;; There is an exact target for this
  6876. (goto-char pos))
  6877. ((string-match "^/\\(.*\\)/$" s)
  6878. ;; A regular expression
  6879. (cond
  6880. ((org-mode-p)
  6881. (org-occur (match-string 1 s)))
  6882. ;;((eq major-mode 'dired-mode)
  6883. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  6884. (t (org-do-occur (match-string 1 s)))))
  6885. (t
  6886. ;; A normal search strings
  6887. (when (equal (string-to-char s) ?*)
  6888. ;; Anchor on headlines, post may include tags.
  6889. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  6890. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  6891. s (substring s 1)))
  6892. (remove-text-properties
  6893. 0 (length s)
  6894. '(face nil mouse-face nil keymap nil fontified nil) s)
  6895. ;; Make a series of regular expressions to find a match
  6896. (setq words (org-split-string s "[ \n\r\t]+")
  6897. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  6898. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  6899. "\\)" markers)
  6900. re2a (concat "[ \t\r\n]\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  6901. re4 (concat "[^a-zA-Z_]\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  6902. re1 (concat pre re2 post)
  6903. re3 (concat pre re4 post)
  6904. re5 (concat pre ".*" re4)
  6905. re2 (concat pre re2)
  6906. re2a (concat pre re2a)
  6907. re4 (concat pre re4)
  6908. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  6909. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  6910. re5 "\\)"
  6911. ))
  6912. (cond
  6913. ((eq type 'org-occur) (org-occur reall))
  6914. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  6915. (t (goto-char (point-min))
  6916. (if (or (org-search-not-self 1 re0 nil t)
  6917. (org-search-not-self 1 re1 nil t)
  6918. (org-search-not-self 1 re2 nil t)
  6919. (org-search-not-self 1 re2a nil t)
  6920. (org-search-not-self 1 re3 nil t)
  6921. (org-search-not-self 1 re4 nil t)
  6922. (org-search-not-self 1 re5 nil t)
  6923. )
  6924. (goto-char (match-beginning 1))
  6925. (goto-char pos)
  6926. (error "No match")))))
  6927. (t
  6928. ;; Normal string-search
  6929. (goto-char (point-min))
  6930. (if (search-forward s nil t)
  6931. (goto-char (match-beginning 0))
  6932. (error "No match"))))
  6933. (and (org-mode-p) (org-show-context 'link-search))))
  6934. (defun org-search-not-self (group &rest args)
  6935. "Execute `re-search-forward', but only accept matches that do not
  6936. enclose the position of `org-open-link-marker'."
  6937. (let ((m org-open-link-marker))
  6938. (catch 'exit
  6939. (while (apply 're-search-forward args)
  6940. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  6941. (goto-char (match-end group))
  6942. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  6943. (> (match-beginning 0) (marker-position m))
  6944. (< (match-end 0) (marker-position m)))
  6945. (save-match-data
  6946. (or (not (org-in-regexp
  6947. org-bracket-link-analytic-regexp 1))
  6948. (not (match-end 4)) ; no description
  6949. (and (<= (match-beginning 4) (point))
  6950. (>= (match-end 4) (point))))))
  6951. (throw 'exit (point))))))))
  6952. (defun org-get-buffer-for-internal-link (buffer)
  6953. "Return a buffer to be used for displaying the link target of internal links."
  6954. (cond
  6955. ((not org-display-internal-link-with-indirect-buffer)
  6956. buffer)
  6957. ((string-match "(Clone)$" (buffer-name buffer))
  6958. (message "Buffer is already a clone, not making another one")
  6959. ;; we also do not modify visibility in this case
  6960. buffer)
  6961. (t ; make a new indirect buffer for displaying the link
  6962. (let* ((bn (buffer-name buffer))
  6963. (ibn (concat bn "(Clone)"))
  6964. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  6965. (with-current-buffer ib (org-overview))
  6966. ib))))
  6967. (defun org-do-occur (regexp &optional cleanup)
  6968. "Call the Emacs command `occur'.
  6969. If CLEANUP is non-nil, remove the printout of the regular expression
  6970. in the *Occur* buffer. This is useful if the regex is long and not useful
  6971. to read."
  6972. (occur regexp)
  6973. (when cleanup
  6974. (let ((cwin (selected-window)) win beg end)
  6975. (when (setq win (get-buffer-window "*Occur*"))
  6976. (select-window win))
  6977. (goto-char (point-min))
  6978. (when (re-search-forward "match[a-z]+" nil t)
  6979. (setq beg (match-end 0))
  6980. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  6981. (setq end (1- (match-beginning 0)))))
  6982. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  6983. (goto-char (point-min))
  6984. (select-window cwin))))
  6985. ;;; The mark ring for links jumps
  6986. (defvar org-mark-ring nil
  6987. "Mark ring for positions before jumps in Org-mode.")
  6988. (defvar org-mark-ring-last-goto nil
  6989. "Last position in the mark ring used to go back.")
  6990. ;; Fill and close the ring
  6991. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  6992. (loop for i from 1 to org-mark-ring-length do
  6993. (push (make-marker) org-mark-ring))
  6994. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  6995. org-mark-ring)
  6996. (defun org-mark-ring-push (&optional pos buffer)
  6997. "Put the current position or POS into the mark ring and rotate it."
  6998. (interactive)
  6999. (setq pos (or pos (point)))
  7000. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  7001. (move-marker (car org-mark-ring)
  7002. (or pos (point))
  7003. (or buffer (current-buffer)))
  7004. (message "%s"
  7005. (substitute-command-keys
  7006. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  7007. (defun org-mark-ring-goto (&optional n)
  7008. "Jump to the previous position in the mark ring.
  7009. With prefix arg N, jump back that many stored positions. When
  7010. called several times in succession, walk through the entire ring.
  7011. Org-mode commands jumping to a different position in the current file,
  7012. or to another Org-mode file, automatically push the old position
  7013. onto the ring."
  7014. (interactive "p")
  7015. (let (p m)
  7016. (if (eq last-command this-command)
  7017. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  7018. (setq p org-mark-ring))
  7019. (setq org-mark-ring-last-goto p)
  7020. (setq m (car p))
  7021. (switch-to-buffer (marker-buffer m))
  7022. (goto-char m)
  7023. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  7024. (defun org-remove-angle-brackets (s)
  7025. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  7026. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  7027. s)
  7028. (defun org-add-angle-brackets (s)
  7029. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  7030. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  7031. s)
  7032. ;;; Following specific links
  7033. (defun org-follow-timestamp-link ()
  7034. (cond
  7035. ((org-at-date-range-p t)
  7036. (let ((org-agenda-start-on-weekday)
  7037. (t1 (match-string 1))
  7038. (t2 (match-string 2)))
  7039. (setq t1 (time-to-days (org-time-string-to-time t1))
  7040. t2 (time-to-days (org-time-string-to-time t2)))
  7041. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  7042. ((org-at-timestamp-p t)
  7043. (org-agenda-list nil (time-to-days (org-time-string-to-time
  7044. (substring (match-string 1) 0 10)))
  7045. 1))
  7046. (t (error "This should not happen"))))
  7047. ;;; Following file links
  7048. (defun org-open-file (path &optional in-emacs line search)
  7049. "Open the file at PATH.
  7050. First, this expands any special file name abbreviations. Then the
  7051. configuration variable `org-file-apps' is checked if it contains an
  7052. entry for this file type, and if yes, the corresponding command is launched.
  7053. If no application is found, Emacs simply visits the file.
  7054. With optional argument IN-EMACS, Emacs will visit the file.
  7055. Optional LINE specifies a line to go to, optional SEARCH a string to
  7056. search for. If LINE or SEARCH is given, the file will always be
  7057. opened in Emacs.
  7058. If the file does not exist, an error is thrown."
  7059. (setq in-emacs (or in-emacs line search))
  7060. (let* ((file (if (equal path "")
  7061. buffer-file-name
  7062. (substitute-in-file-name (expand-file-name path))))
  7063. (apps (append org-file-apps (org-default-apps)))
  7064. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  7065. (dirp (if remp nil (file-directory-p file)))
  7066. (dfile (downcase file))
  7067. (old-buffer (current-buffer))
  7068. (old-pos (point))
  7069. (old-mode major-mode)
  7070. ext cmd)
  7071. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  7072. (setq ext (match-string 1 dfile))
  7073. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  7074. (setq ext (match-string 1 dfile))))
  7075. (if in-emacs
  7076. (setq cmd 'emacs)
  7077. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  7078. (and dirp (cdr (assoc 'directory apps)))
  7079. (cdr (assoc ext apps))
  7080. (cdr (assoc t apps)))))
  7081. (when (eq cmd 'mailcap)
  7082. (require 'mailcap)
  7083. (mailcap-parse-mailcaps)
  7084. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  7085. (command (mailcap-mime-info mime-type)))
  7086. (if (stringp command)
  7087. (setq cmd command)
  7088. (setq cmd 'emacs))))
  7089. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  7090. (not (file-exists-p file))
  7091. (not org-open-non-existing-files))
  7092. (error "No such file: %s" file))
  7093. (cond
  7094. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  7095. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  7096. (while (string-match "['\"]%s['\"]" cmd)
  7097. (setq cmd (replace-match "%s" t t cmd)))
  7098. (while (string-match "%s" cmd)
  7099. (setq cmd (replace-match
  7100. (save-match-data (shell-quote-argument file))
  7101. t t cmd)))
  7102. (save-window-excursion
  7103. (start-process-shell-command cmd nil cmd)))
  7104. ((or (stringp cmd)
  7105. (eq cmd 'emacs))
  7106. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  7107. (widen)
  7108. (if line (goto-line line)
  7109. (if search (org-link-search search))))
  7110. ((consp cmd)
  7111. (eval cmd))
  7112. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  7113. (and (org-mode-p) (eq old-mode 'org-mode)
  7114. (or (not (equal old-buffer (current-buffer)))
  7115. (not (equal old-pos (point))))
  7116. (org-mark-ring-push old-pos old-buffer))))
  7117. (defun org-default-apps ()
  7118. "Return the default applications for this operating system."
  7119. (cond
  7120. ((eq system-type 'darwin)
  7121. org-file-apps-defaults-macosx)
  7122. ((eq system-type 'windows-nt)
  7123. org-file-apps-defaults-windowsnt)
  7124. (t org-file-apps-defaults-gnu)))
  7125. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  7126. (defun org-file-remote-p (file)
  7127. "Test whether FILE specifies a location on a remote system.
  7128. Return non-nil if the location is indeed remote.
  7129. For example, the filename \"/user@host:/foo\" specifies a location
  7130. on the system \"/user@host:\"."
  7131. (cond ((fboundp 'file-remote-p)
  7132. (file-remote-p file))
  7133. ((fboundp 'tramp-handle-file-remote-p)
  7134. (tramp-handle-file-remote-p file))
  7135. ((and (boundp 'ange-ftp-name-format)
  7136. (string-match (car ange-ftp-name-format) file))
  7137. t)
  7138. (t nil)))
  7139. ;;;; Refiling
  7140. (defun org-get-org-file ()
  7141. "Read a filename, with default directory `org-directory'."
  7142. (let ((default (or org-default-notes-file remember-data-file)))
  7143. (read-file-name (format "File name [%s]: " default)
  7144. (file-name-as-directory org-directory)
  7145. default)))
  7146. (defun org-notes-order-reversed-p ()
  7147. "Check if the current file should receive notes in reversed order."
  7148. (cond
  7149. ((not org-reverse-note-order) nil)
  7150. ((eq t org-reverse-note-order) t)
  7151. ((not (listp org-reverse-note-order)) nil)
  7152. (t (catch 'exit
  7153. (let ((all org-reverse-note-order)
  7154. entry)
  7155. (while (setq entry (pop all))
  7156. (if (string-match (car entry) buffer-file-name)
  7157. (throw 'exit (cdr entry))))
  7158. nil)))))
  7159. (defvar org-refile-target-table nil
  7160. "The list of refile targets, created by `org-refile'.")
  7161. (defvar org-agenda-new-buffers nil
  7162. "Buffers created to visit agenda files.")
  7163. (defun org-get-refile-targets (&optional default-buffer)
  7164. "Produce a table with refile targets."
  7165. (let ((entries (or org-refile-targets '((nil . (:level . 1)))))
  7166. targets txt re files f desc descre)
  7167. (with-current-buffer (or default-buffer (current-buffer))
  7168. (while (setq entry (pop entries))
  7169. (setq files (car entry) desc (cdr entry))
  7170. (cond
  7171. ((null files) (setq files (list (current-buffer))))
  7172. ((eq files 'org-agenda-files)
  7173. (setq files (org-agenda-files 'unrestricted)))
  7174. ((and (symbolp files) (fboundp files))
  7175. (setq files (funcall files)))
  7176. ((and (symbolp files) (boundp files))
  7177. (setq files (symbol-value files))))
  7178. (if (stringp files) (setq files (list files)))
  7179. (cond
  7180. ((eq (car desc) :tag)
  7181. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  7182. ((eq (car desc) :todo)
  7183. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  7184. ((eq (car desc) :regexp)
  7185. (setq descre (cdr desc)))
  7186. ((eq (car desc) :level)
  7187. (setq descre (concat "^\\*\\{" (number-to-string
  7188. (if org-odd-levels-only
  7189. (1- (* 2 (cdr desc)))
  7190. (cdr desc)))
  7191. "\\}[ \t]")))
  7192. ((eq (car desc) :maxlevel)
  7193. (setq descre (concat "^\\*\\{1," (number-to-string
  7194. (if org-odd-levels-only
  7195. (1- (* 2 (cdr desc)))
  7196. (cdr desc)))
  7197. "\\}[ \t]")))
  7198. (t (error "Bad refiling target description %s" desc)))
  7199. (while (setq f (pop files))
  7200. (save-excursion
  7201. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  7202. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  7203. (save-excursion
  7204. (save-restriction
  7205. (widen)
  7206. (goto-char (point-min))
  7207. (while (re-search-forward descre nil t)
  7208. (goto-char (point-at-bol))
  7209. (when (looking-at org-complex-heading-regexp)
  7210. (setq txt (match-string 4)
  7211. re (concat "^" (regexp-quote
  7212. (buffer-substring (match-beginning 1)
  7213. (match-end 4)))))
  7214. (if (match-end 5) (setq re (concat re "[ \t]+"
  7215. (regexp-quote
  7216. (match-string 5)))))
  7217. (setq re (concat re "[ \t]*$"))
  7218. (when org-refile-use-outline-path
  7219. (setq txt (mapconcat 'identity
  7220. (append
  7221. (if (eq org-refile-use-outline-path 'file)
  7222. (list (file-name-nondirectory
  7223. (buffer-file-name (buffer-base-buffer))))
  7224. (if (eq org-refile-use-outline-path 'full-file-path)
  7225. (list (buffer-file-name (buffer-base-buffer)))))
  7226. (org-get-outline-path)
  7227. (list txt))
  7228. "/")))
  7229. (push (list txt f re (point)) targets))
  7230. (goto-char (point-at-eol))))))))
  7231. (nreverse targets))))
  7232. (defun org-get-outline-path ()
  7233. "Return the outline path to the current entry, as a list."
  7234. (let (rtn)
  7235. (save-excursion
  7236. (while (org-up-heading-safe)
  7237. (when (looking-at org-complex-heading-regexp)
  7238. (push (org-match-string-no-properties 4) rtn)))
  7239. rtn)))
  7240. (defvar org-refile-history nil
  7241. "History for refiling operations.")
  7242. (defun org-refile (&optional goto default-buffer)
  7243. "Move the entry at point to another heading.
  7244. The list of target headings is compiled using the information in
  7245. `org-refile-targets', which see. This list is created before each use
  7246. and will therefore always be up-to-date.
  7247. At the target location, the entry is filed as a subitem of the target heading.
  7248. Depending on `org-reverse-note-order', the new subitem will either be the
  7249. first of the last subitem.
  7250. With prefix arg GOTO, the command will only visit the target location,
  7251. not actually move anything.
  7252. With a double prefix `C-c C-c', go to the location where the last refiling
  7253. operation has put the subtree."
  7254. (interactive "P")
  7255. (let* ((cbuf (current-buffer))
  7256. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  7257. pos it nbuf file re level reversed)
  7258. (if (equal goto '(16))
  7259. (org-refile-goto-last-stored)
  7260. (when (setq it (org-refile-get-location
  7261. (if goto "Goto: " "Refile to: ") default-buffer))
  7262. (setq file (nth 1 it)
  7263. re (nth 2 it)
  7264. pos (nth 3 it))
  7265. (setq nbuf (or (find-buffer-visiting file)
  7266. (find-file-noselect file)))
  7267. (if goto
  7268. (progn
  7269. (switch-to-buffer nbuf)
  7270. (goto-char pos)
  7271. (org-show-context 'org-goto))
  7272. (org-copy-special)
  7273. (save-excursion
  7274. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  7275. (find-file-noselect file))))
  7276. (setq reversed (org-notes-order-reversed-p))
  7277. (save-excursion
  7278. (save-restriction
  7279. (widen)
  7280. (goto-char pos)
  7281. (looking-at outline-regexp)
  7282. (setq level (org-get-valid-level (funcall outline-level) 1))
  7283. (goto-char
  7284. (if reversed
  7285. (outline-next-heading)
  7286. (or (save-excursion (outline-get-next-sibling))
  7287. (org-end-of-subtree t t)
  7288. (point-max))))
  7289. (bookmark-set "org-refile-last-stored")
  7290. (org-paste-subtree level))))
  7291. (org-cut-special)
  7292. (message "Entry refiled to \"%s\"" (car it)))))))
  7293. (defun org-refile-goto-last-stored ()
  7294. "Go to the location where the last refile was stored."
  7295. (interactive)
  7296. (bookmark-jump "org-refile-last-stored")
  7297. (message "This is the location of the last refile"))
  7298. (defun org-refile-get-location (&optional prompt default-buffer)
  7299. "Prompt the user for a refile location, using PROMPT."
  7300. (let ((org-refile-targets org-refile-targets)
  7301. (org-refile-use-outline-path org-refile-use-outline-path))
  7302. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  7303. (unless org-refile-target-table
  7304. (error "No refile targets"))
  7305. (let* ((cbuf (current-buffer))
  7306. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  7307. (fname (and filename (file-truename filename)))
  7308. (tbl (mapcar
  7309. (lambda (x)
  7310. (if (not (equal fname (file-truename (nth 1 x))))
  7311. (cons (concat (car x) " (" (file-name-nondirectory
  7312. (nth 1 x)) ")")
  7313. (cdr x))
  7314. x))
  7315. org-refile-target-table))
  7316. (completion-ignore-case t))
  7317. (assoc (completing-read prompt tbl nil t nil 'org-refile-history)
  7318. tbl)))
  7319. ;;;; Dynamic blocks
  7320. (defun org-find-dblock (name)
  7321. "Find the first dynamic block with name NAME in the buffer.
  7322. If not found, stay at current position and return nil."
  7323. (let (pos)
  7324. (save-excursion
  7325. (goto-char (point-min))
  7326. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  7327. nil t)
  7328. (match-beginning 0))))
  7329. (if pos (goto-char pos))
  7330. pos))
  7331. (defconst org-dblock-start-re
  7332. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  7333. "Matches the startline of a dynamic block, with parameters.")
  7334. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  7335. "Matches the end of a dyhamic block.")
  7336. (defun org-create-dblock (plist)
  7337. "Create a dynamic block section, with parameters taken from PLIST.
  7338. PLIST must containe a :name entry which is used as name of the block."
  7339. (unless (bolp) (newline))
  7340. (let ((name (plist-get plist :name)))
  7341. (insert "#+BEGIN: " name)
  7342. (while plist
  7343. (if (eq (car plist) :name)
  7344. (setq plist (cddr plist))
  7345. (insert " " (prin1-to-string (pop plist)))))
  7346. (insert "\n\n#+END:\n")
  7347. (beginning-of-line -2)))
  7348. (defun org-prepare-dblock ()
  7349. "Prepare dynamic block for refresh.
  7350. This empties the block, puts the cursor at the insert position and returns
  7351. the property list including an extra property :name with the block name."
  7352. (unless (looking-at org-dblock-start-re)
  7353. (error "Not at a dynamic block"))
  7354. (let* ((begdel (1+ (match-end 0)))
  7355. (name (org-no-properties (match-string 1)))
  7356. (params (append (list :name name)
  7357. (read (concat "(" (match-string 3) ")")))))
  7358. (unless (re-search-forward org-dblock-end-re nil t)
  7359. (error "Dynamic block not terminated"))
  7360. (setq params
  7361. (append params
  7362. (list :content (buffer-substring
  7363. begdel (match-beginning 0)))))
  7364. (delete-region begdel (match-beginning 0))
  7365. (goto-char begdel)
  7366. (open-line 1)
  7367. params))
  7368. (defun org-map-dblocks (&optional command)
  7369. "Apply COMMAND to all dynamic blocks in the current buffer.
  7370. If COMMAND is not given, use `org-update-dblock'."
  7371. (let ((cmd (or command 'org-update-dblock))
  7372. pos)
  7373. (save-excursion
  7374. (goto-char (point-min))
  7375. (while (re-search-forward org-dblock-start-re nil t)
  7376. (goto-char (setq pos (match-beginning 0)))
  7377. (condition-case nil
  7378. (funcall cmd)
  7379. (error (message "Error during update of dynamic block")))
  7380. (goto-char pos)
  7381. (unless (re-search-forward org-dblock-end-re nil t)
  7382. (error "Dynamic block not terminated"))))))
  7383. (defun org-dblock-update (&optional arg)
  7384. "User command for updating dynamic blocks.
  7385. Update the dynamic block at point. With prefix ARG, update all dynamic
  7386. blocks in the buffer."
  7387. (interactive "P")
  7388. (if arg
  7389. (org-update-all-dblocks)
  7390. (or (looking-at org-dblock-start-re)
  7391. (org-beginning-of-dblock))
  7392. (org-update-dblock)))
  7393. (defun org-update-dblock ()
  7394. "Update the dynamic block at point
  7395. This means to empty the block, parse for parameters and then call
  7396. the correct writing function."
  7397. (save-window-excursion
  7398. (let* ((pos (point))
  7399. (line (org-current-line))
  7400. (params (org-prepare-dblock))
  7401. (name (plist-get params :name))
  7402. (cmd (intern (concat "org-dblock-write:" name))))
  7403. (message "Updating dynamic block `%s' at line %d..." name line)
  7404. (funcall cmd params)
  7405. (message "Updating dynamic block `%s' at line %d...done" name line)
  7406. (goto-char pos))))
  7407. (defun org-beginning-of-dblock ()
  7408. "Find the beginning of the dynamic block at point.
  7409. Error if there is no scuh block at point."
  7410. (let ((pos (point))
  7411. beg)
  7412. (end-of-line 1)
  7413. (if (and (re-search-backward org-dblock-start-re nil t)
  7414. (setq beg (match-beginning 0))
  7415. (re-search-forward org-dblock-end-re nil t)
  7416. (> (match-end 0) pos))
  7417. (goto-char beg)
  7418. (goto-char pos)
  7419. (error "Not in a dynamic block"))))
  7420. (defun org-update-all-dblocks ()
  7421. "Update all dynamic blocks in the buffer.
  7422. This function can be used in a hook."
  7423. (when (org-mode-p)
  7424. (org-map-dblocks 'org-update-dblock)))
  7425. ;;;; Completion
  7426. (defconst org-additional-option-like-keywords
  7427. '("BEGIN_HTML" "BEGIN_LaTeX" "END_HTML" "END_LaTeX"
  7428. "ORGTBL" "HTML:" "LaTeX:" "BEGIN:" "END:" "TBLFM"
  7429. "BEGIN_EXAMPLE" "END_EXAMPLE"))
  7430. (defun org-complete (&optional arg)
  7431. "Perform completion on word at point.
  7432. At the beginning of a headline, this completes TODO keywords as given in
  7433. `org-todo-keywords'.
  7434. If the current word is preceded by a backslash, completes the TeX symbols
  7435. that are supported for HTML support.
  7436. If the current word is preceded by \"#+\", completes special words for
  7437. setting file options.
  7438. In the line after \"#+STARTUP:, complete valid keywords.\"
  7439. At all other locations, this simply calls the value of
  7440. `org-completion-fallback-command'."
  7441. (interactive "P")
  7442. (org-without-partial-completion
  7443. (catch 'exit
  7444. (let* ((end (point))
  7445. (beg1 (save-excursion
  7446. (skip-chars-backward (org-re "[:alnum:]_@"))
  7447. (point)))
  7448. (beg (save-excursion
  7449. (skip-chars-backward "a-zA-Z0-9_:$")
  7450. (point)))
  7451. (confirm (lambda (x) (stringp (car x))))
  7452. (searchhead (equal (char-before beg) ?*))
  7453. (tag (and (equal (char-before beg1) ?:)
  7454. (equal (char-after (point-at-bol)) ?*)))
  7455. (prop (and (equal (char-before beg1) ?:)
  7456. (not (equal (char-after (point-at-bol)) ?*))))
  7457. (texp (equal (char-before beg) ?\\))
  7458. (link (equal (char-before beg) ?\[))
  7459. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  7460. beg)
  7461. "#+"))
  7462. (startup (string-match "^#\\+STARTUP:.*"
  7463. (buffer-substring (point-at-bol) (point))))
  7464. (completion-ignore-case opt)
  7465. (type nil)
  7466. (tbl nil)
  7467. (table (cond
  7468. (opt
  7469. (setq type :opt)
  7470. (require 'org-exp)
  7471. (append
  7472. (mapcar
  7473. (lambda (x)
  7474. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  7475. (cons (match-string 2 x) (match-string 1 x)))
  7476. (org-split-string (org-get-current-options) "\n"))
  7477. (mapcar 'list org-additional-option-like-keywords)))
  7478. (startup
  7479. (setq type :startup)
  7480. org-startup-options)
  7481. (link (append org-link-abbrev-alist-local
  7482. org-link-abbrev-alist))
  7483. (texp
  7484. (setq type :tex)
  7485. org-html-entities)
  7486. ((string-match "\\`\\*+[ \t]+\\'"
  7487. (buffer-substring (point-at-bol) beg))
  7488. (setq type :todo)
  7489. (mapcar 'list org-todo-keywords-1))
  7490. (searchhead
  7491. (setq type :searchhead)
  7492. (save-excursion
  7493. (goto-char (point-min))
  7494. (while (re-search-forward org-todo-line-regexp nil t)
  7495. (push (list
  7496. (org-make-org-heading-search-string
  7497. (match-string 3) t))
  7498. tbl)))
  7499. tbl)
  7500. (tag (setq type :tag beg beg1)
  7501. (or org-tag-alist (org-get-buffer-tags)))
  7502. (prop (setq type :prop beg beg1)
  7503. (mapcar 'list (org-buffer-property-keys nil t t)))
  7504. (t (progn
  7505. (call-interactively org-completion-fallback-command)
  7506. (throw 'exit nil)))))
  7507. (pattern (buffer-substring-no-properties beg end))
  7508. (completion (try-completion pattern table confirm)))
  7509. (cond ((eq completion t)
  7510. (if (not (assoc (upcase pattern) table))
  7511. (message "Already complete")
  7512. (if (and (equal type :opt)
  7513. (not (member (car (assoc (upcase pattern) table))
  7514. org-additional-option-like-keywords)))
  7515. (insert (substring (cdr (assoc (upcase pattern) table))
  7516. (length pattern)))
  7517. (if (memq type '(:tag :prop)) (insert ":")))))
  7518. ((null completion)
  7519. (message "Can't find completion for \"%s\"" pattern)
  7520. (ding))
  7521. ((not (string= pattern completion))
  7522. (delete-region beg end)
  7523. (if (string-match " +$" completion)
  7524. (setq completion (replace-match "" t t completion)))
  7525. (insert completion)
  7526. (if (get-buffer-window "*Completions*")
  7527. (delete-window (get-buffer-window "*Completions*")))
  7528. (if (assoc completion table)
  7529. (if (eq type :todo) (insert " ")
  7530. (if (memq type '(:tag :prop)) (insert ":"))))
  7531. (if (and (equal type :opt) (assoc completion table))
  7532. (message "%s" (substitute-command-keys
  7533. "Press \\[org-complete] again to insert example settings"))))
  7534. (t
  7535. (message "Making completion list...")
  7536. (let ((list (sort (all-completions pattern table confirm)
  7537. 'string<)))
  7538. (with-output-to-temp-buffer "*Completions*"
  7539. (condition-case nil
  7540. ;; Protection needed for XEmacs and emacs 21
  7541. (display-completion-list list pattern)
  7542. (error (display-completion-list list)))))
  7543. (message "Making completion list...%s" "done")))))))
  7544. ;;;; TODO, DEADLINE, Comments
  7545. (defun org-toggle-comment ()
  7546. "Change the COMMENT state of an entry."
  7547. (interactive)
  7548. (save-excursion
  7549. (org-back-to-heading)
  7550. (let (case-fold-search)
  7551. (if (looking-at (concat outline-regexp
  7552. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  7553. (replace-match "" t t nil 1)
  7554. (if (looking-at outline-regexp)
  7555. (progn
  7556. (goto-char (match-end 0))
  7557. (insert org-comment-string " ")))))))
  7558. (defvar org-last-todo-state-is-todo nil
  7559. "This is non-nil when the last TODO state change led to a TODO state.
  7560. If the last change removed the TODO tag or switched to DONE, then
  7561. this is nil.")
  7562. (defvar org-setting-tags nil) ; dynamically skiped
  7563. (defun org-parse-local-options (string var)
  7564. "Parse STRING for startup setting relevant for variable VAR."
  7565. (let ((rtn (symbol-value var))
  7566. e opts)
  7567. (save-match-data
  7568. (if (or (not string) (not (string-match "\\S-" string)))
  7569. rtn
  7570. (setq opts (delq nil (mapcar (lambda (x)
  7571. (setq e (assoc x org-startup-options))
  7572. (if (eq (nth 1 e) var) e nil))
  7573. (org-split-string string "[ \t]+"))))
  7574. (if (not opts)
  7575. rtn
  7576. (setq rtn nil)
  7577. (while (setq e (pop opts))
  7578. (if (not (nth 3 e))
  7579. (setq rtn (nth 2 e))
  7580. (if (not (listp rtn)) (setq rtn nil))
  7581. (push (nth 2 e) rtn)))
  7582. rtn)))))
  7583. (defvar org-blocker-hook nil
  7584. "Hook for functions that are allowed to block a state change.
  7585. Each function gets as its single argument a property list, see
  7586. `org-trigger-hook' for more information about this list.
  7587. If any of the functions in this hook returns nil, the state change
  7588. is blocked.")
  7589. (defvar org-trigger-hook nil
  7590. "Hook for functions that are triggered by a state change.
  7591. Each function gets as its single argument a property list with at least
  7592. the following elements:
  7593. (:type type-of-change :position pos-at-entry-start
  7594. :from old-state :to new-state)
  7595. Depending on the type, more properties may be present.
  7596. This mechanism is currently implemented for:
  7597. TODO state changes
  7598. ------------------
  7599. :type todo-state-change
  7600. :from previous state (keyword as a string), or nil
  7601. :to new state (keyword as a string), or nil")
  7602. (defun org-todo (&optional arg)
  7603. "Change the TODO state of an item.
  7604. The state of an item is given by a keyword at the start of the heading,
  7605. like
  7606. *** TODO Write paper
  7607. *** DONE Call mom
  7608. The different keywords are specified in the variable `org-todo-keywords'.
  7609. By default the available states are \"TODO\" and \"DONE\".
  7610. So for this example: when the item starts with TODO, it is changed to DONE.
  7611. When it starts with DONE, the DONE is removed. And when neither TODO nor
  7612. DONE are present, add TODO at the beginning of the heading.
  7613. With C-u prefix arg, use completion to determine the new state.
  7614. With numeric prefix arg, switch to that state.
  7615. For calling through lisp, arg is also interpreted in the following way:
  7616. 'none -> empty state
  7617. \"\"(empty string) -> switch to empty state
  7618. 'done -> switch to DONE
  7619. 'nextset -> switch to the next set of keywords
  7620. 'previousset -> switch to the previous set of keywords
  7621. \"WAITING\" -> switch to the specified keyword, but only if it
  7622. really is a member of `org-todo-keywords'."
  7623. (interactive "P")
  7624. (save-excursion
  7625. (catch 'exit
  7626. (org-back-to-heading)
  7627. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  7628. (or (looking-at (concat " +" org-todo-regexp " *"))
  7629. (looking-at " *"))
  7630. (let* ((match-data (match-data))
  7631. (startpos (point-at-bol))
  7632. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  7633. (org-log-done org-log-done)
  7634. (org-log-repeat org-log-repeat)
  7635. (org-todo-log-states org-todo-log-states)
  7636. (this (match-string 1))
  7637. (hl-pos (match-beginning 0))
  7638. (head (org-get-todo-sequence-head this))
  7639. (ass (assoc head org-todo-kwd-alist))
  7640. (interpret (nth 1 ass))
  7641. (done-word (nth 3 ass))
  7642. (final-done-word (nth 4 ass))
  7643. (last-state (or this ""))
  7644. (completion-ignore-case t)
  7645. (member (member this org-todo-keywords-1))
  7646. (tail (cdr member))
  7647. (state (cond
  7648. ((and org-todo-key-trigger
  7649. (or (and (equal arg '(4)) (eq org-use-fast-todo-selection 'prefix))
  7650. (and (not arg) org-use-fast-todo-selection
  7651. (not (eq org-use-fast-todo-selection 'prefix)))))
  7652. ;; Use fast selection
  7653. (org-fast-todo-selection))
  7654. ((and (equal arg '(4))
  7655. (or (not org-use-fast-todo-selection)
  7656. (not org-todo-key-trigger)))
  7657. ;; Read a state with completion
  7658. (completing-read "State: " (mapcar (lambda(x) (list x))
  7659. org-todo-keywords-1)
  7660. nil t))
  7661. ((eq arg 'right)
  7662. (if this
  7663. (if tail (car tail) nil)
  7664. (car org-todo-keywords-1)))
  7665. ((eq arg 'left)
  7666. (if (equal member org-todo-keywords-1)
  7667. nil
  7668. (if this
  7669. (nth (- (length org-todo-keywords-1) (length tail) 2)
  7670. org-todo-keywords-1)
  7671. (org-last org-todo-keywords-1))))
  7672. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  7673. (setq arg nil))) ; hack to fall back to cycling
  7674. (arg
  7675. ;; user or caller requests a specific state
  7676. (cond
  7677. ((equal arg "") nil)
  7678. ((eq arg 'none) nil)
  7679. ((eq arg 'done) (or done-word (car org-done-keywords)))
  7680. ((eq arg 'nextset)
  7681. (or (car (cdr (member head org-todo-heads)))
  7682. (car org-todo-heads)))
  7683. ((eq arg 'previousset)
  7684. (let ((org-todo-heads (reverse org-todo-heads)))
  7685. (or (car (cdr (member head org-todo-heads)))
  7686. (car org-todo-heads))))
  7687. ((car (member arg org-todo-keywords-1)))
  7688. ((nth (1- (prefix-numeric-value arg))
  7689. org-todo-keywords-1))))
  7690. ((null member) (or head (car org-todo-keywords-1)))
  7691. ((equal this final-done-word) nil) ;; -> make empty
  7692. ((null tail) nil) ;; -> first entry
  7693. ((eq interpret 'sequence)
  7694. (car tail))
  7695. ((memq interpret '(type priority))
  7696. (if (eq this-command last-command)
  7697. (car tail)
  7698. (if (> (length tail) 0)
  7699. (or done-word (car org-done-keywords))
  7700. nil)))
  7701. (t nil)))
  7702. (next (if state (concat " " state " ") " "))
  7703. (change-plist (list :type 'todo-state-change :from this :to state
  7704. :position startpos))
  7705. dolog now-done-p)
  7706. (when org-blocker-hook
  7707. (unless (save-excursion
  7708. (save-match-data
  7709. (run-hook-with-args-until-failure
  7710. 'org-blocker-hook change-plist)))
  7711. (if (interactive-p)
  7712. (error "TODO state change from %s to %s blocked" this state)
  7713. ;; fail silently
  7714. (message "TODO state change from %s to %s blocked" this state)
  7715. (throw 'exit nil))))
  7716. (store-match-data match-data)
  7717. (replace-match next t t)
  7718. (unless (pos-visible-in-window-p hl-pos)
  7719. (message "TODO state changed to %s" (org-trim next)))
  7720. (unless head
  7721. (setq head (org-get-todo-sequence-head state)
  7722. ass (assoc head org-todo-kwd-alist)
  7723. interpret (nth 1 ass)
  7724. done-word (nth 3 ass)
  7725. final-done-word (nth 4 ass)))
  7726. (when (memq arg '(nextset previousset))
  7727. (message "Keyword-Set %d/%d: %s"
  7728. (- (length org-todo-sets) -1
  7729. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  7730. (length org-todo-sets)
  7731. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  7732. (setq org-last-todo-state-is-todo
  7733. (not (member state org-done-keywords)))
  7734. (setq now-done-p (and (member state org-done-keywords)
  7735. (not (member this org-done-keywords))))
  7736. (and logging (org-local-logging logging))
  7737. (when (and (or org-todo-log-states org-log-done)
  7738. (not (memq arg '(nextset previousset))))
  7739. ;; we need to look at recording a time and note
  7740. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  7741. (nth 2 (assoc this org-todo-log-states))))
  7742. (when (and state
  7743. (member state org-not-done-keywords)
  7744. (not (member this org-not-done-keywords)))
  7745. ;; This is now a todo state and was not one before
  7746. ;; If there was a CLOSED time stamp, get rid of it.
  7747. (org-add-planning-info nil nil 'closed))
  7748. (when (and now-done-p org-log-done)
  7749. ;; It is now done, and it was not done before
  7750. (org-add-planning-info 'closed (org-current-time))
  7751. (if (and (not dolog) (eq 'note org-log-done))
  7752. (org-add-log-setup 'done state 'findpos 'note)))
  7753. (when (and state dolog)
  7754. ;; This is a non-nil state, and we need to log it
  7755. (org-add-log-setup 'state state 'findpos dolog)))
  7756. ;; Fixup tag positioning
  7757. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  7758. (run-hooks 'org-after-todo-state-change-hook)
  7759. (if (and arg (not (member state org-done-keywords)))
  7760. (setq head (org-get-todo-sequence-head state)))
  7761. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  7762. ;; Do we need to trigger a repeat?
  7763. (when now-done-p (org-auto-repeat-maybe state))
  7764. ;; Fixup cursor location if close to the keyword
  7765. (if (and (outline-on-heading-p)
  7766. (not (bolp))
  7767. (save-excursion (beginning-of-line 1)
  7768. (looking-at org-todo-line-regexp))
  7769. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  7770. (progn
  7771. (goto-char (or (match-end 2) (match-end 1)))
  7772. (just-one-space)))
  7773. (when org-trigger-hook
  7774. (save-excursion
  7775. (run-hook-with-args 'org-trigger-hook change-plist)))))))
  7776. (defun org-local-logging (value)
  7777. "Get logging settings from a property VALUE."
  7778. (let* (words w a)
  7779. ;; directly set the variables, they are already local.
  7780. (setq org-log-done nil
  7781. org-log-repeat nil
  7782. org-todo-log-states nil)
  7783. (setq words (org-split-string value))
  7784. (while (setq w (pop words))
  7785. (cond
  7786. ((setq a (assoc w org-startup-options))
  7787. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  7788. (set (nth 1 a) (nth 2 a))))
  7789. ((setq a (org-extract-log-state-settings w))
  7790. (and (member (car a) org-todo-keywords-1)
  7791. (push a org-todo-log-states)))))))
  7792. (defun org-get-todo-sequence-head (kwd)
  7793. "Return the head of the TODO sequence to which KWD belongs.
  7794. If KWD is not set, check if there is a text property remembering the
  7795. right sequence."
  7796. (let (p)
  7797. (cond
  7798. ((not kwd)
  7799. (or (get-text-property (point-at-bol) 'org-todo-head)
  7800. (progn
  7801. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  7802. nil (point-at-eol)))
  7803. (get-text-property p 'org-todo-head))))
  7804. ((not (member kwd org-todo-keywords-1))
  7805. (car org-todo-keywords-1))
  7806. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  7807. (defun org-fast-todo-selection ()
  7808. "Fast TODO keyword selection with single keys.
  7809. Returns the new TODO keyword, or nil if no state change should occur."
  7810. (let* ((fulltable org-todo-key-alist)
  7811. (done-keywords org-done-keywords) ;; needed for the faces.
  7812. (maxlen (apply 'max (mapcar
  7813. (lambda (x)
  7814. (if (stringp (car x)) (string-width (car x)) 0))
  7815. fulltable)))
  7816. (expert nil)
  7817. (fwidth (+ maxlen 3 1 3))
  7818. (ncol (/ (- (window-width) 4) fwidth))
  7819. tg cnt e c tbl
  7820. groups ingroup)
  7821. (save-window-excursion
  7822. (if expert
  7823. (set-buffer (get-buffer-create " *Org todo*"))
  7824. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  7825. (erase-buffer)
  7826. (org-set-local 'org-done-keywords done-keywords)
  7827. (setq tbl fulltable cnt 0)
  7828. (while (setq e (pop tbl))
  7829. (cond
  7830. ((equal e '(:startgroup))
  7831. (push '() groups) (setq ingroup t)
  7832. (when (not (= cnt 0))
  7833. (setq cnt 0)
  7834. (insert "\n"))
  7835. (insert "{ "))
  7836. ((equal e '(:endgroup))
  7837. (setq ingroup nil cnt 0)
  7838. (insert "}\n"))
  7839. (t
  7840. (setq tg (car e) c (cdr e))
  7841. (if ingroup (push tg (car groups)))
  7842. (setq tg (org-add-props tg nil 'face
  7843. (org-get-todo-face tg)))
  7844. (if (and (= cnt 0) (not ingroup)) (insert " "))
  7845. (insert "[" c "] " tg (make-string
  7846. (- fwidth 4 (length tg)) ?\ ))
  7847. (when (= (setq cnt (1+ cnt)) ncol)
  7848. (insert "\n")
  7849. (if ingroup (insert " "))
  7850. (setq cnt 0)))))
  7851. (insert "\n")
  7852. (goto-char (point-min))
  7853. (if (and (not expert) (fboundp 'fit-window-to-buffer))
  7854. (fit-window-to-buffer))
  7855. (message "[a-z..]:Set [SPC]:clear")
  7856. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  7857. (cond
  7858. ((or (= c ?\C-g)
  7859. (and (= c ?q) (not (rassoc c fulltable))))
  7860. (setq quit-flag t))
  7861. ((= c ?\ ) nil)
  7862. ((setq e (rassoc c fulltable) tg (car e))
  7863. tg)
  7864. (t (setq quit-flag t))))))
  7865. (defun org-entry-is-todo-p ()
  7866. (member (org-get-todo-state) org-not-done-keywords))
  7867. (defun org-entry-is-done-p ()
  7868. (member (org-get-todo-state) org-done-keywords))
  7869. (defun org-get-todo-state ()
  7870. (save-excursion
  7871. (org-back-to-heading t)
  7872. (and (looking-at org-todo-line-regexp)
  7873. (match-end 2)
  7874. (match-string 2))))
  7875. (defun org-at-date-range-p (&optional inactive-ok)
  7876. "Is the cursor inside a date range?"
  7877. (interactive)
  7878. (save-excursion
  7879. (catch 'exit
  7880. (let ((pos (point)))
  7881. (skip-chars-backward "^[<\r\n")
  7882. (skip-chars-backward "<[")
  7883. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  7884. (>= (match-end 0) pos)
  7885. (throw 'exit t))
  7886. (skip-chars-backward "^<[\r\n")
  7887. (skip-chars-backward "<[")
  7888. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  7889. (>= (match-end 0) pos)
  7890. (throw 'exit t)))
  7891. nil)))
  7892. (defun org-get-repeat ()
  7893. "Check if tere is a deadline/schedule with repeater in this entry."
  7894. (save-match-data
  7895. (save-excursion
  7896. (org-back-to-heading t)
  7897. (if (re-search-forward
  7898. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  7899. (match-string 1)))))
  7900. (defvar org-last-changed-timestamp)
  7901. (defvar org-log-post-message)
  7902. (defvar org-log-note-purpose)
  7903. (defvar org-log-note-how)
  7904. (defun org-auto-repeat-maybe (done-word)
  7905. "Check if the current headline contains a repeated deadline/schedule.
  7906. If yes, set TODO state back to what it was and change the base date
  7907. of repeating deadline/scheduled time stamps to new date.
  7908. This function is run automatically after each state change to a DONE state."
  7909. ;; last-state is dynamically scoped into this function
  7910. (let* ((repeat (org-get-repeat))
  7911. (aa (assoc last-state org-todo-kwd-alist))
  7912. (interpret (nth 1 aa))
  7913. (head (nth 2 aa))
  7914. (whata '(("d" . day) ("m" . month) ("y" . year)))
  7915. (msg "Entry repeats: ")
  7916. (org-log-done nil)
  7917. (org-todo-log-states nil)
  7918. (nshiftmax 10) (nshift 0)
  7919. re type n what ts mb0 time)
  7920. (when repeat
  7921. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  7922. (org-todo (if (eq interpret 'type) last-state head))
  7923. (when org-log-repeat
  7924. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  7925. (memq 'org-add-log-note post-command-hook))
  7926. ;; OK, we are already setup for some record
  7927. (if (eq org-log-repeat 'note)
  7928. ;; make sure we take a note, not only a time stamp
  7929. (setq org-log-note-how 'note))
  7930. ;; Set up for taking a record
  7931. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  7932. 'findpos org-log-repeat)))
  7933. (org-back-to-heading t)
  7934. (org-add-planning-info nil nil 'closed)
  7935. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  7936. org-deadline-time-regexp "\\)\\|\\("
  7937. org-ts-regexp "\\)"))
  7938. (while (re-search-forward
  7939. re (save-excursion (outline-next-heading) (point)) t)
  7940. (setq type (if (match-end 1) org-scheduled-string
  7941. (if (match-end 3) org-deadline-string "Plain:"))
  7942. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0)))
  7943. mb0 (match-beginning 0))
  7944. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  7945. (setq n (string-to-number (match-string 2 ts))
  7946. what (match-string 3 ts))
  7947. (if (equal what "w") (setq n (* n 7) what "d"))
  7948. ;; Preparation, see if we need to modify the start date for the change
  7949. (when (match-end 1)
  7950. (setq time (save-match-data (org-time-string-to-time ts)))
  7951. (cond
  7952. ((equal (match-string 1 ts) ".")
  7953. ;; Shift starting date to today
  7954. (org-timestamp-change
  7955. (- (time-to-days (current-time)) (time-to-days time))
  7956. 'day))
  7957. ((equal (match-string 1 ts) "+")
  7958. (while (or (= nshift 0)
  7959. (<= (time-to-days time) (time-to-days (current-time))))
  7960. (when (= (incf nshift) nshiftmax)
  7961. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  7962. (error "Abort")))
  7963. (org-timestamp-change n (cdr (assoc what whata)))
  7964. (org-at-timestamp-p t)
  7965. (setq ts (match-string 1))
  7966. (setq time (save-match-data (org-time-string-to-time ts))))
  7967. (org-timestamp-change (- n) (cdr (assoc what whata)))
  7968. ;; rematch, so that we have everything in place for the real shift
  7969. (org-at-timestamp-p t)
  7970. (setq ts (match-string 1))
  7971. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  7972. (org-timestamp-change n (cdr (assoc what whata)))
  7973. (setq msg (concat msg type org-last-changed-timestamp " "))))
  7974. (setq org-log-post-message msg)
  7975. (message "%s" msg))))
  7976. (defun org-show-todo-tree (arg)
  7977. "Make a compact tree which shows all headlines marked with TODO.
  7978. The tree will show the lines where the regexp matches, and all higher
  7979. headlines above the match.
  7980. With a \\[universal-argument] prefix, also show the DONE entries.
  7981. With a numeric prefix N, construct a sparse tree for the Nth element
  7982. of `org-todo-keywords-1'."
  7983. (interactive "P")
  7984. (let ((case-fold-search nil)
  7985. (kwd-re
  7986. (cond ((null arg) org-not-done-regexp)
  7987. ((equal arg '(4))
  7988. (let ((kwd (completing-read "Keyword (or KWD1|KWD2|...): "
  7989. (mapcar 'list org-todo-keywords-1))))
  7990. (concat "\\("
  7991. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  7992. "\\)\\>")))
  7993. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  7994. (regexp-quote (nth (1- (prefix-numeric-value arg))
  7995. org-todo-keywords-1)))
  7996. (t (error "Invalid prefix argument: %s" arg)))))
  7997. (message "%d TODO entries found"
  7998. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  7999. (defun org-deadline (&optional remove)
  8000. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  8001. With argument REMOVE, remove any deadline from the item."
  8002. (interactive "P")
  8003. (if remove
  8004. (progn
  8005. (org-remove-timestamp-with-keyword org-deadline-string)
  8006. (message "Item no longer has a deadline."))
  8007. (org-add-planning-info 'deadline nil 'closed)))
  8008. (defun org-schedule (&optional remove)
  8009. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  8010. With argument REMOVE, remove any scheduling date from the item."
  8011. (interactive "P")
  8012. (if remove
  8013. (progn
  8014. (org-remove-timestamp-with-keyword org-scheduled-string)
  8015. (message "Item is no longer scheduled."))
  8016. (org-add-planning-info 'scheduled nil 'closed)))
  8017. (defun org-remove-timestamp-with-keyword (keyword)
  8018. "Remove all time stamps with KEYWORD in the current entry."
  8019. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  8020. beg)
  8021. (save-excursion
  8022. (org-back-to-heading t)
  8023. (setq beg (point))
  8024. (org-end-of-subtree t t)
  8025. (while (re-search-backward re beg t)
  8026. (replace-match "")
  8027. (unless (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  8028. (delete-region (point-at-bol) (min (1+ (point)) (point-max))))))))
  8029. (defun org-add-planning-info (what &optional time &rest remove)
  8030. "Insert new timestamp with keyword in the line directly after the headline.
  8031. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  8032. If non is given, the user is prompted for a date.
  8033. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  8034. be removed."
  8035. (interactive)
  8036. (let (org-time-was-given org-end-time-was-given ts
  8037. end default-time default-input)
  8038. (when (and (not time) (memq what '(scheduled deadline)))
  8039. ;; Try to get a default date/time from existing timestamp
  8040. (save-excursion
  8041. (org-back-to-heading t)
  8042. (setq end (save-excursion (outline-next-heading) (point)))
  8043. (when (re-search-forward (if (eq what 'scheduled)
  8044. org-scheduled-time-regexp
  8045. org-deadline-time-regexp)
  8046. end t)
  8047. (setq ts (match-string 1)
  8048. default-time
  8049. (apply 'encode-time (org-parse-time-string ts))
  8050. default-input (and ts (org-get-compact-tod ts))))))
  8051. (when what
  8052. ;; If necessary, get the time from the user
  8053. (setq time (or time (org-read-date nil 'to-time nil nil
  8054. default-time default-input))))
  8055. (when (and org-insert-labeled-timestamps-at-point
  8056. (member what '(scheduled deadline)))
  8057. (insert
  8058. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  8059. (org-insert-time-stamp time org-time-was-given
  8060. nil nil nil (list org-end-time-was-given))
  8061. (setq what nil))
  8062. (save-excursion
  8063. (save-restriction
  8064. (let (col list elt ts buffer-invisibility-spec)
  8065. (org-back-to-heading t)
  8066. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  8067. (goto-char (match-end 1))
  8068. (setq col (current-column))
  8069. (goto-char (match-end 0))
  8070. (if (eobp) (insert "\n") (forward-char 1))
  8071. (if (and (not (looking-at outline-regexp))
  8072. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  8073. "[^\r\n]*"))
  8074. (not (equal (match-string 1) org-clock-string)))
  8075. (narrow-to-region (match-beginning 0) (match-end 0))
  8076. (insert-before-markers "\n")
  8077. (backward-char 1)
  8078. (narrow-to-region (point) (point))
  8079. (indent-to-column col))
  8080. ;; Check if we have to remove something.
  8081. (setq list (cons what remove))
  8082. (while list
  8083. (setq elt (pop list))
  8084. (goto-char (point-min))
  8085. (when (or (and (eq elt 'scheduled)
  8086. (re-search-forward org-scheduled-time-regexp nil t))
  8087. (and (eq elt 'deadline)
  8088. (re-search-forward org-deadline-time-regexp nil t))
  8089. (and (eq elt 'closed)
  8090. (re-search-forward org-closed-time-regexp nil t)))
  8091. (replace-match "")
  8092. (if (looking-at "--+<[^>]+>") (replace-match ""))
  8093. (if (looking-at " +") (replace-match ""))))
  8094. (goto-char (point-max))
  8095. (when what
  8096. (insert
  8097. (if (not (equal (char-before) ?\ )) " " "")
  8098. (cond ((eq what 'scheduled) org-scheduled-string)
  8099. ((eq what 'deadline) org-deadline-string)
  8100. ((eq what 'closed) org-closed-string))
  8101. " ")
  8102. (setq ts (org-insert-time-stamp
  8103. time
  8104. (or org-time-was-given
  8105. (and (eq what 'closed) org-log-done-with-time))
  8106. (eq what 'closed)
  8107. nil nil (list org-end-time-was-given)))
  8108. (end-of-line 1))
  8109. (goto-char (point-min))
  8110. (widen)
  8111. (if (looking-at "[ \t]+\r?\n")
  8112. (replace-match ""))
  8113. ts)))))
  8114. (defvar org-log-note-marker (make-marker))
  8115. (defvar org-log-note-purpose nil)
  8116. (defvar org-log-note-state nil)
  8117. (defvar org-log-note-how nil)
  8118. (defvar org-log-note-window-configuration nil)
  8119. (defvar org-log-note-return-to (make-marker))
  8120. (defvar org-log-post-message nil
  8121. "Message to be displayed after a log note has been stored.
  8122. The auto-repeater uses this.")
  8123. (defun org-add-note ()
  8124. "Add a note to the current entry.
  8125. This is done in the same way as adding a state change note."
  8126. (interactive)
  8127. (org-add-log-setup 'note nil t nil))
  8128. (defun org-add-log-setup (&optional purpose state findpos how)
  8129. "Set up the post command hook to take a note.
  8130. If this is about to TODO state change, the new state is expected in STATE.
  8131. When FINDPOS is non-nil, find the correct position for the note in
  8132. the current entry. If not, assume that it can be inserted at point."
  8133. (save-excursion
  8134. (when findpos
  8135. (org-back-to-heading t)
  8136. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  8137. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  8138. "[^\r\n]*\\)?"))
  8139. (goto-char (match-end 0))
  8140. (unless org-log-states-order-reversed
  8141. (and (= (char-after) ?\n) (forward-char 1))
  8142. (org-skip-over-state-notes)
  8143. (skip-chars-backward " \t\n\r")))
  8144. (move-marker org-log-note-marker (point))
  8145. (setq org-log-note-purpose purpose
  8146. org-log-note-state state
  8147. org-log-note-how how)
  8148. (add-hook 'post-command-hook 'org-add-log-note 'append)))
  8149. (defun org-skip-over-state-notes ()
  8150. "Skip past the list of State notes in an entry."
  8151. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  8152. (while (looking-at "[ \t]*- State")
  8153. (condition-case nil
  8154. (org-next-item)
  8155. (error (org-end-of-item)))))
  8156. (defun org-add-log-note (&optional purpose)
  8157. "Pop up a window for taking a note, and add this note later at point."
  8158. (remove-hook 'post-command-hook 'org-add-log-note)
  8159. (setq org-log-note-window-configuration (current-window-configuration))
  8160. (delete-other-windows)
  8161. (move-marker org-log-note-return-to (point))
  8162. (switch-to-buffer (marker-buffer org-log-note-marker))
  8163. (goto-char org-log-note-marker)
  8164. (org-switch-to-buffer-other-window "*Org Note*")
  8165. (erase-buffer)
  8166. (if (memq org-log-note-how '(time state))
  8167. (org-store-log-note)
  8168. (let ((org-inhibit-startup t)) (org-mode))
  8169. (insert (format "# Insert note for %s.
  8170. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  8171. (cond
  8172. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  8173. ((eq org-log-note-purpose 'done) "closed todo item")
  8174. ((eq org-log-note-purpose 'state)
  8175. (format "state change to \"%s\"" org-log-note-state))
  8176. ((eq org-log-note-purpose 'note)
  8177. "this entry")
  8178. (t (error "This should not happen")))))
  8179. (org-set-local 'org-finish-function 'org-store-log-note)))
  8180. (defvar org-note-abort nil) ; dynamically scoped
  8181. (defun org-store-log-note ()
  8182. "Finish taking a log note, and insert it to where it belongs."
  8183. (let ((txt (buffer-string))
  8184. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  8185. lines ind)
  8186. (kill-buffer (current-buffer))
  8187. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  8188. (setq txt (replace-match "" t t txt)))
  8189. (if (string-match "\\s-+\\'" txt)
  8190. (setq txt (replace-match "" t t txt)))
  8191. (setq lines (org-split-string txt "\n"))
  8192. (when (and note (string-match "\\S-" note))
  8193. (setq note
  8194. (org-replace-escapes
  8195. note
  8196. (list (cons "%u" (user-login-name))
  8197. (cons "%U" user-full-name)
  8198. (cons "%t" (format-time-string
  8199. (org-time-stamp-format 'long 'inactive)
  8200. (current-time)))
  8201. (cons "%s" (if org-log-note-state
  8202. (concat "\"" org-log-note-state "\"")
  8203. "")))))
  8204. (if lines (setq note (concat note " \\\\")))
  8205. (push note lines))
  8206. (when (or current-prefix-arg org-note-abort) (setq lines nil))
  8207. (when lines
  8208. (save-excursion
  8209. (set-buffer (marker-buffer org-log-note-marker))
  8210. (save-excursion
  8211. (goto-char org-log-note-marker)
  8212. (move-marker org-log-note-marker nil)
  8213. (end-of-line 1)
  8214. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  8215. (indent-relative nil)
  8216. (insert "- " (pop lines))
  8217. (org-indent-line-function)
  8218. (beginning-of-line 1)
  8219. (looking-at "[ \t]*")
  8220. (setq ind (concat (match-string 0) " "))
  8221. (end-of-line 1)
  8222. (while lines (insert "\n" ind (pop lines)))))))
  8223. (set-window-configuration org-log-note-window-configuration)
  8224. (with-current-buffer (marker-buffer org-log-note-return-to)
  8225. (goto-char org-log-note-return-to))
  8226. (move-marker org-log-note-return-to nil)
  8227. (and org-log-post-message (message "%s" org-log-post-message)))
  8228. (defun org-sparse-tree (&optional arg)
  8229. "Create a sparse tree, prompt for the details.
  8230. This command can create sparse trees. You first need to select the type
  8231. of match used to create the tree:
  8232. t Show entries with a specific TODO keyword.
  8233. T Show entries selected by a tags match.
  8234. p Enter a property name and its value (both with completion on existing
  8235. names/values) and show entries with that property.
  8236. r Show entries matching a regular expression
  8237. d Show deadlines due within `org-deadline-warning-days'."
  8238. (interactive "P")
  8239. (let (ans kwd value)
  8240. (message "Sparse tree: [/]regexp [t]odo-kwd [T]ag [p]roperty [d]eadlines [b]efore-date")
  8241. (setq ans (read-char-exclusive))
  8242. (cond
  8243. ((equal ans ?d)
  8244. (call-interactively 'org-check-deadlines))
  8245. ((equal ans ?b)
  8246. (call-interactively 'org-check-before-date))
  8247. ((equal ans ?t)
  8248. (org-show-todo-tree '(4)))
  8249. ((equal ans ?T)
  8250. (call-interactively 'org-tags-sparse-tree))
  8251. ((member ans '(?p ?P))
  8252. (setq kwd (completing-read "Property: "
  8253. (mapcar 'list (org-buffer-property-keys))))
  8254. (setq value (completing-read "Value: "
  8255. (mapcar 'list (org-property-values kwd))))
  8256. (unless (string-match "\\`{.*}\\'" value)
  8257. (setq value (concat "\"" value "\"")))
  8258. (org-tags-sparse-tree arg (concat kwd "=" value)))
  8259. ((member ans '(?r ?R ?/))
  8260. (call-interactively 'org-occur))
  8261. (t (error "No such sparse tree command \"%c\"" ans)))))
  8262. (defvar org-occur-highlights nil
  8263. "List of overlays used for occur matches.")
  8264. (make-variable-buffer-local 'org-occur-highlights)
  8265. (defvar org-occur-parameters nil
  8266. "Parameters of the active org-occur calls.
  8267. This is a list, each call to org-occur pushes as cons cell,
  8268. containing the regular expression and the callback, onto the list.
  8269. The list can contain several entries if `org-occur' has been called
  8270. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  8271. will only contain one set of parameters. When the highlights are
  8272. removed (for example with `C-c C-c', or with the next edit (depending
  8273. on `org-remove-highlights-with-change'), this variable is emptied
  8274. as well.")
  8275. (make-variable-buffer-local 'org-occur-parameters)
  8276. (defun org-occur (regexp &optional keep-previous callback)
  8277. "Make a compact tree which shows all matches of REGEXP.
  8278. The tree will show the lines where the regexp matches, and all higher
  8279. headlines above the match. It will also show the heading after the match,
  8280. to make sure editing the matching entry is easy.
  8281. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  8282. call to `org-occur' will be kept, to allow stacking of calls to this
  8283. command.
  8284. If CALLBACK is non-nil, it is a function which is called to confirm
  8285. that the match should indeed be shown."
  8286. (interactive "sRegexp: \nP")
  8287. (unless keep-previous
  8288. (org-remove-occur-highlights nil nil t))
  8289. (push (cons regexp callback) org-occur-parameters)
  8290. (let ((cnt 0))
  8291. (save-excursion
  8292. (goto-char (point-min))
  8293. (if (or (not keep-previous) ; do not want to keep
  8294. (not org-occur-highlights)) ; no previous matches
  8295. ;; hide everything
  8296. (org-overview))
  8297. (while (re-search-forward regexp nil t)
  8298. (when (or (not callback)
  8299. (save-match-data (funcall callback)))
  8300. (setq cnt (1+ cnt))
  8301. (when org-highlight-sparse-tree-matches
  8302. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  8303. (org-show-context 'occur-tree))))
  8304. (when org-remove-highlights-with-change
  8305. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  8306. nil 'local))
  8307. (unless org-sparse-tree-open-archived-trees
  8308. (org-hide-archived-subtrees (point-min) (point-max)))
  8309. (run-hooks 'org-occur-hook)
  8310. (if (interactive-p)
  8311. (message "%d match(es) for regexp %s" cnt regexp))
  8312. cnt))
  8313. (defun org-show-context (&optional key)
  8314. "Make sure point and context and visible.
  8315. How much context is shown depends upon the variables
  8316. `org-show-hierarchy-above', `org-show-following-heading'. and
  8317. `org-show-siblings'."
  8318. (let ((heading-p (org-on-heading-p t))
  8319. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  8320. (following-p (org-get-alist-option org-show-following-heading key))
  8321. (entry-p (org-get-alist-option org-show-entry-below key))
  8322. (siblings-p (org-get-alist-option org-show-siblings key)))
  8323. (catch 'exit
  8324. ;; Show heading or entry text
  8325. (if (and heading-p (not entry-p))
  8326. (org-flag-heading nil) ; only show the heading
  8327. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  8328. (org-show-hidden-entry))) ; show entire entry
  8329. (when following-p
  8330. ;; Show next sibling, or heading below text
  8331. (save-excursion
  8332. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  8333. (org-flag-heading nil))))
  8334. (when siblings-p (org-show-siblings))
  8335. (when hierarchy-p
  8336. ;; show all higher headings, possibly with siblings
  8337. (save-excursion
  8338. (while (and (condition-case nil
  8339. (progn (org-up-heading-all 1) t)
  8340. (error nil))
  8341. (not (bobp)))
  8342. (org-flag-heading nil)
  8343. (when siblings-p (org-show-siblings))))))))
  8344. (defun org-reveal (&optional siblings)
  8345. "Show current entry, hierarchy above it, and the following headline.
  8346. This can be used to show a consistent set of context around locations
  8347. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  8348. not t for the search context.
  8349. With optional argument SIBLINGS, on each level of the hierarchy all
  8350. siblings are shown. This repairs the tree structure to what it would
  8351. look like when opened with hierarchical calls to `org-cycle'."
  8352. (interactive "P")
  8353. (let ((org-show-hierarchy-above t)
  8354. (org-show-following-heading t)
  8355. (org-show-siblings (if siblings t org-show-siblings)))
  8356. (org-show-context nil)))
  8357. (defun org-highlight-new-match (beg end)
  8358. "Highlight from BEG to END and mark the highlight is an occur headline."
  8359. (let ((ov (org-make-overlay beg end)))
  8360. (org-overlay-put ov 'face 'secondary-selection)
  8361. (push ov org-occur-highlights)))
  8362. (defun org-remove-occur-highlights (&optional beg end noremove)
  8363. "Remove the occur highlights from the buffer.
  8364. BEG and END are ignored. If NOREMOVE is nil, remove this function
  8365. from the `before-change-functions' in the current buffer."
  8366. (interactive)
  8367. (unless org-inhibit-highlight-removal
  8368. (mapc 'org-delete-overlay org-occur-highlights)
  8369. (setq org-occur-highlights nil)
  8370. (setq org-occur-parameters nil)
  8371. (unless noremove
  8372. (remove-hook 'before-change-functions
  8373. 'org-remove-occur-highlights 'local))))
  8374. ;;;; Priorities
  8375. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  8376. "Regular expression matching the priority indicator.")
  8377. (defvar org-remove-priority-next-time nil)
  8378. (defun org-priority-up ()
  8379. "Increase the priority of the current item."
  8380. (interactive)
  8381. (org-priority 'up))
  8382. (defun org-priority-down ()
  8383. "Decrease the priority of the current item."
  8384. (interactive)
  8385. (org-priority 'down))
  8386. (defun org-priority (&optional action)
  8387. "Change the priority of an item by ARG.
  8388. ACTION can be `set', `up', `down', or a character."
  8389. (interactive)
  8390. (setq action (or action 'set))
  8391. (let (current new news have remove)
  8392. (save-excursion
  8393. (org-back-to-heading)
  8394. (if (looking-at org-priority-regexp)
  8395. (setq current (string-to-char (match-string 2))
  8396. have t)
  8397. (setq current org-default-priority))
  8398. (cond
  8399. ((or (eq action 'set) (integerp action))
  8400. (if (integerp action)
  8401. (setq new action)
  8402. (message "Priority %c-%c, SPC to remove: " org-highest-priority org-lowest-priority)
  8403. (setq new (read-char-exclusive)))
  8404. (if (and (= (upcase org-highest-priority) org-highest-priority)
  8405. (= (upcase org-lowest-priority) org-lowest-priority))
  8406. (setq new (upcase new)))
  8407. (cond ((equal new ?\ ) (setq remove t))
  8408. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  8409. (error "Priority must be between `%c' and `%c'"
  8410. org-highest-priority org-lowest-priority))))
  8411. ((eq action 'up)
  8412. (if (and (not have) (eq last-command this-command))
  8413. (setq new org-lowest-priority)
  8414. (setq new (if (and org-priority-start-cycle-with-default (not have))
  8415. org-default-priority (1- current)))))
  8416. ((eq action 'down)
  8417. (if (and (not have) (eq last-command this-command))
  8418. (setq new org-highest-priority)
  8419. (setq new (if (and org-priority-start-cycle-with-default (not have))
  8420. org-default-priority (1+ current)))))
  8421. (t (error "Invalid action")))
  8422. (if (or (< (upcase new) org-highest-priority)
  8423. (> (upcase new) org-lowest-priority))
  8424. (setq remove t))
  8425. (setq news (format "%c" new))
  8426. (if have
  8427. (if remove
  8428. (replace-match "" t t nil 1)
  8429. (replace-match news t t nil 2))
  8430. (if remove
  8431. (error "No priority cookie found in line")
  8432. (looking-at org-todo-line-regexp)
  8433. (if (match-end 2)
  8434. (progn
  8435. (goto-char (match-end 2))
  8436. (insert " [#" news "]"))
  8437. (goto-char (match-beginning 3))
  8438. (insert "[#" news "] ")))))
  8439. (org-preserve-lc (org-set-tags nil 'align))
  8440. (if remove
  8441. (message "Priority removed")
  8442. (message "Priority of current item set to %s" news))))
  8443. (defun org-get-priority (s)
  8444. "Find priority cookie and return priority."
  8445. (save-match-data
  8446. (if (not (string-match org-priority-regexp s))
  8447. (* 1000 (- org-lowest-priority org-default-priority))
  8448. (* 1000 (- org-lowest-priority
  8449. (string-to-char (match-string 2 s)))))))
  8450. ;;;; Tags
  8451. (defun org-scan-tags (action matcher &optional todo-only)
  8452. "Scan headline tags with inheritance and produce output ACTION.
  8453. ACTION can be `sparse-tree' or `agenda'. MATCHER is a Lisp form to be
  8454. evaluated, testing if a given set of tags qualifies a headline for
  8455. inclusion. When TODO-ONLY is non-nil, only lines with a TODO keyword
  8456. are included in the output."
  8457. (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
  8458. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  8459. (org-re
  8460. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  8461. (props (list 'face nil
  8462. 'done-face 'org-done
  8463. 'undone-face nil
  8464. 'mouse-face 'highlight
  8465. 'org-not-done-regexp org-not-done-regexp
  8466. 'org-todo-regexp org-todo-regexp
  8467. 'keymap org-agenda-keymap
  8468. 'help-echo
  8469. (format "mouse-2 or RET jump to org file %s"
  8470. (abbreviate-file-name
  8471. (or (buffer-file-name (buffer-base-buffer))
  8472. (buffer-name (buffer-base-buffer)))))))
  8473. (case-fold-search nil)
  8474. lspos
  8475. tags tags-list tags-alist (llast 0) rtn level category i txt
  8476. todo marker entry priority)
  8477. (save-excursion
  8478. (goto-char (point-min))
  8479. (when (eq action 'sparse-tree)
  8480. (org-overview)
  8481. (org-remove-occur-highlights))
  8482. (while (re-search-forward re nil t)
  8483. (catch :skip
  8484. (setq todo (if (match-end 1) (match-string 2))
  8485. tags (if (match-end 4) (match-string 4)))
  8486. (goto-char (setq lspos (1+ (match-beginning 0))))
  8487. (setq level (org-reduced-level (funcall outline-level))
  8488. category (org-get-category))
  8489. (setq i llast llast level)
  8490. ;; remove tag lists from same and sublevels
  8491. (while (>= i level)
  8492. (when (setq entry (assoc i tags-alist))
  8493. (setq tags-alist (delete entry tags-alist)))
  8494. (setq i (1- i)))
  8495. ;; add the next tags
  8496. (when tags
  8497. (setq tags (mapcar 'downcase (org-split-string tags ":"))
  8498. tags-alist
  8499. (cons (cons level tags) tags-alist)))
  8500. ;; compile tags for current headline
  8501. (setq tags-list
  8502. (if org-use-tag-inheritance
  8503. (apply 'append (mapcar 'cdr tags-alist))
  8504. tags))
  8505. (when (and tags org-use-tag-inheritance
  8506. (not (eq t org-use-tag-inheritance)))
  8507. ;; selective inheritance, remove uninherited ones
  8508. (setcdr (car tags-alist)
  8509. (org-remove-uniherited-tags (cdar tags-alist))))
  8510. (when (and (or (not todo-only) (member todo org-not-done-keywords))
  8511. (eval matcher)
  8512. (or (not org-agenda-skip-archived-trees)
  8513. (not (member org-archive-tag tags-list))))
  8514. (and (eq action 'agenda) (org-agenda-skip))
  8515. ;; list this headline
  8516. (if (eq action 'sparse-tree)
  8517. (progn
  8518. (and org-highlight-sparse-tree-matches
  8519. (org-get-heading) (match-end 0)
  8520. (org-highlight-new-match
  8521. (match-beginning 0) (match-beginning 1)))
  8522. (org-show-context 'tags-tree))
  8523. (setq txt (org-format-agenda-item
  8524. ""
  8525. (concat
  8526. (if org-tags-match-list-sublevels
  8527. (make-string (1- level) ?.) "")
  8528. (org-get-heading))
  8529. category tags-list)
  8530. priority (org-get-priority txt))
  8531. (goto-char lspos)
  8532. (setq marker (org-agenda-new-marker))
  8533. (org-add-props txt props
  8534. 'org-marker marker 'org-hd-marker marker 'org-category category
  8535. 'priority priority 'type "tagsmatch")
  8536. (push txt rtn))
  8537. ;; if we are to skip sublevels, jump to end of subtree
  8538. (or org-tags-match-list-sublevels (org-end-of-subtree t))))))
  8539. (when (and (eq action 'sparse-tree)
  8540. (not org-sparse-tree-open-archived-trees))
  8541. (org-hide-archived-subtrees (point-min) (point-max)))
  8542. (nreverse rtn)))
  8543. (defun org-remove-uniherited-tags (tags)
  8544. "Remove all tags that are not inherited from the list TAGS."
  8545. (cond
  8546. ((eq org-use-tag-inheritance t) tags)
  8547. ((not org-use-tag-inheritance) nil)
  8548. ((stringp org-use-tag-inheritance)
  8549. (delq nil (mapcar
  8550. (lambda (x) (if (string-match org-use-tag-inheritance x) x nil))
  8551. tags)))
  8552. ((listp org-use-tag-inheritance)
  8553. (org-delete-all org-use-tag-inheritance tags))))
  8554. (defvar todo-only) ;; dynamically scoped
  8555. (defun org-tags-sparse-tree (&optional todo-only match)
  8556. "Create a sparse tree according to tags string MATCH.
  8557. MATCH can contain positive and negative selection of tags, like
  8558. \"+WORK+URGENT-WITHBOSS\".
  8559. If optional argument TODO_ONLY is non-nil, only select lines that are
  8560. also TODO lines."
  8561. (interactive "P")
  8562. (org-prepare-agenda-buffers (list (current-buffer)))
  8563. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  8564. (defvar org-cached-props nil)
  8565. (defun org-cached-entry-get (pom property)
  8566. (if (or (eq t org-use-property-inheritance)
  8567. (and (stringp org-use-property-inheritance)
  8568. (string-match org-use-property-inheritance property))
  8569. (and (listp org-use-property-inheritance)
  8570. (member property org-use-property-inheritance)))
  8571. ;; Caching is not possible, check it directly
  8572. (org-entry-get pom property 'inherit)
  8573. ;; Get all properties, so that we can do complicated checks easily
  8574. (cdr (assoc property (or org-cached-props
  8575. (setq org-cached-props
  8576. (org-entry-properties pom)))))))
  8577. (defun org-global-tags-completion-table (&optional files)
  8578. "Return the list of all tags in all agenda buffer/files."
  8579. (save-excursion
  8580. (org-uniquify
  8581. (delq nil
  8582. (apply 'append
  8583. (mapcar
  8584. (lambda (file)
  8585. (set-buffer (find-file-noselect file))
  8586. (append (org-get-buffer-tags)
  8587. (mapcar (lambda (x) (if (stringp (car-safe x))
  8588. (list (car-safe x)) nil))
  8589. org-tag-alist)))
  8590. (if (and files (car files))
  8591. files
  8592. (org-agenda-files))))))))
  8593. (defun org-make-tags-matcher (match)
  8594. "Create the TAGS//TODO matcher form for the selection string MATCH."
  8595. ;; todo-only is scoped dynamically into this function, and the function
  8596. ;; may change it it the matcher asksk for it.
  8597. (unless match
  8598. ;; Get a new match request, with completion
  8599. (let ((org-last-tags-completion-table
  8600. (org-global-tags-completion-table)))
  8601. (setq match (completing-read
  8602. "Match: " 'org-tags-completion-function nil nil nil
  8603. 'org-tags-history))))
  8604. ;; Parse the string and create a lisp form
  8605. (let ((match0 match)
  8606. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL=\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)=\\({[^}]+}\\|\"[^\"]*\"\\)\\|[[:alnum:]_@]+\\)"))
  8607. minus tag mm
  8608. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  8609. orterms term orlist re-p level-p prop-p pn pv cat-p gv)
  8610. (if (string-match "/+" match)
  8611. ;; match contains also a todo-matching request
  8612. (progn
  8613. (setq tagsmatch (substring match 0 (match-beginning 0))
  8614. todomatch (substring match (match-end 0)))
  8615. (if (string-match "^!" todomatch)
  8616. (setq todo-only t todomatch (substring todomatch 1)))
  8617. (if (string-match "^\\s-*$" todomatch)
  8618. (setq todomatch nil)))
  8619. ;; only matching tags
  8620. (setq tagsmatch match todomatch nil))
  8621. ;; Make the tags matcher
  8622. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  8623. (setq tagsmatcher t)
  8624. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  8625. (while (setq term (pop orterms))
  8626. (while (and (equal (substring term -1) "\\") orterms)
  8627. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  8628. (while (string-match re term)
  8629. (setq minus (and (match-end 1)
  8630. (equal (match-string 1 term) "-"))
  8631. tag (match-string 2 term)
  8632. re-p (equal (string-to-char tag) ?{)
  8633. level-p (match-end 3)
  8634. prop-p (match-end 4)
  8635. mm (cond
  8636. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  8637. (level-p `(= level ,(string-to-number
  8638. (match-string 3 term))))
  8639. (prop-p
  8640. (setq pn (match-string 4 term)
  8641. pv (match-string 5 term)
  8642. cat-p (equal pn "CATEGORY")
  8643. re-p (equal (string-to-char pv) ?{)
  8644. pv (substring pv 1 -1))
  8645. (if (equal pn "CATEGORY")
  8646. (setq gv '(get-text-property (point) 'org-category))
  8647. (setq gv `(org-cached-entry-get nil ,pn)))
  8648. (if re-p
  8649. `(string-match ,pv (or ,gv ""))
  8650. `(equal ,pv (or ,gv ""))))
  8651. (t `(member ,(downcase tag) tags-list)))
  8652. mm (if minus (list 'not mm) mm)
  8653. term (substring term (match-end 0)))
  8654. (push mm tagsmatcher))
  8655. (push (if (> (length tagsmatcher) 1)
  8656. (cons 'and tagsmatcher)
  8657. (car tagsmatcher))
  8658. orlist)
  8659. (setq tagsmatcher nil))
  8660. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  8661. (setq tagsmatcher
  8662. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  8663. ;; Make the todo matcher
  8664. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  8665. (setq todomatcher t)
  8666. (setq orterms (org-split-string todomatch "|") orlist nil)
  8667. (while (setq term (pop orterms))
  8668. (while (string-match re term)
  8669. (setq minus (and (match-end 1)
  8670. (equal (match-string 1 term) "-"))
  8671. kwd (match-string 2 term)
  8672. re-p (equal (string-to-char kwd) ?{)
  8673. term (substring term (match-end 0))
  8674. mm (if re-p
  8675. `(string-match ,(substring kwd 1 -1) todo)
  8676. (list 'equal 'todo kwd))
  8677. mm (if minus (list 'not mm) mm))
  8678. (push mm todomatcher))
  8679. (push (if (> (length todomatcher) 1)
  8680. (cons 'and todomatcher)
  8681. (car todomatcher))
  8682. orlist)
  8683. (setq todomatcher nil))
  8684. (setq todomatcher (if (> (length orlist) 1)
  8685. (cons 'or orlist) (car orlist))))
  8686. ;; Return the string and lisp forms of the matcher
  8687. (setq matcher (if todomatcher
  8688. (list 'and tagsmatcher todomatcher)
  8689. tagsmatcher))
  8690. (cons match0 matcher)))
  8691. (defun org-match-any-p (re list)
  8692. "Does re match any element of list?"
  8693. (setq list (mapcar (lambda (x) (string-match re x)) list))
  8694. (delq nil list))
  8695. (defvar org-add-colon-after-tag-completion nil) ;; dynamically skoped param
  8696. (defvar org-tags-overlay (org-make-overlay 1 1))
  8697. (org-detach-overlay org-tags-overlay)
  8698. (defun org-get-tags-at (&optional pos)
  8699. "Get a list of all headline tags applicable at POS.
  8700. POS defaults to point. If tags are inherited, the list contains
  8701. the targets in the same sequence as the headlines appear, i.e.
  8702. sthe tags of the current headline come last."
  8703. (interactive)
  8704. (let (tags ltags lastpos parent)
  8705. (save-excursion
  8706. (save-restriction
  8707. (widen)
  8708. (goto-char (or pos (point)))
  8709. (save-match-data
  8710. (condition-case nil
  8711. (progn
  8712. (org-back-to-heading t)
  8713. (while (not (equal lastpos (point)))
  8714. (setq lastpos (point))
  8715. (when (looking-at (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  8716. (setq ltags (org-split-string
  8717. (org-match-string-no-properties 1) ":"))
  8718. (setq tags (append (org-remove-uniherited-tags ltags)
  8719. tags)))
  8720. (or org-use-tag-inheritance (error ""))
  8721. (org-up-heading-all 1)
  8722. (setq parent t)))
  8723. (error nil))))
  8724. tags)))
  8725. (defun org-align-tags-here (to-col)
  8726. ;; Assumes that this is a headline
  8727. (let ((pos (point)) (col (current-column)) tags)
  8728. (beginning-of-line 1)
  8729. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  8730. (< pos (match-beginning 2)))
  8731. (progn
  8732. (setq tags (match-string 2))
  8733. (goto-char (match-beginning 1))
  8734. (insert " ")
  8735. (delete-region (point) (1+ (match-end 0)))
  8736. (backward-char 1)
  8737. (move-to-column
  8738. (max (1+ (current-column))
  8739. (1+ col)
  8740. (if (> to-col 0)
  8741. to-col
  8742. (- (abs to-col) (length tags))))
  8743. t)
  8744. (insert tags)
  8745. (move-to-column (min (current-column) col) t))
  8746. (goto-char pos))))
  8747. (defun org-set-tags (&optional arg just-align)
  8748. "Set the tags for the current headline.
  8749. With prefix ARG, realign all tags in headings in the current buffer."
  8750. (interactive "P")
  8751. (let* ((re (concat "^" outline-regexp))
  8752. (current (org-get-tags-string))
  8753. (col (current-column))
  8754. (org-setting-tags t)
  8755. table current-tags inherited-tags ; computed below when needed
  8756. tags p0 c0 c1 rpl)
  8757. (if arg
  8758. (save-excursion
  8759. (goto-char (point-min))
  8760. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  8761. (while (re-search-forward re nil t)
  8762. (org-set-tags nil t)
  8763. (end-of-line 1)))
  8764. (message "All tags realigned to column %d" org-tags-column))
  8765. (if just-align
  8766. (setq tags current)
  8767. ;; Get a new set of tags from the user
  8768. (save-excursion
  8769. (setq table (or org-tag-alist (org-get-buffer-tags))
  8770. org-last-tags-completion-table table
  8771. current-tags (org-split-string current ":")
  8772. inherited-tags (nreverse
  8773. (nthcdr (length current-tags)
  8774. (nreverse (org-get-tags-at))))
  8775. tags
  8776. (if (or (eq t org-use-fast-tag-selection)
  8777. (and org-use-fast-tag-selection
  8778. (delq nil (mapcar 'cdr table))))
  8779. (org-fast-tag-selection
  8780. current-tags inherited-tags table
  8781. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  8782. (let ((org-add-colon-after-tag-completion t))
  8783. (org-trim
  8784. (org-without-partial-completion
  8785. (completing-read "Tags: " 'org-tags-completion-function
  8786. nil nil current 'org-tags-history)))))))
  8787. (while (string-match "[-+&]+" tags)
  8788. ;; No boolean logic, just a list
  8789. (setq tags (replace-match ":" t t tags))))
  8790. (if (string-match "\\`[\t ]*\\'" tags)
  8791. (setq tags "")
  8792. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  8793. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  8794. ;; Insert new tags at the correct column
  8795. (beginning-of-line 1)
  8796. (cond
  8797. ((and (equal current "") (equal tags "")))
  8798. ((re-search-forward
  8799. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  8800. (point-at-eol) t)
  8801. (if (equal tags "")
  8802. (setq rpl "")
  8803. (goto-char (match-beginning 0))
  8804. (setq c0 (current-column) p0 (point)
  8805. c1 (max (1+ c0) (if (> org-tags-column 0)
  8806. org-tags-column
  8807. (- (- org-tags-column) (length tags))))
  8808. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  8809. (replace-match rpl t t)
  8810. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  8811. tags)
  8812. (t (error "Tags alignment failed")))
  8813. (move-to-column col)
  8814. (unless just-align
  8815. (run-hooks 'org-after-tags-change-hook)))))
  8816. (defun org-change-tag-in-region (beg end tag off)
  8817. "Add or remove TAG for each entry in the region.
  8818. This works in the agenda, and also in an org-mode buffer."
  8819. (interactive
  8820. (list (region-beginning) (region-end)
  8821. (let ((org-last-tags-completion-table
  8822. (if (org-mode-p)
  8823. (org-get-buffer-tags)
  8824. (org-global-tags-completion-table))))
  8825. (completing-read
  8826. "Tag: " 'org-tags-completion-function nil nil nil
  8827. 'org-tags-history))
  8828. (progn
  8829. (message "[s]et or [r]emove? ")
  8830. (equal (read-char-exclusive) ?r))))
  8831. (if (fboundp 'deactivate-mark) (deactivate-mark))
  8832. (let ((agendap (equal major-mode 'org-agenda-mode))
  8833. l1 l2 m buf pos newhead (cnt 0))
  8834. (goto-char end)
  8835. (setq l2 (1- (org-current-line)))
  8836. (goto-char beg)
  8837. (setq l1 (org-current-line))
  8838. (loop for l from l1 to l2 do
  8839. (goto-line l)
  8840. (setq m (get-text-property (point) 'org-hd-marker))
  8841. (when (or (and (org-mode-p) (org-on-heading-p))
  8842. (and agendap m))
  8843. (setq buf (if agendap (marker-buffer m) (current-buffer))
  8844. pos (if agendap m (point)))
  8845. (with-current-buffer buf
  8846. (save-excursion
  8847. (save-restriction
  8848. (goto-char pos)
  8849. (setq cnt (1+ cnt))
  8850. (org-toggle-tag tag (if off 'off 'on))
  8851. (setq newhead (org-get-heading)))))
  8852. (and agendap (org-agenda-change-all-lines newhead m))))
  8853. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  8854. (defun org-tags-completion-function (string predicate &optional flag)
  8855. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  8856. (confirm (lambda (x) (stringp (car x)))))
  8857. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  8858. (setq s1 (match-string 1 string)
  8859. s2 (match-string 2 string))
  8860. (setq s1 "" s2 string))
  8861. (cond
  8862. ((eq flag nil)
  8863. ;; try completion
  8864. (setq rtn (try-completion s2 ctable confirm))
  8865. (if (stringp rtn)
  8866. (setq rtn
  8867. (concat s1 s2 (substring rtn (length s2))
  8868. (if (and org-add-colon-after-tag-completion
  8869. (assoc rtn ctable))
  8870. ":" ""))))
  8871. rtn)
  8872. ((eq flag t)
  8873. ;; all-completions
  8874. (all-completions s2 ctable confirm)
  8875. )
  8876. ((eq flag 'lambda)
  8877. ;; exact match?
  8878. (assoc s2 ctable)))
  8879. ))
  8880. (defun org-fast-tag-insert (kwd tags face &optional end)
  8881. "Insert KDW, and the TAGS, the latter with face FACE. Also inser END."
  8882. (insert (format "%-12s" (concat kwd ":"))
  8883. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  8884. (or end "")))
  8885. (defun org-fast-tag-show-exit (flag)
  8886. (save-excursion
  8887. (goto-line 3)
  8888. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  8889. (replace-match ""))
  8890. (when flag
  8891. (end-of-line 1)
  8892. (move-to-column (- (window-width) 19) t)
  8893. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  8894. (defun org-set-current-tags-overlay (current prefix)
  8895. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  8896. (if (featurep 'xemacs)
  8897. (org-overlay-display org-tags-overlay (concat prefix s)
  8898. 'secondary-selection)
  8899. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  8900. (org-overlay-display org-tags-overlay (concat prefix s)))))
  8901. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  8902. "Fast tag selection with single keys.
  8903. CURRENT is the current list of tags in the headline, INHERITED is the
  8904. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  8905. possibly with grouping information. TODO-TABLE is a similar table with
  8906. TODO keywords, should these have keys assigned to them.
  8907. If the keys are nil, a-z are automatically assigned.
  8908. Returns the new tags string, or nil to not change the current settings."
  8909. (let* ((fulltable (append table todo-table))
  8910. (maxlen (apply 'max (mapcar
  8911. (lambda (x)
  8912. (if (stringp (car x)) (string-width (car x)) 0))
  8913. fulltable)))
  8914. (buf (current-buffer))
  8915. (expert (eq org-fast-tag-selection-single-key 'expert))
  8916. (buffer-tags nil)
  8917. (fwidth (+ maxlen 3 1 3))
  8918. (ncol (/ (- (window-width) 4) fwidth))
  8919. (i-face 'org-done)
  8920. (c-face 'org-todo)
  8921. tg cnt e c char c1 c2 ntable tbl rtn
  8922. ov-start ov-end ov-prefix
  8923. (exit-after-next org-fast-tag-selection-single-key)
  8924. (done-keywords org-done-keywords)
  8925. groups ingroup)
  8926. (save-excursion
  8927. (beginning-of-line 1)
  8928. (if (looking-at
  8929. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  8930. (setq ov-start (match-beginning 1)
  8931. ov-end (match-end 1)
  8932. ov-prefix "")
  8933. (setq ov-start (1- (point-at-eol))
  8934. ov-end (1+ ov-start))
  8935. (skip-chars-forward "^\n\r")
  8936. (setq ov-prefix
  8937. (concat
  8938. (buffer-substring (1- (point)) (point))
  8939. (if (> (current-column) org-tags-column)
  8940. " "
  8941. (make-string (- org-tags-column (current-column)) ?\ ))))))
  8942. (org-move-overlay org-tags-overlay ov-start ov-end)
  8943. (save-window-excursion
  8944. (if expert
  8945. (set-buffer (get-buffer-create " *Org tags*"))
  8946. (delete-other-windows)
  8947. (split-window-vertically)
  8948. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  8949. (erase-buffer)
  8950. (org-set-local 'org-done-keywords done-keywords)
  8951. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  8952. (org-fast-tag-insert "Current" current c-face "\n\n")
  8953. (org-fast-tag-show-exit exit-after-next)
  8954. (org-set-current-tags-overlay current ov-prefix)
  8955. (setq tbl fulltable char ?a cnt 0)
  8956. (while (setq e (pop tbl))
  8957. (cond
  8958. ((equal e '(:startgroup))
  8959. (push '() groups) (setq ingroup t)
  8960. (when (not (= cnt 0))
  8961. (setq cnt 0)
  8962. (insert "\n"))
  8963. (insert "{ "))
  8964. ((equal e '(:endgroup))
  8965. (setq ingroup nil cnt 0)
  8966. (insert "}\n"))
  8967. (t
  8968. (setq tg (car e) c2 nil)
  8969. (if (cdr e)
  8970. (setq c (cdr e))
  8971. ;; automatically assign a character.
  8972. (setq c1 (string-to-char
  8973. (downcase (substring
  8974. tg (if (= (string-to-char tg) ?@) 1 0)))))
  8975. (if (or (rassoc c1 ntable) (rassoc c1 table))
  8976. (while (or (rassoc char ntable) (rassoc char table))
  8977. (setq char (1+ char)))
  8978. (setq c2 c1))
  8979. (setq c (or c2 char)))
  8980. (if ingroup (push tg (car groups)))
  8981. (setq tg (org-add-props tg nil 'face
  8982. (cond
  8983. ((not (assoc tg table))
  8984. (org-get-todo-face tg))
  8985. ((member tg current) c-face)
  8986. ((member tg inherited) i-face)
  8987. (t nil))))
  8988. (if (and (= cnt 0) (not ingroup)) (insert " "))
  8989. (insert "[" c "] " tg (make-string
  8990. (- fwidth 4 (length tg)) ?\ ))
  8991. (push (cons tg c) ntable)
  8992. (when (= (setq cnt (1+ cnt)) ncol)
  8993. (insert "\n")
  8994. (if ingroup (insert " "))
  8995. (setq cnt 0)))))
  8996. (setq ntable (nreverse ntable))
  8997. (insert "\n")
  8998. (goto-char (point-min))
  8999. (if (and (not expert) (fboundp 'fit-window-to-buffer))
  9000. (fit-window-to-buffer))
  9001. (setq rtn
  9002. (catch 'exit
  9003. (while t
  9004. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  9005. (if groups " [!] no groups" " [!]groups")
  9006. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  9007. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  9008. (cond
  9009. ((= c ?\r) (throw 'exit t))
  9010. ((= c ?!)
  9011. (setq groups (not groups))
  9012. (goto-char (point-min))
  9013. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  9014. ((= c ?\C-c)
  9015. (if (not expert)
  9016. (org-fast-tag-show-exit
  9017. (setq exit-after-next (not exit-after-next)))
  9018. (setq expert nil)
  9019. (delete-other-windows)
  9020. (split-window-vertically)
  9021. (org-switch-to-buffer-other-window " *Org tags*")
  9022. (and (fboundp 'fit-window-to-buffer)
  9023. (fit-window-to-buffer))))
  9024. ((or (= c ?\C-g)
  9025. (and (= c ?q) (not (rassoc c ntable))))
  9026. (org-detach-overlay org-tags-overlay)
  9027. (setq quit-flag t))
  9028. ((= c ?\ )
  9029. (setq current nil)
  9030. (if exit-after-next (setq exit-after-next 'now)))
  9031. ((= c ?\t)
  9032. (condition-case nil
  9033. (setq tg (completing-read
  9034. "Tag: "
  9035. (or buffer-tags
  9036. (with-current-buffer buf
  9037. (org-get-buffer-tags)))))
  9038. (quit (setq tg "")))
  9039. (when (string-match "\\S-" tg)
  9040. (add-to-list 'buffer-tags (list tg))
  9041. (if (member tg current)
  9042. (setq current (delete tg current))
  9043. (push tg current)))
  9044. (if exit-after-next (setq exit-after-next 'now)))
  9045. ((setq e (rassoc c todo-table) tg (car e))
  9046. (with-current-buffer buf
  9047. (save-excursion (org-todo tg)))
  9048. (if exit-after-next (setq exit-after-next 'now)))
  9049. ((setq e (rassoc c ntable) tg (car e))
  9050. (if (member tg current)
  9051. (setq current (delete tg current))
  9052. (loop for g in groups do
  9053. (if (member tg g)
  9054. (mapc (lambda (x)
  9055. (setq current (delete x current)))
  9056. g)))
  9057. (push tg current))
  9058. (if exit-after-next (setq exit-after-next 'now))))
  9059. ;; Create a sorted list
  9060. (setq current
  9061. (sort current
  9062. (lambda (a b)
  9063. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  9064. (if (eq exit-after-next 'now) (throw 'exit t))
  9065. (goto-char (point-min))
  9066. (beginning-of-line 2)
  9067. (delete-region (point) (point-at-eol))
  9068. (org-fast-tag-insert "Current" current c-face)
  9069. (org-set-current-tags-overlay current ov-prefix)
  9070. (while (re-search-forward
  9071. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  9072. (setq tg (match-string 1))
  9073. (add-text-properties
  9074. (match-beginning 1) (match-end 1)
  9075. (list 'face
  9076. (cond
  9077. ((member tg current) c-face)
  9078. ((member tg inherited) i-face)
  9079. (t (get-text-property (match-beginning 1) 'face))))))
  9080. (goto-char (point-min)))))
  9081. (org-detach-overlay org-tags-overlay)
  9082. (if rtn
  9083. (mapconcat 'identity current ":")
  9084. nil))))
  9085. (defun org-get-tags-string ()
  9086. "Get the TAGS string in the current headline."
  9087. (unless (org-on-heading-p t)
  9088. (error "Not on a heading"))
  9089. (save-excursion
  9090. (beginning-of-line 1)
  9091. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9092. (org-match-string-no-properties 1)
  9093. "")))
  9094. (defun org-get-tags ()
  9095. "Get the list of tags specified in the current headline."
  9096. (org-split-string (org-get-tags-string) ":"))
  9097. (defun org-get-buffer-tags ()
  9098. "Get a table of all tags used in the buffer, for completion."
  9099. (let (tags)
  9100. (save-excursion
  9101. (goto-char (point-min))
  9102. (while (re-search-forward
  9103. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  9104. (when (equal (char-after (point-at-bol 0)) ?*)
  9105. (mapc (lambda (x) (add-to-list 'tags x))
  9106. (org-split-string (org-match-string-no-properties 1) ":")))))
  9107. (mapcar 'list tags)))
  9108. ;;;; Properties
  9109. ;;; Setting and retrieving properties
  9110. (defconst org-special-properties
  9111. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "PRIORITY"
  9112. "TIMESTAMP" "TIMESTAMP_IA")
  9113. "The special properties valid in Org-mode.
  9114. These are properties that are not defined in the property drawer,
  9115. but in some other way.")
  9116. (defconst org-default-properties
  9117. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION"
  9118. "LOCATION" "LOGGING" "COLUMNS")
  9119. "Some properties that are used by Org-mode for various purposes.
  9120. Being in this list makes sure that they are offered for completion.")
  9121. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  9122. "Regular expression matching the first line of a property drawer.")
  9123. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  9124. "Regular expression matching the first line of a property drawer.")
  9125. (defun org-property-action ()
  9126. "Do an action on properties."
  9127. (interactive)
  9128. (let (c)
  9129. (org-at-property-p)
  9130. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  9131. (setq c (read-char-exclusive))
  9132. (cond
  9133. ((equal c ?s)
  9134. (call-interactively 'org-set-property))
  9135. ((equal c ?d)
  9136. (call-interactively 'org-delete-property))
  9137. ((equal c ?D)
  9138. (call-interactively 'org-delete-property-globally))
  9139. ((equal c ?c)
  9140. (call-interactively 'org-compute-property-at-point))
  9141. (t (error "No such property action %c" c)))))
  9142. (defun org-at-property-p ()
  9143. "Is the cursor in a property line?"
  9144. ;; FIXME: Does not check if we are actually in the drawer.
  9145. ;; FIXME: also returns true on any drawers.....
  9146. ;; This is used by C-c C-c for property action.
  9147. (save-excursion
  9148. (beginning-of-line 1)
  9149. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  9150. (defun org-get-property-block (&optional beg end force)
  9151. "Return the (beg . end) range of the body of the property drawer.
  9152. BEG and END can be beginning and end of subtree, if not given
  9153. they will be found.
  9154. If the drawer does not exist and FORCE is non-nil, create the drawer."
  9155. (catch 'exit
  9156. (save-excursion
  9157. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  9158. (end (or end (progn (outline-next-heading) (point)))))
  9159. (goto-char beg)
  9160. (if (re-search-forward org-property-start-re end t)
  9161. (setq beg (1+ (match-end 0)))
  9162. (if force
  9163. (save-excursion
  9164. (org-insert-property-drawer)
  9165. (setq end (progn (outline-next-heading) (point))))
  9166. (throw 'exit nil))
  9167. (goto-char beg)
  9168. (if (re-search-forward org-property-start-re end t)
  9169. (setq beg (1+ (match-end 0)))))
  9170. (if (re-search-forward org-property-end-re end t)
  9171. (setq end (match-beginning 0))
  9172. (or force (throw 'exit nil))
  9173. (goto-char beg)
  9174. (setq end beg)
  9175. (org-indent-line-function)
  9176. (insert ":END:\n"))
  9177. (cons beg end)))))
  9178. (defun org-entry-properties (&optional pom which)
  9179. "Get all properties of the entry at point-or-marker POM.
  9180. This includes the TODO keyword, the tags, time strings for deadline,
  9181. scheduled, and clocking, and any additional properties defined in the
  9182. entry. The return value is an alist, keys may occur multiple times
  9183. if the property key was used several times.
  9184. POM may also be nil, in which case the current entry is used.
  9185. If WHICH is nil or `all', get all properties. If WHICH is
  9186. `special' or `standard', only get that subclass."
  9187. (setq which (or which 'all))
  9188. (org-with-point-at pom
  9189. (let ((clockstr (substring org-clock-string 0 -1))
  9190. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  9191. beg end range props sum-props key value string clocksum)
  9192. (save-excursion
  9193. (when (condition-case nil (org-back-to-heading t) (error nil))
  9194. (setq beg (point))
  9195. (setq sum-props (get-text-property (point) 'org-summaries))
  9196. (setq clocksum (get-text-property (point) :org-clock-minutes))
  9197. (outline-next-heading)
  9198. (setq end (point))
  9199. (when (memq which '(all special))
  9200. ;; Get the special properties, like TODO and tags
  9201. (goto-char beg)
  9202. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  9203. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  9204. (when (looking-at org-priority-regexp)
  9205. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  9206. (when (and (setq value (org-get-tags-string))
  9207. (string-match "\\S-" value))
  9208. (push (cons "TAGS" value) props))
  9209. (when (setq value (org-get-tags-at))
  9210. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  9211. props))
  9212. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  9213. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  9214. string (if (equal key clockstr)
  9215. (org-no-properties
  9216. (org-trim
  9217. (buffer-substring
  9218. (match-beginning 3) (goto-char (point-at-eol)))))
  9219. (substring (org-match-string-no-properties 3) 1 -1)))
  9220. (unless key
  9221. (if (= (char-after (match-beginning 3)) ?\[)
  9222. (setq key "TIMESTAMP_IA")
  9223. (setq key "TIMESTAMP")))
  9224. (when (or (equal key clockstr) (not (assoc key props)))
  9225. (push (cons key string) props)))
  9226. )
  9227. (when (memq which '(all standard))
  9228. ;; Get the standard properties, like :PORP: ...
  9229. (setq range (org-get-property-block beg end))
  9230. (when range
  9231. (goto-char (car range))
  9232. (while (re-search-forward
  9233. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  9234. (cdr range) t)
  9235. (setq key (org-match-string-no-properties 1)
  9236. value (org-trim (or (org-match-string-no-properties 2) "")))
  9237. (unless (member key excluded)
  9238. (push (cons key (or value "")) props)))))
  9239. (if clocksum
  9240. (push (cons "CLOCKSUM"
  9241. (org-column-number-to-string (/ (float clocksum) 60.)
  9242. 'add_times))
  9243. props))
  9244. (append sum-props (nreverse props)))))))
  9245. (defun org-entry-get (pom property &optional inherit)
  9246. "Get value of PROPERTY for entry at point-or-marker POM.
  9247. If INHERIT is non-nil and the entry does not have the property,
  9248. then also check higher levels of the hierarchy.
  9249. If INHERIT is the symbol `selective', use inheritance only if the setting
  9250. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  9251. If the property is present but empty, the return value is the empty string.
  9252. If the property is not present at all, nil is returned."
  9253. (org-with-point-at pom
  9254. (if (and inherit (if (eq inherit 'selective)
  9255. (org-property-inherit-p property)
  9256. t))
  9257. (org-entry-get-with-inheritance property)
  9258. (if (member property org-special-properties)
  9259. ;; We need a special property. Use brute force, get all properties.
  9260. (cdr (assoc property (org-entry-properties nil 'special)))
  9261. (let ((range (org-get-property-block)))
  9262. (if (and range
  9263. (goto-char (car range))
  9264. (re-search-forward
  9265. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)?")
  9266. (cdr range) t))
  9267. ;; Found the property, return it.
  9268. (if (match-end 1)
  9269. (org-match-string-no-properties 1)
  9270. "")))))))
  9271. (defun org-property-or-variable-value (var &optional inherit)
  9272. "Check if there is a property fixing the value of VAR.
  9273. If yes, return this value. If not, return the current value of the variable."
  9274. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  9275. (if (and prop (stringp prop) (string-match "\\S-" prop))
  9276. (read prop)
  9277. (symbol-value var))))
  9278. (defun org-entry-delete (pom property)
  9279. "Delete the property PROPERTY from entry at point-or-marker POM."
  9280. (org-with-point-at pom
  9281. (if (member property org-special-properties)
  9282. nil ; cannot delete these properties.
  9283. (let ((range (org-get-property-block)))
  9284. (if (and range
  9285. (goto-char (car range))
  9286. (re-search-forward
  9287. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)")
  9288. (cdr range) t))
  9289. (progn
  9290. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  9291. t)
  9292. nil)))))
  9293. ;; Multi-values properties are properties that contain multiple values
  9294. ;; These values are assumed to be single words, separated by whitespace.
  9295. (defun org-entry-add-to-multivalued-property (pom property value)
  9296. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  9297. (let* ((old (org-entry-get pom property))
  9298. (values (and old (org-split-string old "[ \t]"))))
  9299. (unless (member value values)
  9300. (setq values (cons value values))
  9301. (org-entry-put pom property
  9302. (mapconcat 'identity values " ")))))
  9303. (defun org-entry-remove-from-multivalued-property (pom property value)
  9304. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  9305. (let* ((old (org-entry-get pom property))
  9306. (values (and old (org-split-string old "[ \t]"))))
  9307. (when (member value values)
  9308. (setq values (delete value values))
  9309. (org-entry-put pom property
  9310. (mapconcat 'identity values " ")))))
  9311. (defun org-entry-member-in-multivalued-property (pom property value)
  9312. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  9313. (let* ((old (org-entry-get pom property))
  9314. (values (and old (org-split-string old "[ \t]"))))
  9315. (member value values)))
  9316. (defvar org-entry-property-inherited-from (make-marker))
  9317. (defun org-entry-get-with-inheritance (property)
  9318. "Get entry property, and search higher levels if not present."
  9319. (let (tmp)
  9320. (save-excursion
  9321. (save-restriction
  9322. (widen)
  9323. (catch 'ex
  9324. (while t
  9325. (when (setq tmp (org-entry-get nil property))
  9326. (org-back-to-heading t)
  9327. (move-marker org-entry-property-inherited-from (point))
  9328. (throw 'ex tmp))
  9329. (or (org-up-heading-safe) (throw 'ex nil)))))
  9330. (or tmp (cdr (assoc property org-local-properties))
  9331. (cdr (assoc property org-global-properties))))))
  9332. (defun org-entry-put (pom property value)
  9333. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  9334. (org-with-point-at pom
  9335. (org-back-to-heading t)
  9336. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  9337. range)
  9338. (cond
  9339. ((equal property "TODO")
  9340. (when (and (stringp value) (string-match "\\S-" value)
  9341. (not (member value org-todo-keywords-1)))
  9342. (error "\"%s\" is not a valid TODO state" value))
  9343. (if (or (not value)
  9344. (not (string-match "\\S-" value)))
  9345. (setq value 'none))
  9346. (org-todo value)
  9347. (org-set-tags nil 'align))
  9348. ((equal property "PRIORITY")
  9349. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  9350. (string-to-char value) ?\ ))
  9351. (org-set-tags nil 'align))
  9352. ((equal property "SCHEDULED")
  9353. (if (re-search-forward org-scheduled-time-regexp end t)
  9354. (cond
  9355. ((eq value 'earlier) (org-timestamp-change -1 'day))
  9356. ((eq value 'later) (org-timestamp-change 1 'day))
  9357. (t (call-interactively 'org-schedule)))
  9358. (call-interactively 'org-schedule)))
  9359. ((equal property "DEADLINE")
  9360. (if (re-search-forward org-deadline-time-regexp end t)
  9361. (cond
  9362. ((eq value 'earlier) (org-timestamp-change -1 'day))
  9363. ((eq value 'later) (org-timestamp-change 1 'day))
  9364. (t (call-interactively 'org-deadline)))
  9365. (call-interactively 'org-deadline)))
  9366. ((member property org-special-properties)
  9367. (error "The %s property can not yet be set with `org-entry-put'"
  9368. property))
  9369. (t ; a non-special property
  9370. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  9371. (setq range (org-get-property-block beg end 'force))
  9372. (goto-char (car range))
  9373. (if (re-search-forward
  9374. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  9375. (progn
  9376. (delete-region (match-beginning 1) (match-end 1))
  9377. (goto-char (match-beginning 1)))
  9378. (goto-char (cdr range))
  9379. (insert "\n")
  9380. (backward-char 1)
  9381. (org-indent-line-function)
  9382. (insert ":" property ":"))
  9383. (and value (insert " " value))
  9384. (org-indent-line-function)))))))
  9385. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  9386. "Get all property keys in the current buffer.
  9387. With INCLUDE-SPECIALS, also list the special properties that relect things
  9388. like tags and TODO state.
  9389. With INCLUDE-DEFAULTS, also include properties that has special meaning
  9390. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  9391. With INCLUDE-COLUMNS, also include property names given in COLUMN
  9392. formats in the current buffer."
  9393. (let (rtn range cfmt cols s p)
  9394. (save-excursion
  9395. (save-restriction
  9396. (widen)
  9397. (goto-char (point-min))
  9398. (while (re-search-forward org-property-start-re nil t)
  9399. (setq range (org-get-property-block))
  9400. (goto-char (car range))
  9401. (while (re-search-forward
  9402. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  9403. (cdr range) t)
  9404. (add-to-list 'rtn (org-match-string-no-properties 1)))
  9405. (outline-next-heading))))
  9406. (when include-specials
  9407. (setq rtn (append org-special-properties rtn)))
  9408. (when include-defaults
  9409. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties))
  9410. (when include-columns
  9411. (save-excursion
  9412. (save-restriction
  9413. (widen)
  9414. (goto-char (point-min))
  9415. (while (re-search-forward
  9416. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  9417. nil t)
  9418. (setq cfmt (match-string 2) s 0)
  9419. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  9420. cfmt s)
  9421. (setq s (match-end 0)
  9422. p (match-string 1 cfmt))
  9423. (unless (or (equal p "ITEM")
  9424. (member p org-special-properties))
  9425. (add-to-list 'rtn (match-string 1 cfmt))))))))
  9426. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  9427. (defun org-property-values (key)
  9428. "Return a list of all values of property KEY."
  9429. (save-excursion
  9430. (save-restriction
  9431. (widen)
  9432. (goto-char (point-min))
  9433. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  9434. values)
  9435. (while (re-search-forward re nil t)
  9436. (add-to-list 'values (org-trim (match-string 1))))
  9437. (delete "" values)))))
  9438. (defun org-insert-property-drawer ()
  9439. "Insert a property drawer into the current entry."
  9440. (interactive)
  9441. (org-back-to-heading t)
  9442. (looking-at outline-regexp)
  9443. (let ((indent (- (match-end 0)(match-beginning 0)))
  9444. (beg (point))
  9445. (re (concat "^[ \t]*" org-keyword-time-regexp))
  9446. end hiddenp)
  9447. (outline-next-heading)
  9448. (setq end (point))
  9449. (goto-char beg)
  9450. (while (re-search-forward re end t))
  9451. (setq hiddenp (org-invisible-p))
  9452. (end-of-line 1)
  9453. (and (equal (char-after) ?\n) (forward-char 1))
  9454. (org-skip-over-state-notes)
  9455. (skip-chars-backward " \t\n\r")
  9456. (if (eq (char-before) ?*) (forward-char 1))
  9457. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  9458. (beginning-of-line 0)
  9459. (indent-to-column indent)
  9460. (beginning-of-line 2)
  9461. (indent-to-column indent)
  9462. (beginning-of-line 0)
  9463. (if hiddenp
  9464. (save-excursion
  9465. (org-back-to-heading t)
  9466. (hide-entry))
  9467. (org-flag-drawer t))))
  9468. (defun org-set-property (property value)
  9469. "In the current entry, set PROPERTY to VALUE.
  9470. When called interactively, this will prompt for a property name, offering
  9471. completion on existing and default properties. And then it will prompt
  9472. for a value, offering competion either on allowed values (via an inherited
  9473. xxx_ALL property) or on existing values in other instances of this property
  9474. in the current file."
  9475. (interactive
  9476. (let* ((prop (completing-read
  9477. "Property: " (mapcar 'list (org-buffer-property-keys nil t t))))
  9478. (cur (org-entry-get nil prop))
  9479. (allowed (org-property-get-allowed-values nil prop 'table))
  9480. (existing (mapcar 'list (org-property-values prop)))
  9481. (val (if allowed
  9482. (completing-read "Value: " allowed nil 'req-match)
  9483. (completing-read
  9484. (concat "Value" (if (and cur (string-match "\\S-" cur))
  9485. (concat "[" cur "]") "")
  9486. ": ")
  9487. existing nil nil "" nil cur))))
  9488. (list prop (if (equal val "") cur val))))
  9489. (unless (equal (org-entry-get nil property) value)
  9490. (org-entry-put nil property value)))
  9491. (defun org-delete-property (property)
  9492. "In the current entry, delete PROPERTY."
  9493. (interactive
  9494. (let* ((prop (completing-read
  9495. "Property: " (org-entry-properties nil 'standard))))
  9496. (list prop)))
  9497. (message "Property %s %s" property
  9498. (if (org-entry-delete nil property)
  9499. "deleted"
  9500. "was not present in the entry")))
  9501. (defun org-delete-property-globally (property)
  9502. "Remove PROPERTY globally, from all entries."
  9503. (interactive
  9504. (let* ((prop (completing-read
  9505. "Globally remove property: "
  9506. (mapcar 'list (org-buffer-property-keys)))))
  9507. (list prop)))
  9508. (save-excursion
  9509. (save-restriction
  9510. (widen)
  9511. (goto-char (point-min))
  9512. (let ((cnt 0))
  9513. (while (re-search-forward
  9514. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  9515. nil t)
  9516. (setq cnt (1+ cnt))
  9517. (replace-match ""))
  9518. (message "Property \"%s\" removed from %d entries" property cnt)))))
  9519. (defvar org-columns-current-fmt-compiled) ; defined below
  9520. (defun org-compute-property-at-point ()
  9521. "Compute the property at point.
  9522. This looks for an enclosing column format, extracts the operator and
  9523. then applies it to the proerty in the column format's scope."
  9524. (interactive)
  9525. (unless (org-at-property-p)
  9526. (error "Not at a property"))
  9527. (let ((prop (org-match-string-no-properties 2)))
  9528. (org-columns-get-format-and-top-level)
  9529. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  9530. (error "No operator defined for property %s" prop))
  9531. (org-columns-compute prop)))
  9532. (defun org-property-get-allowed-values (pom property &optional table)
  9533. "Get allowed values for the property PROPERTY.
  9534. When TABLE is non-nil, return an alist that can directly be used for
  9535. completion."
  9536. (let (vals)
  9537. (cond
  9538. ((equal property "TODO")
  9539. (setq vals (org-with-point-at pom
  9540. (append org-todo-keywords-1 '("")))))
  9541. ((equal property "PRIORITY")
  9542. (let ((n org-lowest-priority))
  9543. (while (>= n org-highest-priority)
  9544. (push (char-to-string n) vals)
  9545. (setq n (1- n)))))
  9546. ((member property org-special-properties))
  9547. (t
  9548. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  9549. (when (and vals (string-match "\\S-" vals))
  9550. (setq vals (car (read-from-string (concat "(" vals ")"))))
  9551. (setq vals (mapcar (lambda (x)
  9552. (cond ((stringp x) x)
  9553. ((numberp x) (number-to-string x))
  9554. ((symbolp x) (symbol-name x))
  9555. (t "???")))
  9556. vals)))))
  9557. (if table (mapcar 'list vals) vals)))
  9558. (defun org-property-previous-allowed-value (&optional previous)
  9559. "Switch to the next allowed value for this property."
  9560. (interactive)
  9561. (org-property-next-allowed-value t))
  9562. (defun org-property-next-allowed-value (&optional previous)
  9563. "Switch to the next allowed value for this property."
  9564. (interactive)
  9565. (unless (org-at-property-p)
  9566. (error "Not at a property"))
  9567. (let* ((key (match-string 2))
  9568. (value (match-string 3))
  9569. (allowed (or (org-property-get-allowed-values (point) key)
  9570. (and (member value '("[ ]" "[-]" "[X]"))
  9571. '("[ ]" "[X]"))))
  9572. nval)
  9573. (unless allowed
  9574. (error "Allowed values for this property have not been defined"))
  9575. (if previous (setq allowed (reverse allowed)))
  9576. (if (member value allowed)
  9577. (setq nval (car (cdr (member value allowed)))))
  9578. (setq nval (or nval (car allowed)))
  9579. (if (equal nval value)
  9580. (error "Only one allowed value for this property"))
  9581. (org-at-property-p)
  9582. (replace-match (concat " :" key ": " nval) t t)
  9583. (org-indent-line-function)
  9584. (beginning-of-line 1)
  9585. (skip-chars-forward " \t")))
  9586. (defun org-find-entry-with-id (ident)
  9587. "Locate the entry that contains the ID property with exact value IDENT.
  9588. IDENT can be a string, a symbol or a number, this function will search for
  9589. the string representation of it.
  9590. Return the position where this entry starts, or nil if there is no such entry."
  9591. (let ((id (cond
  9592. ((stringp ident) ident)
  9593. ((symbol-name ident) (symbol-name ident))
  9594. ((numberp ident) (number-to-string ident))
  9595. (t (error "IDENT %s must be a string, symbol or number" ident))))
  9596. (case-fold-search nil))
  9597. (save-excursion
  9598. (save-restriction
  9599. (widen)
  9600. (goto-char (point-min))
  9601. (when (re-search-forward
  9602. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  9603. nil t)
  9604. (org-back-to-heading)
  9605. (point))))))
  9606. ;;; Column View
  9607. (defvar org-columns-overlays nil
  9608. "Holds the list of current column overlays.")
  9609. (defvar org-columns-current-fmt nil
  9610. "Local variable, holds the currently active column format.")
  9611. (defvar org-columns-current-fmt-compiled nil
  9612. "Local variable, holds the currently active column format.
  9613. This is the compiled version of the format.")
  9614. (defvar org-columns-current-widths nil
  9615. "Loval variable, holds the currently widths of fields.")
  9616. (defvar org-columns-current-maxwidths nil
  9617. "Loval variable, holds the currently active maximum column widths.")
  9618. (defvar org-columns-begin-marker (make-marker)
  9619. "Points to the position where last a column creation command was called.")
  9620. (defvar org-columns-top-level-marker (make-marker)
  9621. "Points to the position where current columns region starts.")
  9622. (defvar org-columns-map (make-sparse-keymap)
  9623. "The keymap valid in column display.")
  9624. (defun org-columns-content ()
  9625. "Switch to contents view while in columns view."
  9626. (interactive)
  9627. (org-overview)
  9628. (org-content))
  9629. (org-defkey org-columns-map "c" 'org-columns-content)
  9630. (org-defkey org-columns-map "o" 'org-overview)
  9631. (org-defkey org-columns-map "e" 'org-columns-edit-value)
  9632. (org-defkey org-columns-map "\C-c\C-t" 'org-columns-todo)
  9633. (org-defkey org-columns-map "\C-c\C-c" 'org-columns-set-tags-or-toggle)
  9634. (org-defkey org-columns-map "\C-c\C-o" 'org-columns-open-link)
  9635. (org-defkey org-columns-map "v" 'org-columns-show-value)
  9636. (org-defkey org-columns-map "q" 'org-columns-quit)
  9637. (org-defkey org-columns-map "r" 'org-columns-redo)
  9638. (org-defkey org-columns-map "g" 'org-columns-redo)
  9639. (org-defkey org-columns-map [left] 'backward-char)
  9640. (org-defkey org-columns-map "\M-b" 'backward-char)
  9641. (org-defkey org-columns-map "a" 'org-columns-edit-allowed)
  9642. (org-defkey org-columns-map "s" 'org-columns-edit-attributes)
  9643. (org-defkey org-columns-map "\M-f" (lambda () (interactive) (goto-char (1+ (point)))))
  9644. (org-defkey org-columns-map [right] (lambda () (interactive) (goto-char (1+ (point)))))
  9645. (org-defkey org-columns-map [(shift right)] 'org-columns-next-allowed-value)
  9646. (org-defkey org-columns-map "n" 'org-columns-next-allowed-value)
  9647. (org-defkey org-columns-map [(shift left)] 'org-columns-previous-allowed-value)
  9648. (org-defkey org-columns-map "p" 'org-columns-previous-allowed-value)
  9649. (org-defkey org-columns-map "<" 'org-columns-narrow)
  9650. (org-defkey org-columns-map ">" 'org-columns-widen)
  9651. (org-defkey org-columns-map [(meta right)] 'org-columns-move-right)
  9652. (org-defkey org-columns-map [(meta left)] 'org-columns-move-left)
  9653. (org-defkey org-columns-map [(shift meta right)] 'org-columns-new)
  9654. (org-defkey org-columns-map [(shift meta left)] 'org-columns-delete)
  9655. (easy-menu-define org-columns-menu org-columns-map "Org Column Menu"
  9656. '("Column"
  9657. ["Edit property" org-columns-edit-value t]
  9658. ["Next allowed value" org-columns-next-allowed-value t]
  9659. ["Previous allowed value" org-columns-previous-allowed-value t]
  9660. ["Show full value" org-columns-show-value t]
  9661. ["Edit allowed values" org-columns-edit-allowed t]
  9662. "--"
  9663. ["Edit column attributes" org-columns-edit-attributes t]
  9664. ["Increase column width" org-columns-widen t]
  9665. ["Decrease column width" org-columns-narrow t]
  9666. "--"
  9667. ["Move column right" org-columns-move-right t]
  9668. ["Move column left" org-columns-move-left t]
  9669. ["Add column" org-columns-new t]
  9670. ["Delete column" org-columns-delete t]
  9671. "--"
  9672. ["CONTENTS" org-columns-content t]
  9673. ["OVERVIEW" org-overview t]
  9674. ["Refresh columns display" org-columns-redo t]
  9675. "--"
  9676. ["Open link" org-columns-open-link t]
  9677. "--"
  9678. ["Quit" org-columns-quit t]))
  9679. (defun org-columns-new-overlay (beg end &optional string face)
  9680. "Create a new column overlay and add it to the list."
  9681. (let ((ov (org-make-overlay beg end)))
  9682. (org-overlay-put ov 'face (or face 'secondary-selection))
  9683. (org-overlay-display ov string face)
  9684. (push ov org-columns-overlays)
  9685. ov))
  9686. (defun org-columns-display-here (&optional props)
  9687. "Overlay the current line with column display."
  9688. (interactive)
  9689. (let* ((fmt org-columns-current-fmt-compiled)
  9690. (beg (point-at-bol))
  9691. (level-face (save-excursion
  9692. (beginning-of-line 1)
  9693. (and (looking-at "\\(\\**\\)\\(\\* \\)")
  9694. (org-get-level-face 2))))
  9695. (color (list :foreground
  9696. (face-attribute (or level-face 'default) :foreground)))
  9697. props pom property ass width f string ov column val modval)
  9698. ;; Check if the entry is in another buffer.
  9699. (unless props
  9700. (if (eq major-mode 'org-agenda-mode)
  9701. (setq pom (or (get-text-property (point) 'org-hd-marker)
  9702. (get-text-property (point) 'org-marker))
  9703. props (if pom (org-entry-properties pom) nil))
  9704. (setq props (org-entry-properties nil))))
  9705. ;; Walk the format
  9706. (while (setq column (pop fmt))
  9707. (setq property (car column)
  9708. ass (if (equal property "ITEM")
  9709. (cons "ITEM"
  9710. (save-match-data
  9711. (org-no-properties
  9712. (org-remove-tabs
  9713. (buffer-substring-no-properties
  9714. (point-at-bol) (point-at-eol))))))
  9715. (assoc property props))
  9716. width (or (cdr (assoc property org-columns-current-maxwidths))
  9717. (nth 2 column)
  9718. (length property))
  9719. f (format "%%-%d.%ds | " width width)
  9720. val (or (cdr ass) "")
  9721. modval (if (equal property "ITEM")
  9722. (org-columns-cleanup-item val org-columns-current-fmt-compiled))
  9723. string (format f (or modval val)))
  9724. ;; Create the overlay
  9725. (org-unmodified
  9726. (setq ov (org-columns-new-overlay
  9727. beg (setq beg (1+ beg)) string
  9728. (list color 'org-column)))
  9729. (org-overlay-put ov 'keymap org-columns-map)
  9730. (org-overlay-put ov 'org-columns-key property)
  9731. (org-overlay-put ov 'org-columns-value (cdr ass))
  9732. (org-overlay-put ov 'org-columns-value-modified modval)
  9733. (org-overlay-put ov 'org-columns-pom pom)
  9734. (org-overlay-put ov 'org-columns-format f))
  9735. (if (or (not (char-after beg))
  9736. (equal (char-after beg) ?\n))
  9737. (let ((inhibit-read-only t))
  9738. (save-excursion
  9739. (goto-char beg)
  9740. (org-unmodified (insert " ")))))) ;; FIXME: add props and remove later?
  9741. ;; Make the rest of the line disappear.
  9742. (org-unmodified
  9743. (setq ov (org-columns-new-overlay beg (point-at-eol)))
  9744. (org-overlay-put ov 'invisible t)
  9745. (org-overlay-put ov 'keymap org-columns-map)
  9746. (org-overlay-put ov 'intangible t)
  9747. (push ov org-columns-overlays)
  9748. (setq ov (org-make-overlay (1- (point-at-eol)) (1+ (point-at-eol))))
  9749. (org-overlay-put ov 'keymap org-columns-map)
  9750. (push ov org-columns-overlays)
  9751. (let ((inhibit-read-only t))
  9752. (put-text-property (max (point-min) (1- (point-at-bol)))
  9753. (min (point-max) (1+ (point-at-eol)))
  9754. 'read-only "Type `e' to edit property")))))
  9755. (defvar org-columns-full-header-line-format nil
  9756. "Fthe full header line format, will be shifted by horizontal scrolling." )
  9757. (defvar org-previous-header-line-format nil
  9758. "The header line format before column view was turned on.")
  9759. (defvar org-columns-inhibit-recalculation nil
  9760. "Inhibit recomputing of columns on column view startup.")
  9761. (defvar header-line-format)
  9762. (defvar org-columns-previous-hscroll 0)
  9763. (defun org-columns-display-here-title ()
  9764. "Overlay the newline before the current line with the table title."
  9765. (interactive)
  9766. (let ((fmt org-columns-current-fmt-compiled)
  9767. string (title "")
  9768. property width f column str widths)
  9769. (while (setq column (pop fmt))
  9770. (setq property (car column)
  9771. str (or (nth 1 column) property)
  9772. width (or (cdr (assoc property org-columns-current-maxwidths))
  9773. (nth 2 column)
  9774. (length str))
  9775. widths (push width widths)
  9776. f (format "%%-%d.%ds | " width width)
  9777. string (format f str)
  9778. title (concat title string)))
  9779. (setq title (concat
  9780. (org-add-props " " nil 'display '(space :align-to 0))
  9781. (org-add-props title nil 'face '(:weight bold :underline t :inherit default))))
  9782. (org-set-local 'org-previous-header-line-format header-line-format)
  9783. (org-set-local 'org-columns-current-widths (nreverse widths))
  9784. (setq org-columns-full-header-line-format title)
  9785. (setq org-columns-previous-hscroll -1)
  9786. ; (org-columns-hscoll-title)
  9787. (org-add-hook 'post-command-hook 'org-columns-hscoll-title nil 'local)))
  9788. (defun org-columns-hscoll-title ()
  9789. "Set the header-line-format so that it scrolls along with the table."
  9790. (sit-for .0001) ; need to force a redisplay to update window-hscroll
  9791. (when (not (= (window-hscroll) org-columns-previous-hscroll))
  9792. (setq header-line-format
  9793. (concat (substring org-columns-full-header-line-format 0 1)
  9794. (substring org-columns-full-header-line-format
  9795. (1+ (window-hscroll))))
  9796. org-columns-previous-hscroll (window-hscroll))
  9797. (force-mode-line-update)))
  9798. (defun org-columns-remove-overlays ()
  9799. "Remove all currently active column overlays."
  9800. (interactive)
  9801. (when (marker-buffer org-columns-begin-marker)
  9802. (with-current-buffer (marker-buffer org-columns-begin-marker)
  9803. (when (local-variable-p 'org-previous-header-line-format)
  9804. (setq header-line-format org-previous-header-line-format)
  9805. (kill-local-variable 'org-previous-header-line-format)
  9806. (remove-hook 'post-command-hook 'org-columns-hscoll-title 'local))
  9807. (move-marker org-columns-begin-marker nil)
  9808. (move-marker org-columns-top-level-marker nil)
  9809. (org-unmodified
  9810. (mapc 'org-delete-overlay org-columns-overlays)
  9811. (setq org-columns-overlays nil)
  9812. (let ((inhibit-read-only t))
  9813. (remove-text-properties (point-min) (point-max) '(read-only t)))))))
  9814. (defun org-columns-cleanup-item (item fmt)
  9815. "Remove from ITEM what is a column in the format FMT."
  9816. (if (not org-complex-heading-regexp)
  9817. item
  9818. (when (string-match org-complex-heading-regexp item)
  9819. (concat
  9820. (org-add-props (concat (match-string 1 item) " ") nil
  9821. 'org-whitespace (* 2 (1- (org-reduced-level (- (match-end 1) (match-beginning 1))))))
  9822. (and (match-end 2) (not (assoc "TODO" fmt)) (concat " " (match-string 2 item)))
  9823. (and (match-end 3) (not (assoc "PRIORITY" fmt)) (concat " " (match-string 3 item)))
  9824. " " (match-string 4 item)
  9825. (and (match-end 5) (not (assoc "TAGS" fmt)) (concat " " (match-string 5 item)))))))
  9826. (defun org-columns-show-value ()
  9827. "Show the full value of the property."
  9828. (interactive)
  9829. (let ((value (get-char-property (point) 'org-columns-value)))
  9830. (message "Value is: %s" (or value ""))))
  9831. (defun org-columns-quit ()
  9832. "Remove the column overlays and in this way exit column editing."
  9833. (interactive)
  9834. (org-unmodified
  9835. (org-columns-remove-overlays)
  9836. (let ((inhibit-read-only t))
  9837. (remove-text-properties (point-min) (point-max) '(read-only t))))
  9838. (when (eq major-mode 'org-agenda-mode)
  9839. (message
  9840. "Modification not yet reflected in Agenda buffer, use `r' to refresh")))
  9841. (defun org-columns-check-computed ()
  9842. "Check if this column value is computed.
  9843. If yes, throw an error indicating that changing it does not make sense."
  9844. (let ((val (get-char-property (point) 'org-columns-value)))
  9845. (when (and (stringp val)
  9846. (get-char-property 0 'org-computed val))
  9847. (error "This value is computed from the entry's children"))))
  9848. (defun org-columns-todo (&optional arg)
  9849. "Change the TODO state during column view."
  9850. (interactive "P")
  9851. (org-columns-edit-value "TODO"))
  9852. (defun org-columns-set-tags-or-toggle (&optional arg)
  9853. "Toggle checkbox at point, or set tags for current headline."
  9854. (interactive "P")
  9855. (if (string-match "\\`\\[[ xX-]\\]\\'"
  9856. (get-char-property (point) 'org-columns-value))
  9857. (org-columns-next-allowed-value)
  9858. (org-columns-edit-value "TAGS")))
  9859. (defun org-columns-edit-value (&optional key)
  9860. "Edit the value of the property at point in column view.
  9861. Where possible, use the standard interface for changing this line."
  9862. (interactive)
  9863. (org-columns-check-computed)
  9864. (let* ((external-key key)
  9865. (col (current-column))
  9866. (key (or key (get-char-property (point) 'org-columns-key)))
  9867. (value (get-char-property (point) 'org-columns-value))
  9868. (bol (point-at-bol)) (eol (point-at-eol))
  9869. (pom (or (get-text-property bol 'org-hd-marker)
  9870. (point))) ; keep despite of compiler waring
  9871. (line-overlays
  9872. (delq nil (mapcar (lambda (x)
  9873. (and (eq (overlay-buffer x) (current-buffer))
  9874. (>= (overlay-start x) bol)
  9875. (<= (overlay-start x) eol)
  9876. x))
  9877. org-columns-overlays)))
  9878. nval eval allowed)
  9879. (cond
  9880. ((equal key "CLOCKSUM")
  9881. (error "This special column cannot be edited"))
  9882. ((equal key "ITEM")
  9883. (setq eval '(org-with-point-at pom
  9884. (org-edit-headline))))
  9885. ((equal key "TODO")
  9886. (setq eval '(org-with-point-at pom
  9887. (let ((current-prefix-arg
  9888. (if external-key current-prefix-arg '(4))))
  9889. (call-interactively 'org-todo)))))
  9890. ((equal key "PRIORITY")
  9891. (setq eval '(org-with-point-at pom
  9892. (call-interactively 'org-priority))))
  9893. ((equal key "TAGS")
  9894. (setq eval '(org-with-point-at pom
  9895. (let ((org-fast-tag-selection-single-key
  9896. (if (eq org-fast-tag-selection-single-key 'expert)
  9897. t org-fast-tag-selection-single-key)))
  9898. (call-interactively 'org-set-tags)))))
  9899. ((equal key "DEADLINE")
  9900. (setq eval '(org-with-point-at pom
  9901. (call-interactively 'org-deadline))))
  9902. ((equal key "SCHEDULED")
  9903. (setq eval '(org-with-point-at pom
  9904. (call-interactively 'org-schedule))))
  9905. (t
  9906. (setq allowed (org-property-get-allowed-values pom key 'table))
  9907. (if allowed
  9908. (setq nval (completing-read "Value: " allowed nil t))
  9909. (setq nval (read-string "Edit: " value)))
  9910. (setq nval (org-trim nval))
  9911. (when (not (equal nval value))
  9912. (setq eval '(org-entry-put pom key nval)))))
  9913. (when eval
  9914. (let ((inhibit-read-only t))
  9915. (remove-text-properties (max (point-min) (1- bol)) eol '(read-only t))
  9916. (unwind-protect
  9917. (progn
  9918. (setq org-columns-overlays
  9919. (org-delete-all line-overlays org-columns-overlays))
  9920. (mapc 'org-delete-overlay line-overlays)
  9921. (org-columns-eval eval))
  9922. (org-columns-display-here))))
  9923. (move-to-column col)
  9924. (if (and (org-mode-p)
  9925. (nth 3 (assoc key org-columns-current-fmt-compiled)))
  9926. (org-columns-update key))))
  9927. (defun org-edit-headline () ; FIXME: this is not columns specific. Make interactive????? Use from agenda????
  9928. "Edit the current headline, the part without TODO keyword, TAGS."
  9929. (org-back-to-heading)
  9930. (when (looking-at org-todo-line-regexp)
  9931. (let ((pre (buffer-substring (match-beginning 0) (match-beginning 3)))
  9932. (txt (match-string 3))
  9933. (post "")
  9934. txt2)
  9935. (if (string-match (org-re "[ \t]+:[[:alnum:]:_@]+:[ \t]*$") txt)
  9936. (setq post (match-string 0 txt)
  9937. txt (substring txt 0 (match-beginning 0))))
  9938. (setq txt2 (read-string "Edit: " txt))
  9939. (when (not (equal txt txt2))
  9940. (beginning-of-line 1)
  9941. (insert pre txt2 post)
  9942. (delete-region (point) (point-at-eol))
  9943. (org-set-tags nil t)))))
  9944. (defun org-columns-edit-allowed ()
  9945. "Edit the list of allowed values for the current property."
  9946. (interactive)
  9947. (let* ((key (get-char-property (point) 'org-columns-key))
  9948. (key1 (concat key "_ALL"))
  9949. (allowed (org-entry-get (point) key1 t))
  9950. nval)
  9951. ;; FIXME: Cover editing TODO, TAGS etc in-buffer settings.????
  9952. (setq nval (read-string "Allowed: " allowed))
  9953. (org-entry-put
  9954. (cond ((marker-position org-entry-property-inherited-from)
  9955. org-entry-property-inherited-from)
  9956. ((marker-position org-columns-top-level-marker)
  9957. org-columns-top-level-marker))
  9958. key1 nval)))
  9959. (defun org-columns-eval (form)
  9960. (let (hidep)
  9961. (save-excursion
  9962. (beginning-of-line 1)
  9963. ;; `next-line' is needed here, because it skips invisible line.
  9964. (condition-case nil (org-no-warnings (next-line 1)) (error nil))
  9965. (setq hidep (org-on-heading-p 1)))
  9966. (eval form)
  9967. (and hidep (hide-entry))))
  9968. (defun org-columns-previous-allowed-value ()
  9969. "Switch to the previous allowed value for this column."
  9970. (interactive)
  9971. (org-columns-next-allowed-value t))
  9972. (defun org-columns-next-allowed-value (&optional previous)
  9973. "Switch to the next allowed value for this column."
  9974. (interactive)
  9975. (org-columns-check-computed)
  9976. (let* ((col (current-column))
  9977. (key (get-char-property (point) 'org-columns-key))
  9978. (value (get-char-property (point) 'org-columns-value))
  9979. (bol (point-at-bol)) (eol (point-at-eol))
  9980. (pom (or (get-text-property bol 'org-hd-marker)
  9981. (point))) ; keep despite of compiler waring
  9982. (line-overlays
  9983. (delq nil (mapcar (lambda (x)
  9984. (and (eq (overlay-buffer x) (current-buffer))
  9985. (>= (overlay-start x) bol)
  9986. (<= (overlay-start x) eol)
  9987. x))
  9988. org-columns-overlays)))
  9989. (allowed (or (org-property-get-allowed-values pom key)
  9990. (and (memq
  9991. (nth 4 (assoc key org-columns-current-fmt-compiled))
  9992. '(checkbox checkbox-n-of-m checkbox-percent))
  9993. '("[ ]" "[X]"))))
  9994. nval)
  9995. (when (equal key "ITEM")
  9996. (error "Cannot edit item headline from here"))
  9997. (unless (or allowed (member key '("SCHEDULED" "DEADLINE")))
  9998. (error "Allowed values for this property have not been defined"))
  9999. (if (member key '("SCHEDULED" "DEADLINE"))
  10000. (setq nval (if previous 'earlier 'later))
  10001. (if previous (setq allowed (reverse allowed)))
  10002. (if (member value allowed)
  10003. (setq nval (car (cdr (member value allowed)))))
  10004. (setq nval (or nval (car allowed)))
  10005. (if (equal nval value)
  10006. (error "Only one allowed value for this property")))
  10007. (let ((inhibit-read-only t))
  10008. (remove-text-properties (1- bol) eol '(read-only t))
  10009. (unwind-protect
  10010. (progn
  10011. (setq org-columns-overlays
  10012. (org-delete-all line-overlays org-columns-overlays))
  10013. (mapc 'org-delete-overlay line-overlays)
  10014. (org-columns-eval '(org-entry-put pom key nval)))
  10015. (org-columns-display-here)))
  10016. (move-to-column col)
  10017. (if (and (org-mode-p)
  10018. (nth 3 (assoc key org-columns-current-fmt-compiled)))
  10019. (org-columns-update key))))
  10020. (defun org-verify-version (task)
  10021. (cond
  10022. ((eq task 'columns)
  10023. (if (or (featurep 'xemacs)
  10024. (< emacs-major-version 22))
  10025. (error "Emacs 22 is required for the columns feature")))))
  10026. (defun org-columns-open-link (&optional arg)
  10027. (interactive "P")
  10028. (let ((value (get-char-property (point) 'org-columns-value)))
  10029. (org-open-link-from-string value arg)))
  10030. (defun org-open-link-from-string (s &optional arg)
  10031. "Open a link in the string S, as if it was in Org-mode."
  10032. (interactive)
  10033. (with-temp-buffer
  10034. (let ((org-inhibit-startup t))
  10035. (org-mode)
  10036. (insert s)
  10037. (goto-char (point-min))
  10038. (org-open-at-point arg))))
  10039. (defun org-columns-get-format-and-top-level ()
  10040. (let (fmt)
  10041. (when (condition-case nil (org-back-to-heading) (error nil))
  10042. (move-marker org-entry-property-inherited-from nil)
  10043. (setq fmt (org-entry-get nil "COLUMNS" t)))
  10044. (setq fmt (or fmt org-columns-default-format))
  10045. (org-set-local 'org-columns-current-fmt fmt)
  10046. (org-columns-compile-format fmt)
  10047. (if (marker-position org-entry-property-inherited-from)
  10048. (move-marker org-columns-top-level-marker
  10049. org-entry-property-inherited-from)
  10050. (move-marker org-columns-top-level-marker (point)))
  10051. fmt))
  10052. (defun org-columns ()
  10053. "Turn on column view on an org-mode file."
  10054. (interactive)
  10055. (org-verify-version 'columns)
  10056. (org-columns-remove-overlays)
  10057. (move-marker org-columns-begin-marker (point))
  10058. (let (beg end fmt cache maxwidths)
  10059. (setq fmt (org-columns-get-format-and-top-level))
  10060. (save-excursion
  10061. (goto-char org-columns-top-level-marker)
  10062. (setq beg (point))
  10063. (unless org-columns-inhibit-recalculation
  10064. (org-columns-compute-all))
  10065. (setq end (or (condition-case nil (org-end-of-subtree t t) (error nil))
  10066. (point-max)))
  10067. ;; Get and cache the properties
  10068. (goto-char beg)
  10069. (when (assoc "CLOCKSUM" org-columns-current-fmt-compiled)
  10070. (save-excursion
  10071. (save-restriction
  10072. (narrow-to-region beg end)
  10073. (org-clock-sum))))
  10074. (while (re-search-forward (concat "^" outline-regexp) end t)
  10075. (push (cons (org-current-line) (org-entry-properties)) cache))
  10076. (when cache
  10077. (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
  10078. (org-set-local 'org-columns-current-maxwidths maxwidths)
  10079. (org-columns-display-here-title)
  10080. (mapc (lambda (x)
  10081. (goto-line (car x))
  10082. (org-columns-display-here (cdr x)))
  10083. cache)))))
  10084. (defun org-columns-new (&optional prop title width op fmt &rest rest)
  10085. "Insert a new column, to the left of the current column."
  10086. (interactive)
  10087. (let ((editp (and prop (assoc prop org-columns-current-fmt-compiled)))
  10088. cell)
  10089. (setq prop (completing-read
  10090. "Property: " (mapcar 'list (org-buffer-property-keys t nil t))
  10091. nil nil prop))
  10092. (setq title (read-string (concat "Column title [" prop "]: ") (or title prop)))
  10093. (setq width (read-string "Column width: " (if width (number-to-string width))))
  10094. (if (string-match "\\S-" width)
  10095. (setq width (string-to-number width))
  10096. (setq width nil))
  10097. (setq fmt (completing-read "Summary [none]: "
  10098. '(("none") ("add_numbers") ("currency") ("add_times") ("checkbox") ("checkbox-n-of-m") ("checkbox-percent"))
  10099. nil t))
  10100. (if (string-match "\\S-" fmt)
  10101. (setq fmt (intern fmt))
  10102. (setq fmt nil))
  10103. (if (eq fmt 'none) (setq fmt nil))
  10104. (if editp
  10105. (progn
  10106. (setcar editp prop)
  10107. (setcdr editp (list title width nil fmt)))
  10108. (setq cell (nthcdr (1- (current-column))
  10109. org-columns-current-fmt-compiled))
  10110. (setcdr cell (cons (list prop title width nil fmt)
  10111. (cdr cell))))
  10112. (org-columns-store-format)
  10113. (org-columns-redo)))
  10114. (defun org-columns-delete ()
  10115. "Delete the column at point from columns view."
  10116. (interactive)
  10117. (let* ((n (current-column))
  10118. (title (nth 1 (nth n org-columns-current-fmt-compiled))))
  10119. (when (y-or-n-p
  10120. (format "Are you sure you want to remove column \"%s\"? " title))
  10121. (setq org-columns-current-fmt-compiled
  10122. (delq (nth n org-columns-current-fmt-compiled)
  10123. org-columns-current-fmt-compiled))
  10124. (org-columns-store-format)
  10125. (org-columns-redo)
  10126. (if (>= (current-column) (length org-columns-current-fmt-compiled))
  10127. (backward-char 1)))))
  10128. (defun org-columns-edit-attributes ()
  10129. "Edit the attributes of the current column."
  10130. (interactive)
  10131. (let* ((n (current-column))
  10132. (info (nth n org-columns-current-fmt-compiled)))
  10133. (apply 'org-columns-new info)))
  10134. (defun org-columns-widen (arg)
  10135. "Make the column wider by ARG characters."
  10136. (interactive "p")
  10137. (let* ((n (current-column))
  10138. (entry (nth n org-columns-current-fmt-compiled))
  10139. (width (or (nth 2 entry)
  10140. (cdr (assoc (car entry) org-columns-current-maxwidths)))))
  10141. (setq width (max 1 (+ width arg)))
  10142. (setcar (nthcdr 2 entry) width)
  10143. (org-columns-store-format)
  10144. (org-columns-redo)))
  10145. (defun org-columns-narrow (arg)
  10146. "Make the column nrrower by ARG characters."
  10147. (interactive "p")
  10148. (org-columns-widen (- arg)))
  10149. (defun org-columns-move-right ()
  10150. "Swap this column with the one to the right."
  10151. (interactive)
  10152. (let* ((n (current-column))
  10153. (cell (nthcdr n org-columns-current-fmt-compiled))
  10154. e)
  10155. (when (>= n (1- (length org-columns-current-fmt-compiled)))
  10156. (error "Cannot shift this column further to the right"))
  10157. (setq e (car cell))
  10158. (setcar cell (car (cdr cell)))
  10159. (setcdr cell (cons e (cdr (cdr cell))))
  10160. (org-columns-store-format)
  10161. (org-columns-redo)
  10162. (forward-char 1)))
  10163. (defun org-columns-move-left ()
  10164. "Swap this column with the one to the left."
  10165. (interactive)
  10166. (let* ((n (current-column)))
  10167. (when (= n 0)
  10168. (error "Cannot shift this column further to the left"))
  10169. (backward-char 1)
  10170. (org-columns-move-right)
  10171. (backward-char 1)))
  10172. (defun org-columns-store-format ()
  10173. "Store the text version of the current columns format in appropriate place.
  10174. This is either in the COLUMNS property of the node starting the current column
  10175. display, or in the #+COLUMNS line of the current buffer."
  10176. (let (fmt (cnt 0))
  10177. (setq fmt (org-columns-uncompile-format org-columns-current-fmt-compiled))
  10178. (org-set-local 'org-columns-current-fmt fmt)
  10179. (if (marker-position org-columns-top-level-marker)
  10180. (save-excursion
  10181. (goto-char org-columns-top-level-marker)
  10182. (if (and (org-at-heading-p)
  10183. (org-entry-get nil "COLUMNS"))
  10184. (org-entry-put nil "COLUMNS" fmt)
  10185. (goto-char (point-min))
  10186. ;; Overwrite all #+COLUMNS lines....
  10187. (while (re-search-forward "^#\\+COLUMNS:.*" nil t)
  10188. (setq cnt (1+ cnt))
  10189. (replace-match (concat "#+COLUMNS: " fmt) t t))
  10190. (unless (> cnt 0)
  10191. (goto-char (point-min))
  10192. (or (org-on-heading-p t) (outline-next-heading))
  10193. (let ((inhibit-read-only t))
  10194. (insert-before-markers "#+COLUMNS: " fmt "\n")))
  10195. (org-set-local 'org-columns-default-format fmt))))))
  10196. (defvar org-overriding-columns-format nil
  10197. "When set, overrides any other definition.")
  10198. (defvar org-agenda-view-columns-initially nil
  10199. "When set, switch to columns view immediately after creating the agenda.")
  10200. (defun org-agenda-columns ()
  10201. "Turn on column view in the agenda."
  10202. (interactive)
  10203. (org-verify-version 'columns)
  10204. (org-columns-remove-overlays)
  10205. (move-marker org-columns-begin-marker (point))
  10206. (let (fmt cache maxwidths m)
  10207. (cond
  10208. ((and (local-variable-p 'org-overriding-columns-format)
  10209. org-overriding-columns-format)
  10210. (setq fmt org-overriding-columns-format))
  10211. ((setq m (get-text-property (point-at-bol) 'org-hd-marker))
  10212. (setq fmt (or (org-entry-get m "COLUMNS" t)
  10213. (with-current-buffer (marker-buffer m)
  10214. org-columns-default-format))))
  10215. ((and (boundp 'org-columns-current-fmt)
  10216. (local-variable-p 'org-columns-current-fmt)
  10217. org-columns-current-fmt)
  10218. (setq fmt org-columns-current-fmt))
  10219. ((setq m (next-single-property-change (point-min) 'org-hd-marker))
  10220. (setq m (get-text-property m 'org-hd-marker))
  10221. (setq fmt (or (org-entry-get m "COLUMNS" t)
  10222. (with-current-buffer (marker-buffer m)
  10223. org-columns-default-format)))))
  10224. (setq fmt (or fmt org-columns-default-format))
  10225. (org-set-local 'org-columns-current-fmt fmt)
  10226. (org-columns-compile-format fmt)
  10227. (save-excursion
  10228. ;; Get and cache the properties
  10229. (goto-char (point-min))
  10230. (while (not (eobp))
  10231. (when (setq m (or (get-text-property (point) 'org-hd-marker)
  10232. (get-text-property (point) 'org-marker)))
  10233. (push (cons (org-current-line) (org-entry-properties m)) cache))
  10234. (beginning-of-line 2))
  10235. (when cache
  10236. (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
  10237. (org-set-local 'org-columns-current-maxwidths maxwidths)
  10238. (org-columns-display-here-title)
  10239. (mapc (lambda (x)
  10240. (goto-line (car x))
  10241. (org-columns-display-here (cdr x)))
  10242. cache)))))
  10243. (defun org-columns-get-autowidth-alist (s cache)
  10244. "Derive the maximum column widths from the format and the cache."
  10245. (let ((start 0) rtn)
  10246. (while (string-match (org-re "%\\([[:alpha:]][[:alnum:]_-]*\\)") s start)
  10247. (push (cons (match-string 1 s) 1) rtn)
  10248. (setq start (match-end 0)))
  10249. (mapc (lambda (x)
  10250. (setcdr x (apply 'max
  10251. (mapcar
  10252. (lambda (y)
  10253. (length (or (cdr (assoc (car x) (cdr y))) " ")))
  10254. cache))))
  10255. rtn)
  10256. rtn))
  10257. (defun org-columns-compute-all ()
  10258. "Compute all columns that have operators defined."
  10259. (org-unmodified
  10260. (remove-text-properties (point-min) (point-max) '(org-summaries t)))
  10261. (let ((columns org-columns-current-fmt-compiled) col)
  10262. (while (setq col (pop columns))
  10263. (when (nth 3 col)
  10264. (save-excursion
  10265. (org-columns-compute (car col)))))))
  10266. (defun org-columns-update (property)
  10267. "Recompute PROPERTY, and update the columns display for it."
  10268. (org-columns-compute property)
  10269. (let (fmt val pos)
  10270. (save-excursion
  10271. (mapc (lambda (ov)
  10272. (when (equal (org-overlay-get ov 'org-columns-key) property)
  10273. (setq pos (org-overlay-start ov))
  10274. (goto-char pos)
  10275. (when (setq val (cdr (assoc property
  10276. (get-text-property
  10277. (point-at-bol) 'org-summaries))))
  10278. (setq fmt (org-overlay-get ov 'org-columns-format))
  10279. (org-overlay-put ov 'org-columns-value val)
  10280. (org-overlay-put ov 'display (format fmt val)))))
  10281. org-columns-overlays))))
  10282. (defun org-columns-compute (property)
  10283. "Sum the values of property PROPERTY hierarchically, for the entire buffer."
  10284. (interactive)
  10285. (let* ((re (concat "^" outline-regexp))
  10286. (lmax 30) ; Does anyone use deeper levels???
  10287. (lsum (make-vector lmax 0))
  10288. (lflag (make-vector lmax nil))
  10289. (level 0)
  10290. (ass (assoc property org-columns-current-fmt-compiled))
  10291. (format (nth 4 ass))
  10292. (printf (nth 5 ass))
  10293. (beg org-columns-top-level-marker)
  10294. last-level val valflag flag end sumpos sum-alist sum str str1 useval)
  10295. (save-excursion
  10296. ;; Find the region to compute
  10297. (goto-char beg)
  10298. (setq end (condition-case nil (org-end-of-subtree t) (error (point-max))))
  10299. (goto-char end)
  10300. ;; Walk the tree from the back and do the computations
  10301. (while (re-search-backward re beg t)
  10302. (setq sumpos (match-beginning 0)
  10303. last-level level
  10304. level (org-outline-level)
  10305. val (org-entry-get nil property)
  10306. valflag (and val (string-match "\\S-" val)))
  10307. (cond
  10308. ((< level last-level)
  10309. ;; put the sum of lower levels here as a property
  10310. (setq sum (aref lsum last-level) ; current sum
  10311. flag (aref lflag last-level) ; any valid entries from children?
  10312. str (org-column-number-to-string sum format printf)
  10313. str1 (org-add-props (copy-sequence str) nil 'org-computed t 'face 'bold)
  10314. useval (if flag str1 (if valflag val ""))
  10315. sum-alist (get-text-property sumpos 'org-summaries))
  10316. (if (assoc property sum-alist)
  10317. (setcdr (assoc property sum-alist) useval)
  10318. (push (cons property useval) sum-alist)
  10319. (org-unmodified
  10320. (add-text-properties sumpos (1+ sumpos)
  10321. (list 'org-summaries sum-alist))))
  10322. (when val
  10323. (org-entry-put nil property (if flag str val)))
  10324. ;; add current to current level accumulator
  10325. (when (or flag valflag)
  10326. (aset lsum level (+ (aref lsum level)
  10327. (if flag sum (org-column-string-to-number
  10328. (if flag str val) format))))
  10329. (aset lflag level t))
  10330. ;; clear accumulators for deeper levels
  10331. (loop for l from (1+ level) to (1- lmax) do
  10332. (aset lsum l 0)
  10333. (aset lflag l nil)))
  10334. ((>= level last-level)
  10335. ;; add what we have here to the accumulator for this level
  10336. (aset lsum level (+ (aref lsum level)
  10337. (org-column-string-to-number (or val "0") format)))
  10338. (and valflag (aset lflag level t)))
  10339. (t (error "This should not happen")))))))
  10340. (defun org-columns-redo ()
  10341. "Construct the column display again."
  10342. (interactive)
  10343. (message "Recomputing columns...")
  10344. (save-excursion
  10345. (if (marker-position org-columns-begin-marker)
  10346. (goto-char org-columns-begin-marker))
  10347. (org-columns-remove-overlays)
  10348. (if (org-mode-p)
  10349. (call-interactively 'org-columns)
  10350. (call-interactively 'org-agenda-columns)))
  10351. (message "Recomputing columns...done"))
  10352. (defun org-columns-not-in-agenda ()
  10353. (if (eq major-mode 'org-agenda-mode)
  10354. (error "This command is only allowed in Org-mode buffers")))
  10355. (defun org-string-to-number (s)
  10356. "Convert string to number, and interpret hh:mm:ss."
  10357. (if (not (string-match ":" s))
  10358. (string-to-number s)
  10359. (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
  10360. (while l
  10361. (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
  10362. sum)))
  10363. (defun org-column-number-to-string (n fmt &optional printf)
  10364. "Convert a computed column number to a string value, according to FMT."
  10365. (cond
  10366. ((eq fmt 'add_times)
  10367. (let* ((h (floor n)) (m (floor (+ 0.5 (* 60 (- n h))))))
  10368. (format "%d:%02d" h m)))
  10369. ((eq fmt 'checkbox)
  10370. (cond ((= n (floor n)) "[X]")
  10371. ((> n 1.) "[-]")
  10372. (t "[ ]")))
  10373. ((memq fmt '(checkbox-n-of-m checkbox-percent))
  10374. (let* ((n1 (floor n)) (n2 (floor (+ .5 (* 1000000 (- n n1))))))
  10375. (org-nofm-to-completion n1 (+ n2 n1) (eq fmt 'checkbox-percent))))
  10376. (printf (format printf n))
  10377. ((eq fmt 'currency)
  10378. (format "%.2f" n))
  10379. (t (number-to-string n))))
  10380. (defun org-nofm-to-completion (n m &optional percent)
  10381. (if (not percent)
  10382. (format "[%d/%d]" n m)
  10383. (format "[%d%%]"(floor (+ 0.5 (* 100. (/ (* 1.0 n) m)))))))
  10384. (defun org-column-string-to-number (s fmt)
  10385. "Convert a column value to a number that can be used for column computing."
  10386. (cond
  10387. ((string-match ":" s)
  10388. (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
  10389. (while l
  10390. (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
  10391. sum))
  10392. ((memq fmt '(checkbox checkbox-n-of-m checkbox-percent))
  10393. (if (equal s "[X]") 1. 0.000001))
  10394. (t (string-to-number s))))
  10395. (defun org-columns-uncompile-format (cfmt)
  10396. "Turn the compiled columns format back into a string representation."
  10397. (let ((rtn "") e s prop title op width fmt printf)
  10398. (while (setq e (pop cfmt))
  10399. (setq prop (car e)
  10400. title (nth 1 e)
  10401. width (nth 2 e)
  10402. op (nth 3 e)
  10403. fmt (nth 4 e)
  10404. printf (nth 5 e))
  10405. (cond
  10406. ((eq fmt 'add_times) (setq op ":"))
  10407. ((eq fmt 'checkbox) (setq op "X"))
  10408. ((eq fmt 'checkbox-n-of-m) (setq op "X/"))
  10409. ((eq fmt 'checkbox-percent) (setq op "X%"))
  10410. ((eq fmt 'add_numbers) (setq op "+"))
  10411. ((eq fmt 'currency) (setq op "$")))
  10412. (if (and op printf) (setq op (concat op ";" printf)))
  10413. (if (equal title prop) (setq title nil))
  10414. (setq s (concat "%" (if width (number-to-string width))
  10415. prop
  10416. (if title (concat "(" title ")"))
  10417. (if op (concat "{" op "}"))))
  10418. (setq rtn (concat rtn " " s)))
  10419. (org-trim rtn)))
  10420. (defun org-columns-compile-format (fmt)
  10421. "Turn a column format string into an alist of specifications.
  10422. The alist has one entry for each column in the format. The elements of
  10423. that list are:
  10424. property the property
  10425. title the title field for the columns
  10426. width the column width in characters, can be nil for automatic
  10427. operator the operator if any
  10428. format the output format for computed results, derived from operator
  10429. printf a printf format for computed values"
  10430. (let ((start 0) width prop title op f printf)
  10431. (setq org-columns-current-fmt-compiled nil)
  10432. (while (string-match
  10433. (org-re "%\\([0-9]+\\)?\\([[:alnum:]_-]+\\)\\(?:(\\([^)]+\\))\\)?\\(?:{\\([^}]+\\)}\\)?\\s-*")
  10434. fmt start)
  10435. (setq start (match-end 0)
  10436. width (match-string 1 fmt)
  10437. prop (match-string 2 fmt)
  10438. title (or (match-string 3 fmt) prop)
  10439. op (match-string 4 fmt)
  10440. f nil
  10441. printf nil)
  10442. (if width (setq width (string-to-number width)))
  10443. (when (and op (string-match ";" op))
  10444. (setq printf (substring op (match-end 0))
  10445. op (substring op 0 (match-beginning 0))))
  10446. (cond
  10447. ((equal op "+") (setq f 'add_numbers))
  10448. ((equal op "$") (setq f 'currency))
  10449. ((equal op ":") (setq f 'add_times))
  10450. ((equal op "X") (setq f 'checkbox))
  10451. ((equal op "X/") (setq f 'checkbox-n-of-m))
  10452. ((equal op "X%") (setq f 'checkbox-percent))
  10453. )
  10454. (push (list prop title width op f printf) org-columns-current-fmt-compiled))
  10455. (setq org-columns-current-fmt-compiled
  10456. (nreverse org-columns-current-fmt-compiled))))
  10457. ;;; Dynamic block for Column view
  10458. (defun org-columns-capture-view (&optional maxlevel skip-empty-rows)
  10459. "Get the column view of the current buffer or subtree.
  10460. The first optional argument MAXLEVEL sets the level limit. A
  10461. second optional argument SKIP-EMPTY-ROWS tells whether to skip
  10462. empty rows, an empty row being one where all the column view
  10463. specifiers except ITEM are empty. This function returns a list
  10464. containing the title row and all other rows. Each row is a list
  10465. of fields."
  10466. (save-excursion
  10467. (let* ((title (mapcar 'cadr org-columns-current-fmt-compiled))
  10468. (n (length title)) row tbl)
  10469. (goto-char (point-min))
  10470. (while (and (re-search-forward "^\\(\\*+\\) " nil t)
  10471. (or (null maxlevel)
  10472. (>= maxlevel
  10473. (if org-odd-levels-only
  10474. (/ (1+ (length (match-string 1))) 2)
  10475. (length (match-string 1))))))
  10476. (when (get-char-property (match-beginning 0) 'org-columns-key)
  10477. (setq row nil)
  10478. (loop for i from 0 to (1- n) do
  10479. (push (or (get-char-property (+ (match-beginning 0) i) 'org-columns-value-modified)
  10480. (get-char-property (+ (match-beginning 0) i) 'org-columns-value)
  10481. "")
  10482. row))
  10483. (setq row (nreverse row))
  10484. (unless (and skip-empty-rows
  10485. (eq 1 (length (delete "" (delete-dups row)))))
  10486. (push row tbl))))
  10487. (append (list title 'hline) (nreverse tbl)))))
  10488. (defun org-dblock-write:columnview (params)
  10489. "Write the column view table.
  10490. PARAMS is a property list of parameters:
  10491. :width enforce same column widths with <N> specifiers.
  10492. :id the :ID: property of the entry where the columns view
  10493. should be built, as a string. When `local', call locally.
  10494. When `global' call column view with the cursor at the beginning
  10495. of the buffer (usually this means that the whole buffer switches
  10496. to column view).
  10497. :hlines When t, insert a hline before each item. When a number, insert
  10498. a hline before each level <= that number.
  10499. :vlines When t, make each column a colgroup to enforce vertical lines.
  10500. :maxlevel When set to a number, don't capture headlines below this level.
  10501. :skip-empty-rows
  10502. When t, skip rows where all specifiers other than ITEM are empty."
  10503. (let ((pos (move-marker (make-marker) (point)))
  10504. (hlines (plist-get params :hlines))
  10505. (vlines (plist-get params :vlines))
  10506. (maxlevel (plist-get params :maxlevel))
  10507. (skip-empty-rows (plist-get params :skip-empty-rows))
  10508. tbl id idpos nfields tmp)
  10509. (save-excursion
  10510. (save-restriction
  10511. (when (setq id (plist-get params :id))
  10512. (cond ((not id) nil)
  10513. ((eq id 'global) (goto-char (point-min)))
  10514. ((eq id 'local) nil)
  10515. ((setq idpos (org-find-entry-with-id id))
  10516. (goto-char idpos))
  10517. (t (error "Cannot find entry with :ID: %s" id))))
  10518. (org-columns)
  10519. (setq tbl (org-columns-capture-view maxlevel skip-empty-rows))
  10520. (setq nfields (length (car tbl)))
  10521. (org-columns-quit)))
  10522. (goto-char pos)
  10523. (move-marker pos nil)
  10524. (when tbl
  10525. (when (plist-get params :hlines)
  10526. (setq tmp nil)
  10527. (while tbl
  10528. (if (eq (car tbl) 'hline)
  10529. (push (pop tbl) tmp)
  10530. (if (string-match "\\` *\\(\\*+\\)" (caar tbl))
  10531. (if (and (not (eq (car tmp) 'hline))
  10532. (or (eq hlines t)
  10533. (and (numberp hlines) (<= (- (match-end 1) (match-beginning 1)) hlines))))
  10534. (push 'hline tmp)))
  10535. (push (pop tbl) tmp)))
  10536. (setq tbl (nreverse tmp)))
  10537. (when vlines
  10538. (setq tbl (mapcar (lambda (x)
  10539. (if (eq 'hline x) x (cons "" x)))
  10540. tbl))
  10541. (setq tbl (append tbl (list (cons "/" (make-list nfields "<>"))))))
  10542. (setq pos (point))
  10543. (insert (org-listtable-to-string tbl))
  10544. (when (plist-get params :width)
  10545. (insert "\n|" (mapconcat (lambda (x) (format "<%d>" (max 3 x)))
  10546. org-columns-current-widths "|")))
  10547. (goto-char pos)
  10548. (org-table-align))))
  10549. (defun org-listtable-to-string (tbl)
  10550. "Convert a listtable TBL to a string that contains the Org-mode table.
  10551. The table still need to be alligned. The resulting string has no leading
  10552. and tailing newline characters."
  10553. (mapconcat
  10554. (lambda (x)
  10555. (cond
  10556. ((listp x)
  10557. (concat "|" (mapconcat 'identity x "|") "|"))
  10558. ((eq x 'hline) "|-|")
  10559. (t (error "Garbage in listtable: %s" x))))
  10560. tbl "\n"))
  10561. (defun org-insert-columns-dblock ()
  10562. "Create a dynamic block capturing a column view table."
  10563. (interactive)
  10564. (let ((defaults '(:name "columnview" :hlines 1))
  10565. (id (completing-read
  10566. "Capture columns (local, global, entry with :ID: property) [local]: "
  10567. (append '(("global") ("local"))
  10568. (mapcar 'list (org-property-values "ID"))))))
  10569. (if (equal id "") (setq id 'local))
  10570. (if (equal id "global") (setq id 'global))
  10571. (setq defaults (append defaults (list :id id)))
  10572. (org-create-dblock defaults)
  10573. (org-update-dblock)))
  10574. ;;;; Timestamps
  10575. (defvar org-last-changed-timestamp nil)
  10576. (defvar org-time-was-given) ; dynamically scoped parameter
  10577. (defvar org-end-time-was-given) ; dynamically scoped parameter
  10578. (defvar org-ts-what) ; dynamically scoped parameter
  10579. (defun org-time-stamp (arg)
  10580. "Prompt for a date/time and insert a time stamp.
  10581. If the user specifies a time like HH:MM, or if this command is called
  10582. with a prefix argument, the time stamp will contain date and time.
  10583. Otherwise, only the date will be included. All parts of a date not
  10584. specified by the user will be filled in from the current date/time.
  10585. So if you press just return without typing anything, the time stamp
  10586. will represent the current date/time. If there is already a timestamp
  10587. at the cursor, it will be modified."
  10588. (interactive "P")
  10589. (let* ((ts nil)
  10590. (default-time
  10591. ;; Default time is either today, or, when entering a range,
  10592. ;; the range start.
  10593. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  10594. (save-excursion
  10595. (re-search-backward
  10596. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  10597. (- (point) 20) t)))
  10598. (apply 'encode-time (org-parse-time-string (match-string 1)))
  10599. (current-time)))
  10600. (default-input (and ts (org-get-compact-tod ts)))
  10601. org-time-was-given org-end-time-was-given time)
  10602. (cond
  10603. ((and (org-at-timestamp-p)
  10604. (eq last-command 'org-time-stamp)
  10605. (eq this-command 'org-time-stamp))
  10606. (insert "--")
  10607. (setq time (let ((this-command this-command))
  10608. (org-read-date arg 'totime nil nil default-time default-input)))
  10609. (org-insert-time-stamp time (or org-time-was-given arg)))
  10610. ((org-at-timestamp-p)
  10611. (setq time (let ((this-command this-command))
  10612. (org-read-date arg 'totime nil nil default-time default-input)))
  10613. (when (org-at-timestamp-p) ; just to get the match data
  10614. (replace-match "")
  10615. (setq org-last-changed-timestamp
  10616. (org-insert-time-stamp
  10617. time (or org-time-was-given arg)
  10618. nil nil nil (list org-end-time-was-given))))
  10619. (message "Timestamp updated"))
  10620. (t
  10621. (setq time (let ((this-command this-command))
  10622. (org-read-date arg 'totime nil nil default-time default-input)))
  10623. (org-insert-time-stamp time (or org-time-was-given arg)
  10624. nil nil nil (list org-end-time-was-given))))))
  10625. ;; FIXME: can we use this for something else, like computing time differences?
  10626. (defun org-get-compact-tod (s)
  10627. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  10628. (let* ((t1 (match-string 1 s))
  10629. (h1 (string-to-number (match-string 2 s)))
  10630. (m1 (string-to-number (match-string 3 s)))
  10631. (t2 (and (match-end 4) (match-string 5 s)))
  10632. (h2 (and t2 (string-to-number (match-string 6 s))))
  10633. (m2 (and t2 (string-to-number (match-string 7 s))))
  10634. dh dm)
  10635. (if (not t2)
  10636. t1
  10637. (setq dh (- h2 h1) dm (- m2 m1))
  10638. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  10639. (concat t1 "+" (number-to-string dh)
  10640. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  10641. (defun org-time-stamp-inactive (&optional arg)
  10642. "Insert an inactive time stamp.
  10643. An inactive time stamp is enclosed in square brackets instead of angle
  10644. brackets. It is inactive in the sense that it does not trigger agenda entries,
  10645. does not link to the calendar and cannot be changed with the S-cursor keys.
  10646. So these are more for recording a certain time/date."
  10647. (interactive "P")
  10648. (let (org-time-was-given org-end-time-was-given time)
  10649. (setq time (org-read-date arg 'totime))
  10650. (org-insert-time-stamp time (or org-time-was-given arg) 'inactive
  10651. nil nil (list org-end-time-was-given))))
  10652. (defvar org-date-ovl (org-make-overlay 1 1))
  10653. (org-overlay-put org-date-ovl 'face 'org-warning)
  10654. (org-detach-overlay org-date-ovl)
  10655. (defvar org-ans1) ; dynamically scoped parameter
  10656. (defvar org-ans2) ; dynamically scoped parameter
  10657. (defvar org-plain-time-of-day-regexp) ; defined below
  10658. (defvar org-read-date-overlay nil)
  10659. (defvar org-dcst nil) ; dynamically scoped
  10660. (defun org-read-date (&optional with-time to-time from-string prompt
  10661. default-time default-input)
  10662. "Read a date, possibly a time, and make things smooth for the user.
  10663. The prompt will suggest to enter an ISO date, but you can also enter anything
  10664. which will at least partially be understood by `parse-time-string'.
  10665. Unrecognized parts of the date will default to the current day, month, year,
  10666. hour and minute. If this command is called to replace a timestamp at point,
  10667. of to enter the second timestamp of a range, the default time is taken from the
  10668. existing stamp. For example,
  10669. 3-2-5 --> 2003-02-05
  10670. feb 15 --> currentyear-02-15
  10671. sep 12 9 --> 2009-09-12
  10672. 12:45 --> today 12:45
  10673. 22 sept 0:34 --> currentyear-09-22 0:34
  10674. 12 --> currentyear-currentmonth-12
  10675. Fri --> nearest Friday (today or later)
  10676. etc.
  10677. Furthermore you can specify a relative date by giving, as the *first* thing
  10678. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  10679. change in days weeks, months, years.
  10680. With a single plus or minus, the date is relative to today. With a double
  10681. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  10682. +4d --> four days from today
  10683. +4 --> same as above
  10684. +2w --> two weeks from today
  10685. ++5 --> five days from default date
  10686. The function understands only English month and weekday abbreviations,
  10687. but this can be configured with the variables `parse-time-months' and
  10688. `parse-time-weekdays'.
  10689. While prompting, a calendar is popped up - you can also select the
  10690. date with the mouse (button 1). The calendar shows a period of three
  10691. months. To scroll it to other months, use the keys `>' and `<'.
  10692. If you don't like the calendar, turn it off with
  10693. \(setq org-read-date-popup-calendar nil)
  10694. With optional argument TO-TIME, the date will immediately be converted
  10695. to an internal time.
  10696. With an optional argument WITH-TIME, the prompt will suggest to also
  10697. insert a time. Note that when WITH-TIME is not set, you can still
  10698. enter a time, and this function will inform the calling routine about
  10699. this change. The calling routine may then choose to change the format
  10700. used to insert the time stamp into the buffer to include the time.
  10701. With optional argument FROM-STRING, read from this string instead from
  10702. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  10703. the time/date that is used for everything that is not specified by the
  10704. user."
  10705. (require 'parse-time)
  10706. (let* ((org-time-stamp-rounding-minutes
  10707. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  10708. (org-dcst org-display-custom-times)
  10709. (ct (org-current-time))
  10710. (def (or default-time ct))
  10711. (defdecode (decode-time def))
  10712. (dummy (progn
  10713. (when (< (nth 2 defdecode) org-extend-today-until)
  10714. (setcar (nthcdr 2 defdecode) -1)
  10715. (setcar (nthcdr 1 defdecode) 59)
  10716. (setq def (apply 'encode-time defdecode)
  10717. defdecode (decode-time def)))))
  10718. (calendar-move-hook nil)
  10719. (calendar-view-diary-initially-flag nil)
  10720. (view-diary-entries-initially nil)
  10721. (calendar-view-holidays-initially-flag nil)
  10722. (view-calendar-holidays-initially nil)
  10723. (timestr (format-time-string
  10724. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  10725. (prompt (concat (if prompt (concat prompt " ") "")
  10726. (format "Date+time [%s]: " timestr)))
  10727. ans (org-ans0 "") org-ans1 org-ans2 final)
  10728. (cond
  10729. (from-string (setq ans from-string))
  10730. (org-read-date-popup-calendar
  10731. (save-excursion
  10732. (save-window-excursion
  10733. (calendar)
  10734. (calendar-forward-day (- (time-to-days def)
  10735. (calendar-absolute-from-gregorian
  10736. (calendar-current-date))))
  10737. (org-eval-in-calendar nil t)
  10738. (let* ((old-map (current-local-map))
  10739. (map (copy-keymap calendar-mode-map))
  10740. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  10741. (org-defkey map (kbd "RET") 'org-calendar-select)
  10742. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  10743. 'org-calendar-select-mouse)
  10744. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  10745. 'org-calendar-select-mouse)
  10746. (org-defkey minibuffer-local-map [(meta shift left)]
  10747. (lambda () (interactive)
  10748. (org-eval-in-calendar '(calendar-backward-month 1))))
  10749. (org-defkey minibuffer-local-map [(meta shift right)]
  10750. (lambda () (interactive)
  10751. (org-eval-in-calendar '(calendar-forward-month 1))))
  10752. (org-defkey minibuffer-local-map [(meta shift up)]
  10753. (lambda () (interactive)
  10754. (org-eval-in-calendar '(calendar-backward-year 1))))
  10755. (org-defkey minibuffer-local-map [(meta shift down)]
  10756. (lambda () (interactive)
  10757. (org-eval-in-calendar '(calendar-forward-year 1))))
  10758. (org-defkey minibuffer-local-map [(shift up)]
  10759. (lambda () (interactive)
  10760. (org-eval-in-calendar '(calendar-backward-week 1))))
  10761. (org-defkey minibuffer-local-map [(shift down)]
  10762. (lambda () (interactive)
  10763. (org-eval-in-calendar '(calendar-forward-week 1))))
  10764. (org-defkey minibuffer-local-map [(shift left)]
  10765. (lambda () (interactive)
  10766. (org-eval-in-calendar '(calendar-backward-day 1))))
  10767. (org-defkey minibuffer-local-map [(shift right)]
  10768. (lambda () (interactive)
  10769. (org-eval-in-calendar '(calendar-forward-day 1))))
  10770. (org-defkey minibuffer-local-map ">"
  10771. (lambda () (interactive)
  10772. (org-eval-in-calendar '(scroll-calendar-left 1))))
  10773. (org-defkey minibuffer-local-map "<"
  10774. (lambda () (interactive)
  10775. (org-eval-in-calendar '(scroll-calendar-right 1))))
  10776. (unwind-protect
  10777. (progn
  10778. (use-local-map map)
  10779. (add-hook 'post-command-hook 'org-read-date-display)
  10780. (setq org-ans0 (read-string prompt default-input nil nil))
  10781. ;; org-ans0: from prompt
  10782. ;; org-ans1: from mouse click
  10783. ;; org-ans2: from calendar motion
  10784. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  10785. (remove-hook 'post-command-hook 'org-read-date-display)
  10786. (use-local-map old-map)
  10787. (when org-read-date-overlay
  10788. (org-delete-overlay org-read-date-overlay)
  10789. (setq org-read-date-overlay nil)))))))
  10790. (t ; Naked prompt only
  10791. (unwind-protect
  10792. (setq ans (read-string prompt default-input nil timestr))
  10793. (when org-read-date-overlay
  10794. (org-delete-overlay org-read-date-overlay)
  10795. (setq org-read-date-overlay nil)))))
  10796. (setq final (org-read-date-analyze ans def defdecode))
  10797. (if to-time
  10798. (apply 'encode-time final)
  10799. (if (and (boundp 'org-time-was-given) org-time-was-given)
  10800. (format "%04d-%02d-%02d %02d:%02d"
  10801. (nth 5 final) (nth 4 final) (nth 3 final)
  10802. (nth 2 final) (nth 1 final))
  10803. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  10804. (defvar def)
  10805. (defvar defdecode)
  10806. (defvar with-time)
  10807. (defun org-read-date-display ()
  10808. "Display the currrent date prompt interpretation in the minibuffer."
  10809. (when org-read-date-display-live
  10810. (when org-read-date-overlay
  10811. (org-delete-overlay org-read-date-overlay))
  10812. (let ((p (point)))
  10813. (end-of-line 1)
  10814. (while (not (equal (buffer-substring
  10815. (max (point-min) (- (point) 4)) (point))
  10816. " "))
  10817. (insert " "))
  10818. (goto-char p))
  10819. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  10820. " " (or org-ans1 org-ans2)))
  10821. (org-end-time-was-given nil)
  10822. (f (org-read-date-analyze ans def defdecode))
  10823. (fmts (if org-dcst
  10824. org-time-stamp-custom-formats
  10825. org-time-stamp-formats))
  10826. (fmt (if (or with-time
  10827. (and (boundp 'org-time-was-given) org-time-was-given))
  10828. (cdr fmts)
  10829. (car fmts)))
  10830. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  10831. (when (and org-end-time-was-given
  10832. (string-match org-plain-time-of-day-regexp txt))
  10833. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  10834. org-end-time-was-given
  10835. (substring txt (match-end 0)))))
  10836. (setq org-read-date-overlay
  10837. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  10838. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  10839. (defun org-read-date-analyze (ans def defdecode)
  10840. "Analyze the combined answer of the date prompt."
  10841. ;; FIXME: cleanup and comment
  10842. (let (delta deltan deltaw deltadef year month day
  10843. hour minute second wday pm h2 m2 tl wday1
  10844. iso-year iso-weekday iso-week iso-year iso-date)
  10845. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  10846. (setq ans (replace-match "" t t ans)
  10847. deltan (car delta)
  10848. deltaw (nth 1 delta)
  10849. deltadef (nth 2 delta)))
  10850. ;; Check if there is an iso week date in there
  10851. ;; If yes, sore the info and ostpone interpreting it until the rest
  10852. ;; of the parsing is done
  10853. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  10854. (setq iso-year (if (match-end 1) (org-small-year-to-year (string-to-number (match-string 1 ans))))
  10855. iso-weekday (if (match-end 3) (string-to-number (match-string 3 ans)))
  10856. iso-week (string-to-number (match-string 2 ans)))
  10857. (setq ans (replace-match "" t t ans)))
  10858. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  10859. (when (string-match
  10860. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  10861. (setq year (if (match-end 2)
  10862. (string-to-number (match-string 2 ans))
  10863. (string-to-number (format-time-string "%Y")))
  10864. month (string-to-number (match-string 3 ans))
  10865. day (string-to-number (match-string 4 ans)))
  10866. (if (< year 100) (setq year (+ 2000 year)))
  10867. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  10868. t nil ans)))
  10869. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  10870. ;; If there is a time with am/pm, and *no* time without it, we convert
  10871. ;; so that matching will be successful.
  10872. (loop for i from 1 to 2 do ; twice, for end time as well
  10873. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  10874. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  10875. (setq hour (string-to-number (match-string 1 ans))
  10876. minute (if (match-end 3)
  10877. (string-to-number (match-string 3 ans))
  10878. 0)
  10879. pm (equal ?p
  10880. (string-to-char (downcase (match-string 4 ans)))))
  10881. (if (and (= hour 12) (not pm))
  10882. (setq hour 0)
  10883. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  10884. (setq ans (replace-match (format "%02d:%02d" hour minute)
  10885. t t ans))))
  10886. ;; Check if a time range is given as a duration
  10887. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  10888. (setq hour (string-to-number (match-string 1 ans))
  10889. h2 (+ hour (string-to-number (match-string 3 ans)))
  10890. minute (string-to-number (match-string 2 ans))
  10891. m2 (+ minute (if (match-end 5) (string-to-number
  10892. (match-string 5 ans))0)))
  10893. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  10894. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  10895. t t ans)))
  10896. ;; Check if there is a time range
  10897. (when (boundp 'org-end-time-was-given)
  10898. (setq org-time-was-given nil)
  10899. (when (and (string-match org-plain-time-of-day-regexp ans)
  10900. (match-end 8))
  10901. (setq org-end-time-was-given (match-string 8 ans))
  10902. (setq ans (concat (substring ans 0 (match-beginning 7))
  10903. (substring ans (match-end 7))))))
  10904. (setq tl (parse-time-string ans)
  10905. day (or (nth 3 tl) (nth 3 defdecode))
  10906. month (or (nth 4 tl)
  10907. (if (and org-read-date-prefer-future
  10908. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  10909. (1+ (nth 4 defdecode))
  10910. (nth 4 defdecode)))
  10911. year (or (nth 5 tl)
  10912. (if (and org-read-date-prefer-future
  10913. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  10914. (1+ (nth 5 defdecode))
  10915. (nth 5 defdecode)))
  10916. hour (or (nth 2 tl) (nth 2 defdecode))
  10917. minute (or (nth 1 tl) (nth 1 defdecode))
  10918. second (or (nth 0 tl) 0)
  10919. wday (nth 6 tl))
  10920. ;; Special date definitions below
  10921. (cond
  10922. (iso-week
  10923. ;; There was an iso week
  10924. (setq year (or iso-year year)
  10925. day (or iso-weekday wday 1)
  10926. wday nil ; to make sure that the trigger below does not match
  10927. iso-date (calendar-gregorian-from-absolute
  10928. (calendar-absolute-from-iso
  10929. (list iso-week day year))))
  10930. ; FIXME: Should we also push ISO weeks into the future?
  10931. ; (when (and org-read-date-prefer-future
  10932. ; (not iso-year)
  10933. ; (< (calendar-absolute-from-gregorian iso-date)
  10934. ; (time-to-days (current-time))))
  10935. ; (setq year (1+ year)
  10936. ; iso-date (calendar-gregorian-from-absolute
  10937. ; (calendar-absolute-from-iso
  10938. ; (list iso-week day year)))))
  10939. (setq month (car iso-date)
  10940. year (nth 2 iso-date)
  10941. day (nth 1 iso-date)))
  10942. (deltan
  10943. (unless deltadef
  10944. (let ((now (decode-time (current-time))))
  10945. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  10946. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  10947. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  10948. ((equal deltaw "m") (setq month (+ month deltan)))
  10949. ((equal deltaw "y") (setq year (+ year deltan)))))
  10950. ((and wday (not (nth 3 tl)))
  10951. ;; Weekday was given, but no day, so pick that day in the week
  10952. ;; on or after the derived date.
  10953. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  10954. (unless (equal wday wday1)
  10955. (setq day (+ day (% (- wday wday1 -7) 7))))))
  10956. (if (and (boundp 'org-time-was-given)
  10957. (nth 2 tl))
  10958. (setq org-time-was-given t))
  10959. (if (< year 100) (setq year (+ 2000 year)))
  10960. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  10961. (list second minute hour day month year)))
  10962. (defvar parse-time-weekdays)
  10963. (defun org-read-date-get-relative (s today default)
  10964. "Check string S for special relative date string.
  10965. TODAY and DEFAULT are internal times, for today and for a default.
  10966. Return shift list (N what def-flag)
  10967. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  10968. N is the number of WHATs to shift.
  10969. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  10970. the DEFAULT date rather than TODAY."
  10971. (when (string-match
  10972. (concat
  10973. "\\`[ \t]*\\([-+]\\{1,2\\}\\)"
  10974. "\\([0-9]+\\)?"
  10975. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  10976. "\\([ \t]\\|$\\)") s)
  10977. (let* ((dir (if (match-end 1)
  10978. (string-to-char (substring (match-string 1 s) -1))
  10979. ?+))
  10980. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  10981. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  10982. (what (if (match-end 3) (match-string 3 s) "d"))
  10983. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  10984. (date (if rel default today))
  10985. (wday (nth 6 (decode-time date)))
  10986. delta)
  10987. (if wday1
  10988. (progn
  10989. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  10990. (if (= dir ?-) (setq delta (- delta 7)))
  10991. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  10992. (list delta "d" rel))
  10993. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  10994. (defun org-eval-in-calendar (form &optional keepdate)
  10995. "Eval FORM in the calendar window and return to current window.
  10996. Also, store the cursor date in variable org-ans2."
  10997. (let ((sw (selected-window)))
  10998. (select-window (get-buffer-window "*Calendar*"))
  10999. (eval form)
  11000. (when (and (not keepdate) (calendar-cursor-to-date))
  11001. (let* ((date (calendar-cursor-to-date))
  11002. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  11003. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  11004. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  11005. (select-window sw)))
  11006. ; ;; Update the prompt to show new default date
  11007. ; (save-excursion
  11008. ; (goto-char (point-min))
  11009. ; (when (and org-ans2
  11010. ; (re-search-forward "\\[[-0-9]+\\]" nil t)
  11011. ; (get-text-property (match-end 0) 'field))
  11012. ; (let ((inhibit-read-only t))
  11013. ; (replace-match (concat "[" org-ans2 "]") t t)
  11014. ; (add-text-properties (point-min) (1+ (match-end 0))
  11015. ; (text-properties-at (1+ (point-min)))))))))
  11016. (defun org-calendar-select ()
  11017. "Return to `org-read-date' with the date currently selected.
  11018. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  11019. (interactive)
  11020. (when (calendar-cursor-to-date)
  11021. (let* ((date (calendar-cursor-to-date))
  11022. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  11023. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  11024. (if (active-minibuffer-window) (exit-minibuffer))))
  11025. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  11026. "Insert a date stamp for the date given by the internal TIME.
  11027. WITH-HM means, use the stamp format that includes the time of the day.
  11028. INACTIVE means use square brackets instead of angular ones, so that the
  11029. stamp will not contribute to the agenda.
  11030. PRE and POST are optional strings to be inserted before and after the
  11031. stamp.
  11032. The command returns the inserted time stamp."
  11033. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  11034. stamp)
  11035. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  11036. (insert-before-markers (or pre ""))
  11037. (insert-before-markers (setq stamp (format-time-string fmt time)))
  11038. (when (listp extra)
  11039. (setq extra (car extra))
  11040. (if (and (stringp extra)
  11041. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  11042. (setq extra (format "-%02d:%02d"
  11043. (string-to-number (match-string 1 extra))
  11044. (string-to-number (match-string 2 extra))))
  11045. (setq extra nil)))
  11046. (when extra
  11047. (backward-char 1)
  11048. (insert-before-markers extra)
  11049. (forward-char 1))
  11050. (insert-before-markers (or post ""))
  11051. stamp))
  11052. (defun org-toggle-time-stamp-overlays ()
  11053. "Toggle the use of custom time stamp formats."
  11054. (interactive)
  11055. (setq org-display-custom-times (not org-display-custom-times))
  11056. (unless org-display-custom-times
  11057. (let ((p (point-min)) (bmp (buffer-modified-p)))
  11058. (while (setq p (next-single-property-change p 'display))
  11059. (if (and (get-text-property p 'display)
  11060. (eq (get-text-property p 'face) 'org-date))
  11061. (remove-text-properties
  11062. p (setq p (next-single-property-change p 'display))
  11063. '(display t))))
  11064. (set-buffer-modified-p bmp)))
  11065. (if (featurep 'xemacs)
  11066. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  11067. (org-restart-font-lock)
  11068. (setq org-table-may-need-update t)
  11069. (if org-display-custom-times
  11070. (message "Time stamps are overlayed with custom format")
  11071. (message "Time stamp overlays removed")))
  11072. (defun org-display-custom-time (beg end)
  11073. "Overlay modified time stamp format over timestamp between BED and END."
  11074. (let* ((ts (buffer-substring beg end))
  11075. t1 w1 with-hm tf time str w2 (off 0))
  11076. (save-match-data
  11077. (setq t1 (org-parse-time-string ts t))
  11078. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\)?\\'" ts)
  11079. (setq off (- (match-end 0) (match-beginning 0)))))
  11080. (setq end (- end off))
  11081. (setq w1 (- end beg)
  11082. with-hm (and (nth 1 t1) (nth 2 t1))
  11083. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  11084. time (org-fix-decoded-time t1)
  11085. str (org-add-props
  11086. (format-time-string
  11087. (substring tf 1 -1) (apply 'encode-time time))
  11088. nil 'mouse-face 'highlight)
  11089. w2 (length str))
  11090. (if (not (= w2 w1))
  11091. (add-text-properties (1+ beg) (+ 2 beg)
  11092. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  11093. (if (featurep 'xemacs)
  11094. (progn
  11095. (put-text-property beg end 'invisible t)
  11096. (put-text-property beg end 'end-glyph (make-glyph str)))
  11097. (put-text-property beg end 'display str))))
  11098. (defun org-translate-time (string)
  11099. "Translate all timestamps in STRING to custom format.
  11100. But do this only if the variable `org-display-custom-times' is set."
  11101. (when org-display-custom-times
  11102. (save-match-data
  11103. (let* ((start 0)
  11104. (re org-ts-regexp-both)
  11105. t1 with-hm inactive tf time str beg end)
  11106. (while (setq start (string-match re string start))
  11107. (setq beg (match-beginning 0)
  11108. end (match-end 0)
  11109. t1 (save-match-data
  11110. (org-parse-time-string (substring string beg end) t))
  11111. with-hm (and (nth 1 t1) (nth 2 t1))
  11112. inactive (equal (substring string beg (1+ beg)) "[")
  11113. tf (funcall (if with-hm 'cdr 'car)
  11114. org-time-stamp-custom-formats)
  11115. time (org-fix-decoded-time t1)
  11116. str (format-time-string
  11117. (concat
  11118. (if inactive "[" "<") (substring tf 1 -1)
  11119. (if inactive "]" ">"))
  11120. (apply 'encode-time time))
  11121. string (replace-match str t t string)
  11122. start (+ start (length str)))))))
  11123. string)
  11124. (defun org-fix-decoded-time (time)
  11125. "Set 0 instead of nil for the first 6 elements of time.
  11126. Don't touch the rest."
  11127. (let ((n 0))
  11128. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  11129. (defun org-days-to-time (timestamp-string)
  11130. "Difference between TIMESTAMP-STRING and now in days."
  11131. (- (time-to-days (org-time-string-to-time timestamp-string))
  11132. (time-to-days (current-time))))
  11133. (defun org-deadline-close (timestamp-string &optional ndays)
  11134. "Is the time in TIMESTAMP-STRING close to the current date?"
  11135. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  11136. (and (< (org-days-to-time timestamp-string) ndays)
  11137. (not (org-entry-is-done-p))))
  11138. (defun org-get-wdays (ts)
  11139. "Get the deadline lead time appropriate for timestring TS."
  11140. (cond
  11141. ((<= org-deadline-warning-days 0)
  11142. ;; 0 or negative, enforce this value no matter what
  11143. (- org-deadline-warning-days))
  11144. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\)" ts)
  11145. ;; lead time is specified.
  11146. (floor (* (string-to-number (match-string 1 ts))
  11147. (cdr (assoc (match-string 2 ts)
  11148. '(("d" . 1) ("w" . 7)
  11149. ("m" . 30.4) ("y" . 365.25)))))))
  11150. ;; go for the default.
  11151. (t org-deadline-warning-days)))
  11152. (defun org-calendar-select-mouse (ev)
  11153. "Return to `org-read-date' with the date currently selected.
  11154. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  11155. (interactive "e")
  11156. (mouse-set-point ev)
  11157. (when (calendar-cursor-to-date)
  11158. (let* ((date (calendar-cursor-to-date))
  11159. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  11160. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  11161. (if (active-minibuffer-window) (exit-minibuffer))))
  11162. (defun org-check-deadlines (ndays)
  11163. "Check if there are any deadlines due or past due.
  11164. A deadline is considered due if it happens within `org-deadline-warning-days'
  11165. days from today's date. If the deadline appears in an entry marked DONE,
  11166. it is not shown. The prefix arg NDAYS can be used to test that many
  11167. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  11168. (interactive "P")
  11169. (let* ((org-warn-days
  11170. (cond
  11171. ((equal ndays '(4)) 100000)
  11172. (ndays (prefix-numeric-value ndays))
  11173. (t (abs org-deadline-warning-days))))
  11174. (case-fold-search nil)
  11175. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  11176. (callback
  11177. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  11178. (message "%d deadlines past-due or due within %d days"
  11179. (org-occur regexp nil callback)
  11180. org-warn-days)))
  11181. (defun org-check-before-date (date)
  11182. "Check if there are deadlines or scheduled entries before DATE."
  11183. (interactive (list (org-read-date)))
  11184. (let ((case-fold-search nil)
  11185. (regexp (concat "\\<\\(" org-deadline-string
  11186. "\\|" org-scheduled-string
  11187. "\\) *<\\([^>]+\\)>"))
  11188. (callback
  11189. (lambda () (time-less-p
  11190. (org-time-string-to-time (match-string 2))
  11191. (org-time-string-to-time date)))))
  11192. (message "%d entries before %s"
  11193. (org-occur regexp nil callback) date)))
  11194. (defun org-evaluate-time-range (&optional to-buffer)
  11195. "Evaluate a time range by computing the difference between start and end.
  11196. Normally the result is just printed in the echo area, but with prefix arg
  11197. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  11198. If the time range is actually in a table, the result is inserted into the
  11199. next column.
  11200. For time difference computation, a year is assumed to be exactly 365
  11201. days in order to avoid rounding problems."
  11202. (interactive "P")
  11203. (or
  11204. (org-clock-update-time-maybe)
  11205. (save-excursion
  11206. (unless (org-at-date-range-p t)
  11207. (goto-char (point-at-bol))
  11208. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  11209. (if (not (org-at-date-range-p t))
  11210. (error "Not at a time-stamp range, and none found in current line")))
  11211. (let* ((ts1 (match-string 1))
  11212. (ts2 (match-string 2))
  11213. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  11214. (match-end (match-end 0))
  11215. (time1 (org-time-string-to-time ts1))
  11216. (time2 (org-time-string-to-time ts2))
  11217. (t1 (time-to-seconds time1))
  11218. (t2 (time-to-seconds time2))
  11219. (diff (abs (- t2 t1)))
  11220. (negative (< (- t2 t1) 0))
  11221. ;; (ys (floor (* 365 24 60 60)))
  11222. (ds (* 24 60 60))
  11223. (hs (* 60 60))
  11224. (fy "%dy %dd %02d:%02d")
  11225. (fy1 "%dy %dd")
  11226. (fd "%dd %02d:%02d")
  11227. (fd1 "%dd")
  11228. (fh "%02d:%02d")
  11229. y d h m align)
  11230. (if havetime
  11231. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  11232. y 0
  11233. d (floor (/ diff ds)) diff (mod diff ds)
  11234. h (floor (/ diff hs)) diff (mod diff hs)
  11235. m (floor (/ diff 60)))
  11236. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  11237. y 0
  11238. d (floor (+ (/ diff ds) 0.5))
  11239. h 0 m 0))
  11240. (if (not to-buffer)
  11241. (message "%s" (org-make-tdiff-string y d h m))
  11242. (if (org-at-table-p)
  11243. (progn
  11244. (goto-char match-end)
  11245. (setq align t)
  11246. (and (looking-at " *|") (goto-char (match-end 0))))
  11247. (goto-char match-end))
  11248. (if (looking-at
  11249. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  11250. (replace-match ""))
  11251. (if negative (insert " -"))
  11252. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  11253. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  11254. (insert " " (format fh h m))))
  11255. (if align (org-table-align))
  11256. (message "Time difference inserted")))))
  11257. (defun org-make-tdiff-string (y d h m)
  11258. (let ((fmt "")
  11259. (l nil))
  11260. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  11261. l (push y l)))
  11262. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  11263. l (push d l)))
  11264. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  11265. l (push h l)))
  11266. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  11267. l (push m l)))
  11268. (apply 'format fmt (nreverse l))))
  11269. (defun org-time-string-to-time (s)
  11270. (apply 'encode-time (org-parse-time-string s)))
  11271. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  11272. "Convert a time stamp to an absolute day number.
  11273. If there is a specifyer for a cyclic time stamp, get the closest date to
  11274. DAYNR.
  11275. PREFER and SHOW_ALL are passed through to `org-closest-date'."
  11276. (cond
  11277. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  11278. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  11279. daynr
  11280. (+ daynr 1000)))
  11281. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  11282. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  11283. (time-to-days (current-time))) (match-string 0 s)
  11284. prefer show-all))
  11285. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  11286. (defun org-days-to-iso-week (days)
  11287. "Return the iso week number."
  11288. (require 'cal-iso)
  11289. (car (calendar-iso-from-absolute days)))
  11290. (defun org-small-year-to-year (year)
  11291. "Convert 2-digit years into 4-digit years.
  11292. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  11293. The year 2000 cannot be abbreviated. Any year lager than 99
  11294. is retrned unchanged."
  11295. (if (< year 38)
  11296. (setq year (+ 2000 year))
  11297. (if (< year 100)
  11298. (setq year (+ 1900 year))))
  11299. year)
  11300. (defun org-time-from-absolute (d)
  11301. "Return the time corresponding to date D.
  11302. D may be an absolute day number, or a calendar-type list (month day year)."
  11303. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  11304. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  11305. (defun org-calendar-holiday ()
  11306. "List of holidays, for Diary display in Org-mode."
  11307. (require 'holidays)
  11308. (let ((hl (funcall
  11309. (if (fboundp 'calendar-check-holidays)
  11310. 'calendar-check-holidays 'check-calendar-holidays) date)))
  11311. (if hl (mapconcat 'identity hl "; "))))
  11312. (defun org-diary-sexp-entry (sexp entry date)
  11313. "Process a SEXP diary ENTRY for DATE."
  11314. (require 'diary-lib)
  11315. (let ((result (if calendar-debug-sexp
  11316. (let ((stack-trace-on-error t))
  11317. (eval (car (read-from-string sexp))))
  11318. (condition-case nil
  11319. (eval (car (read-from-string sexp)))
  11320. (error
  11321. (beep)
  11322. (message "Bad sexp at line %d in %s: %s"
  11323. (org-current-line)
  11324. (buffer-file-name) sexp)
  11325. (sleep-for 2))))))
  11326. (cond ((stringp result) result)
  11327. ((and (consp result)
  11328. (stringp (cdr result))) (cdr result))
  11329. (result entry)
  11330. (t nil))))
  11331. (defun org-diary-to-ical-string (frombuf)
  11332. "Get iCalendar entries from diary entries in buffer FROMBUF.
  11333. This uses the icalendar.el library."
  11334. (let* ((tmpdir (if (featurep 'xemacs)
  11335. (temp-directory)
  11336. temporary-file-directory))
  11337. (tmpfile (make-temp-name
  11338. (expand-file-name "orgics" tmpdir)))
  11339. buf rtn b e)
  11340. (save-excursion
  11341. (set-buffer frombuf)
  11342. (icalendar-export-region (point-min) (point-max) tmpfile)
  11343. (setq buf (find-buffer-visiting tmpfile))
  11344. (set-buffer buf)
  11345. (goto-char (point-min))
  11346. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  11347. (setq b (match-beginning 0)))
  11348. (goto-char (point-max))
  11349. (if (re-search-backward "^END:VEVENT" nil t)
  11350. (setq e (match-end 0)))
  11351. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  11352. (kill-buffer buf)
  11353. (kill-buffer frombuf)
  11354. (delete-file tmpfile)
  11355. rtn))
  11356. (defun org-closest-date (start current change prefer show-all)
  11357. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  11358. When PREFER is `past' return a date that is either CURRENT or past.
  11359. When PREFER is `future', return a date that is either CURRENT or future.
  11360. When SHOW-ALL is nil, only return the current occurence of a time stamp."
  11361. ;; Make the proper lists from the dates
  11362. (catch 'exit
  11363. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  11364. dn dw sday cday n1 n2
  11365. d m y y1 y2 date1 date2 nmonths nm ny m2)
  11366. (setq start (org-date-to-gregorian start)
  11367. current (org-date-to-gregorian
  11368. (if show-all
  11369. current
  11370. (time-to-days (current-time))))
  11371. sday (calendar-absolute-from-gregorian start)
  11372. cday (calendar-absolute-from-gregorian current))
  11373. (if (<= cday sday) (throw 'exit sday))
  11374. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  11375. (setq dn (string-to-number (match-string 1 change))
  11376. dw (cdr (assoc (match-string 2 change) a1)))
  11377. (error "Invalid change specifyer: %s" change))
  11378. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  11379. (cond
  11380. ((eq dw 'day)
  11381. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  11382. n2 (+ n1 dn)))
  11383. ((eq dw 'year)
  11384. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  11385. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  11386. (setq date1 (list m d y1)
  11387. n1 (calendar-absolute-from-gregorian date1)
  11388. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  11389. n2 (calendar-absolute-from-gregorian date2)))
  11390. ((eq dw 'month)
  11391. ;; approx number of month between the tow dates
  11392. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  11393. ;; How often does dn fit in there?
  11394. (setq d (nth 1 start) m (car start) y (nth 2 start)
  11395. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  11396. m (+ m nm)
  11397. ny (floor (/ m 12))
  11398. y (+ y ny)
  11399. m (- m (* ny 12)))
  11400. (while (> m 12) (setq m (- m 12) y (1+ y)))
  11401. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  11402. (setq m2 (+ m dn) y2 y)
  11403. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  11404. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  11405. (while (< n2 cday)
  11406. (setq n1 n2 m m2 y y2)
  11407. (setq m2 (+ m dn) y2 y)
  11408. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  11409. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  11410. (if show-all
  11411. (cond
  11412. ((eq prefer 'past) n1)
  11413. ((eq prefer 'future) (if (= cday n1) n1 n2))
  11414. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  11415. (cond
  11416. ((eq prefer 'past) n1)
  11417. ((eq prefer 'future) (if (= cday n1) n1 n2))
  11418. (t (if (= cday n1) n1 n2)))))))
  11419. (defun org-date-to-gregorian (date)
  11420. "Turn any specification of DATE into a gregorian date for the calendar."
  11421. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  11422. ((and (listp date) (= (length date) 3)) date)
  11423. ((stringp date)
  11424. (setq date (org-parse-time-string date))
  11425. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  11426. ((listp date)
  11427. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  11428. (defun org-parse-time-string (s &optional nodefault)
  11429. "Parse the standard Org-mode time string.
  11430. This should be a lot faster than the normal `parse-time-string'.
  11431. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  11432. hour and minute fields will be nil if not given."
  11433. (if (string-match org-ts-regexp0 s)
  11434. (list 0
  11435. (if (or (match-beginning 8) (not nodefault))
  11436. (string-to-number (or (match-string 8 s) "0")))
  11437. (if (or (match-beginning 7) (not nodefault))
  11438. (string-to-number (or (match-string 7 s) "0")))
  11439. (string-to-number (match-string 4 s))
  11440. (string-to-number (match-string 3 s))
  11441. (string-to-number (match-string 2 s))
  11442. nil nil nil)
  11443. (make-list 9 0)))
  11444. (defun org-timestamp-up (&optional arg)
  11445. "Increase the date item at the cursor by one.
  11446. If the cursor is on the year, change the year. If it is on the month or
  11447. the day, change that.
  11448. With prefix ARG, change by that many units."
  11449. (interactive "p")
  11450. (org-timestamp-change (prefix-numeric-value arg)))
  11451. (defun org-timestamp-down (&optional arg)
  11452. "Decrease the date item at the cursor by one.
  11453. If the cursor is on the year, change the year. If it is on the month or
  11454. the day, change that.
  11455. With prefix ARG, change by that many units."
  11456. (interactive "p")
  11457. (org-timestamp-change (- (prefix-numeric-value arg))))
  11458. (defun org-timestamp-up-day (&optional arg)
  11459. "Increase the date in the time stamp by one day.
  11460. With prefix ARG, change that many days."
  11461. (interactive "p")
  11462. (if (and (not (org-at-timestamp-p t))
  11463. (org-on-heading-p))
  11464. (org-todo 'up)
  11465. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  11466. (defun org-timestamp-down-day (&optional arg)
  11467. "Decrease the date in the time stamp by one day.
  11468. With prefix ARG, change that many days."
  11469. (interactive "p")
  11470. (if (and (not (org-at-timestamp-p t))
  11471. (org-on-heading-p))
  11472. (org-todo 'down)
  11473. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  11474. (defun org-at-timestamp-p (&optional inactive-ok)
  11475. "Determine if the cursor is in or at a timestamp."
  11476. (interactive)
  11477. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  11478. (pos (point))
  11479. (ans (or (looking-at tsr)
  11480. (save-excursion
  11481. (skip-chars-backward "^[<\n\r\t")
  11482. (if (> (point) (point-min)) (backward-char 1))
  11483. (and (looking-at tsr)
  11484. (> (- (match-end 0) pos) -1))))))
  11485. (and ans
  11486. (boundp 'org-ts-what)
  11487. (setq org-ts-what
  11488. (cond
  11489. ((= pos (match-beginning 0)) 'bracket)
  11490. ((= pos (1- (match-end 0))) 'bracket)
  11491. ((org-pos-in-match-range pos 2) 'year)
  11492. ((org-pos-in-match-range pos 3) 'month)
  11493. ((org-pos-in-match-range pos 7) 'hour)
  11494. ((org-pos-in-match-range pos 8) 'minute)
  11495. ((or (org-pos-in-match-range pos 4)
  11496. (org-pos-in-match-range pos 5)) 'day)
  11497. ((and (> pos (or (match-end 8) (match-end 5)))
  11498. (< pos (match-end 0)))
  11499. (- pos (or (match-end 8) (match-end 5))))
  11500. (t 'day))))
  11501. ans))
  11502. (defun org-toggle-timestamp-type ()
  11503. "Toggle the type (<active> or [inactive]) of a time stamp."
  11504. (interactive)
  11505. (when (org-at-timestamp-p t)
  11506. (save-excursion
  11507. (goto-char (match-beginning 0))
  11508. (insert (if (equal (char-after) ?<) "[" "<")) (delete-char 1)
  11509. (goto-char (1- (match-end 0)))
  11510. (insert (if (equal (char-after) ?>) "]" ">")) (delete-char 1))
  11511. (message "Timestamp is now %sactive"
  11512. (if (equal (char-before) ?>) "in" ""))))
  11513. (defun org-timestamp-change (n &optional what)
  11514. "Change the date in the time stamp at point.
  11515. The date will be changed by N times WHAT. WHAT can be `day', `month',
  11516. `year', `minute', `second'. If WHAT is not given, the cursor position
  11517. in the timestamp determines what will be changed."
  11518. (let ((pos (point))
  11519. with-hm inactive
  11520. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  11521. org-ts-what
  11522. extra rem
  11523. ts time time0)
  11524. (if (not (org-at-timestamp-p t))
  11525. (error "Not at a timestamp"))
  11526. (if (and (not what) (eq org-ts-what 'bracket))
  11527. (org-toggle-timestamp-type)
  11528. (if (and (not what) (not (eq org-ts-what 'day))
  11529. org-display-custom-times
  11530. (get-text-property (point) 'display)
  11531. (not (get-text-property (1- (point)) 'display)))
  11532. (setq org-ts-what 'day))
  11533. (setq org-ts-what (or what org-ts-what)
  11534. inactive (= (char-after (match-beginning 0)) ?\[)
  11535. ts (match-string 0))
  11536. (replace-match "")
  11537. (if (string-match
  11538. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\)*\\)[]>]"
  11539. ts)
  11540. (setq extra (match-string 1 ts)))
  11541. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  11542. (setq with-hm t))
  11543. (setq time0 (org-parse-time-string ts))
  11544. (when (and (eq org-ts-what 'minute)
  11545. (eq current-prefix-arg nil))
  11546. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  11547. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  11548. (setcar (cdr time0) (+ (nth 1 time0)
  11549. (if (> n 0) (- rem) (- dm rem))))))
  11550. (setq time
  11551. (encode-time (or (car time0) 0)
  11552. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  11553. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  11554. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  11555. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  11556. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  11557. (nthcdr 6 time0)))
  11558. (when (integerp org-ts-what)
  11559. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  11560. (if (eq what 'calendar)
  11561. (let ((cal-date (org-get-date-from-calendar)))
  11562. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  11563. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  11564. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  11565. (setcar time0 (or (car time0) 0))
  11566. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  11567. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  11568. (setq time (apply 'encode-time time0))))
  11569. (setq org-last-changed-timestamp
  11570. (org-insert-time-stamp time with-hm inactive nil nil extra))
  11571. (org-clock-update-time-maybe)
  11572. (goto-char pos)
  11573. ;; Try to recenter the calendar window, if any
  11574. (if (and org-calendar-follow-timestamp-change
  11575. (get-buffer-window "*Calendar*" t)
  11576. (memq org-ts-what '(day month year)))
  11577. (org-recenter-calendar (time-to-days time))))))
  11578. (defun org-modify-ts-extra (s pos n dm)
  11579. "Change the different parts of the lead-time and repeat fields in timestamp."
  11580. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  11581. ng h m new rem)
  11582. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  11583. (cond
  11584. ((or (org-pos-in-match-range pos 2)
  11585. (org-pos-in-match-range pos 3))
  11586. (setq m (string-to-number (match-string 3 s))
  11587. h (string-to-number (match-string 2 s)))
  11588. (if (org-pos-in-match-range pos 2)
  11589. (setq h (+ h n))
  11590. (setq n (* dm (org-no-warnings (signum n))))
  11591. (when (not (= 0 (setq rem (% m dm))))
  11592. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  11593. (setq m (+ m n)))
  11594. (if (< m 0) (setq m (+ m 60) h (1- h)))
  11595. (if (> m 59) (setq m (- m 60) h (1+ h)))
  11596. (setq h (min 24 (max 0 h)))
  11597. (setq ng 1 new (format "-%02d:%02d" h m)))
  11598. ((org-pos-in-match-range pos 6)
  11599. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  11600. ((org-pos-in-match-range pos 5)
  11601. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  11602. ((org-pos-in-match-range pos 9)
  11603. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  11604. ((org-pos-in-match-range pos 8)
  11605. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  11606. (when ng
  11607. (setq s (concat
  11608. (substring s 0 (match-beginning ng))
  11609. new
  11610. (substring s (match-end ng))))))
  11611. s))
  11612. (defun org-recenter-calendar (date)
  11613. "If the calendar is visible, recenter it to DATE."
  11614. (let* ((win (selected-window))
  11615. (cwin (get-buffer-window "*Calendar*" t))
  11616. (calendar-move-hook nil))
  11617. (when cwin
  11618. (select-window cwin)
  11619. (calendar-goto-date (if (listp date) date
  11620. (calendar-gregorian-from-absolute date)))
  11621. (select-window win))))
  11622. (defun org-goto-calendar (&optional arg)
  11623. "Go to the Emacs calendar at the current date.
  11624. If there is a time stamp in the current line, go to that date.
  11625. A prefix ARG can be used to force the current date."
  11626. (interactive "P")
  11627. (let ((tsr org-ts-regexp) diff
  11628. (calendar-move-hook nil)
  11629. (calendar-view-holidays-initially-flag nil)
  11630. (view-calendar-holidays-initially nil)
  11631. (calendar-view-diary-initially-flag nil)
  11632. (view-diary-entries-initially nil))
  11633. (if (or (org-at-timestamp-p)
  11634. (save-excursion
  11635. (beginning-of-line 1)
  11636. (looking-at (concat ".*" tsr))))
  11637. (let ((d1 (time-to-days (current-time)))
  11638. (d2 (time-to-days
  11639. (org-time-string-to-time (match-string 1)))))
  11640. (setq diff (- d2 d1))))
  11641. (calendar)
  11642. (calendar-goto-today)
  11643. (if (and diff (not arg)) (calendar-forward-day diff))))
  11644. (defun org-get-date-from-calendar ()
  11645. "Return a list (month day year) of date at point in calendar."
  11646. (with-current-buffer "*Calendar*"
  11647. (save-match-data
  11648. (calendar-cursor-to-date))))
  11649. (defun org-date-from-calendar ()
  11650. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  11651. If there is already a time stamp at the cursor position, update it."
  11652. (interactive)
  11653. (if (org-at-timestamp-p t)
  11654. (org-timestamp-change 0 'calendar)
  11655. (let ((cal-date (org-get-date-from-calendar)))
  11656. (org-insert-time-stamp
  11657. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  11658. (defun org-minutes-to-hours (m)
  11659. "Compute H:MM from a number of minutes."
  11660. (let ((h (/ m 60)))
  11661. (setq m (- m (* 60 h)))
  11662. (format "%d:%02d" h m)))
  11663. ;;;; Agenda files
  11664. ;;;###autoload
  11665. (defun org-iswitchb (&optional arg)
  11666. "Use `iswitchb-read-buffer' to prompt for an Org buffer to switch to.
  11667. With a prefix argument, restrict available to files.
  11668. With two prefix arguments, restrict available buffers to agenda files.
  11669. Due to some yet unresolved reason, global function
  11670. `iswitchb-mode' needs to be active for this function to work."
  11671. (interactive "P")
  11672. (require 'iswitchb)
  11673. (let ((enabled iswitchb-mode) blist)
  11674. (or enabled (iswitchb-mode 1))
  11675. (setq blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  11676. ((equal arg '(16)) (org-buffer-list 'agenda))
  11677. (t (org-buffer-list))))
  11678. (unwind-protect
  11679. (let ((iswitchb-make-buflist-hook
  11680. (lambda ()
  11681. (setq iswitchb-temp-buflist
  11682. (mapcar 'buffer-name blist)))))
  11683. (switch-to-buffer
  11684. (iswitchb-read-buffer
  11685. "Switch-to: " nil t))
  11686. (or enabled (iswitchb-mode -1))))))
  11687. (defun org-buffer-list (&optional predicate tmp)
  11688. "Return a list of Org buffers.
  11689. PREDICATE can be either 'export, 'files or 'agenda.
  11690. 'export restrict the list to Export buffers.
  11691. 'files restrict the list to buffers visiting Org files.
  11692. 'agenda restrict the list to buffers visiting agenda files.
  11693. If TMP is non-nil, don't include temporary buffers."
  11694. (let (filter blist)
  11695. (setq filter
  11696. (cond ((eq predicate 'files) "\.org$")
  11697. ((eq predicate 'export) "\*Org .*Export")
  11698. (t "\*Org \\|\.org$")))
  11699. (setq blist
  11700. (mapcar
  11701. (lambda(b)
  11702. (let ((bname (buffer-name b))
  11703. (bfile (buffer-file-name b)))
  11704. (if (and (string-match filter bname)
  11705. (if (eq predicate 'agenda)
  11706. (member bfile
  11707. (mapcar (lambda(f) (file-truename f))
  11708. org-agenda-files)) t)
  11709. (if tmp (not (string-match "tmp" bname)) t)) b)))
  11710. (buffer-list)))
  11711. (delete nil blist)))
  11712. (defun org-agenda-files (&optional unrestricted)
  11713. "Get the list of agenda files.
  11714. Optional UNRESTRICTED means return the full list even if a restriction
  11715. is currently in place."
  11716. (let ((files
  11717. (cond
  11718. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  11719. ((stringp org-agenda-files) (org-read-agenda-file-list))
  11720. ((listp org-agenda-files) org-agenda-files)
  11721. (t (error "Invalid value of `org-agenda-files'")))))
  11722. (setq files (apply 'append
  11723. (mapcar (lambda (f)
  11724. (if (file-directory-p f)
  11725. (directory-files f t
  11726. org-agenda-file-regexp)
  11727. (list f)))
  11728. files)))
  11729. (if org-agenda-skip-unavailable-files
  11730. (delq nil
  11731. (mapcar (function
  11732. (lambda (file)
  11733. (and (file-readable-p file) file)))
  11734. files))
  11735. files))) ; `org-check-agenda-file' will remove them from the list
  11736. (defun org-edit-agenda-file-list ()
  11737. "Edit the list of agenda files.
  11738. Depending on setup, this either uses customize to edit the variable
  11739. `org-agenda-files', or it visits the file that is holding the list. In the
  11740. latter case, the buffer is set up in a way that saving it automatically kills
  11741. the buffer and restores the previous window configuration."
  11742. (interactive)
  11743. (if (stringp org-agenda-files)
  11744. (let ((cw (current-window-configuration)))
  11745. (find-file org-agenda-files)
  11746. (org-set-local 'org-window-configuration cw)
  11747. (org-add-hook 'after-save-hook
  11748. (lambda ()
  11749. (set-window-configuration
  11750. (prog1 org-window-configuration
  11751. (kill-buffer (current-buffer))))
  11752. (org-install-agenda-files-menu)
  11753. (message "New agenda file list installed"))
  11754. nil 'local)
  11755. (message "%s" (substitute-command-keys
  11756. "Edit list and finish with \\[save-buffer]")))
  11757. (customize-variable 'org-agenda-files)))
  11758. (defun org-store-new-agenda-file-list (list)
  11759. "Set new value for the agenda file list and save it correcly."
  11760. (if (stringp org-agenda-files)
  11761. (let ((f org-agenda-files) b)
  11762. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  11763. (with-temp-file f
  11764. (insert (mapconcat 'identity list "\n") "\n")))
  11765. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  11766. (setq org-agenda-files list)
  11767. (customize-save-variable 'org-agenda-files org-agenda-files))))
  11768. (defun org-read-agenda-file-list ()
  11769. "Read the list of agenda files from a file."
  11770. (when (stringp org-agenda-files)
  11771. (with-temp-buffer
  11772. (insert-file-contents org-agenda-files)
  11773. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  11774. ;;;###autoload
  11775. (defun org-cycle-agenda-files ()
  11776. "Cycle through the files in `org-agenda-files'.
  11777. If the current buffer visits an agenda file, find the next one in the list.
  11778. If the current buffer does not, find the first agenda file."
  11779. (interactive)
  11780. (let* ((fs (org-agenda-files t))
  11781. (files (append fs (list (car fs))))
  11782. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  11783. file)
  11784. (unless files (error "No agenda files"))
  11785. (catch 'exit
  11786. (while (setq file (pop files))
  11787. (if (equal (file-truename file) tcf)
  11788. (when (car files)
  11789. (find-file (car files))
  11790. (throw 'exit t))))
  11791. (find-file (car fs)))
  11792. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  11793. (defun org-agenda-file-to-front (&optional to-end)
  11794. "Move/add the current file to the top of the agenda file list.
  11795. If the file is not present in the list, it is added to the front. If it is
  11796. present, it is moved there. With optional argument TO-END, add/move to the
  11797. end of the list."
  11798. (interactive "P")
  11799. (let ((org-agenda-skip-unavailable-files nil)
  11800. (file-alist (mapcar (lambda (x)
  11801. (cons (file-truename x) x))
  11802. (org-agenda-files t)))
  11803. (ctf (file-truename buffer-file-name))
  11804. x had)
  11805. (setq x (assoc ctf file-alist) had x)
  11806. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  11807. (if to-end
  11808. (setq file-alist (append (delq x file-alist) (list x)))
  11809. (setq file-alist (cons x (delq x file-alist))))
  11810. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  11811. (org-install-agenda-files-menu)
  11812. (message "File %s to %s of agenda file list"
  11813. (if had "moved" "added") (if to-end "end" "front"))))
  11814. (defun org-remove-file (&optional file)
  11815. "Remove current file from the list of files in variable `org-agenda-files'.
  11816. These are the files which are being checked for agenda entries.
  11817. Optional argument FILE means, use this file instead of the current."
  11818. (interactive)
  11819. (let* ((org-agenda-skip-unavailable-files nil)
  11820. (file (or file buffer-file-name))
  11821. (true-file (file-truename file))
  11822. (afile (abbreviate-file-name file))
  11823. (files (delq nil (mapcar
  11824. (lambda (x)
  11825. (if (equal true-file
  11826. (file-truename x))
  11827. nil x))
  11828. (org-agenda-files t)))))
  11829. (if (not (= (length files) (length (org-agenda-files t))))
  11830. (progn
  11831. (org-store-new-agenda-file-list files)
  11832. (org-install-agenda-files-menu)
  11833. (message "Removed file: %s" afile))
  11834. (message "File was not in list: %s (not removed)" afile))))
  11835. (defun org-file-menu-entry (file)
  11836. (vector file (list 'find-file file) t))
  11837. (defun org-check-agenda-file (file)
  11838. "Make sure FILE exists. If not, ask user what to do."
  11839. (when (not (file-exists-p file))
  11840. (message "non-existent file %s. [R]emove from list or [A]bort?"
  11841. (abbreviate-file-name file))
  11842. (let ((r (downcase (read-char-exclusive))))
  11843. (cond
  11844. ((equal r ?r)
  11845. (org-remove-file file)
  11846. (throw 'nextfile t))
  11847. (t (error "Abort"))))))
  11848. (defun org-get-agenda-file-buffer (file)
  11849. "Get a buffer visiting FILE. If the buffer needs to be created, add
  11850. it to the list of buffers which might be released later."
  11851. (let ((buf (org-find-base-buffer-visiting file)))
  11852. (if buf
  11853. buf ; just return it
  11854. ;; Make a new buffer and remember it
  11855. (setq buf (find-file-noselect file))
  11856. (if buf (push buf org-agenda-new-buffers))
  11857. buf)))
  11858. (defun org-release-buffers (blist)
  11859. "Release all buffers in list, asking the user for confirmation when needed.
  11860. When a buffer is unmodified, it is just killed. When modified, it is saved
  11861. \(if the user agrees) and then killed."
  11862. (let (buf file)
  11863. (while (setq buf (pop blist))
  11864. (setq file (buffer-file-name buf))
  11865. (when (and (buffer-modified-p buf)
  11866. file
  11867. (y-or-n-p (format "Save file %s? " file)))
  11868. (with-current-buffer buf (save-buffer)))
  11869. (kill-buffer buf))))
  11870. (defun org-prepare-agenda-buffers (files)
  11871. "Create buffers for all agenda files, protect archived trees and comments."
  11872. (interactive)
  11873. (let ((pa '(:org-archived t))
  11874. (pc '(:org-comment t))
  11875. (pall '(:org-archived t :org-comment t))
  11876. (inhibit-read-only t)
  11877. (rea (concat ":" org-archive-tag ":"))
  11878. bmp file re)
  11879. (save-excursion
  11880. (save-restriction
  11881. (while (setq file (pop files))
  11882. (if (bufferp file)
  11883. (set-buffer file)
  11884. (org-check-agenda-file file)
  11885. (set-buffer (org-get-agenda-file-buffer file)))
  11886. (widen)
  11887. (setq bmp (buffer-modified-p))
  11888. (org-refresh-category-properties)
  11889. (setq org-todo-keywords-for-agenda
  11890. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  11891. (setq org-done-keywords-for-agenda
  11892. (append org-done-keywords-for-agenda org-done-keywords))
  11893. (save-excursion
  11894. (remove-text-properties (point-min) (point-max) pall)
  11895. (when org-agenda-skip-archived-trees
  11896. (goto-char (point-min))
  11897. (while (re-search-forward rea nil t)
  11898. (if (org-on-heading-p t)
  11899. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  11900. (goto-char (point-min))
  11901. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  11902. (while (re-search-forward re nil t)
  11903. (add-text-properties
  11904. (match-beginning 0) (org-end-of-subtree t) pc)))
  11905. (set-buffer-modified-p bmp))))))
  11906. ;;;; Embedded LaTeX
  11907. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  11908. "Keymap for the minor `org-cdlatex-mode'.")
  11909. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  11910. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  11911. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  11912. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  11913. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  11914. (defvar org-cdlatex-texmathp-advice-is-done nil
  11915. "Flag remembering if we have applied the advice to texmathp already.")
  11916. (define-minor-mode org-cdlatex-mode
  11917. "Toggle the minor `org-cdlatex-mode'.
  11918. This mode supports entering LaTeX environment and math in LaTeX fragments
  11919. in Org-mode.
  11920. \\{org-cdlatex-mode-map}"
  11921. nil " OCDL" nil
  11922. (when org-cdlatex-mode (require 'cdlatex))
  11923. (unless org-cdlatex-texmathp-advice-is-done
  11924. (setq org-cdlatex-texmathp-advice-is-done t)
  11925. (defadvice texmathp (around org-math-always-on activate)
  11926. "Always return t in org-mode buffers.
  11927. This is because we want to insert math symbols without dollars even outside
  11928. the LaTeX math segments. If Orgmode thinks that point is actually inside
  11929. en embedded LaTeX fragement, let texmathp do its job.
  11930. \\[org-cdlatex-mode-map]"
  11931. (interactive)
  11932. (let (p)
  11933. (cond
  11934. ((not (org-mode-p)) ad-do-it)
  11935. ((eq this-command 'cdlatex-math-symbol)
  11936. (setq ad-return-value t
  11937. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  11938. (t
  11939. (let ((p (org-inside-LaTeX-fragment-p)))
  11940. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  11941. (setq ad-return-value t
  11942. texmathp-why '("Org-mode embedded math" . 0))
  11943. (if p ad-do-it)))))))))
  11944. (defun turn-on-org-cdlatex ()
  11945. "Unconditionally turn on `org-cdlatex-mode'."
  11946. (org-cdlatex-mode 1))
  11947. (defun org-inside-LaTeX-fragment-p ()
  11948. "Test if point is inside a LaTeX fragment.
  11949. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  11950. sequence appearing also before point.
  11951. Even though the matchers for math are configurable, this function assumes
  11952. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  11953. delimiters are skipped when they have been removed by customization.
  11954. The return value is nil, or a cons cell with the delimiter and
  11955. and the position of this delimiter.
  11956. This function does a reasonably good job, but can locally be fooled by
  11957. for example currency specifications. For example it will assume being in
  11958. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  11959. fragments that are properly closed, but during editing, we have to live
  11960. with the uncertainty caused by missing closing delimiters. This function
  11961. looks only before point, not after."
  11962. (catch 'exit
  11963. (let ((pos (point))
  11964. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  11965. (lim (progn
  11966. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  11967. (point)))
  11968. dd-on str (start 0) m re)
  11969. (goto-char pos)
  11970. (when dodollar
  11971. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  11972. re (nth 1 (assoc "$" org-latex-regexps)))
  11973. (while (string-match re str start)
  11974. (cond
  11975. ((= (match-end 0) (length str))
  11976. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  11977. ((= (match-end 0) (- (length str) 5))
  11978. (throw 'exit nil))
  11979. (t (setq start (match-end 0))))))
  11980. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  11981. (goto-char pos)
  11982. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  11983. (and (match-beginning 2) (throw 'exit nil))
  11984. ;; count $$
  11985. (while (re-search-backward "\\$\\$" lim t)
  11986. (setq dd-on (not dd-on)))
  11987. (goto-char pos)
  11988. (if dd-on (cons "$$" m))))))
  11989. (defun org-try-cdlatex-tab ()
  11990. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  11991. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  11992. - inside a LaTeX fragment, or
  11993. - after the first word in a line, where an abbreviation expansion could
  11994. insert a LaTeX environment."
  11995. (when org-cdlatex-mode
  11996. (cond
  11997. ((save-excursion
  11998. (skip-chars-backward "a-zA-Z0-9*")
  11999. (skip-chars-backward " \t")
  12000. (bolp))
  12001. (cdlatex-tab) t)
  12002. ((org-inside-LaTeX-fragment-p)
  12003. (cdlatex-tab) t)
  12004. (t nil))))
  12005. (defun org-cdlatex-underscore-caret (&optional arg)
  12006. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  12007. Revert to the normal definition outside of these fragments."
  12008. (interactive "P")
  12009. (if (org-inside-LaTeX-fragment-p)
  12010. (call-interactively 'cdlatex-sub-superscript)
  12011. (let (org-cdlatex-mode)
  12012. (call-interactively (key-binding (vector last-input-event))))))
  12013. (defun org-cdlatex-math-modify (&optional arg)
  12014. "Execute `cdlatex-math-modify' in LaTeX fragments.
  12015. Revert to the normal definition outside of these fragments."
  12016. (interactive "P")
  12017. (if (org-inside-LaTeX-fragment-p)
  12018. (call-interactively 'cdlatex-math-modify)
  12019. (let (org-cdlatex-mode)
  12020. (call-interactively (key-binding (vector last-input-event))))))
  12021. (defvar org-latex-fragment-image-overlays nil
  12022. "List of overlays carrying the images of latex fragments.")
  12023. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  12024. (defun org-remove-latex-fragment-image-overlays ()
  12025. "Remove all overlays with LaTeX fragment images in current buffer."
  12026. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  12027. (setq org-latex-fragment-image-overlays nil))
  12028. (defun org-preview-latex-fragment (&optional subtree)
  12029. "Preview the LaTeX fragment at point, or all locally or globally.
  12030. If the cursor is in a LaTeX fragment, create the image and overlay
  12031. it over the source code. If there is no fragment at point, display
  12032. all fragments in the current text, from one headline to the next. With
  12033. prefix SUBTREE, display all fragments in the current subtree. With a
  12034. double prefix `C-u C-u', or when the cursor is before the first headline,
  12035. display all fragments in the buffer.
  12036. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  12037. (interactive "P")
  12038. (org-remove-latex-fragment-image-overlays)
  12039. (save-excursion
  12040. (save-restriction
  12041. (let (beg end at msg)
  12042. (cond
  12043. ((or (equal subtree '(16))
  12044. (not (save-excursion
  12045. (re-search-backward (concat "^" outline-regexp) nil t))))
  12046. (setq beg (point-min) end (point-max)
  12047. msg "Creating images for buffer...%s"))
  12048. ((equal subtree '(4))
  12049. (org-back-to-heading)
  12050. (setq beg (point) end (org-end-of-subtree t)
  12051. msg "Creating images for subtree...%s"))
  12052. (t
  12053. (if (setq at (org-inside-LaTeX-fragment-p))
  12054. (goto-char (max (point-min) (- (cdr at) 2)))
  12055. (org-back-to-heading))
  12056. (setq beg (point) end (progn (outline-next-heading) (point))
  12057. msg (if at "Creating image...%s"
  12058. "Creating images for entry...%s"))))
  12059. (message msg "")
  12060. (narrow-to-region beg end)
  12061. (goto-char beg)
  12062. (org-format-latex
  12063. (concat "ltxpng/" (file-name-sans-extension
  12064. (file-name-nondirectory
  12065. buffer-file-name)))
  12066. default-directory 'overlays msg at 'forbuffer)
  12067. (message msg "done. Use `C-c C-c' to remove images.")))))
  12068. (defvar org-latex-regexps
  12069. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  12070. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  12071. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  12072. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([ .,?;:'\")\000]\\|$\\)" 2 nil)
  12073. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  12074. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  12075. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  12076. "Regular expressions for matching embedded LaTeX.")
  12077. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  12078. "Replace LaTeX fragments with links to an image, and produce images."
  12079. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  12080. (let* ((prefixnodir (file-name-nondirectory prefix))
  12081. (absprefix (expand-file-name prefix dir))
  12082. (todir (file-name-directory absprefix))
  12083. (opt org-format-latex-options)
  12084. (matchers (plist-get opt :matchers))
  12085. (re-list org-latex-regexps)
  12086. (cnt 0) txt link beg end re e checkdir
  12087. m n block linkfile movefile ov)
  12088. ;; Check if there are old images files with this prefix, and remove them
  12089. (when (file-directory-p todir)
  12090. (mapc 'delete-file
  12091. (directory-files
  12092. todir 'full
  12093. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  12094. ;; Check the different regular expressions
  12095. (while (setq e (pop re-list))
  12096. (setq m (car e) re (nth 1 e) n (nth 2 e)
  12097. block (if (nth 3 e) "\n\n" ""))
  12098. (when (member m matchers)
  12099. (goto-char (point-min))
  12100. (while (re-search-forward re nil t)
  12101. (when (or (not at) (equal (cdr at) (match-beginning n)))
  12102. (setq txt (match-string n)
  12103. beg (match-beginning n) end (match-end n)
  12104. cnt (1+ cnt)
  12105. linkfile (format "%s_%04d.png" prefix cnt)
  12106. movefile (format "%s_%04d.png" absprefix cnt)
  12107. link (concat block "[[file:" linkfile "]]" block))
  12108. (if msg (message msg cnt))
  12109. (goto-char beg)
  12110. (unless checkdir ; make sure the directory exists
  12111. (setq checkdir t)
  12112. (or (file-directory-p todir) (make-directory todir)))
  12113. (org-create-formula-image
  12114. txt movefile opt forbuffer)
  12115. (if overlays
  12116. (progn
  12117. (setq ov (org-make-overlay beg end))
  12118. (if (featurep 'xemacs)
  12119. (progn
  12120. (org-overlay-put ov 'invisible t)
  12121. (org-overlay-put
  12122. ov 'end-glyph
  12123. (make-glyph (vector 'png :file movefile))))
  12124. (org-overlay-put
  12125. ov 'display
  12126. (list 'image :type 'png :file movefile :ascent 'center)))
  12127. (push ov org-latex-fragment-image-overlays)
  12128. (goto-char end))
  12129. (delete-region beg end)
  12130. (insert link))))))))
  12131. ;; This function borrows from Ganesh Swami's latex2png.el
  12132. (defun org-create-formula-image (string tofile options buffer)
  12133. (let* ((tmpdir (if (featurep 'xemacs)
  12134. (temp-directory)
  12135. temporary-file-directory))
  12136. (texfilebase (make-temp-name
  12137. (expand-file-name "orgtex" tmpdir)))
  12138. (texfile (concat texfilebase ".tex"))
  12139. (dvifile (concat texfilebase ".dvi"))
  12140. (pngfile (concat texfilebase ".png"))
  12141. (fnh (face-attribute 'default :height nil))
  12142. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  12143. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  12144. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  12145. "Black"))
  12146. (bg (or (plist-get options (if buffer :background :html-background))
  12147. "Transparent")))
  12148. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  12149. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  12150. (with-temp-file texfile
  12151. (insert org-format-latex-header
  12152. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  12153. (let ((dir default-directory))
  12154. (condition-case nil
  12155. (progn
  12156. (cd tmpdir)
  12157. (call-process "latex" nil nil nil texfile))
  12158. (error nil))
  12159. (cd dir))
  12160. (if (not (file-exists-p dvifile))
  12161. (progn (message "Failed to create dvi file from %s" texfile) nil)
  12162. (call-process "dvipng" nil nil nil
  12163. "-E" "-fg" fg "-bg" bg
  12164. "-D" dpi
  12165. ;;"-x" scale "-y" scale
  12166. "-T" "tight"
  12167. "-o" pngfile
  12168. dvifile)
  12169. (if (not (file-exists-p pngfile))
  12170. (progn (message "Failed to create png file from %s" texfile) nil)
  12171. ;; Use the requested file name and clean up
  12172. (copy-file pngfile tofile 'replace)
  12173. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  12174. (delete-file (concat texfilebase e)))
  12175. pngfile))))
  12176. (defun org-dvipng-color (attr)
  12177. "Return an rgb color specification for dvipng."
  12178. (apply 'format "rgb %s %s %s"
  12179. (mapcar 'org-normalize-color
  12180. (color-values (face-attribute 'default attr nil)))))
  12181. (defun org-normalize-color (value)
  12182. "Return string to be used as color value for an RGB component."
  12183. (format "%g" (/ value 65535.0)))
  12184. ;;;; Key bindings
  12185. ;; Make `C-c C-x' a prefix key
  12186. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  12187. ;; TAB key with modifiers
  12188. (org-defkey org-mode-map "\C-i" 'org-cycle)
  12189. (org-defkey org-mode-map [(tab)] 'org-cycle)
  12190. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  12191. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  12192. (org-defkey org-mode-map "\M-\t" 'org-complete)
  12193. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  12194. ;; The following line is necessary under Suse GNU/Linux
  12195. (unless (featurep 'xemacs)
  12196. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  12197. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  12198. (define-key org-mode-map [backtab] 'org-shifttab)
  12199. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  12200. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  12201. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  12202. ;; Cursor keys with modifiers
  12203. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  12204. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  12205. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  12206. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  12207. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  12208. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  12209. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  12210. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  12211. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  12212. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  12213. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  12214. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  12215. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  12216. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  12217. ;;; Extra keys for tty access.
  12218. ;; We only set them when really needed because otherwise the
  12219. ;; menus don't show the simple keys
  12220. (when (or (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  12221. (not window-system))
  12222. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  12223. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  12224. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  12225. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  12226. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  12227. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  12228. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  12229. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  12230. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  12231. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  12232. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  12233. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  12234. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  12235. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  12236. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  12237. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  12238. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  12239. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  12240. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  12241. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  12242. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  12243. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
  12244. ;; All the other keys
  12245. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  12246. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  12247. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree)
  12248. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  12249. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  12250. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  12251. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  12252. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-attic-sibling)
  12253. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  12254. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  12255. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  12256. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  12257. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  12258. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  12259. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  12260. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  12261. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  12262. (org-defkey org-mode-map "\C-c\\" 'org-tags-sparse-tree) ; Minor-mode res.
  12263. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  12264. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  12265. (org-defkey org-mode-map [(control return)] 'org-insert-heading-after-current)
  12266. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  12267. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  12268. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  12269. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  12270. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  12271. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  12272. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  12273. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  12274. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  12275. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  12276. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  12277. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  12278. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  12279. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  12280. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  12281. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  12282. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  12283. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  12284. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  12285. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  12286. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  12287. (org-defkey org-mode-map "\C-c^" 'org-sort)
  12288. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  12289. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  12290. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  12291. (org-defkey org-mode-map "\C-m" 'org-return)
  12292. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  12293. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  12294. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  12295. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  12296. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  12297. (org-defkey org-mode-map "\C-c'" 'org-table-edit-formulas)
  12298. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  12299. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  12300. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  12301. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  12302. (org-defkey org-mode-map "\C-c\C-q" 'org-table-wrap-region)
  12303. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  12304. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  12305. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  12306. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  12307. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  12308. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-cut-special)
  12309. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  12310. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  12311. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  12312. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  12313. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  12314. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  12315. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  12316. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  12317. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  12318. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  12319. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  12320. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  12321. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  12322. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  12323. (org-defkey org-mode-map "\C-c\C-xr" 'org-insert-columns-dblock)
  12324. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  12325. (when (featurep 'xemacs)
  12326. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  12327. (defvar org-table-auto-blank-field) ; defined in org-table.el
  12328. (defun org-self-insert-command (N)
  12329. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  12330. If the cursor is in a table looking at whitespace, the whitespace is
  12331. overwritten, and the table is not marked as requiring realignment."
  12332. (interactive "p")
  12333. (if (and (org-table-p)
  12334. (progn
  12335. ;; check if we blank the field, and if that triggers align
  12336. (and (featurep 'org-table) org-table-auto-blank-field
  12337. (member last-command
  12338. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  12339. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  12340. ;; got extra space, this field does not determine column width
  12341. (let (org-table-may-need-update) (org-table-blank-field))
  12342. ;; no extra space, this field may determine column width
  12343. (org-table-blank-field)))
  12344. t)
  12345. (eq N 1)
  12346. (looking-at "[^|\n]* |"))
  12347. (let (org-table-may-need-update)
  12348. (goto-char (1- (match-end 0)))
  12349. (delete-backward-char 1)
  12350. (goto-char (match-beginning 0))
  12351. (self-insert-command N))
  12352. (setq org-table-may-need-update t)
  12353. (self-insert-command N)
  12354. (org-fix-tags-on-the-fly)))
  12355. (defun org-fix-tags-on-the-fly ()
  12356. (when (and (equal (char-after (point-at-bol)) ?*)
  12357. (org-on-heading-p))
  12358. (org-align-tags-here org-tags-column)))
  12359. (defun org-delete-backward-char (N)
  12360. "Like `delete-backward-char', insert whitespace at field end in tables.
  12361. When deleting backwards, in tables this function will insert whitespace in
  12362. front of the next \"|\" separator, to keep the table aligned. The table will
  12363. still be marked for re-alignment if the field did fill the entire column,
  12364. because, in this case the deletion might narrow the column."
  12365. (interactive "p")
  12366. (if (and (org-table-p)
  12367. (eq N 1)
  12368. (string-match "|" (buffer-substring (point-at-bol) (point)))
  12369. (looking-at ".*?|"))
  12370. (let ((pos (point))
  12371. (noalign (looking-at "[^|\n\r]* |"))
  12372. (c org-table-may-need-update))
  12373. (backward-delete-char N)
  12374. (skip-chars-forward "^|")
  12375. (insert " ")
  12376. (goto-char (1- pos))
  12377. ;; noalign: if there were two spaces at the end, this field
  12378. ;; does not determine the width of the column.
  12379. (if noalign (setq org-table-may-need-update c)))
  12380. (backward-delete-char N)
  12381. (org-fix-tags-on-the-fly)))
  12382. (defun org-delete-char (N)
  12383. "Like `delete-char', but insert whitespace at field end in tables.
  12384. When deleting characters, in tables this function will insert whitespace in
  12385. front of the next \"|\" separator, to keep the table aligned. The table will
  12386. still be marked for re-alignment if the field did fill the entire column,
  12387. because, in this case the deletion might narrow the column."
  12388. (interactive "p")
  12389. (if (and (org-table-p)
  12390. (not (bolp))
  12391. (not (= (char-after) ?|))
  12392. (eq N 1))
  12393. (if (looking-at ".*?|")
  12394. (let ((pos (point))
  12395. (noalign (looking-at "[^|\n\r]* |"))
  12396. (c org-table-may-need-update))
  12397. (replace-match (concat
  12398. (substring (match-string 0) 1 -1)
  12399. " |"))
  12400. (goto-char pos)
  12401. ;; noalign: if there were two spaces at the end, this field
  12402. ;; does not determine the width of the column.
  12403. (if noalign (setq org-table-may-need-update c)))
  12404. (delete-char N))
  12405. (delete-char N)
  12406. (org-fix-tags-on-the-fly)))
  12407. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  12408. (put 'org-self-insert-command 'delete-selection t)
  12409. (put 'orgtbl-self-insert-command 'delete-selection t)
  12410. (put 'org-delete-char 'delete-selection 'supersede)
  12411. (put 'org-delete-backward-char 'delete-selection 'supersede)
  12412. ;; Make `flyspell-mode' delay after some commands
  12413. (put 'org-self-insert-command 'flyspell-delayed t)
  12414. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  12415. (put 'org-delete-char 'flyspell-delayed t)
  12416. (put 'org-delete-backward-char 'flyspell-delayed t)
  12417. ;; Make pabbrev-mode expand after org-mode commands
  12418. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  12419. (put 'orgybl-self-insert-command 'pabbrev-expand-after-command t)
  12420. ;; How to do this: Measure non-white length of current string
  12421. ;; If equal to column width, we should realign.
  12422. (defun org-remap (map &rest commands)
  12423. "In MAP, remap the functions given in COMMANDS.
  12424. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  12425. (let (new old)
  12426. (while commands
  12427. (setq old (pop commands) new (pop commands))
  12428. (if (fboundp 'command-remapping)
  12429. (org-defkey map (vector 'remap old) new)
  12430. (substitute-key-definition old new map global-map)))))
  12431. (when (eq org-enable-table-editor 'optimized)
  12432. ;; If the user wants maximum table support, we need to hijack
  12433. ;; some standard editing functions
  12434. (org-remap org-mode-map
  12435. 'self-insert-command 'org-self-insert-command
  12436. 'delete-char 'org-delete-char
  12437. 'delete-backward-char 'org-delete-backward-char)
  12438. (org-defkey org-mode-map "|" 'org-force-self-insert))
  12439. (defun org-shiftcursor-error ()
  12440. "Throw an error because Shift-Cursor command was applied in wrong context."
  12441. (error "This command is active in special context like tables, headlines or timestamps"))
  12442. (defun org-shifttab (&optional arg)
  12443. "Global visibility cycling or move to previous table field.
  12444. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  12445. on context.
  12446. See the individual commands for more information."
  12447. (interactive "P")
  12448. (cond
  12449. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  12450. (arg (message "Content view to level: ")
  12451. (org-content (prefix-numeric-value arg))
  12452. (setq org-cycle-global-status 'overview))
  12453. (t (call-interactively 'org-global-cycle))))
  12454. (defun org-shiftmetaleft ()
  12455. "Promote subtree or delete table column.
  12456. Calls `org-promote-subtree', `org-outdent-item',
  12457. or `org-table-delete-column', depending on context.
  12458. See the individual commands for more information."
  12459. (interactive)
  12460. (cond
  12461. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  12462. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  12463. ((org-at-item-p) (call-interactively 'org-outdent-item))
  12464. (t (org-shiftcursor-error))))
  12465. (defun org-shiftmetaright ()
  12466. "Demote subtree or insert table column.
  12467. Calls `org-demote-subtree', `org-indent-item',
  12468. or `org-table-insert-column', depending on context.
  12469. See the individual commands for more information."
  12470. (interactive)
  12471. (cond
  12472. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  12473. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  12474. ((org-at-item-p) (call-interactively 'org-indent-item))
  12475. (t (org-shiftcursor-error))))
  12476. (defun org-shiftmetaup (&optional arg)
  12477. "Move subtree up or kill table row.
  12478. Calls `org-move-subtree-up' or `org-table-kill-row' or
  12479. `org-move-item-up' depending on context. See the individual commands
  12480. for more information."
  12481. (interactive "P")
  12482. (cond
  12483. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  12484. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  12485. ((org-at-item-p) (call-interactively 'org-move-item-up))
  12486. (t (org-shiftcursor-error))))
  12487. (defun org-shiftmetadown (&optional arg)
  12488. "Move subtree down or insert table row.
  12489. Calls `org-move-subtree-down' or `org-table-insert-row' or
  12490. `org-move-item-down', depending on context. See the individual
  12491. commands for more information."
  12492. (interactive "P")
  12493. (cond
  12494. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  12495. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  12496. ((org-at-item-p) (call-interactively 'org-move-item-down))
  12497. (t (org-shiftcursor-error))))
  12498. (defun org-metaleft (&optional arg)
  12499. "Promote heading or move table column to left.
  12500. Calls `org-do-promote' or `org-table-move-column', depending on context.
  12501. With no specific context, calls the Emacs default `backward-word'.
  12502. See the individual commands for more information."
  12503. (interactive "P")
  12504. (cond
  12505. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  12506. ((or (org-on-heading-p) (org-region-active-p))
  12507. (call-interactively 'org-do-promote))
  12508. ((org-at-item-p) (call-interactively 'org-outdent-item))
  12509. (t (call-interactively 'backward-word))))
  12510. (defun org-metaright (&optional arg)
  12511. "Demote subtree or move table column to right.
  12512. Calls `org-do-demote' or `org-table-move-column', depending on context.
  12513. With no specific context, calls the Emacs default `forward-word'.
  12514. See the individual commands for more information."
  12515. (interactive "P")
  12516. (cond
  12517. ((org-at-table-p) (call-interactively 'org-table-move-column))
  12518. ((or (org-on-heading-p) (org-region-active-p))
  12519. (call-interactively 'org-do-demote))
  12520. ((org-at-item-p) (call-interactively 'org-indent-item))
  12521. (t (call-interactively 'forward-word))))
  12522. (defun org-metaup (&optional arg)
  12523. "Move subtree up or move table row up.
  12524. Calls `org-move-subtree-up' or `org-table-move-row' or
  12525. `org-move-item-up', depending on context. See the individual commands
  12526. for more information."
  12527. (interactive "P")
  12528. (cond
  12529. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  12530. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  12531. ((org-at-item-p) (call-interactively 'org-move-item-up))
  12532. (t (transpose-lines 1) (beginning-of-line -1))))
  12533. (defun org-metadown (&optional arg)
  12534. "Move subtree down or move table row down.
  12535. Calls `org-move-subtree-down' or `org-table-move-row' or
  12536. `org-move-item-down', depending on context. See the individual
  12537. commands for more information."
  12538. (interactive "P")
  12539. (cond
  12540. ((org-at-table-p) (call-interactively 'org-table-move-row))
  12541. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  12542. ((org-at-item-p) (call-interactively 'org-move-item-down))
  12543. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  12544. (defun org-shiftup (&optional arg)
  12545. "Increase item in timestamp or increase priority of current headline.
  12546. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  12547. depending on context. See the individual commands for more information."
  12548. (interactive "P")
  12549. (cond
  12550. ((org-at-timestamp-p t)
  12551. (call-interactively (if org-edit-timestamp-down-means-later
  12552. 'org-timestamp-down 'org-timestamp-up)))
  12553. ((org-on-heading-p) (call-interactively 'org-priority-up))
  12554. ((org-at-item-p) (call-interactively 'org-previous-item))
  12555. ((org-clocktable-try-shift 'up arg))
  12556. (t (call-interactively 'org-beginning-of-item) (beginning-of-line 1))))
  12557. (defun org-shiftdown (&optional arg)
  12558. "Decrease item in timestamp or decrease priority of current headline.
  12559. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  12560. depending on context. See the individual commands for more information."
  12561. (interactive "P")
  12562. (cond
  12563. ((org-at-timestamp-p t)
  12564. (call-interactively (if org-edit-timestamp-down-means-later
  12565. 'org-timestamp-up 'org-timestamp-down)))
  12566. ((org-on-heading-p) (call-interactively 'org-priority-down))
  12567. ((org-clocktable-try-shift 'down arg))
  12568. (t (call-interactively 'org-next-item))))
  12569. (defun org-shiftright (&optional arg)
  12570. "Next TODO keyword or timestamp one day later, depending on context."
  12571. (interactive "P")
  12572. (cond
  12573. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  12574. ((org-on-heading-p) (org-call-with-arg 'org-todo 'right))
  12575. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet nil))
  12576. ((org-at-property-p) (call-interactively 'org-property-next-allowed-value))
  12577. ((org-clocktable-try-shift 'right arg))
  12578. (t (org-shiftcursor-error))))
  12579. (defun org-shiftleft (&optional arg)
  12580. "Previous TODO keyword or timestamp one day earlier, depending on context."
  12581. (interactive "P")
  12582. (cond
  12583. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  12584. ((org-on-heading-p) (org-call-with-arg 'org-todo 'left))
  12585. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet 'previous))
  12586. ((org-at-property-p)
  12587. (call-interactively 'org-property-previous-allowed-value))
  12588. ((org-clocktable-try-shift 'left arg))
  12589. (t (org-shiftcursor-error))))
  12590. (defun org-shiftcontrolright ()
  12591. "Switch to next TODO set."
  12592. (interactive)
  12593. (cond
  12594. ((org-on-heading-p) (org-call-with-arg 'org-todo 'nextset))
  12595. (t (org-shiftcursor-error))))
  12596. (defun org-shiftcontrolleft ()
  12597. "Switch to previous TODO set."
  12598. (interactive)
  12599. (cond
  12600. ((org-on-heading-p) (org-call-with-arg 'org-todo 'previousset))
  12601. (t (org-shiftcursor-error))))
  12602. (defun org-ctrl-c-ret ()
  12603. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  12604. (interactive)
  12605. (cond
  12606. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  12607. (t (call-interactively 'org-insert-heading))))
  12608. (defun org-copy-special ()
  12609. "Copy region in table or copy current subtree.
  12610. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  12611. See the individual commands for more information."
  12612. (interactive)
  12613. (call-interactively
  12614. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  12615. (defun org-cut-special ()
  12616. "Cut region in table or cut current subtree.
  12617. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  12618. See the individual commands for more information."
  12619. (interactive)
  12620. (call-interactively
  12621. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  12622. (defun org-paste-special (arg)
  12623. "Paste rectangular region into table, or past subtree relative to level.
  12624. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  12625. See the individual commands for more information."
  12626. (interactive "P")
  12627. (if (org-at-table-p)
  12628. (org-table-paste-rectangle)
  12629. (org-paste-subtree arg)))
  12630. (defun org-ctrl-c-ctrl-c (&optional arg)
  12631. "Set tags in headline, or update according to changed information at point.
  12632. This command does many different things, depending on context:
  12633. - If the cursor is in a headline, prompt for tags and insert them
  12634. into the current line, aligned to `org-tags-column'. When called
  12635. with prefix arg, realign all tags in the current buffer.
  12636. - If the cursor is in one of the special #+KEYWORD lines, this
  12637. triggers scanning the buffer for these lines and updating the
  12638. information.
  12639. - If the cursor is inside a table, realign the table. This command
  12640. works even if the automatic table editor has been turned off.
  12641. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  12642. the entire table.
  12643. - If the cursor is a the beginning of a dynamic block, update it.
  12644. - If the cursor is inside a table created by the table.el package,
  12645. activate that table.
  12646. - If the current buffer is a remember buffer, close note and file it.
  12647. with a prefix argument, file it without further interaction to the default
  12648. location.
  12649. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  12650. links in this buffer.
  12651. - If the cursor is on a numbered item in a plain list, renumber the
  12652. ordered list.
  12653. - If the cursor is on a checkbox, toggle it."
  12654. (interactive "P")
  12655. (let ((org-enable-table-editor t))
  12656. (cond
  12657. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  12658. org-occur-highlights
  12659. org-latex-fragment-image-overlays)
  12660. (and (boundp 'org-clock-overlays) (org-remove-clock-overlays))
  12661. (org-remove-occur-highlights)
  12662. (org-remove-latex-fragment-image-overlays)
  12663. (message "Temporary highlights/overlays removed from current buffer"))
  12664. ((and (local-variable-p 'org-finish-function (current-buffer))
  12665. (fboundp org-finish-function))
  12666. (funcall org-finish-function))
  12667. ((org-at-property-p)
  12668. (call-interactively 'org-property-action))
  12669. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  12670. ((org-on-heading-p) (call-interactively 'org-set-tags))
  12671. ((org-at-table.el-p)
  12672. (require 'table)
  12673. (beginning-of-line 1)
  12674. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  12675. (call-interactively 'table-recognize-table))
  12676. ((org-at-table-p)
  12677. (org-table-maybe-eval-formula)
  12678. (if arg
  12679. (call-interactively 'org-table-recalculate)
  12680. (org-table-maybe-recalculate-line))
  12681. (call-interactively 'org-table-align))
  12682. ((org-at-item-checkbox-p)
  12683. (call-interactively 'org-toggle-checkbox))
  12684. ((org-at-item-p)
  12685. (call-interactively 'org-maybe-renumber-ordered-list))
  12686. ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
  12687. ;; Dynamic block
  12688. (beginning-of-line 1)
  12689. (org-update-dblock))
  12690. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  12691. (cond
  12692. ((equal (match-string 1) "TBLFM")
  12693. ;; Recalculate the table before this line
  12694. (save-excursion
  12695. (beginning-of-line 1)
  12696. (skip-chars-backward " \r\n\t")
  12697. (if (org-at-table-p)
  12698. (org-call-with-arg 'org-table-recalculate t))))
  12699. (t
  12700. (call-interactively 'org-mode-restart))))
  12701. (t (error "C-c C-c can do nothing useful at this location.")))))
  12702. (defun org-mode-restart ()
  12703. "Restart Org-mode, to scan again for special lines.
  12704. Also updates the keyword regular expressions."
  12705. (interactive)
  12706. (let ((org-inhibit-startup t)) (org-mode))
  12707. (message "Org-mode restarted to refresh keyword and special line setup"))
  12708. (defun org-kill-note-or-show-branches ()
  12709. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  12710. (interactive)
  12711. (if (not org-finish-function)
  12712. (call-interactively 'show-branches)
  12713. (let ((org-note-abort t))
  12714. (funcall org-finish-function))))
  12715. (defun org-return (&optional indent)
  12716. "Goto next table row or insert a newline.
  12717. Calls `org-table-next-row' or `newline', depending on context.
  12718. See the individual commands for more information."
  12719. (interactive)
  12720. (cond
  12721. ((bobp) (if indent (newline-and-indent) (newline)))
  12722. ((and (org-at-heading-p)
  12723. (looking-at
  12724. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  12725. (org-show-entry)
  12726. (end-of-line 1)
  12727. (newline))
  12728. ((org-at-table-p)
  12729. (org-table-justify-field-maybe)
  12730. (call-interactively 'org-table-next-row))
  12731. (t (if indent (newline-and-indent) (newline)))))
  12732. (defun org-return-indent ()
  12733. "Goto next table row or insert a newline and indent.
  12734. Calls `org-table-next-row' or `newline-and-indent', depending on
  12735. context. See the individual commands for more information."
  12736. (interactive)
  12737. (org-return t))
  12738. (defun org-ctrl-c-star ()
  12739. "Compute table, or change heading status of lines.
  12740. Calls `org-table-recalculate' or `org-toggle-region-headlines',
  12741. depending on context. This will also turn a plain list item or a normal
  12742. line into a subheading."
  12743. (interactive)
  12744. (cond
  12745. ((org-at-table-p)
  12746. (call-interactively 'org-table-recalculate))
  12747. ((org-region-active-p)
  12748. ;; Convert all lines in region to list items
  12749. (call-interactively 'org-toggle-region-headings))
  12750. ((org-on-heading-p)
  12751. (org-toggle-region-headings (point-at-bol)
  12752. (min (1+ (point-at-eol)) (point-max))))
  12753. ((org-at-item-p)
  12754. ;; Convert to heading
  12755. (let ((level (save-match-data
  12756. (save-excursion
  12757. (condition-case nil
  12758. (progn
  12759. (org-back-to-heading t)
  12760. (funcall outline-level))
  12761. (error 0))))))
  12762. (replace-match
  12763. (concat (make-string (org-get-valid-level level 1) ?*) " ") t t)))
  12764. (t (org-toggle-region-headings (point-at-bol)
  12765. (min (1+ (point-at-eol)) (point-max))))))
  12766. (defun org-ctrl-c-minus ()
  12767. "Insert separator line in table or modify bullet status of line.
  12768. Also turns a plain line or a region of lines into list items.
  12769. Calls `org-table-insert-hline', `org-toggle-region-items', or
  12770. `org-cycle-list-bullet', depending on context."
  12771. (interactive)
  12772. (cond
  12773. ((org-at-table-p)
  12774. (call-interactively 'org-table-insert-hline))
  12775. ((org-on-heading-p)
  12776. ;; Convert to item
  12777. (save-excursion
  12778. (beginning-of-line 1)
  12779. (if (looking-at "\\*+ ")
  12780. (replace-match (concat (make-string (- (match-end 0) (point) 1) ?\ ) "- ")))))
  12781. ((org-region-active-p)
  12782. ;; Convert all lines in region to list items
  12783. (call-interactively 'org-toggle-region-items))
  12784. ((org-in-item-p)
  12785. (call-interactively 'org-cycle-list-bullet))
  12786. (t (org-toggle-region-items (point-at-bol)
  12787. (min (1+ (point-at-eol)) (point-max))))))
  12788. (defun org-toggle-region-items (beg end)
  12789. "Convert all lines in region to list items.
  12790. If the first line is already an item, convert all list items in the region
  12791. to normal lines."
  12792. (interactive "r")
  12793. (let (l2 l)
  12794. (save-excursion
  12795. (goto-char end)
  12796. (setq l2 (org-current-line))
  12797. (goto-char beg)
  12798. (beginning-of-line 1)
  12799. (setq l (1- (org-current-line)))
  12800. (if (org-at-item-p)
  12801. ;; We already have items, de-itemize
  12802. (while (< (setq l (1+ l)) l2)
  12803. (when (org-at-item-p)
  12804. (goto-char (match-beginning 2))
  12805. (delete-region (match-beginning 2) (match-end 2))
  12806. (and (looking-at "[ \t]+") (replace-match "")))
  12807. (beginning-of-line 2))
  12808. (while (< (setq l (1+ l)) l2)
  12809. (unless (org-at-item-p)
  12810. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  12811. (replace-match "\\1- \\2")))
  12812. (beginning-of-line 2))))))
  12813. (defun org-toggle-region-headings (beg end)
  12814. "Convert all lines in region to list items.
  12815. If the first line is already an item, convert all list items in the region
  12816. to normal lines."
  12817. (interactive "r")
  12818. (let (l2 l)
  12819. (save-excursion
  12820. (goto-char end)
  12821. (setq l2 (org-current-line))
  12822. (goto-char beg)
  12823. (beginning-of-line 1)
  12824. (setq l (1- (org-current-line)))
  12825. (if (org-on-heading-p)
  12826. ;; We already have headlines, de-star them
  12827. (while (< (setq l (1+ l)) l2)
  12828. (when (org-on-heading-p t)
  12829. (and (looking-at outline-regexp) (replace-match "")))
  12830. (beginning-of-line 2))
  12831. (let* ((stars (save-excursion
  12832. (re-search-backward org-complex-heading-regexp nil t)
  12833. (or (match-string 1) "*")))
  12834. (add-stars (if org-odd-levels-only "**" "*"))
  12835. (rpl (concat stars add-stars " \\2")))
  12836. (while (< (setq l (1+ l)) l2)
  12837. (unless (org-on-heading-p)
  12838. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  12839. (replace-match rpl)))
  12840. (beginning-of-line 2)))))))
  12841. (defun org-meta-return (&optional arg)
  12842. "Insert a new heading or wrap a region in a table.
  12843. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  12844. See the individual commands for more information."
  12845. (interactive "P")
  12846. (cond
  12847. ((org-at-table-p)
  12848. (call-interactively 'org-table-wrap-region))
  12849. (t (call-interactively 'org-insert-heading))))
  12850. ;;; Menu entries
  12851. ;; Define the Org-mode menus
  12852. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  12853. '("Tbl"
  12854. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  12855. ["Next Field" org-cycle (org-at-table-p)]
  12856. ["Previous Field" org-shifttab (org-at-table-p)]
  12857. ["Next Row" org-return (org-at-table-p)]
  12858. "--"
  12859. ["Blank Field" org-table-blank-field (org-at-table-p)]
  12860. ["Edit Field" org-table-edit-field (org-at-table-p)]
  12861. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  12862. "--"
  12863. ("Column"
  12864. ["Move Column Left" org-metaleft (org-at-table-p)]
  12865. ["Move Column Right" org-metaright (org-at-table-p)]
  12866. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  12867. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  12868. ("Row"
  12869. ["Move Row Up" org-metaup (org-at-table-p)]
  12870. ["Move Row Down" org-metadown (org-at-table-p)]
  12871. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  12872. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  12873. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  12874. "--"
  12875. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  12876. ("Rectangle"
  12877. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  12878. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  12879. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  12880. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  12881. "--"
  12882. ("Calculate"
  12883. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  12884. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  12885. ["Edit Formulas" org-table-edit-formulas (org-at-table-p)]
  12886. "--"
  12887. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  12888. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  12889. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  12890. "--"
  12891. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  12892. "--"
  12893. ["Sum Column/Rectangle" org-table-sum
  12894. (or (org-at-table-p) (org-region-active-p))]
  12895. ["Which Column?" org-table-current-column (org-at-table-p)])
  12896. ["Debug Formulas"
  12897. org-table-toggle-formula-debugger
  12898. :style toggle :selected org-table-formula-debug]
  12899. ["Show Col/Row Numbers"
  12900. org-table-toggle-coordinate-overlays
  12901. :style toggle :selected org-table-overlay-coordinates]
  12902. "--"
  12903. ["Create" org-table-create (and (not (org-at-table-p))
  12904. org-enable-table-editor)]
  12905. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  12906. ["Import from File" org-table-import (not (org-at-table-p))]
  12907. ["Export to File" org-table-export (org-at-table-p)]
  12908. "--"
  12909. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  12910. (easy-menu-define org-org-menu org-mode-map "Org menu"
  12911. '("Org"
  12912. ("Show/Hide"
  12913. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))
  12914. :help "Cycle subtree visibility: folded->children->all->folded"]
  12915. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))
  12916. :help "Cycle global visibility: overview->content->all"]
  12917. ["Sparse Tree..." org-sparse-tree
  12918. :help "Create sparse trees using different search criteria"]
  12919. ["Reveal Context" org-reveal :active t
  12920. :help "Show hidden context around point, including the outline hierarchy"]
  12921. ["Show All" show-all :active t
  12922. :help "Show all text in the buffer, including drawers"]
  12923. "--"
  12924. ["Subtree to indirect buffer" org-tree-to-indirect-buffer :active t
  12925. :help "Open the subtree at point in a separate window, using an indirect buffer"])
  12926. "--"
  12927. ["New Heading" org-insert-heading t]
  12928. ("Navigate Headings"
  12929. ["Up" outline-up-heading t]
  12930. ["Next" outline-next-visible-heading t]
  12931. ["Previous" outline-previous-visible-heading t]
  12932. ["Next Same Level" outline-forward-same-level t]
  12933. ["Previous Same Level" outline-backward-same-level t]
  12934. "--"
  12935. ["Jump" org-goto t])
  12936. ("Edit Structure"
  12937. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  12938. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  12939. "--"
  12940. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  12941. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  12942. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  12943. "--"
  12944. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  12945. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  12946. ["Demote Heading" org-metaright (not (org-at-table-p))]
  12947. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  12948. "--"
  12949. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  12950. "--"
  12951. ["Convert to odd levels" org-convert-to-odd-levels t]
  12952. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  12953. ("Editing"
  12954. ["Emphasis..." org-emphasize t])
  12955. ("Archive"
  12956. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  12957. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  12958. ; :active t :keys "C-u C-c C-x C-a"]
  12959. ["Sparse trees open ARCHIVE trees"
  12960. (setq org-sparse-tree-open-archived-trees
  12961. (not org-sparse-tree-open-archived-trees))
  12962. :style toggle :selected org-sparse-tree-open-archived-trees]
  12963. ["Cycling opens ARCHIVE trees"
  12964. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  12965. :style toggle :selected org-cycle-open-archived-trees]
  12966. ["Agenda includes ARCHIVE trees"
  12967. (setq org-agenda-skip-archived-trees (not org-agenda-skip-archived-trees))
  12968. :style toggle :selected (not org-agenda-skip-archived-trees)]
  12969. "--"
  12970. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  12971. ; ["Check and Move Children" (org-archive-subtree '(4))
  12972. ; :active t :keys "C-u C-c C-x C-s"]
  12973. )
  12974. "--"
  12975. ("TODO Lists"
  12976. ["TODO/DONE/-" org-todo t]
  12977. ("Select keyword"
  12978. ["Next keyword" org-shiftright (org-on-heading-p)]
  12979. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  12980. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  12981. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  12982. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  12983. ["Show TODO Tree" org-show-todo-tree t]
  12984. ["Global TODO list" org-todo-list t]
  12985. "--"
  12986. ["Set Priority" org-priority t]
  12987. ["Priority Up" org-shiftup t]
  12988. ["Priority Down" org-shiftdown t])
  12989. ("TAGS and Properties"
  12990. ["Set Tags" 'org-ctrl-c-ctrl-c (org-at-heading-p)]
  12991. ["Change tag in region" 'org-change-tag-in-region (org-region-active-p)]
  12992. "--"
  12993. ["Set property" 'org-set-property t]
  12994. ["Column view of properties" org-columns t]
  12995. ["Insert Column View DBlock" org-insert-columns-dblock t])
  12996. ("Dates and Scheduling"
  12997. ["Timestamp" org-time-stamp t]
  12998. ["Timestamp (inactive)" org-time-stamp-inactive t]
  12999. ("Change Date"
  13000. ["1 Day Later" org-shiftright t]
  13001. ["1 Day Earlier" org-shiftleft t]
  13002. ["1 ... Later" org-shiftup t]
  13003. ["1 ... Earlier" org-shiftdown t])
  13004. ["Compute Time Range" org-evaluate-time-range t]
  13005. ["Schedule Item" org-schedule t]
  13006. ["Deadline" org-deadline t]
  13007. "--"
  13008. ["Custom time format" org-toggle-time-stamp-overlays
  13009. :style radio :selected org-display-custom-times]
  13010. "--"
  13011. ["Goto Calendar" org-goto-calendar t]
  13012. ["Date from Calendar" org-date-from-calendar t])
  13013. ("Logging work"
  13014. ["Clock in" org-clock-in t]
  13015. ["Clock out" org-clock-out t]
  13016. ["Clock cancel" org-clock-cancel t]
  13017. ["Goto running clock" org-clock-goto t]
  13018. ["Display times" org-clock-display t]
  13019. ["Create clock table" org-clock-report t]
  13020. "--"
  13021. ["Record DONE time"
  13022. (progn (setq org-log-done (not org-log-done))
  13023. (message "Switching to %s will %s record a timestamp"
  13024. (car org-done-keywords)
  13025. (if org-log-done "automatically" "not")))
  13026. :style toggle :selected org-log-done])
  13027. "--"
  13028. ["Agenda Command..." org-agenda t]
  13029. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  13030. ("File List for Agenda")
  13031. ("Special views current file"
  13032. ["TODO Tree" org-show-todo-tree t]
  13033. ["Check Deadlines" org-check-deadlines t]
  13034. ["Timeline" org-timeline t]
  13035. ["Tags Tree" org-tags-sparse-tree t])
  13036. "--"
  13037. ("Hyperlinks"
  13038. ["Store Link (Global)" org-store-link t]
  13039. ["Insert Link" org-insert-link t]
  13040. ["Follow Link" org-open-at-point t]
  13041. "--"
  13042. ["Next link" org-next-link :help "Move forward to next link in the buffer"]
  13043. ["Previous link" org-previous-link t]
  13044. "--"
  13045. ["Descriptive Links"
  13046. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  13047. :style radio :help "Hide link part of links, only show the description"
  13048. :selected (member '(org-link) buffer-invisibility-spec)]
  13049. ["Literal Links"
  13050. (progn
  13051. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  13052. :style radio :help "Show full links without hiding anything"
  13053. :selected (not (member '(org-link) buffer-invisibility-spec))])
  13054. "--"
  13055. ["Export/Publish..." org-export t]
  13056. ("LaTeX"
  13057. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  13058. :selected org-cdlatex-mode]
  13059. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  13060. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  13061. ["Modify math symbol" org-cdlatex-math-modify
  13062. (org-inside-LaTeX-fragment-p)]
  13063. ["Export LaTeX fragments as images"
  13064. (if (featurep 'org-exp)
  13065. (setq org-export-with-LaTeX-fragments
  13066. (not org-export-with-LaTeX-fragments))
  13067. (require 'org-exp))
  13068. :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
  13069. org-export-with-LaTeX-fragments)])
  13070. "--"
  13071. ("Documentation"
  13072. ["Show Version" org-version t]
  13073. ["Info Documentation" org-info t])
  13074. ("Customize"
  13075. ["Browse Org Group" org-customize t]
  13076. "--"
  13077. ["Expand This Menu" org-create-customize-menu
  13078. (fboundp 'customize-menu-create)])
  13079. "--"
  13080. ["Refresh setup" org-mode-restart t]
  13081. ))
  13082. (defun org-info (&optional node)
  13083. "Read documentation for Org-mode in the info system.
  13084. With optional NODE, go directly to that node."
  13085. (interactive)
  13086. (info (format "(org)%s" (or node ""))))
  13087. (defun org-install-agenda-files-menu ()
  13088. (let ((bl (buffer-list)))
  13089. (save-excursion
  13090. (while bl
  13091. (set-buffer (pop bl))
  13092. (if (org-mode-p) (setq bl nil)))
  13093. (when (org-mode-p)
  13094. (easy-menu-change
  13095. '("Org") "File List for Agenda"
  13096. (append
  13097. (list
  13098. ["Edit File List" (org-edit-agenda-file-list) t]
  13099. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  13100. ["Remove Current File from List" org-remove-file t]
  13101. ["Cycle through agenda files" org-cycle-agenda-files t]
  13102. ["Occur in all agenda files" org-occur-in-agenda-files t]
  13103. "--")
  13104. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  13105. ;;;; Documentation
  13106. (defun org-customize ()
  13107. "Call the customize function with org as argument."
  13108. (interactive)
  13109. (customize-browse 'org))
  13110. (defun org-create-customize-menu ()
  13111. "Create a full customization menu for Org-mode, insert it into the menu."
  13112. (interactive)
  13113. (if (fboundp 'customize-menu-create)
  13114. (progn
  13115. (easy-menu-change
  13116. '("Org") "Customize"
  13117. `(["Browse Org group" org-customize t]
  13118. "--"
  13119. ,(customize-menu-create 'org)
  13120. ["Set" Custom-set t]
  13121. ["Save" Custom-save t]
  13122. ["Reset to Current" Custom-reset-current t]
  13123. ["Reset to Saved" Custom-reset-saved t]
  13124. ["Reset to Standard Settings" Custom-reset-standard t]))
  13125. (message "\"Org\"-menu now contains full customization menu"))
  13126. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  13127. ;;;; Miscellaneous stuff
  13128. ;;; Generally useful functions
  13129. (defun org-plist-delete (plist property)
  13130. "Delete PROPERTY from PLIST.
  13131. This is in contrast to merely setting it to 0."
  13132. (let (p)
  13133. (while plist
  13134. (if (not (eq property (car plist)))
  13135. (setq p (plist-put p (car plist) (nth 1 plist))))
  13136. (setq plist (cddr plist)))
  13137. p))
  13138. (defun org-force-self-insert (N)
  13139. "Needed to enforce self-insert under remapping."
  13140. (interactive "p")
  13141. (self-insert-command N))
  13142. (defun org-string-width (s)
  13143. "Compute width of string, ignoring invisible characters.
  13144. This ignores character with invisibility property `org-link', and also
  13145. characters with property `org-cwidth', because these will become invisible
  13146. upon the next fontification round."
  13147. (let (b l)
  13148. (when (or (eq t buffer-invisibility-spec)
  13149. (assq 'org-link buffer-invisibility-spec))
  13150. (while (setq b (text-property-any 0 (length s)
  13151. 'invisible 'org-link s))
  13152. (setq s (concat (substring s 0 b)
  13153. (substring s (or (next-single-property-change
  13154. b 'invisible s) (length s)))))))
  13155. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  13156. (setq s (concat (substring s 0 b)
  13157. (substring s (or (next-single-property-change
  13158. b 'org-cwidth s) (length s))))))
  13159. (setq l (string-width s) b -1)
  13160. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  13161. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  13162. l))
  13163. (defun org-trim (s)
  13164. "Remove whitespace at beginning and end of string."
  13165. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  13166. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  13167. s)
  13168. (defun org-wrap (string &optional width lines)
  13169. "Wrap string to either a number of lines, or a width in characters.
  13170. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  13171. that costs. If there is a word longer than WIDTH, the text is actually
  13172. wrapped to the length of that word.
  13173. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  13174. many lines, whatever width that takes.
  13175. The return value is a list of lines, without newlines at the end."
  13176. (let* ((words (org-split-string string "[ \t\n]+"))
  13177. (maxword (apply 'max (mapcar 'org-string-width words)))
  13178. w ll)
  13179. (cond (width
  13180. (org-do-wrap words (max maxword width)))
  13181. (lines
  13182. (setq w maxword)
  13183. (setq ll (org-do-wrap words maxword))
  13184. (if (<= (length ll) lines)
  13185. ll
  13186. (setq ll words)
  13187. (while (> (length ll) lines)
  13188. (setq w (1+ w))
  13189. (setq ll (org-do-wrap words w)))
  13190. ll))
  13191. (t (error "Cannot wrap this")))))
  13192. (defun org-do-wrap (words width)
  13193. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  13194. (let (lines line)
  13195. (while words
  13196. (setq line (pop words))
  13197. (while (and words (< (+ (length line) (length (car words))) width))
  13198. (setq line (concat line " " (pop words))))
  13199. (setq lines (push line lines)))
  13200. (nreverse lines)))
  13201. (defun org-split-string (string &optional separators)
  13202. "Splits STRING into substrings at SEPARATORS.
  13203. No empty strings are returned if there are matches at the beginning
  13204. and end of string."
  13205. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  13206. (start 0)
  13207. notfirst
  13208. (list nil))
  13209. (while (and (string-match rexp string
  13210. (if (and notfirst
  13211. (= start (match-beginning 0))
  13212. (< start (length string)))
  13213. (1+ start) start))
  13214. (< (match-beginning 0) (length string)))
  13215. (setq notfirst t)
  13216. (or (eq (match-beginning 0) 0)
  13217. (and (eq (match-beginning 0) (match-end 0))
  13218. (eq (match-beginning 0) start))
  13219. (setq list
  13220. (cons (substring string start (match-beginning 0))
  13221. list)))
  13222. (setq start (match-end 0)))
  13223. (or (eq start (length string))
  13224. (setq list
  13225. (cons (substring string start)
  13226. list)))
  13227. (nreverse list)))
  13228. (defun org-context ()
  13229. "Return a list of contexts of the current cursor position.
  13230. If several contexts apply, all are returned.
  13231. Each context entry is a list with a symbol naming the context, and
  13232. two positions indicating start and end of the context. Possible
  13233. contexts are:
  13234. :headline anywhere in a headline
  13235. :headline-stars on the leading stars in a headline
  13236. :todo-keyword on a TODO keyword (including DONE) in a headline
  13237. :tags on the TAGS in a headline
  13238. :priority on the priority cookie in a headline
  13239. :item on the first line of a plain list item
  13240. :item-bullet on the bullet/number of a plain list item
  13241. :checkbox on the checkbox in a plain list item
  13242. :table in an org-mode table
  13243. :table-special on a special filed in a table
  13244. :table-table in a table.el table
  13245. :link on a hyperlink
  13246. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  13247. :target on a <<target>>
  13248. :radio-target on a <<<radio-target>>>
  13249. :latex-fragment on a LaTeX fragment
  13250. :latex-preview on a LaTeX fragment with overlayed preview image
  13251. This function expects the position to be visible because it uses font-lock
  13252. faces as a help to recognize the following contexts: :table-special, :link,
  13253. and :keyword."
  13254. (let* ((f (get-text-property (point) 'face))
  13255. (faces (if (listp f) f (list f)))
  13256. (p (point)) clist o)
  13257. ;; First the large context
  13258. (cond
  13259. ((org-on-heading-p t)
  13260. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  13261. (when (progn
  13262. (beginning-of-line 1)
  13263. (looking-at org-todo-line-tags-regexp))
  13264. (push (org-point-in-group p 1 :headline-stars) clist)
  13265. (push (org-point-in-group p 2 :todo-keyword) clist)
  13266. (push (org-point-in-group p 4 :tags) clist))
  13267. (goto-char p)
  13268. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  13269. (if (looking-at "\\[#[A-Z0-9]\\]")
  13270. (push (org-point-in-group p 0 :priority) clist)))
  13271. ((org-at-item-p)
  13272. (push (org-point-in-group p 2 :item-bullet) clist)
  13273. (push (list :item (point-at-bol)
  13274. (save-excursion (org-end-of-item) (point)))
  13275. clist)
  13276. (and (org-at-item-checkbox-p)
  13277. (push (org-point-in-group p 0 :checkbox) clist)))
  13278. ((org-at-table-p)
  13279. (push (list :table (org-table-begin) (org-table-end)) clist)
  13280. (if (memq 'org-formula faces)
  13281. (push (list :table-special
  13282. (previous-single-property-change p 'face)
  13283. (next-single-property-change p 'face)) clist)))
  13284. ((org-at-table-p 'any)
  13285. (push (list :table-table) clist)))
  13286. (goto-char p)
  13287. ;; Now the small context
  13288. (cond
  13289. ((org-at-timestamp-p)
  13290. (push (org-point-in-group p 0 :timestamp) clist))
  13291. ((memq 'org-link faces)
  13292. (push (list :link
  13293. (previous-single-property-change p 'face)
  13294. (next-single-property-change p 'face)) clist))
  13295. ((memq 'org-special-keyword faces)
  13296. (push (list :keyword
  13297. (previous-single-property-change p 'face)
  13298. (next-single-property-change p 'face)) clist))
  13299. ((org-on-target-p)
  13300. (push (org-point-in-group p 0 :target) clist)
  13301. (goto-char (1- (match-beginning 0)))
  13302. (if (looking-at org-radio-target-regexp)
  13303. (push (org-point-in-group p 0 :radio-target) clist))
  13304. (goto-char p))
  13305. ((setq o (car (delq nil
  13306. (mapcar
  13307. (lambda (x)
  13308. (if (memq x org-latex-fragment-image-overlays) x))
  13309. (org-overlays-at (point))))))
  13310. (push (list :latex-fragment
  13311. (org-overlay-start o) (org-overlay-end o)) clist)
  13312. (push (list :latex-preview
  13313. (org-overlay-start o) (org-overlay-end o)) clist))
  13314. ((org-inside-LaTeX-fragment-p)
  13315. ;; FIXME: positions wrong.
  13316. (push (list :latex-fragment (point) (point)) clist)))
  13317. (setq clist (nreverse (delq nil clist)))
  13318. clist))
  13319. ;; FIXME: Compare with at-regexp-p Do we need both?
  13320. (defun org-in-regexp (re &optional nlines visually)
  13321. "Check if point is inside a match of regexp.
  13322. Normally only the current line is checked, but you can include NLINES extra
  13323. lines both before and after point into the search.
  13324. If VISUALLY is set, require that the cursor is not after the match but
  13325. really on, so that the block visually is on the match."
  13326. (catch 'exit
  13327. (let ((pos (point))
  13328. (eol (point-at-eol (+ 1 (or nlines 0))))
  13329. (inc (if visually 1 0)))
  13330. (save-excursion
  13331. (beginning-of-line (- 1 (or nlines 0)))
  13332. (while (re-search-forward re eol t)
  13333. (if (and (<= (match-beginning 0) pos)
  13334. (>= (+ inc (match-end 0)) pos))
  13335. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  13336. (defun org-at-regexp-p (regexp)
  13337. "Is point inside a match of REGEXP in the current line?"
  13338. (catch 'exit
  13339. (save-excursion
  13340. (let ((pos (point)) (end (point-at-eol)))
  13341. (beginning-of-line 1)
  13342. (while (re-search-forward regexp end t)
  13343. (if (and (<= (match-beginning 0) pos)
  13344. (>= (match-end 0) pos))
  13345. (throw 'exit t)))
  13346. nil))))
  13347. (defun org-occur-in-agenda-files (regexp &optional nlines)
  13348. "Call `multi-occur' with buffers for all agenda files."
  13349. (interactive "sOrg-files matching: \np")
  13350. (let* ((files (org-agenda-files))
  13351. (tnames (mapcar 'file-truename files))
  13352. (extra org-agenda-text-search-extra-files)
  13353. f)
  13354. (while (setq f (pop extra))
  13355. (unless (member (file-truename f) tnames)
  13356. (add-to-list 'files f 'append)
  13357. (add-to-list 'tnames (file-truename f) 'append)))
  13358. (multi-occur
  13359. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  13360. regexp)))
  13361. (if (boundp 'occur-mode-find-occurrence-hook)
  13362. ;; Emacs 23
  13363. (add-hook 'occur-mode-find-occurrence-hook
  13364. (lambda ()
  13365. (when (org-mode-p)
  13366. (org-reveal))))
  13367. ;; Emacs 22
  13368. (defadvice occur-mode-goto-occurrence
  13369. (after org-occur-reveal activate)
  13370. (and (org-mode-p) (org-reveal)))
  13371. (defadvice occur-mode-goto-occurrence-other-window
  13372. (after org-occur-reveal activate)
  13373. (and (org-mode-p) (org-reveal)))
  13374. (defadvice occur-mode-display-occurrence
  13375. (after org-occur-reveal activate)
  13376. (when (org-mode-p)
  13377. (let ((pos (occur-mode-find-occurrence)))
  13378. (with-current-buffer (marker-buffer pos)
  13379. (save-excursion
  13380. (goto-char pos)
  13381. (org-reveal)))))))
  13382. (defun org-uniquify (list)
  13383. "Remove duplicate elements from LIST."
  13384. (let (res)
  13385. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  13386. res))
  13387. (defun org-delete-all (elts list)
  13388. "Remove all elements in ELTS from LIST."
  13389. (while elts
  13390. (setq list (delete (pop elts) list)))
  13391. list)
  13392. (defun org-back-over-empty-lines ()
  13393. "Move backwards over witespace, to the beginning of the first empty line.
  13394. Returns the number of empty lines passed."
  13395. (let ((pos (point)))
  13396. (skip-chars-backward " \t\n\r")
  13397. (beginning-of-line 2)
  13398. (goto-char (min (point) pos))
  13399. (count-lines (point) pos)))
  13400. (defun org-skip-whitespace ()
  13401. (skip-chars-forward " \t\n\r"))
  13402. (defun org-point-in-group (point group &optional context)
  13403. "Check if POINT is in match-group GROUP.
  13404. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  13405. match. If the match group does ot exist or point is not inside it,
  13406. return nil."
  13407. (and (match-beginning group)
  13408. (>= point (match-beginning group))
  13409. (<= point (match-end group))
  13410. (if context
  13411. (list context (match-beginning group) (match-end group))
  13412. t)))
  13413. (defun org-switch-to-buffer-other-window (&rest args)
  13414. "Switch to buffer in a second window on the current frame.
  13415. In particular, do not allow pop-up frames."
  13416. (let (pop-up-frames special-display-buffer-names special-display-regexps
  13417. special-display-function)
  13418. (apply 'switch-to-buffer-other-window args)))
  13419. (defun org-combine-plists (&rest plists)
  13420. "Create a single property list from all plists in PLISTS.
  13421. The process starts by copying the first list, and then setting properties
  13422. from the other lists. Settings in the last list are the most significant
  13423. ones and overrule settings in the other lists."
  13424. (let ((rtn (copy-sequence (pop plists)))
  13425. p v ls)
  13426. (while plists
  13427. (setq ls (pop plists))
  13428. (while ls
  13429. (setq p (pop ls) v (pop ls))
  13430. (setq rtn (plist-put rtn p v))))
  13431. rtn))
  13432. (defun org-move-line-down (arg)
  13433. "Move the current line down. With prefix argument, move it past ARG lines."
  13434. (interactive "p")
  13435. (let ((col (current-column))
  13436. beg end pos)
  13437. (beginning-of-line 1) (setq beg (point))
  13438. (beginning-of-line 2) (setq end (point))
  13439. (beginning-of-line (+ 1 arg))
  13440. (setq pos (move-marker (make-marker) (point)))
  13441. (insert (delete-and-extract-region beg end))
  13442. (goto-char pos)
  13443. (move-to-column col)))
  13444. (defun org-move-line-up (arg)
  13445. "Move the current line up. With prefix argument, move it past ARG lines."
  13446. (interactive "p")
  13447. (let ((col (current-column))
  13448. beg end pos)
  13449. (beginning-of-line 1) (setq beg (point))
  13450. (beginning-of-line 2) (setq end (point))
  13451. (beginning-of-line (- arg))
  13452. (setq pos (move-marker (make-marker) (point)))
  13453. (insert (delete-and-extract-region beg end))
  13454. (goto-char pos)
  13455. (move-to-column col)))
  13456. (defun org-replace-escapes (string table)
  13457. "Replace %-escapes in STRING with values in TABLE.
  13458. TABLE is an association list with keys like \"%a\" and string values.
  13459. The sequences in STRING may contain normal field width and padding information,
  13460. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  13461. so values can contain further %-escapes if they are define later in TABLE."
  13462. (let ((case-fold-search nil)
  13463. e re rpl)
  13464. (while (setq e (pop table))
  13465. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  13466. (while (string-match re string)
  13467. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  13468. (cdr e)))
  13469. (setq string (replace-match rpl t t string))))
  13470. string))
  13471. (defun org-sublist (list start end)
  13472. "Return a section of LIST, from START to END.
  13473. Counting starts at 1."
  13474. (let (rtn (c start))
  13475. (setq list (nthcdr (1- start) list))
  13476. (while (and list (<= c end))
  13477. (push (pop list) rtn)
  13478. (setq c (1+ c)))
  13479. (nreverse rtn)))
  13480. (defun org-find-base-buffer-visiting (file)
  13481. "Like `find-buffer-visiting' but alway return the base buffer and
  13482. not an indirect buffer."
  13483. (let ((buf (find-buffer-visiting file)))
  13484. (if buf
  13485. (or (buffer-base-buffer buf) buf)
  13486. nil)))
  13487. (defun org-image-file-name-regexp ()
  13488. "Return regexp matching the file names of images."
  13489. (if (fboundp 'image-file-name-regexp)
  13490. (image-file-name-regexp)
  13491. (let ((image-file-name-extensions
  13492. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  13493. "xbm" "xpm" "pbm" "pgm" "ppm")))
  13494. (concat "\\."
  13495. (regexp-opt (nconc (mapcar 'upcase
  13496. image-file-name-extensions)
  13497. image-file-name-extensions)
  13498. t)
  13499. "\\'"))))
  13500. (defun org-file-image-p (file)
  13501. "Return non-nil if FILE is an image."
  13502. (save-match-data
  13503. (string-match (org-image-file-name-regexp) file)))
  13504. ;;; Paragraph filling stuff.
  13505. ;; We want this to be just right, so use the full arsenal.
  13506. (defun org-indent-line-function ()
  13507. "Indent line like previous, but further if previous was headline or item."
  13508. (interactive)
  13509. (let* ((pos (point))
  13510. (itemp (org-at-item-p))
  13511. column bpos bcol tpos tcol bullet btype bullet-type)
  13512. ;; Find the previous relevant line
  13513. (beginning-of-line 1)
  13514. (cond
  13515. ((looking-at "#") (setq column 0))
  13516. ((looking-at "\\*+ ") (setq column 0))
  13517. (t
  13518. (beginning-of-line 0)
  13519. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]"))
  13520. (beginning-of-line 0))
  13521. (cond
  13522. ((looking-at "\\*+[ \t]+")
  13523. (goto-char (match-end 0))
  13524. (setq column (current-column)))
  13525. ((org-in-item-p)
  13526. (org-beginning-of-item)
  13527. ; (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  13528. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\)?")
  13529. (setq bpos (match-beginning 1) tpos (match-end 0)
  13530. bcol (progn (goto-char bpos) (current-column))
  13531. tcol (progn (goto-char tpos) (current-column))
  13532. bullet (match-string 1)
  13533. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  13534. (if (not itemp)
  13535. (setq column tcol)
  13536. (goto-char pos)
  13537. (beginning-of-line 1)
  13538. (if (looking-at "\\S-")
  13539. (progn
  13540. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  13541. (setq bullet (match-string 1)
  13542. btype (if (string-match "[0-9]" bullet) "n" bullet))
  13543. (setq column (if (equal btype bullet-type) bcol tcol)))
  13544. (setq column (org-get-indentation)))))
  13545. (t (setq column (org-get-indentation))))))
  13546. (goto-char pos)
  13547. (if (<= (current-column) (current-indentation))
  13548. (indent-line-to column)
  13549. (save-excursion (indent-line-to column)))
  13550. (setq column (current-column))
  13551. (beginning-of-line 1)
  13552. (if (looking-at
  13553. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  13554. (replace-match (concat "\\1" (format org-property-format
  13555. (match-string 2) (match-string 3)))
  13556. t nil))
  13557. (move-to-column column)))
  13558. (defun org-set-autofill-regexps ()
  13559. (interactive)
  13560. ;; In the paragraph separator we include headlines, because filling
  13561. ;; text in a line directly attached to a headline would otherwise
  13562. ;; fill the headline as well.
  13563. (org-set-local 'comment-start-skip "^#+[ \t]*")
  13564. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  13565. ;; The paragraph starter includes hand-formatted lists.
  13566. (org-set-local 'paragraph-start
  13567. "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  13568. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  13569. ;; But only if the user has not turned off tables or fixed-width regions
  13570. (org-set-local
  13571. 'auto-fill-inhibit-regexp
  13572. (concat "\\*+ \\|#\\+"
  13573. "\\|[ \t]*" org-keyword-time-regexp
  13574. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  13575. (concat
  13576. "\\|[ \t]*["
  13577. (if org-enable-table-editor "|" "")
  13578. (if org-enable-fixed-width-editor ":" "")
  13579. "]"))))
  13580. ;; We use our own fill-paragraph function, to make sure that tables
  13581. ;; and fixed-width regions are not wrapped. That function will pass
  13582. ;; through to `fill-paragraph' when appropriate.
  13583. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  13584. ; Adaptive filling: To get full control, first make sure that
  13585. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  13586. (org-set-local 'adaptive-fill-regexp "\000")
  13587. (org-set-local 'adaptive-fill-function
  13588. 'org-adaptive-fill-function)
  13589. (org-set-local
  13590. 'align-mode-rules-list
  13591. '((org-in-buffer-settings
  13592. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  13593. (modes . '(org-mode))))))
  13594. (defun org-fill-paragraph (&optional justify)
  13595. "Re-align a table, pass through to fill-paragraph if no table."
  13596. (let ((table-p (org-at-table-p))
  13597. (table.el-p (org-at-table.el-p)))
  13598. (cond ((and (equal (char-after (point-at-bol)) ?*)
  13599. (save-excursion (goto-char (point-at-bol))
  13600. (looking-at outline-regexp)))
  13601. t) ; skip headlines
  13602. (table.el-p t) ; skip table.el tables
  13603. (table-p (org-table-align) t) ; align org-mode tables
  13604. (t nil)))) ; call paragraph-fill
  13605. ;; For reference, this is the default value of adaptive-fill-regexp
  13606. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  13607. (defun org-adaptive-fill-function ()
  13608. "Return a fill prefix for org-mode files.
  13609. In particular, this makes sure hanging paragraphs for hand-formatted lists
  13610. work correctly."
  13611. (cond ((looking-at "#[ \t]+")
  13612. (match-string 0))
  13613. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] \\)?")
  13614. (save-excursion
  13615. (goto-char (match-end 0))
  13616. (make-string (current-column) ?\ )))
  13617. (t nil)))
  13618. ;;; Other stuff.
  13619. (defun org-toggle-fixed-width-section (arg)
  13620. "Toggle the fixed-width export.
  13621. If there is no active region, the QUOTE keyword at the current headline is
  13622. inserted or removed. When present, it causes the text between this headline
  13623. and the next to be exported as fixed-width text, and unmodified.
  13624. If there is an active region, this command adds or removes a colon as the
  13625. first character of this line. If the first character of a line is a colon,
  13626. this line is also exported in fixed-width font."
  13627. (interactive "P")
  13628. (let* ((cc 0)
  13629. (regionp (org-region-active-p))
  13630. (beg (if regionp (region-beginning) (point)))
  13631. (end (if regionp (region-end)))
  13632. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  13633. (case-fold-search nil)
  13634. (re "[ \t]*\\(:\\)")
  13635. off)
  13636. (if regionp
  13637. (save-excursion
  13638. (goto-char beg)
  13639. (setq cc (current-column))
  13640. (beginning-of-line 1)
  13641. (setq off (looking-at re))
  13642. (while (> nlines 0)
  13643. (setq nlines (1- nlines))
  13644. (beginning-of-line 1)
  13645. (cond
  13646. (arg
  13647. (move-to-column cc t)
  13648. (insert ":\n")
  13649. (forward-line -1))
  13650. ((and off (looking-at re))
  13651. (replace-match "" t t nil 1))
  13652. ((not off) (move-to-column cc t) (insert ":")))
  13653. (forward-line 1)))
  13654. (save-excursion
  13655. (org-back-to-heading)
  13656. (if (looking-at (concat outline-regexp
  13657. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  13658. (replace-match "" t t nil 1)
  13659. (if (looking-at outline-regexp)
  13660. (progn
  13661. (goto-char (match-end 0))
  13662. (insert org-quote-string " "))))))))
  13663. ;;;; Functions extending outline functionality
  13664. (defun org-beginning-of-line (&optional arg)
  13665. "Go to the beginning of the current line. If that is invisible, continue
  13666. to a visible line beginning. This makes the function of C-a more intuitive.
  13667. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  13668. first attempt, and only move to after the tags when the cursor is already
  13669. beyond the end of the headline."
  13670. (interactive "P")
  13671. (let ((pos (point)))
  13672. (beginning-of-line 1)
  13673. (if (bobp)
  13674. nil
  13675. (backward-char 1)
  13676. (if (org-invisible-p)
  13677. (while (and (not (bobp)) (org-invisible-p))
  13678. (backward-char 1)
  13679. (beginning-of-line 1))
  13680. (forward-char 1)))
  13681. (when org-special-ctrl-a/e
  13682. (cond
  13683. ((and (looking-at org-todo-line-regexp)
  13684. (= (char-after (match-end 1)) ?\ ))
  13685. (goto-char
  13686. (if (eq org-special-ctrl-a/e t)
  13687. (cond ((> pos (match-beginning 3)) (match-beginning 3))
  13688. ((= pos (point)) (match-beginning 3))
  13689. (t (point)))
  13690. (cond ((> pos (point)) (point))
  13691. ((not (eq last-command this-command)) (point))
  13692. (t (match-beginning 3))))))
  13693. ((org-at-item-p)
  13694. (goto-char
  13695. (if (eq org-special-ctrl-a/e t)
  13696. (cond ((> pos (match-end 4)) (match-end 4))
  13697. ((= pos (point)) (match-end 4))
  13698. (t (point)))
  13699. (cond ((> pos (point)) (point))
  13700. ((not (eq last-command this-command)) (point))
  13701. (t (match-end 4))))))))))
  13702. (defun org-end-of-line (&optional arg)
  13703. "Go to the end of the line.
  13704. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  13705. first attempt, and only move to after the tags when the cursor is already
  13706. beyond the end of the headline."
  13707. (interactive "P")
  13708. (if (or (not org-special-ctrl-a/e)
  13709. (not (org-on-heading-p)))
  13710. (end-of-line arg)
  13711. (let ((pos (point)))
  13712. (beginning-of-line 1)
  13713. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  13714. (if (eq org-special-ctrl-a/e t)
  13715. (if (or (< pos (match-beginning 1))
  13716. (= pos (match-end 0)))
  13717. (goto-char (match-beginning 1))
  13718. (goto-char (match-end 0)))
  13719. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  13720. (goto-char (match-end 0))
  13721. (goto-char (match-beginning 1))))
  13722. (end-of-line arg)))))
  13723. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  13724. (define-key org-mode-map "\C-e" 'org-end-of-line)
  13725. (defun org-kill-line (&optional arg)
  13726. "Kill line, to tags or end of line."
  13727. (interactive "P")
  13728. (cond
  13729. ((or (not org-special-ctrl-k)
  13730. (bolp)
  13731. (not (org-on-heading-p)))
  13732. (call-interactively 'kill-line))
  13733. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  13734. (kill-region (point) (match-beginning 1))
  13735. (org-set-tags nil t))
  13736. (t (kill-region (point) (point-at-eol)))))
  13737. (define-key org-mode-map "\C-k" 'org-kill-line)
  13738. (defun org-invisible-p ()
  13739. "Check if point is at a character currently not visible."
  13740. ;; Early versions of noutline don't have `outline-invisible-p'.
  13741. (if (fboundp 'outline-invisible-p)
  13742. (outline-invisible-p)
  13743. (get-char-property (point) 'invisible)))
  13744. (defun org-invisible-p2 ()
  13745. "Check if point is at a character currently not visible."
  13746. (save-excursion
  13747. (if (and (eolp) (not (bobp))) (backward-char 1))
  13748. ;; Early versions of noutline don't have `outline-invisible-p'.
  13749. (if (fboundp 'outline-invisible-p)
  13750. (outline-invisible-p)
  13751. (get-char-property (point) 'invisible))))
  13752. (defalias 'org-back-to-heading 'outline-back-to-heading)
  13753. (defalias 'org-on-heading-p 'outline-on-heading-p)
  13754. (defalias 'org-at-heading-p 'outline-on-heading-p)
  13755. (defun org-at-heading-or-item-p ()
  13756. (or (org-on-heading-p) (org-at-item-p)))
  13757. (defun org-on-target-p ()
  13758. (or (org-in-regexp org-radio-target-regexp)
  13759. (org-in-regexp org-target-regexp)))
  13760. (defun org-up-heading-all (arg)
  13761. "Move to the heading line of which the present line is a subheading.
  13762. This function considers both visible and invisible heading lines.
  13763. With argument, move up ARG levels."
  13764. (if (fboundp 'outline-up-heading-all)
  13765. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  13766. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  13767. (defun org-up-heading-safe ()
  13768. "Move to the heading line of which the present line is a subheading.
  13769. This version will not throw an error. It will return the level of the
  13770. headline found, or nil if no higher level is found."
  13771. (let ((pos (point)) start-level level
  13772. (re (concat "^" outline-regexp)))
  13773. (catch 'exit
  13774. (outline-back-to-heading t)
  13775. (setq start-level (funcall outline-level))
  13776. (if (equal start-level 1) (throw 'exit nil))
  13777. (while (re-search-backward re nil t)
  13778. (setq level (funcall outline-level))
  13779. (if (< level start-level) (throw 'exit level)))
  13780. nil)))
  13781. (defun org-first-sibling-p ()
  13782. "Is this heading the first child of its parents?"
  13783. (interactive)
  13784. (let ((re (concat "^" outline-regexp))
  13785. level l)
  13786. (unless (org-at-heading-p t)
  13787. (error "Not at a heading"))
  13788. (setq level (funcall outline-level))
  13789. (save-excursion
  13790. (if (not (re-search-backward re nil t))
  13791. t
  13792. (setq l (funcall outline-level))
  13793. (< l level)))))
  13794. (defun org-goto-sibling (&optional previous)
  13795. "Goto the next sibling, even if it is invisible.
  13796. When PREVIOUS is set, go to the previous sibling instead. Returns t
  13797. when a sibling was found. When none is found, return nil and don't
  13798. move point."
  13799. (let ((fun (if previous 're-search-backward 're-search-forward))
  13800. (pos (point))
  13801. (re (concat "^" outline-regexp))
  13802. level l)
  13803. (when (condition-case nil (org-back-to-heading t) (error nil))
  13804. (setq level (funcall outline-level))
  13805. (catch 'exit
  13806. (or previous (forward-char 1))
  13807. (while (funcall fun re nil t)
  13808. (setq l (funcall outline-level))
  13809. (when (< l level) (goto-char pos) (throw 'exit nil))
  13810. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  13811. (goto-char pos)
  13812. nil))))
  13813. (defun org-show-siblings ()
  13814. "Show all siblings of the current headline."
  13815. (save-excursion
  13816. (while (org-goto-sibling) (org-flag-heading nil)))
  13817. (save-excursion
  13818. (while (org-goto-sibling 'previous)
  13819. (org-flag-heading nil))))
  13820. (defun org-show-hidden-entry ()
  13821. "Show an entry where even the heading is hidden."
  13822. (save-excursion
  13823. (org-show-entry)))
  13824. (defun org-flag-heading (flag &optional entry)
  13825. "Flag the current heading. FLAG non-nil means make invisible.
  13826. When ENTRY is non-nil, show the entire entry."
  13827. (save-excursion
  13828. (org-back-to-heading t)
  13829. ;; Check if we should show the entire entry
  13830. (if entry
  13831. (progn
  13832. (org-show-entry)
  13833. (save-excursion
  13834. (and (outline-next-heading)
  13835. (org-flag-heading nil))))
  13836. (outline-flag-region (max (point-min) (1- (point)))
  13837. (save-excursion (outline-end-of-heading) (point))
  13838. flag))))
  13839. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  13840. ;; This is an exact copy of the original function, but it uses
  13841. ;; `org-back-to-heading', to make it work also in invisible
  13842. ;; trees. And is uses an invisible-OK argument.
  13843. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  13844. (org-back-to-heading invisible-OK)
  13845. (let ((first t)
  13846. (level (funcall outline-level)))
  13847. (while (and (not (eobp))
  13848. (or first (> (funcall outline-level) level)))
  13849. (setq first nil)
  13850. (outline-next-heading))
  13851. (unless to-heading
  13852. (if (memq (preceding-char) '(?\n ?\^M))
  13853. (progn
  13854. ;; Go to end of line before heading
  13855. (forward-char -1)
  13856. (if (memq (preceding-char) '(?\n ?\^M))
  13857. ;; leave blank line before heading
  13858. (forward-char -1))))))
  13859. (point))
  13860. (defun org-show-subtree ()
  13861. "Show everything after this heading at deeper levels."
  13862. (outline-flag-region
  13863. (point)
  13864. (save-excursion
  13865. (outline-end-of-subtree) (outline-next-heading) (point))
  13866. nil))
  13867. (defun org-show-entry ()
  13868. "Show the body directly following this heading.
  13869. Show the heading too, if it is currently invisible."
  13870. (interactive)
  13871. (save-excursion
  13872. (condition-case nil
  13873. (progn
  13874. (org-back-to-heading t)
  13875. (outline-flag-region
  13876. (max (point-min) (1- (point)))
  13877. (save-excursion
  13878. (re-search-forward
  13879. (concat "[\r\n]\\(" outline-regexp "\\)") nil 'move)
  13880. (or (match-beginning 1) (point-max)))
  13881. nil))
  13882. (error nil))))
  13883. (defun org-make-options-regexp (kwds)
  13884. "Make a regular expression for keyword lines."
  13885. (concat
  13886. "^"
  13887. "#?[ \t]*\\+\\("
  13888. (mapconcat 'regexp-quote kwds "\\|")
  13889. "\\):[ \t]*"
  13890. "\\(.+\\)"))
  13891. ;; Make isearch reveal the necessary context
  13892. (defun org-isearch-end ()
  13893. "Reveal context after isearch exits."
  13894. (when isearch-success ; only if search was successful
  13895. (if (featurep 'xemacs)
  13896. ;; Under XEmacs, the hook is run in the correct place,
  13897. ;; we directly show the context.
  13898. (org-show-context 'isearch)
  13899. ;; In Emacs the hook runs *before* restoring the overlays.
  13900. ;; So we have to use a one-time post-command-hook to do this.
  13901. ;; (Emacs 22 has a special variable, see function `org-mode')
  13902. (unless (and (boundp 'isearch-mode-end-hook-quit)
  13903. isearch-mode-end-hook-quit)
  13904. ;; Only when the isearch was not quitted.
  13905. (org-add-hook 'post-command-hook 'org-isearch-post-command
  13906. 'append 'local)))))
  13907. (defun org-isearch-post-command ()
  13908. "Remove self from hook, and show context."
  13909. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  13910. (org-show-context 'isearch))
  13911. ;;;; Integration with and fixes for other packages
  13912. ;;; Imenu support
  13913. (defvar org-imenu-markers nil
  13914. "All markers currently used by Imenu.")
  13915. (make-variable-buffer-local 'org-imenu-markers)
  13916. (defun org-imenu-new-marker (&optional pos)
  13917. "Return a new marker for use by Imenu, and remember the marker."
  13918. (let ((m (make-marker)))
  13919. (move-marker m (or pos (point)))
  13920. (push m org-imenu-markers)
  13921. m))
  13922. (defun org-imenu-get-tree ()
  13923. "Produce the index for Imenu."
  13924. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  13925. (setq org-imenu-markers nil)
  13926. (let* ((n org-imenu-depth)
  13927. (re (concat "^" outline-regexp))
  13928. (subs (make-vector (1+ n) nil))
  13929. (last-level 0)
  13930. m tree level head)
  13931. (save-excursion
  13932. (save-restriction
  13933. (widen)
  13934. (goto-char (point-max))
  13935. (while (re-search-backward re nil t)
  13936. (setq level (org-reduced-level (funcall outline-level)))
  13937. (when (<= level n)
  13938. (looking-at org-complex-heading-regexp)
  13939. (setq head (org-match-string-no-properties 4)
  13940. m (org-imenu-new-marker))
  13941. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  13942. (if (>= level last-level)
  13943. (push (cons head m) (aref subs level))
  13944. (push (cons head (aref subs (1+ level))) (aref subs level))
  13945. (loop for i from (1+ level) to n do (aset subs i nil)))
  13946. (setq last-level level)))))
  13947. (aref subs 1)))
  13948. (eval-after-load "imenu"
  13949. '(progn
  13950. (add-hook 'imenu-after-jump-hook
  13951. (lambda () (org-show-context 'org-goto)))))
  13952. ;; Speedbar support
  13953. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  13954. "Overlay marking the agenda restriction line in speedbar.")
  13955. (org-overlay-put org-speedbar-restriction-lock-overlay
  13956. 'face 'org-agenda-restriction-lock)
  13957. (org-overlay-put org-speedbar-restriction-lock-overlay
  13958. 'help-echo "Agendas are currently limited to this item.")
  13959. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  13960. (defun org-speedbar-set-agenda-restriction ()
  13961. "Restrict future agenda commands to the location at point in speedbar.
  13962. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  13963. (interactive)
  13964. (require 'org-agenda)
  13965. (let (p m tp np dir txt w)
  13966. (cond
  13967. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  13968. 'org-imenu t))
  13969. (setq m (get-text-property p 'org-imenu-marker))
  13970. (save-excursion
  13971. (save-restriction
  13972. (set-buffer (marker-buffer m))
  13973. (goto-char m)
  13974. (org-agenda-set-restriction-lock 'subtree))))
  13975. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  13976. 'speedbar-function 'speedbar-find-file))
  13977. (setq tp (previous-single-property-change
  13978. (1+ p) 'speedbar-function)
  13979. np (next-single-property-change
  13980. tp 'speedbar-function)
  13981. dir (speedbar-line-directory)
  13982. txt (buffer-substring-no-properties (or tp (point-min))
  13983. (or np (point-max))))
  13984. (save-excursion
  13985. (save-restriction
  13986. (set-buffer (find-file-noselect
  13987. (let ((default-directory dir))
  13988. (expand-file-name txt))))
  13989. (unless (org-mode-p)
  13990. (error "Cannot restrict to non-Org-mode file"))
  13991. (org-agenda-set-restriction-lock 'file))))
  13992. (t (error "Don't know how to restrict Org-mode's agenda")))
  13993. (org-move-overlay org-speedbar-restriction-lock-overlay
  13994. (point-at-bol) (point-at-eol))
  13995. (setq current-prefix-arg nil)
  13996. (org-agenda-maybe-redo)))
  13997. (eval-after-load "speedbar"
  13998. '(progn
  13999. (speedbar-add-supported-extension ".org")
  14000. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  14001. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  14002. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  14003. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  14004. (add-hook 'speedbar-visiting-tag-hook
  14005. (lambda () (org-show-context 'org-goto)))))
  14006. ;;; Fixes and Hacks for problems with other packages
  14007. ;; Make flyspell not check words in links, to not mess up our keymap
  14008. (defun org-mode-flyspell-verify ()
  14009. "Don't let flyspell put overlays at active buttons."
  14010. (not (get-text-property (point) 'keymap)))
  14011. ;; Make `bookmark-jump' show the jump location if it was hidden.
  14012. (eval-after-load "bookmark"
  14013. '(if (boundp 'bookmark-after-jump-hook)
  14014. ;; We can use the hook
  14015. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  14016. ;; Hook not available, use advice
  14017. (defadvice bookmark-jump (after org-make-visible activate)
  14018. "Make the position visible."
  14019. (org-bookmark-jump-unhide))))
  14020. (defun org-bookmark-jump-unhide ()
  14021. "Unhide the current position, to show the bookmark location."
  14022. (and (org-mode-p)
  14023. (or (org-invisible-p)
  14024. (save-excursion (goto-char (max (point-min) (1- (point))))
  14025. (org-invisible-p)))
  14026. (org-show-context 'bookmark-jump)))
  14027. ;; Make session.el ignore our circular variable
  14028. (eval-after-load "session"
  14029. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  14030. ;;;; Experimental code
  14031. (defun org-closed-in-range ()
  14032. "Sparse tree of items closed in a certain time range.
  14033. Still experimental, may disappear in the future."
  14034. (interactive)
  14035. ;; Get the time interval from the user.
  14036. (let* ((time1 (time-to-seconds
  14037. (org-read-date nil 'to-time nil "Starting date: ")))
  14038. (time2 (time-to-seconds
  14039. (org-read-date nil 'to-time nil "End date:")))
  14040. ;; callback function
  14041. (callback (lambda ()
  14042. (let ((time
  14043. (time-to-seconds
  14044. (apply 'encode-time
  14045. (org-parse-time-string
  14046. (match-string 1))))))
  14047. ;; check if time in interval
  14048. (and (>= time time1) (<= time time2))))))
  14049. ;; make tree, check each match with the callback
  14050. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  14051. ;;;; Finish up
  14052. (provide 'org)
  14053. (run-hooks 'org-load-hook)
  14054. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  14055. ;;; org.el ends here