org.el 531 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357
  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.10c
  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 of the License, or
  15. ;; (at your option) 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. If not, see <http://www.gnu.org/licenses/>.
  22. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  23. ;;
  24. ;;; Commentary:
  25. ;;
  26. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  27. ;; project planning with a fast and effective plain-text system.
  28. ;;
  29. ;; Org-mode develops organizational tasks around NOTES files that contain
  30. ;; information about projects as plain text. Org-mode is implemented on
  31. ;; top of outline-mode, which makes it possible to keep the content of
  32. ;; large files well structured. Visibility cycling and structure editing
  33. ;; help to work with the tree. Tables are easily created with a built-in
  34. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  35. ;; and scheduling. It dynamically compiles entries into an agenda that
  36. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  37. ;; Plain text URL-like links connect to websites, emails, Usenet
  38. ;; messages, BBDB entries, and any files related to the projects. For
  39. ;; printing and sharing of notes, an Org-mode file can be exported as a
  40. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  41. ;; iCalendar file. It can also serve as a publishing tool for a set of
  42. ;; linked webpages.
  43. ;;
  44. ;; Installation and Activation
  45. ;; ---------------------------
  46. ;; See the corresponding sections in the manual at
  47. ;;
  48. ;; http://orgmode.org/org.html#Installation
  49. ;;
  50. ;; Documentation
  51. ;; -------------
  52. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  53. ;; distribution also contains a PDF version of it. At the homepage of
  54. ;; Org-mode, you can read the same text online as HTML. There is also an
  55. ;; excellent reference card made by Philip Rooke. This card can be found
  56. ;; in the etc/ directory of Emacs 22.
  57. ;;
  58. ;; A list of recent changes can be found at
  59. ;; http://orgmode.org/Changes.html
  60. ;;
  61. ;;; Code:
  62. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  63. (defvar org-table-formula-constants-local nil
  64. "Local version of `org-table-formula-constants'.")
  65. (make-variable-buffer-local 'org-table-formula-constants-local)
  66. ;;;; Require other packages
  67. (eval-when-compile
  68. (require 'cl)
  69. (require 'gnus-sum)
  70. (require 'calendar))
  71. ;; For XEmacs, noutline is not yet provided by outline.el, so arrange for
  72. ;; the file noutline.el being loaded.
  73. (if (featurep 'xemacs) (condition-case nil (require 'noutline)))
  74. ;; We require noutline, which might be provided in outline.el
  75. (require 'outline) (require 'noutline)
  76. ;; Other stuff we need.
  77. (require 'time-date)
  78. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  79. (require 'easymenu)
  80. (require 'org-macs)
  81. (require 'org-compat)
  82. (require 'org-faces)
  83. (require 'org-list)
  84. ;;;; Customization variables
  85. ;;; Version
  86. (defconst org-version "6.10c"
  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. (when (org-bound-and-true-p org-modules)
  125. (let ((a (member 'org-infojs org-modules)))
  126. (and a (setcar a 'org-jsinfo))))
  127. (defcustom org-modules '(org-bbdb org-bibtex org-gnus org-info org-jsinfo org-irc org-mew org-mhe org-rmail org-vm org-wl)
  128. "Modules that should always be loaded together with org.el.
  129. If a description starts with <C>, the file is not part of Emacs
  130. and loading it will require that you have downloaded and properly installed
  131. the org-mode distribution.
  132. You can also use this system to load external packages (i.e. neither Org
  133. core modules, not modules from the CONTRIB directory). Just add symbols
  134. to the end of the list. If the package is called org-xyz.el, then you need
  135. to add the symbol `xyz', and the package must have a call to
  136. (provide 'org-xyz)"
  137. :group 'org
  138. :set 'org-set-modules
  139. :type
  140. '(set :greedy t
  141. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  142. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  143. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  144. (const :tag " id: Global id's for identifying entries" org-id)
  145. (const :tag " info: Links to Info nodes" org-info)
  146. (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
  147. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  148. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  149. (const :tag " mew Links to Mew folders/messages" org-mew)
  150. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  151. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  152. (const :tag " vm: Links to VM folders/messages" org-vm)
  153. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  154. (const :tag " mouse: Additional mouse support" org-mouse)
  155. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  156. (const :tag "C annotation-helper: Call Remeber directly from Browser" org-annotation-helper)
  157. (const :tag "C bookmark: Org links to bookmarks" org-bookmark)
  158. (const :tag "C depend: TODO dependencies for Org-mode" org-depend)
  159. (const :tag "C elisp-symbol: Org links to emacs-lisp symbols" org-elisp-symbol)
  160. (const :tag "C eval: Include command output as text" org-eval)
  161. (const :tag "C expiry: Expiry mechanism for Org entries" org-expiry)
  162. (const :tag "C id: Global id's for identifying entries" org-id)
  163. (const :tag "C interactive-query: Interactive modification of tags query" org-interactive-query)
  164. (const :tag "C mairix: Hook mairix search into Org for different MUAs" org-mairix)
  165. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  166. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  167. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  168. (const :tag "C registry: A registry for Org links" org-registry)
  169. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  170. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  171. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  172. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  173. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  174. (defgroup org-startup nil
  175. "Options concerning startup of Org-mode."
  176. :tag "Org Startup"
  177. :group 'org)
  178. (defcustom org-startup-folded t
  179. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  180. This can also be configured on a per-file basis by adding one of
  181. the following lines anywhere in the buffer:
  182. #+STARTUP: fold
  183. #+STARTUP: nofold
  184. #+STARTUP: content"
  185. :group 'org-startup
  186. :type '(choice
  187. (const :tag "nofold: show all" nil)
  188. (const :tag "fold: overview" t)
  189. (const :tag "content: all headlines" content)))
  190. (defcustom org-startup-truncated t
  191. "Non-nil means, entering Org-mode will set `truncate-lines'.
  192. This is useful since some lines containing links can be very long and
  193. uninteresting. Also tables look terrible when wrapped."
  194. :group 'org-startup
  195. :type 'boolean)
  196. (defcustom org-startup-align-all-tables nil
  197. "Non-nil means, align all tables when visiting a file.
  198. This is useful when the column width in tables is forced with <N> cookies
  199. in table fields. Such tables will look correct only after the first re-align.
  200. This can also be configured on a per-file basis by adding one of
  201. the following lines anywhere in the buffer:
  202. #+STARTUP: align
  203. #+STARTUP: noalign"
  204. :group 'org-startup
  205. :type 'boolean)
  206. (defcustom org-insert-mode-line-in-empty-file nil
  207. "Non-nil means insert the first line setting Org-mode in empty files.
  208. When the function `org-mode' is called interactively in an empty file, this
  209. normally means that the file name does not automatically trigger Org-mode.
  210. To ensure that the file will always be in Org-mode in the future, a
  211. line enforcing Org-mode will be inserted into the buffer, if this option
  212. has been set."
  213. :group 'org-startup
  214. :type 'boolean)
  215. (defcustom org-replace-disputed-keys nil
  216. "Non-nil means use alternative key bindings for some keys.
  217. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  218. These keys are also used by other packages like `CUA-mode' or `windmove.el'.
  219. If you want to use Org-mode together with one of these other modes,
  220. or more generally if you would like to move some Org-mode commands to
  221. other keys, set this variable and configure the keys with the variable
  222. `org-disputed-keys'.
  223. This option is only relevant at load-time of Org-mode, and must be set
  224. *before* org.el is loaded. Changing it requires a restart of Emacs to
  225. become effective."
  226. :group 'org-startup
  227. :type 'boolean)
  228. (defcustom org-use-extra-keys nil
  229. "Non-nil means use extra key sequence definitions for certain
  230. commands. This happens automatically if you run XEmacs or if
  231. window-system is nil. This variable lets you do the same
  232. manually. You must set it before loading org.
  233. Example: on Carbon Emacs 22 running graphically, with an external
  234. keyboard on a Powerbook, the default way of setting M-left might
  235. not work for either Alt or ESC. Setting this variable will make
  236. it work for ESC."
  237. :group 'org-startup
  238. :type 'boolean)
  239. (if (fboundp 'defvaralias)
  240. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  241. (defcustom org-disputed-keys
  242. '(([(shift up)] . [(meta p)])
  243. ([(shift down)] . [(meta n)])
  244. ([(shift left)] . [(meta -)])
  245. ([(shift right)] . [(meta +)])
  246. ([(control shift right)] . [(meta shift +)])
  247. ([(control shift left)] . [(meta shift -)]))
  248. "Keys for which Org-mode and other modes compete.
  249. This is an alist, cars are the default keys, second element specifies
  250. the alternative to use when `org-replace-disputed-keys' is t.
  251. Keys can be specified in any syntax supported by `define-key'.
  252. The value of this option takes effect only at Org-mode's startup,
  253. therefore you'll have to restart Emacs to apply it after changing."
  254. :group 'org-startup
  255. :type 'alist)
  256. (defun org-key (key)
  257. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  258. Or return the original if not disputed."
  259. (if org-replace-disputed-keys
  260. (let* ((nkey (key-description key))
  261. (x (org-find-if (lambda (x)
  262. (equal (key-description (car x)) nkey))
  263. org-disputed-keys)))
  264. (if x (cdr x) key))
  265. key))
  266. (defun org-find-if (predicate seq)
  267. (catch 'exit
  268. (while seq
  269. (if (funcall predicate (car seq))
  270. (throw 'exit (car seq))
  271. (pop seq)))))
  272. (defun org-defkey (keymap key def)
  273. "Define a key, possibly translated, as returned by `org-key'."
  274. (define-key keymap (org-key key) def))
  275. (defcustom org-ellipsis nil
  276. "The ellipsis to use in the Org-mode outline.
  277. When nil, just use the standard three dots. When a string, use that instead,
  278. When a face, use the standart 3 dots, but with the specified face.
  279. The change affects only Org-mode (which will then use its own display table).
  280. Changing this requires executing `M-x org-mode' in a buffer to become
  281. effective."
  282. :group 'org-startup
  283. :type '(choice (const :tag "Default" nil)
  284. (face :tag "Face" :value org-warning)
  285. (string :tag "String" :value "...#")))
  286. (defvar org-display-table nil
  287. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  288. (defgroup org-keywords nil
  289. "Keywords in Org-mode."
  290. :tag "Org Keywords"
  291. :group 'org)
  292. (defcustom org-deadline-string "DEADLINE:"
  293. "String to mark deadline entries.
  294. A deadline is this string, followed by a time stamp. Should be a word,
  295. terminated by a colon. You can insert a schedule keyword and
  296. a timestamp with \\[org-deadline].
  297. Changes become only effective after restarting Emacs."
  298. :group 'org-keywords
  299. :type 'string)
  300. (defcustom org-scheduled-string "SCHEDULED:"
  301. "String to mark scheduled TODO entries.
  302. A schedule is this string, followed by a time stamp. Should be a word,
  303. terminated by a colon. You can insert a schedule keyword and
  304. a timestamp with \\[org-schedule].
  305. Changes become only effective after restarting Emacs."
  306. :group 'org-keywords
  307. :type 'string)
  308. (defcustom org-closed-string "CLOSED:"
  309. "String used as the prefix for timestamps logging closing a TODO entry."
  310. :group 'org-keywords
  311. :type 'string)
  312. (defcustom org-clock-string "CLOCK:"
  313. "String used as prefix for timestamps clocking work hours on an item."
  314. :group 'org-keywords
  315. :type 'string)
  316. (defcustom org-comment-string "COMMENT"
  317. "Entries starting with this keyword will never be exported.
  318. An entry can be toggled between COMMENT and normal with
  319. \\[org-toggle-comment].
  320. Changes become only effective after restarting Emacs."
  321. :group 'org-keywords
  322. :type 'string)
  323. (defcustom org-quote-string "QUOTE"
  324. "Entries starting with this keyword will be exported in fixed-width font.
  325. Quoting applies only to the text in the entry following the headline, and does
  326. not extend beyond the next headline, even if that is lower level.
  327. An entry can be toggled between QUOTE and normal with
  328. \\[org-toggle-fixed-width-section]."
  329. :group 'org-keywords
  330. :type 'string)
  331. (defconst org-repeat-re
  332. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\([.+]?\\+[0-9]+[dwmy]\\)"
  333. "Regular expression for specifying repeated events.
  334. After a match, group 1 contains the repeat expression.")
  335. (defgroup org-structure nil
  336. "Options concerning the general structure of Org-mode files."
  337. :tag "Org Structure"
  338. :group 'org)
  339. (defgroup org-reveal-location nil
  340. "Options about how to make context of a location visible."
  341. :tag "Org Reveal Location"
  342. :group 'org-structure)
  343. (defconst org-context-choice
  344. '(choice
  345. (const :tag "Always" t)
  346. (const :tag "Never" nil)
  347. (repeat :greedy t :tag "Individual contexts"
  348. (cons
  349. (choice :tag "Context"
  350. (const agenda)
  351. (const org-goto)
  352. (const occur-tree)
  353. (const tags-tree)
  354. (const link-search)
  355. (const mark-goto)
  356. (const bookmark-jump)
  357. (const isearch)
  358. (const default))
  359. (boolean))))
  360. "Contexts for the reveal options.")
  361. (defcustom org-show-hierarchy-above '((default . t))
  362. "Non-nil means, show full hierarchy when revealing a location.
  363. Org-mode often shows locations in an org-mode file which might have
  364. been invisible before. When this is set, the hierarchy of headings
  365. above the exposed location is shown.
  366. Turning this off for example for sparse trees makes them very compact.
  367. Instead of t, this can also be an alist specifying this option for different
  368. contexts. Valid contexts are
  369. agenda when exposing an entry from the agenda
  370. org-goto when using the command `org-goto' on key C-c C-j
  371. occur-tree when using the command `org-occur' on key C-c /
  372. tags-tree when constructing a sparse tree based on tags matches
  373. link-search when exposing search matches associated with a link
  374. mark-goto when exposing the jump goal of a mark
  375. bookmark-jump when exposing a bookmark location
  376. isearch when exiting from an incremental search
  377. default default for all contexts not set explicitly"
  378. :group 'org-reveal-location
  379. :type org-context-choice)
  380. (defcustom org-show-following-heading '((default . nil))
  381. "Non-nil means, show following heading when revealing a location.
  382. Org-mode often shows locations in an org-mode file which might have
  383. been invisible before. When this is set, the heading following the
  384. match is shown.
  385. Turning this off for example for sparse trees makes them very compact,
  386. but makes it harder to edit the location of the match. In such a case,
  387. use the command \\[org-reveal] to show more context.
  388. Instead of t, this can also be an alist specifying this option for different
  389. contexts. See `org-show-hierarchy-above' for valid contexts."
  390. :group 'org-reveal-location
  391. :type org-context-choice)
  392. (defcustom org-show-siblings '((default . nil) (isearch t))
  393. "Non-nil means, show all sibling heading when revealing a location.
  394. Org-mode often shows locations in an org-mode file which might have
  395. been invisible before. When this is set, the sibling of the current entry
  396. heading are all made visible. If `org-show-hierarchy-above' is t,
  397. the same happens on each level of the hierarchy above the current entry.
  398. By default this is on for the isearch context, off for all other contexts.
  399. Turning this off for example for sparse trees makes them very compact,
  400. but makes it harder to edit the location of the match. In such a case,
  401. use the command \\[org-reveal] to show more context.
  402. Instead of t, this can also be an alist specifying this option for different
  403. contexts. See `org-show-hierarchy-above' for valid contexts."
  404. :group 'org-reveal-location
  405. :type org-context-choice)
  406. (defcustom org-show-entry-below '((default . nil))
  407. "Non-nil means, show the entry below a headline when revealing a location.
  408. Org-mode often shows locations in an org-mode file which might have
  409. been invisible before. When this is set, the text below the headline that is
  410. exposed is also shown.
  411. By default this is off for all contexts.
  412. Instead of t, this can also be an alist specifying this option for different
  413. contexts. See `org-show-hierarchy-above' for valid contexts."
  414. :group 'org-reveal-location
  415. :type org-context-choice)
  416. (defcustom org-indirect-buffer-display 'other-window
  417. "How should indirect tree buffers be displayed?
  418. This applies to indirect buffers created with the commands
  419. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  420. Valid values are:
  421. current-window Display in the current window
  422. other-window Just display in another window.
  423. dedicated-frame Create one new frame, and re-use it each time.
  424. new-frame Make a new frame each time. Note that in this case
  425. previously-made indirect buffers are kept, and you need to
  426. kill these buffers yourself."
  427. :group 'org-structure
  428. :group 'org-agenda-windows
  429. :type '(choice
  430. (const :tag "In current window" current-window)
  431. (const :tag "In current frame, other window" other-window)
  432. (const :tag "Each time a new frame" new-frame)
  433. (const :tag "One dedicated frame" dedicated-frame)))
  434. (defgroup org-cycle nil
  435. "Options concerning visibility cycling in Org-mode."
  436. :tag "Org Cycle"
  437. :group 'org-structure)
  438. (defcustom org-drawers '("PROPERTIES" "CLOCK")
  439. "Names of drawers. Drawers are not opened by cycling on the headline above.
  440. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  441. this:
  442. :DRAWERNAME:
  443. .....
  444. :END:
  445. The drawer \"PROPERTIES\" is special for capturing properties through
  446. the property API.
  447. Drawers can be defined on the per-file basis with a line like:
  448. #+DRAWERS: HIDDEN STATE PROPERTIES"
  449. :group 'org-structure
  450. :type '(repeat (string :tag "Drawer Name")))
  451. (defcustom org-cycle-global-at-bob nil
  452. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  453. This makes it possible to do global cycling without having to use S-TAB or
  454. C-u TAB. For this special case to work, the first line of the buffer
  455. must not be a headline - it may be empty ot some other text. When used in
  456. this way, `org-cycle-hook' is disables temporarily, to make sure the
  457. cursor stays at the beginning of the buffer.
  458. When this option is nil, don't do anything special at the beginning
  459. of the buffer."
  460. :group 'org-cycle
  461. :type 'boolean)
  462. (defcustom org-cycle-emulate-tab t
  463. "Where should `org-cycle' emulate TAB.
  464. nil Never
  465. white Only in completely white lines
  466. whitestart Only at the beginning of lines, before the first non-white char
  467. t Everywhere except in headlines
  468. exc-hl-bol Everywhere except at the start of a headline
  469. If TAB is used in a place where it does not emulate TAB, the current subtree
  470. visibility is cycled."
  471. :group 'org-cycle
  472. :type '(choice (const :tag "Never" nil)
  473. (const :tag "Only in completely white lines" white)
  474. (const :tag "Before first char in a line" whitestart)
  475. (const :tag "Everywhere except in headlines" t)
  476. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  477. ))
  478. (defcustom org-cycle-separator-lines 2
  479. "Number of empty lines needed to keep an empty line between collapsed trees.
  480. If you leave an empty line between the end of a subtree and the following
  481. headline, this empty line is hidden when the subtree is folded.
  482. Org-mode will leave (exactly) one empty line visible if the number of
  483. empty lines is equal or larger to the number given in this variable.
  484. So the default 2 means, at least 2 empty lines after the end of a subtree
  485. are needed to produce free space between a collapsed subtree and the
  486. following headline.
  487. Special case: when 0, never leave empty lines in collapsed view."
  488. :group 'org-cycle
  489. :type 'integer)
  490. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  491. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  492. org-cycle-hide-drawers
  493. org-cycle-show-empty-lines
  494. org-optimize-window-after-visibility-change)
  495. "Hook that is run after `org-cycle' has changed the buffer visibility.
  496. The function(s) in this hook must accept a single argument which indicates
  497. the new state that was set by the most recent `org-cycle' command. The
  498. argument is a symbol. After a global state change, it can have the values
  499. `overview', `content', or `all'. After a local state change, it can have
  500. the values `folded', `children', or `subtree'."
  501. :group 'org-cycle
  502. :type 'hook)
  503. (defgroup org-edit-structure nil
  504. "Options concerning structure editing in Org-mode."
  505. :tag "Org Edit Structure"
  506. :group 'org-structure)
  507. (defcustom org-odd-levels-only nil
  508. "Non-nil means, skip even levels and only use odd levels for the outline.
  509. This has the effect that two stars are being added/taken away in
  510. promotion/demotion commands. It also influences how levels are
  511. handled by the exporters.
  512. Changing it requires restart of `font-lock-mode' to become effective
  513. for fontification also in regions already fontified.
  514. You may also set this on a per-file basis by adding one of the following
  515. lines to the buffer:
  516. #+STARTUP: odd
  517. #+STARTUP: oddeven"
  518. :group 'org-edit-structure
  519. :group 'org-font-lock
  520. :type 'boolean)
  521. (defcustom org-adapt-indentation t
  522. "Non-nil means, adapt indentation when promoting and demoting.
  523. When this is set and the *entire* text in an entry is indented, the
  524. indentation is increased by one space in a demotion command, and
  525. decreased by one in a promotion command. If any line in the entry
  526. body starts at column 0, indentation is not changed at all."
  527. :group 'org-edit-structure
  528. :type 'boolean)
  529. (defcustom org-special-ctrl-a/e nil
  530. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  531. When t, `C-a' will bring back the cursor to the beginning of the
  532. headline text, i.e. after the stars and after a possible TODO keyword.
  533. In an item, this will be the position after the bullet.
  534. When the cursor is already at that position, another `C-a' will bring
  535. it to the beginning of the line.
  536. `C-e' will jump to the end of the headline, ignoring the presence of tags
  537. in the headline. A second `C-e' will then jump to the true end of the
  538. line, after any tags.
  539. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  540. and only a directly following, identical keypress will bring the cursor
  541. to the special positions."
  542. :group 'org-edit-structure
  543. :type '(choice
  544. (const :tag "off" nil)
  545. (const :tag "after bullet first" t)
  546. (const :tag "border first" reversed)))
  547. (if (fboundp 'defvaralias)
  548. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  549. (defcustom org-special-ctrl-k nil
  550. "Non-nil means `C-k' will behave specially in headlines.
  551. When nil, `C-k' will call the default `kill-line' command.
  552. When t, the following will happen while the cursor is in the headline:
  553. - When the cursor is at the beginning of a headline, kill the entire
  554. line and possible the folded subtree below the line.
  555. - When in the middle of the headline text, kill the headline up to the tags.
  556. - When after the headline text, kill the tags."
  557. :group 'org-edit-structure
  558. :type 'boolean)
  559. (defcustom org-yank-folded-subtrees t
  560. "Non-nil means, when yanking subtrees, fold them.
  561. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  562. it starts with a heading and all other headings in it are either children
  563. or siblings, then fold all the subtrees."
  564. :group 'org-edit-structure
  565. :type 'boolean)
  566. (defcustom org-yank-adjusted-subtrees t
  567. "Non-nil means, when yanking subtrees, adjust the level.
  568. With this setting, `org-paste-subtree' is used to insert the subtree, see
  569. this function for details."
  570. :group 'org-edit-structure
  571. :type 'boolean)
  572. (defcustom org-M-RET-may-split-line '((default . t))
  573. "Non-nil means, M-RET will split the line at the cursor position.
  574. When nil, it will go to the end of the line before making a
  575. new line.
  576. You may also set this option in a different way for different
  577. contexts. Valid contexts are:
  578. headline when creating a new headline
  579. item when creating a new item
  580. table in a table field
  581. default the value to be used for all contexts not explicitly
  582. customized"
  583. :group 'org-structure
  584. :group 'org-table
  585. :type '(choice
  586. (const :tag "Always" t)
  587. (const :tag "Never" nil)
  588. (repeat :greedy t :tag "Individual contexts"
  589. (cons
  590. (choice :tag "Context"
  591. (const headline)
  592. (const item)
  593. (const table)
  594. (const default))
  595. (boolean)))))
  596. (defcustom org-insert-heading-respect-content nil
  597. "Non-nil means, insert new headings after the current subtree.
  598. When nil, the new heading is created directly after the current line.
  599. The commands \\[org-insert-heading-respect-content] and
  600. \\[org-insert-todo-heading-respect-content] turn this variable on
  601. for the duration of the command."
  602. :group 'org-structure
  603. :type 'boolean)
  604. (defcustom org-blank-before-new-entry '((heading . nil)
  605. (plain-list-item . nil))
  606. "Should `org-insert-heading' leave a blank line before new heading/item?
  607. The value is an alist, with `heading' and `plain-list-item' as car,
  608. and a boolean flag as cdr."
  609. :group 'org-edit-structure
  610. :type '(list
  611. (cons (const heading) (boolean))
  612. (cons (const plain-list-item) (boolean))))
  613. (defcustom org-insert-heading-hook nil
  614. "Hook being run after inserting a new heading."
  615. :group 'org-edit-structure
  616. :type 'hook)
  617. (defcustom org-enable-fixed-width-editor t
  618. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  619. This currently only means, they are never auto-wrapped.
  620. When nil, such lines will be treated like ordinary lines.
  621. See also the QUOTE keyword."
  622. :group 'org-edit-structure
  623. :type 'boolean)
  624. (defcustom org-edit-src-region-extra nil
  625. "Additional regexps to identify regions for editing with `org-edit-src-code'.
  626. For examples see the function `org-edit-src-find-region-and-lang'.
  627. The regular expression identifying the begin marker should end with a newline,
  628. and the regexp marking the end line should start with a newline, to make sure
  629. there are kept outside the narrowed region."
  630. :group 'org-edit-structure
  631. :type '(repeat
  632. (list
  633. (regexp :tag "begin regexp")
  634. (regexp :tag "end regexp")
  635. (choice :tag "language"
  636. (string :tag "specify")
  637. (integer :tag "from match group")
  638. (const :tag "from `lang' element")
  639. (const :tag "from `style' element")))))
  640. (defcustom org-edit-fixed-width-region-mode 'artist-mode
  641. "The mode that should be used to edit fixed-width regions.
  642. These are the regions where each line starts with a colon."
  643. :group 'org-edit-structure
  644. :type '(choice
  645. (const artist-mode)
  646. (const picture-mode)
  647. (const fundamental-mode)
  648. (function :tag "Other (specify)")))
  649. (defcustom org-goto-auto-isearch t
  650. "Non-nil means, typing characters in org-goto starts incremental search."
  651. :group 'org-edit-structure
  652. :type 'boolean)
  653. (defgroup org-sparse-trees nil
  654. "Options concerning sparse trees in Org-mode."
  655. :tag "Org Sparse Trees"
  656. :group 'org-structure)
  657. (defcustom org-highlight-sparse-tree-matches t
  658. "Non-nil means, highlight all matches that define a sparse tree.
  659. The highlights will automatically disappear the next time the buffer is
  660. changed by an edit command."
  661. :group 'org-sparse-trees
  662. :type 'boolean)
  663. (defcustom org-remove-highlights-with-change t
  664. "Non-nil means, any change to the buffer will remove temporary highlights.
  665. Such highlights are created by `org-occur' and `org-clock-display'.
  666. When nil, `C-c C-c needs to be used to get rid of the highlights.
  667. The highlights created by `org-preview-latex-fragment' always need
  668. `C-c C-c' to be removed."
  669. :group 'org-sparse-trees
  670. :group 'org-time
  671. :type 'boolean)
  672. (defcustom org-occur-hook '(org-first-headline-recenter)
  673. "Hook that is run after `org-occur' has constructed a sparse tree.
  674. This can be used to recenter the window to show as much of the structure
  675. as possible."
  676. :group 'org-sparse-trees
  677. :type 'hook)
  678. (defgroup org-imenu-and-speedbar nil
  679. "Options concerning imenu and speedbar in Org-mode."
  680. :tag "Org Imenu and Speedbar"
  681. :group 'org-structure)
  682. (defcustom org-imenu-depth 2
  683. "The maximum level for Imenu access to Org-mode headlines.
  684. This also applied for speedbar access."
  685. :group 'org-imenu-and-speedbar
  686. :type 'number)
  687. (defgroup org-table nil
  688. "Options concerning tables in Org-mode."
  689. :tag "Org Table"
  690. :group 'org)
  691. (defcustom org-enable-table-editor 'optimized
  692. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  693. When nil, such lines will be treated like ordinary lines.
  694. When equal to the symbol `optimized', the table editor will be optimized to
  695. do the following:
  696. - Automatic overwrite mode in front of whitespace in table fields.
  697. This makes the structure of the table stay in tact as long as the edited
  698. field does not exceed the column width.
  699. - Minimize the number of realigns. Normally, the table is aligned each time
  700. TAB or RET are pressed to move to another field. With optimization this
  701. happens only if changes to a field might have changed the column width.
  702. Optimization requires replacing the functions `self-insert-command',
  703. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  704. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  705. very good at guessing when a re-align will be necessary, but you can always
  706. force one with \\[org-ctrl-c-ctrl-c].
  707. If you would like to use the optimized version in Org-mode, but the
  708. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  709. This variable can be used to turn on and off the table editor during a session,
  710. but in order to toggle optimization, a restart is required.
  711. See also the variable `org-table-auto-blank-field'."
  712. :group 'org-table
  713. :type '(choice
  714. (const :tag "off" nil)
  715. (const :tag "on" t)
  716. (const :tag "on, optimized" optimized)))
  717. (defcustom org-table-tab-recognizes-table.el t
  718. "Non-nil means, TAB will automatically notice a table.el table.
  719. When it sees such a table, it moves point into it and - if necessary -
  720. calls `table-recognize-table'."
  721. :group 'org-table-editing
  722. :type 'boolean)
  723. (defgroup org-link nil
  724. "Options concerning links in Org-mode."
  725. :tag "Org Link"
  726. :group 'org)
  727. (defvar org-link-abbrev-alist-local nil
  728. "Buffer-local version of `org-link-abbrev-alist', which see.
  729. The value of this is taken from the #+LINK lines.")
  730. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  731. (defcustom org-link-abbrev-alist nil
  732. "Alist of link abbreviations.
  733. The car of each element is a string, to be replaced at the start of a link.
  734. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  735. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  736. [[linkkey:tag][description]]
  737. If REPLACE is a string, the tag will simply be appended to create the link.
  738. If the string contains \"%s\", the tag will be inserted there.
  739. REPLACE may also be a function that will be called with the tag as the
  740. only argument to create the link, which should be returned as a string.
  741. See the manual for examples."
  742. :group 'org-link
  743. :type 'alist)
  744. (defcustom org-descriptive-links t
  745. "Non-nil means, hide link part and only show description of bracket links.
  746. Bracket links are like [[link][descritpion]]. This variable sets the initial
  747. state in new org-mode buffers. The setting can then be toggled on a
  748. per-buffer basis from the Org->Hyperlinks menu."
  749. :group 'org-link
  750. :type 'boolean)
  751. (defcustom org-link-file-path-type 'adaptive
  752. "How the path name in file links should be stored.
  753. Valid values are:
  754. relative Relative to the current directory, i.e. the directory of the file
  755. into which the link is being inserted.
  756. absolute Absolute path, if possible with ~ for home directory.
  757. noabbrev Absolute path, no abbreviation of home directory.
  758. adaptive Use relative path for files in the current directory and sub-
  759. directories of it. For other files, use an absolute path."
  760. :group 'org-link
  761. :type '(choice
  762. (const relative)
  763. (const absolute)
  764. (const noabbrev)
  765. (const adaptive)))
  766. (defcustom org-activate-links '(bracket angle plain radio tag date)
  767. "Types of links that should be activated in Org-mode files.
  768. This is a list of symbols, each leading to the activation of a certain link
  769. type. In principle, it does not hurt to turn on most link types - there may
  770. be a small gain when turning off unused link types. The types are:
  771. bracket The recommended [[link][description]] or [[link]] links with hiding.
  772. angular Links in angular brackes that may contain whitespace like
  773. <bbdb:Carsten Dominik>.
  774. plain Plain links in normal text, no whitespace, like http://google.com.
  775. radio Text that is matched by a radio target, see manual for details.
  776. tag Tag settings in a headline (link to tag search).
  777. date Time stamps (link to calendar).
  778. Changing this variable requires a restart of Emacs to become effective."
  779. :group 'org-link
  780. :type '(set (const :tag "Double bracket links (new style)" bracket)
  781. (const :tag "Angular bracket links (old style)" angular)
  782. (const :tag "Plain text links" plain)
  783. (const :tag "Radio target matches" radio)
  784. (const :tag "Tags" tag)
  785. (const :tag "Timestamps" date)))
  786. (defcustom org-make-link-description-function nil
  787. "Function to use to generate link descriptions from links. If
  788. nil the link location will be used. This function must take two
  789. parameters; the first is the link and the second the description
  790. org-insert-link has generated, and should return the description
  791. to use."
  792. :group 'org-link
  793. :type 'function)
  794. (defgroup org-link-store nil
  795. "Options concerning storing links in Org-mode."
  796. :tag "Org Store Link"
  797. :group 'org-link)
  798. (defcustom org-email-link-description-format "Email %c: %.30s"
  799. "Format of the description part of a link to an email or usenet message.
  800. The following %-excapes will be replaced by corresponding information:
  801. %F full \"From\" field
  802. %f name, taken from \"From\" field, address if no name
  803. %T full \"To\" field
  804. %t first name in \"To\" field, address if no name
  805. %c correspondent. Unually \"from NAME\", but if you sent it yourself, it
  806. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  807. %s subject
  808. %m message-id.
  809. You may use normal field width specification between the % and the letter.
  810. This is for example useful to limit the length of the subject.
  811. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  812. :group 'org-link-store
  813. :type 'string)
  814. (defcustom org-from-is-user-regexp
  815. (let (r1 r2)
  816. (when (and user-mail-address (not (string= user-mail-address "")))
  817. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  818. (when (and user-full-name (not (string= user-full-name "")))
  819. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  820. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  821. "Regexp mached against the \"From:\" header of an email or usenet message.
  822. It should match if the message is from the user him/herself."
  823. :group 'org-link-store
  824. :type 'regexp)
  825. (defcustom org-context-in-file-links t
  826. "Non-nil means, file links from `org-store-link' contain context.
  827. A search string will be added to the file name with :: as separator and
  828. used to find the context when the link is activated by the command
  829. `org-open-at-point'.
  830. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  831. negates this setting for the duration of the command."
  832. :group 'org-link-store
  833. :type 'boolean)
  834. (defcustom org-keep-stored-link-after-insertion nil
  835. "Non-nil means, keep link in list for entire session.
  836. The command `org-store-link' adds a link pointing to the current
  837. location to an internal list. These links accumulate during a session.
  838. The command `org-insert-link' can be used to insert links into any
  839. Org-mode file (offering completion for all stored links). When this
  840. option is nil, every link which has been inserted once using \\[org-insert-link]
  841. will be removed from the list, to make completing the unused links
  842. more efficient."
  843. :group 'org-link-store
  844. :type 'boolean)
  845. (defgroup org-link-follow nil
  846. "Options concerning following links in Org-mode."
  847. :tag "Org Follow Link"
  848. :group 'org-link)
  849. (defcustom org-follow-link-hook nil
  850. "Hook that is run after a link has been followed."
  851. :group 'org-link-follow
  852. :type 'hook)
  853. (defcustom org-tab-follows-link nil
  854. "Non-nil means, on links TAB will follow the link.
  855. Needs to be set before org.el is loaded."
  856. :group 'org-link-follow
  857. :type 'boolean)
  858. (defcustom org-return-follows-link nil
  859. "Non-nil means, on links RET will follow the link.
  860. Needs to be set before org.el is loaded."
  861. :group 'org-link-follow
  862. :type 'boolean)
  863. (defcustom org-mouse-1-follows-link
  864. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  865. "Non-nil means, mouse-1 on a link will follow the link.
  866. A longer mouse click will still set point. Does not work on XEmacs.
  867. Needs to be set before org.el is loaded."
  868. :group 'org-link-follow
  869. :type 'boolean)
  870. (defcustom org-mark-ring-length 4
  871. "Number of different positions to be recorded in the ring
  872. Changing this requires a restart of Emacs to work correctly."
  873. :group 'org-link-follow
  874. :type 'interger)
  875. (defcustom org-link-frame-setup
  876. '((vm . vm-visit-folder-other-frame)
  877. (gnus . gnus-other-frame)
  878. (file . find-file-other-window))
  879. "Setup the frame configuration for following links.
  880. When following a link with Emacs, it may often be useful to display
  881. this link in another window or frame. This variable can be used to
  882. set this up for the different types of links.
  883. For VM, use any of
  884. `vm-visit-folder'
  885. `vm-visit-folder-other-frame'
  886. For Gnus, use any of
  887. `gnus'
  888. `gnus-other-frame'
  889. `org-gnus-no-new-news'
  890. For FILE, use any of
  891. `find-file'
  892. `find-file-other-window'
  893. `find-file-other-frame'
  894. For the calendar, use the variable `calendar-setup'.
  895. For BBDB, it is currently only possible to display the matches in
  896. another window."
  897. :group 'org-link-follow
  898. :type '(list
  899. (cons (const vm)
  900. (choice
  901. (const vm-visit-folder)
  902. (const vm-visit-folder-other-window)
  903. (const vm-visit-folder-other-frame)))
  904. (cons (const gnus)
  905. (choice
  906. (const gnus)
  907. (const gnus-other-frame)
  908. (const org-gnus-no-new-news)))
  909. (cons (const file)
  910. (choice
  911. (const find-file)
  912. (const find-file-other-window)
  913. (const find-file-other-frame)))))
  914. (defcustom org-display-internal-link-with-indirect-buffer nil
  915. "Non-nil means, use indirect buffer to display infile links.
  916. Activating internal links (from one location in a file to another location
  917. in the same file) normally just jumps to the location. When the link is
  918. activated with a C-u prefix (or with mouse-3), the link is displayed in
  919. another window. When this option is set, the other window actually displays
  920. an indirect buffer clone of the current buffer, to avoid any visibility
  921. changes to the current buffer."
  922. :group 'org-link-follow
  923. :type 'boolean)
  924. (defcustom org-open-non-existing-files nil
  925. "Non-nil means, `org-open-file' will open non-existing files.
  926. When nil, an error will be generated."
  927. :group 'org-link-follow
  928. :type 'boolean)
  929. (defcustom org-open-directory-means-index-dot-org nil
  930. "Non-nil means, a link to a directory really means to index.org.
  931. When nil, following a directory link will run dired or open a finder/explorer
  932. window on that directory."
  933. :group 'org-link-follow
  934. :type 'boolean)
  935. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  936. "Function and arguments to call for following mailto links.
  937. This is a list with the first element being a lisp function, and the
  938. remaining elements being arguments to the function. In string arguments,
  939. %a will be replaced by the address, and %s will be replaced by the subject
  940. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  941. :group 'org-link-follow
  942. :type '(choice
  943. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  944. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  945. (const :tag "message-mail" (message-mail "%a" "%s"))
  946. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  947. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  948. "Non-nil means, ask for confirmation before executing shell links.
  949. Shell links can be dangerous: just think about a link
  950. [[shell:rm -rf ~/*][Google Search]]
  951. This link would show up in your Org-mode document as \"Google Search\",
  952. but really it would remove your entire home directory.
  953. Therefore we advise against setting this variable to nil.
  954. Just change it to `y-or-n-p' of you want to confirm with a
  955. single keystroke rather than having to type \"yes\"."
  956. :group 'org-link-follow
  957. :type '(choice
  958. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  959. (const :tag "with y-or-n (faster)" y-or-n-p)
  960. (const :tag "no confirmation (dangerous)" nil)))
  961. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  962. "Non-nil means, ask for confirmation before executing Emacs Lisp links.
  963. Elisp links can be dangerous: just think about a link
  964. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  965. This link would show up in your Org-mode document as \"Google Search\",
  966. but really it would remove your entire home directory.
  967. Therefore we advise against setting this variable to nil.
  968. Just change it to `y-or-n-p' of you want to confirm with a
  969. single keystroke rather than having to type \"yes\"."
  970. :group 'org-link-follow
  971. :type '(choice
  972. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  973. (const :tag "with y-or-n (faster)" y-or-n-p)
  974. (const :tag "no confirmation (dangerous)" nil)))
  975. (defconst org-file-apps-defaults-gnu
  976. '((remote . emacs)
  977. (t . mailcap))
  978. "Default file applications on a UNIX or GNU/Linux system.
  979. See `org-file-apps'.")
  980. (defconst org-file-apps-defaults-macosx
  981. '((remote . emacs)
  982. (t . "open %s")
  983. ("ps.gz" . "gv %s")
  984. ("eps.gz" . "gv %s")
  985. ("dvi" . "xdvi %s")
  986. ("fig" . "xfig %s"))
  987. "Default file applications on a MacOS X system.
  988. The system \"open\" is known as a default, but we use X11 applications
  989. for some files for which the OS does not have a good default.
  990. See `org-file-apps'.")
  991. (defconst org-file-apps-defaults-windowsnt
  992. (list
  993. '(remote . emacs)
  994. (cons t
  995. (list (if (featurep 'xemacs)
  996. 'mswindows-shell-execute
  997. 'w32-shell-execute)
  998. "open" 'file)))
  999. "Default file applications on a Windows NT system.
  1000. The system \"open\" is used for most files.
  1001. See `org-file-apps'.")
  1002. (defcustom org-file-apps
  1003. '(
  1004. (auto-mode . emacs)
  1005. ("\\.x?html?\\'" . default)
  1006. ("\\.pdf\\'" . default)
  1007. )
  1008. "External applications for opening `file:path' items in a document.
  1009. Org-mode uses system defaults for different file types, but
  1010. you can use this variable to set the application for a given file
  1011. extension. The entries in this list are cons cells where the car identifies
  1012. files and the cdr the corresponding command. Possible values for the
  1013. file identifier are
  1014. \"regex\" Regular expression matched against the file name. For backward
  1015. compatibility, this can also be a string with only alphanumeric
  1016. characters, which is then interpreted as an extension.
  1017. `directory' Matches a directory
  1018. `remote' Matches a remote file, accessible through tramp or efs.
  1019. Remote files most likely should be visited through Emacs
  1020. because external applications cannot handle such paths.
  1021. `auto-mode' Matches files that are mached by any entry in `auto-mode-alist',
  1022. so all files Emacs knows how to handle. Useing this with
  1023. command `emacs' will open most files in Emacs. Beware that this
  1024. will also open html files insite Emacs, unless you add
  1025. (\"html\" . default) to the list as well.
  1026. t Default for files not matched by any of the other options.
  1027. Possible values for the command are:
  1028. `emacs' The file will be visited by the current Emacs process.
  1029. `default' Use the default application for this file type, which is the
  1030. association for t in the list, most likely in the system-specific
  1031. part.
  1032. This can be used to overrule an unwanted seting in the
  1033. system-specific variable.
  1034. string A command to be executed by a shell; %s will be replaced
  1035. by the path to the file.
  1036. sexp A Lisp form which will be evaluated. The file path will
  1037. be available in the Lisp variable `file'.
  1038. For more examples, see the system specific constants
  1039. `org-file-apps-defaults-macosx'
  1040. `org-file-apps-defaults-windowsnt'
  1041. `org-file-apps-defaults-gnu'."
  1042. :group 'org-link-follow
  1043. :type '(repeat
  1044. (cons (choice :value ""
  1045. (string :tag "Extension")
  1046. (const :tag "Default for unrecognized files" t)
  1047. (const :tag "Remote file" remote)
  1048. (const :tag "Links to a directory" directory)
  1049. (const :tag "Any files that have Emacs modes"
  1050. auto-mode))
  1051. (choice :value ""
  1052. (const :tag "Visit with Emacs" emacs)
  1053. (const :tag "Use system default" default)
  1054. (string :tag "Command")
  1055. (sexp :tag "Lisp form")))))
  1056. (defgroup org-refile nil
  1057. "Options concerning refiling entries in Org-mode."
  1058. :tag "Org Remember"
  1059. :group 'org)
  1060. (defcustom org-directory "~/org"
  1061. "Directory with org files.
  1062. This directory will be used as default to prompt for org files.
  1063. Used by the hooks for remember.el."
  1064. :group 'org-refile
  1065. :group 'org-remember
  1066. :type 'directory)
  1067. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1068. "Default target for storing notes.
  1069. Used by the hooks for remember.el. This can be a string, or nil to mean
  1070. the value of `remember-data-file'.
  1071. You can set this on a per-template basis with the variable
  1072. `org-remember-templates'."
  1073. :group 'org-refile
  1074. :group 'org-remember
  1075. :type '(choice
  1076. (const :tag "Default from remember-data-file" nil)
  1077. file))
  1078. (defcustom org-goto-interface 'outline
  1079. "The default interface to be used for `org-goto'.
  1080. Allowed vaues are:
  1081. outline The interface shows an outline of the relevant file
  1082. and the correct heading is found by moving through
  1083. the outline or by searching with incremental search.
  1084. outline-path-completion Headlines in the current buffer are offered via
  1085. completion."
  1086. :group 'org-refile
  1087. :type '(choice
  1088. (const :tag "Outline" outline)
  1089. (const :tag "Outline-path-completion" outline-path-completion)))
  1090. (defcustom org-reverse-note-order nil
  1091. "Non-nil means, store new notes at the beginning of a file or entry.
  1092. When nil, new notes will be filed to the end of a file or entry.
  1093. This can also be a list with cons cells of regular expressions that
  1094. are matched against file names, and values."
  1095. :group 'org-remember
  1096. :type '(choice
  1097. (const :tag "Reverse always" t)
  1098. (const :tag "Reverse never" nil)
  1099. (repeat :tag "By file name regexp"
  1100. (cons regexp boolean))))
  1101. (defcustom org-refile-targets nil
  1102. "Targets for refiling entries with \\[org-refile].
  1103. This is list of cons cells. Each cell contains:
  1104. - a specification of the files to be considered, either a list of files,
  1105. or a symbol whose function or variable value will be used to retrieve
  1106. a file name or a list of file names. Nil means, refile to a different
  1107. heading in the current buffer.
  1108. - A specification of how to find candidate refile targets. This may be
  1109. any of
  1110. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1111. This tag has to be present in all target headlines, inheritance will
  1112. not be considered.
  1113. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1114. todo keyword.
  1115. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1116. headlines that are refiling targets.
  1117. - a cons cell (:level . N). Any headline of level N is considered a target.
  1118. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1119. When this variable is nil, all top-level headlines in the current buffer
  1120. are used, equivalent to the vlaue `((nil . (:level . 1))'."
  1121. :group 'org-remember
  1122. :type '(repeat
  1123. (cons
  1124. (choice :value org-agenda-files
  1125. (const :tag "All agenda files" org-agenda-files)
  1126. (const :tag "Current buffer" nil)
  1127. (function) (variable) (file))
  1128. (choice :tag "Identify target headline by"
  1129. (cons :tag "Specific tag" (const :tag) (string))
  1130. (cons :tag "TODO keyword" (const :todo) (string))
  1131. (cons :tag "Regular expression" (const :regexp) (regexp))
  1132. (cons :tag "Level number" (const :level) (integer))
  1133. (cons :tag "Max Level number" (const :maxlevel) (integer))))))
  1134. (defcustom org-refile-use-outline-path nil
  1135. "Non-nil means, provide refile targets as paths.
  1136. So a level 3 headline will be available as level1/level2/level3.
  1137. When the value is `file', also include the file name (without directory)
  1138. into the path. When `full-file-path', include the full file path."
  1139. :group 'org-remember
  1140. :type '(choice
  1141. (const :tag "Not" nil)
  1142. (const :tag "Yes" t)
  1143. (const :tag "Start with file name" file)
  1144. (const :tag "Start with full file path" full-file-path)))
  1145. (defgroup org-todo nil
  1146. "Options concerning TODO items in Org-mode."
  1147. :tag "Org TODO"
  1148. :group 'org)
  1149. (defgroup org-progress nil
  1150. "Options concerning Progress logging in Org-mode."
  1151. :tag "Org Progress"
  1152. :group 'org-time)
  1153. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1154. "List of TODO entry keyword sequences and their interpretation.
  1155. \\<org-mode-map>This is a list of sequences.
  1156. Each sequence starts with a symbol, either `sequence' or `type',
  1157. indicating if the keywords should be interpreted as a sequence of
  1158. action steps, or as different types of TODO items. The first
  1159. keywords are states requiring action - these states will select a headline
  1160. for inclusion into the global TODO list Org-mode produces. If one of
  1161. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1162. signify that no further action is necessary. If \"|\" is not found,
  1163. the last keyword is treated as the only DONE state of the sequence.
  1164. The command \\[org-todo] cycles an entry through these states, and one
  1165. additional state where no keyword is present. For details about this
  1166. cycling, see the manual.
  1167. TODO keywords and interpretation can also be set on a per-file basis with
  1168. the special #+SEQ_TODO and #+TYP_TODO lines.
  1169. Each keyword can optionally specify a character for fast state selection
  1170. \(in combination with the variable `org-use-fast-todo-selection')
  1171. and specifiers for state change logging, using the same syntax
  1172. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1173. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1174. indicates to record a time stamp each time this state is selected.
  1175. Each keyword may also specify if a timestamp or a note should be
  1176. recorded when entering or leaving the state, by adding additional
  1177. characters in the parenthesis after the keyword. This looks like this:
  1178. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1179. record only the time of the state change. With X and Y being either
  1180. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1181. Y when leaving the state if and only if the *target* state does not
  1182. define X. You may omit any of the fast-selection key or X or /Y,
  1183. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1184. For backward compatibility, this variable may also be just a list
  1185. of keywords - in this case the interptetation (sequence or type) will be
  1186. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1187. :group 'org-todo
  1188. :group 'org-keywords
  1189. :type '(choice
  1190. (repeat :tag "Old syntax, just keywords"
  1191. (string :tag "Keyword"))
  1192. (repeat :tag "New syntax"
  1193. (cons
  1194. (choice
  1195. :tag "Interpretation"
  1196. (const :tag "Sequence (cycling hits every state)" sequence)
  1197. (const :tag "Type (cycling directly to DONE)" type))
  1198. (repeat
  1199. (string :tag "Keyword"))))))
  1200. (defvar org-todo-keywords-1 nil
  1201. "All TODO and DONE keywords active in a buffer.")
  1202. (make-variable-buffer-local 'org-todo-keywords-1)
  1203. (defvar org-todo-keywords-for-agenda nil)
  1204. (defvar org-done-keywords-for-agenda nil)
  1205. (defvar org-todo-keyword-alist-for-agenda nil)
  1206. (defvar org-tag-alist-for-agenda nil)
  1207. (defvar org-agenda-contributing-files nil)
  1208. (defvar org-not-done-keywords nil)
  1209. (make-variable-buffer-local 'org-not-done-keywords)
  1210. (defvar org-done-keywords nil)
  1211. (make-variable-buffer-local 'org-done-keywords)
  1212. (defvar org-todo-heads nil)
  1213. (make-variable-buffer-local 'org-todo-heads)
  1214. (defvar org-todo-sets nil)
  1215. (make-variable-buffer-local 'org-todo-sets)
  1216. (defvar org-todo-log-states nil)
  1217. (make-variable-buffer-local 'org-todo-log-states)
  1218. (defvar org-todo-kwd-alist nil)
  1219. (make-variable-buffer-local 'org-todo-kwd-alist)
  1220. (defvar org-todo-key-alist nil)
  1221. (make-variable-buffer-local 'org-todo-key-alist)
  1222. (defvar org-todo-key-trigger nil)
  1223. (make-variable-buffer-local 'org-todo-key-trigger)
  1224. (defcustom org-todo-interpretation 'sequence
  1225. "Controls how TODO keywords are interpreted.
  1226. This variable is in principle obsolete and is only used for
  1227. backward compatibility, if the interpretation of todo keywords is
  1228. not given already in `org-todo-keywords'. See that variable for
  1229. more information."
  1230. :group 'org-todo
  1231. :group 'org-keywords
  1232. :type '(choice (const sequence)
  1233. (const type)))
  1234. (defcustom org-use-fast-todo-selection 'prefix
  1235. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1236. This variable describes if and under what circumstances the cycling
  1237. mechanism for TODO keywords will be replaced by a single-key, direct
  1238. selection scheme.
  1239. When nil, fast selection is never used.
  1240. When the symbol `prefix', it will be used when `org-todo' is called with
  1241. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1242. in an agenda buffer.
  1243. When t, fast selection is used by default. In this case, the prefix
  1244. argument forces cycling instead.
  1245. In all cases, the special interface is only used if access keys have actually
  1246. been assigned by the user, i.e. if keywords in the configuration are followed
  1247. by a letter in parenthesis, like TODO(t)."
  1248. :group 'org-todo
  1249. :type '(choice
  1250. (const :tag "Never" nil)
  1251. (const :tag "By default" t)
  1252. (const :tag "Only with C-u C-c C-t" prefix)))
  1253. (defcustom org-provide-todo-statistics t
  1254. "Non-nil means, update todo statistics after insert and toggle.
  1255. When this is set, todo statistics is updated in the parent of the current
  1256. entry each time a todo state is changed."
  1257. :group 'org-todo
  1258. :type 'boolean)
  1259. (defcustom org-after-todo-state-change-hook nil
  1260. "Hook which is run after the state of a TODO item was changed.
  1261. The new state (a string with a TODO keyword, or nil) is available in the
  1262. Lisp variable `state'."
  1263. :group 'org-todo
  1264. :type 'hook)
  1265. (defcustom org-todo-state-tags-triggers nil
  1266. "Tag changes that should be triggered by TODO state changes.
  1267. This is a list. Each entry is
  1268. (state-change (tag . flag) .......)
  1269. State-change can be a string with a state, and empty string to indicate the
  1270. state that has no TODO keyword, or it can be one of the symbols `todo'
  1271. or `done', meaning any not-done or done state, respectively."
  1272. :group 'org-todo
  1273. :group 'org-tags
  1274. :type '(repeat
  1275. (cons (choice :tag "When changing to"
  1276. (const :tag "Not-done state" todo)
  1277. (const :tag "Done state" done)
  1278. (string :tag "State"))
  1279. (repeat
  1280. (cons :tag "Tag action"
  1281. (string :tag "Tag")
  1282. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  1283. (defcustom org-log-done nil
  1284. "Non-nil means, record a CLOSED timestamp when moving an entry to DONE.
  1285. When equal to the list (done), also prompt for a closing note.
  1286. This can also be configured on a per-file basis by adding one of
  1287. the following lines anywhere in the buffer:
  1288. #+STARTUP: logdone
  1289. #+STARTUP: lognotedone
  1290. #+STARTUP: nologdone"
  1291. :group 'org-todo
  1292. :group 'org-progress
  1293. :type '(choice
  1294. (const :tag "No logging" nil)
  1295. (const :tag "Record CLOSED timestamp" time)
  1296. (const :tag "Record CLOSED timestamp with closing note." note)))
  1297. ;; Normalize old uses of org-log-done.
  1298. (cond
  1299. ((eq org-log-done t) (setq org-log-done 'time))
  1300. ((and (listp org-log-done) (memq 'done org-log-done))
  1301. (setq org-log-done 'note)))
  1302. (defcustom org-log-note-clock-out nil
  1303. "Non-nil means, record a note when clocking out of an item.
  1304. This can also be configured on a per-file basis by adding one of
  1305. the following lines anywhere in the buffer:
  1306. #+STARTUP: lognoteclock-out
  1307. #+STARTUP: nolognoteclock-out"
  1308. :group 'org-todo
  1309. :group 'org-progress
  1310. :type 'boolean)
  1311. (defcustom org-log-done-with-time t
  1312. "Non-nil means, the CLOSED time stamp will contain date and time.
  1313. When nil, only the date will be recorded."
  1314. :group 'org-progress
  1315. :type 'boolean)
  1316. (defcustom org-log-note-headings
  1317. '((done . "CLOSING NOTE %t")
  1318. (state . "State %-12s %t")
  1319. (note . "Note taken on %t")
  1320. (clock-out . ""))
  1321. "Headings for notes added to entries.
  1322. The value is an alist, with the car being a symbol indicating the note
  1323. context, and the cdr is the heading to be used. The heading may also be the
  1324. empty string.
  1325. %t in the heading will be replaced by a time stamp.
  1326. %s will be replaced by the new TODO state, in double quotes.
  1327. %u will be replaced by the user name.
  1328. %U will be replaced by the full user name."
  1329. :group 'org-todo
  1330. :group 'org-progress
  1331. :type '(list :greedy t
  1332. (cons (const :tag "Heading when closing an item" done) string)
  1333. (cons (const :tag
  1334. "Heading when changing todo state (todo sequence only)"
  1335. state) string)
  1336. (cons (const :tag "Heading when just taking a note" note) string)
  1337. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1338. (unless (assq 'note org-log-note-headings)
  1339. (push '(note . "%t") org-log-note-headings))
  1340. (defcustom org-log-state-notes-insert-after-drawers nil
  1341. "Non-nil means, insert state change notes after any drawers in entry.
  1342. Only the drawers that *immediately* follow the headline and the
  1343. deadline/scheduled line are skipped.
  1344. When nil, insert notes right after the heading and perhaps the line
  1345. with deadline/scheduling if present."
  1346. :group 'org-todo
  1347. :group 'org-progress
  1348. :type 'boolean)
  1349. (defcustom org-log-states-order-reversed t
  1350. "Non-nil means, the latest state change note will be directly after heading.
  1351. When nil, the notes will be orderer according to time."
  1352. :group 'org-todo
  1353. :group 'org-progress
  1354. :type 'boolean)
  1355. (defcustom org-log-repeat 'time
  1356. "Non-nil means, record moving through the DONE state when triggering repeat.
  1357. An auto-repeating tasks is immediately switched back to TODO when marked
  1358. done. If you are not logging state changes (by adding \"@\" or \"!\" to
  1359. the TODO keyword definition, or recording a closing note by setting
  1360. `org-log-done', there will be no record of the task moving through DONE.
  1361. This variable forces taking a note anyway. Possible values are:
  1362. nil Don't force a record
  1363. time Record a time stamp
  1364. note Record a note
  1365. This option can also be set with on a per-file-basis with
  1366. #+STARTUP: logrepeat
  1367. #+STARTUP: lognoterepeat
  1368. #+STARTUP: nologrepeat
  1369. You can have local logging settings for a subtree by setting the LOGGING
  1370. property to one or more of these keywords."
  1371. :group 'org-todo
  1372. :group 'org-progress
  1373. :type '(choice
  1374. (const :tag "Don't force a record" nil)
  1375. (const :tag "Force recording the DONE state" time)
  1376. (const :tag "Force recording a note with the DONE state" note)))
  1377. (defgroup org-priorities nil
  1378. "Priorities in Org-mode."
  1379. :tag "Org Priorities"
  1380. :group 'org-todo)
  1381. (defcustom org-highest-priority ?A
  1382. "The highest priority of TODO items. A character like ?A, ?B etc.
  1383. Must have a smaller ASCII number than `org-lowest-priority'."
  1384. :group 'org-priorities
  1385. :type 'character)
  1386. (defcustom org-lowest-priority ?C
  1387. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1388. Must have a larger ASCII number than `org-highest-priority'."
  1389. :group 'org-priorities
  1390. :type 'character)
  1391. (defcustom org-default-priority ?B
  1392. "The default priority of TODO items.
  1393. This is the priority an item get if no explicit priority is given."
  1394. :group 'org-priorities
  1395. :type 'character)
  1396. (defcustom org-priority-start-cycle-with-default t
  1397. "Non-nil means, start with default priority when starting to cycle.
  1398. When this is nil, the first step in the cycle will be (depending on the
  1399. command used) one higher or lower that the default priority."
  1400. :group 'org-priorities
  1401. :type 'boolean)
  1402. (defgroup org-time nil
  1403. "Options concerning time stamps and deadlines in Org-mode."
  1404. :tag "Org Time"
  1405. :group 'org)
  1406. (defcustom org-insert-labeled-timestamps-at-point nil
  1407. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1408. When nil, these labeled time stamps are forces into the second line of an
  1409. entry, just after the headline. When scheduling from the global TODO list,
  1410. the time stamp will always be forced into the second line."
  1411. :group 'org-time
  1412. :type 'boolean)
  1413. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1414. "Formats for `format-time-string' which are used for time stamps.
  1415. It is not recommended to change this constant.")
  1416. (defcustom org-time-stamp-rounding-minutes '(0 5)
  1417. "Number of minutes to round time stamps to.
  1418. These are two values, the first applies when first creating a time stamp.
  1419. The second applies when changing it with the commands `S-up' and `S-down'.
  1420. When changing the time stamp, this means that it will change in steps
  1421. of N minutes, as given by the second value.
  1422. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  1423. numbers should be factors of 60, so for example 5, 10, 15.
  1424. When this is larger than 1, you can still force an exact time-stamp by using
  1425. a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
  1426. and by using a prefix arg to `S-up/down' to specify the exact number
  1427. of minutes to shift."
  1428. :group 'org-time
  1429. :get '(lambda (var) ; Make sure all entries have 5 elements
  1430. (if (integerp (default-value var))
  1431. (list (default-value var) 5)
  1432. (default-value var)))
  1433. :type '(list
  1434. (integer :tag "when inserting times")
  1435. (integer :tag "when modifying times")))
  1436. ;; Normalize old customizations of this variable.
  1437. (when (integerp org-time-stamp-rounding-minutes)
  1438. (setq org-time-stamp-rounding-minutes
  1439. (list org-time-stamp-rounding-minutes
  1440. org-time-stamp-rounding-minutes)))
  1441. (defcustom org-display-custom-times nil
  1442. "Non-nil means, overlay custom formats over all time stamps.
  1443. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1444. To turn this on on a per-file basis, insert anywhere in the file:
  1445. #+STARTUP: customtime"
  1446. :group 'org-time
  1447. :set 'set-default
  1448. :type 'sexp)
  1449. (make-variable-buffer-local 'org-display-custom-times)
  1450. (defcustom org-time-stamp-custom-formats
  1451. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1452. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1453. These are overlayed over the default ISO format if the variable
  1454. `org-display-custom-times' is set. Time like %H:%M should be at the
  1455. end of the second format."
  1456. :group 'org-time
  1457. :type 'sexp)
  1458. (defun org-time-stamp-format (&optional long inactive)
  1459. "Get the right format for a time string."
  1460. (let ((f (if long (cdr org-time-stamp-formats)
  1461. (car org-time-stamp-formats))))
  1462. (if inactive
  1463. (concat "[" (substring f 1 -1) "]")
  1464. f)))
  1465. (defcustom org-time-clocksum-format "%d:%02d"
  1466. "The format string used when creating CLOCKSUM lines, or when
  1467. org-mode generates a time duration."
  1468. :group 'org-time
  1469. :type 'string)
  1470. (defcustom org-deadline-warning-days 14
  1471. "No. of days before expiration during which a deadline becomes active.
  1472. This variable governs the display in sparse trees and in the agenda.
  1473. When 0 or negative, it means use this number (the absolute value of it)
  1474. even if a deadline has a different individual lead time specified."
  1475. :group 'org-time
  1476. :group 'org-agenda-daily/weekly
  1477. :type 'number)
  1478. (defcustom org-read-date-prefer-future t
  1479. "Non-nil means, assume future for incomplete date input from user.
  1480. This affects the following situations:
  1481. 1. The user gives a day, but no month.
  1482. For example, if today is the 15th, and you enter \"3\", Org-mode will
  1483. read this as the third of *next* month. However, if you enter \"17\",
  1484. it will be considered as *this* month.
  1485. 2. The user gives a month but not a year.
  1486. For example, if it is april and you enter \"feb 2\", this will be read
  1487. as feb 2, *next* year. \"May 5\", however, will be this year.
  1488. Currently this does not work for ISO week specifications.
  1489. When this option is nil, the current month and year will always be used
  1490. as defaults."
  1491. :group 'org-time
  1492. :type 'boolean)
  1493. (defcustom org-read-date-display-live t
  1494. "Non-nil means, display current interpretation of date prompt live.
  1495. This display will be in an overlay, in the minibuffer."
  1496. :group 'org-time
  1497. :type 'boolean)
  1498. (defcustom org-read-date-popup-calendar t
  1499. "Non-nil means, pop up a calendar when prompting for a date.
  1500. In the calendar, the date can be selected with mouse-1. However, the
  1501. minibuffer will also be active, and you can simply enter the date as well.
  1502. When nil, only the minibuffer will be available."
  1503. :group 'org-time
  1504. :type 'boolean)
  1505. (if (fboundp 'defvaralias)
  1506. (defvaralias 'org-popup-calendar-for-date-prompt
  1507. 'org-read-date-popup-calendar))
  1508. (defcustom org-extend-today-until 0
  1509. "The hour when your day really ends. Must be an integer.
  1510. This has influence for the following applications:
  1511. - When switching the agenda to \"today\". It it is still earlier than
  1512. the time given here, the day recognized as TODAY is actually yesterday.
  1513. - When a date is read from the user and it is still before the time given
  1514. here, the current date and time will be assumed to be yesterday, 23:59.
  1515. Also, timestamps inserted in remember templates follow this rule.
  1516. IMPORTANT: This is a feature whose implementation is and likely will
  1517. remain incomplete. Really, it is only here because past midnight seems to
  1518. be the favorite working time of John Wiegley :-)"
  1519. :group 'org-time
  1520. :type 'number)
  1521. (defcustom org-edit-timestamp-down-means-later nil
  1522. "Non-nil means, S-down will increase the time in a time stamp.
  1523. When nil, S-up will increase."
  1524. :group 'org-time
  1525. :type 'boolean)
  1526. (defcustom org-calendar-follow-timestamp-change t
  1527. "Non-nil means, make the calendar window follow timestamp changes.
  1528. When a timestamp is modified and the calendar window is visible, it will be
  1529. moved to the new date."
  1530. :group 'org-time
  1531. :type 'boolean)
  1532. (defgroup org-tags nil
  1533. "Options concerning tags in Org-mode."
  1534. :tag "Org Tags"
  1535. :group 'org)
  1536. (defcustom org-tag-alist nil
  1537. "List of tags allowed in Org-mode files.
  1538. When this list is nil, Org-mode will base TAG input on what is already in the
  1539. buffer.
  1540. The value of this variable is an alist, the car of each entry must be a
  1541. keyword as a string, the cdr may be a character that is used to select
  1542. that tag through the fast-tag-selection interface.
  1543. See the manual for details."
  1544. :group 'org-tags
  1545. :type '(repeat
  1546. (choice
  1547. (cons (string :tag "Tag name")
  1548. (character :tag "Access char"))
  1549. (const :tag "Start radio group" (:startgroup))
  1550. (const :tag "End radio group" (:endgroup)))))
  1551. (defvar org-file-tags nil
  1552. "List of tags that can be inherited by all entries in the file.
  1553. The tags will be inherited if the variable `org-use-tag-inheritance'
  1554. says they should be.
  1555. This variable is populated from #+TAG lines.")
  1556. (defcustom org-use-fast-tag-selection 'auto
  1557. "Non-nil means, use fast tag selection scheme.
  1558. This is a special interface to select and deselect tags with single keys.
  1559. When nil, fast selection is never used.
  1560. When the symbol `auto', fast selection is used if and only if selection
  1561. characters for tags have been configured, either through the variable
  1562. `org-tag-alist' or through a #+TAGS line in the buffer.
  1563. When t, fast selection is always used and selection keys are assigned
  1564. automatically if necessary."
  1565. :group 'org-tags
  1566. :type '(choice
  1567. (const :tag "Always" t)
  1568. (const :tag "Never" nil)
  1569. (const :tag "When selection characters are configured" 'auto)))
  1570. (defcustom org-fast-tag-selection-single-key nil
  1571. "Non-nil means, fast tag selection exits after first change.
  1572. When nil, you have to press RET to exit it.
  1573. During fast tag selection, you can toggle this flag with `C-c'.
  1574. This variable can also have the value `expert'. In this case, the window
  1575. displaying the tags menu is not even shown, until you press C-c again."
  1576. :group 'org-tags
  1577. :type '(choice
  1578. (const :tag "No" nil)
  1579. (const :tag "Yes" t)
  1580. (const :tag "Expert" expert)))
  1581. (defvar org-fast-tag-selection-include-todo nil
  1582. "Non-nil means, fast tags selection interface will also offer TODO states.
  1583. This is an undocumented feature, you should not rely on it.")
  1584. (defcustom org-tags-column (if (featurep 'xemacs) -79 -80)
  1585. "The column to which tags should be indented in a headline.
  1586. If this number is positive, it specifies the column. If it is negative,
  1587. it means that the tags should be flushright to that column. For example,
  1588. -80 works well for a normal 80 character screen."
  1589. :group 'org-tags
  1590. :type 'integer)
  1591. (defcustom org-auto-align-tags t
  1592. "Non-nil means, realign tags after pro/demotion of TODO state change.
  1593. These operations change the length of a headline and therefore shift
  1594. the tags around. With this options turned on, after each such operation
  1595. the tags are again aligned to `org-tags-column'."
  1596. :group 'org-tags
  1597. :type 'boolean)
  1598. (defcustom org-use-tag-inheritance t
  1599. "Non-nil means, tags in levels apply also for sublevels.
  1600. When nil, only the tags directly given in a specific line apply there.
  1601. If this option is t, a match early-on in a tree can lead to a large
  1602. number of matches in the subtree. If you only want to see the first
  1603. match in a tree during a search, check out the variable
  1604. `org-tags-match-list-sublevels'.
  1605. This may also be a list of tags that should be inherited, or a regexp that
  1606. matches tags that should be inherited."
  1607. :group 'org-tags
  1608. :type '(choice
  1609. (const :tag "Not" nil)
  1610. (const :tag "Always" t)
  1611. (repeat :tag "Specific tags" (string :tag "Tag"))
  1612. (regexp :tag "Tags matched by regexp")))
  1613. (defun org-tag-inherit-p (tag)
  1614. "Check if TAG is one that should be inherited."
  1615. (cond
  1616. ((eq org-use-tag-inheritance t) t)
  1617. ((not org-use-tag-inheritance) nil)
  1618. ((stringp org-use-tag-inheritance)
  1619. (string-match org-use-tag-inheritance tag))
  1620. ((listp org-use-tag-inheritance)
  1621. (member tag org-use-tag-inheritance))
  1622. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  1623. (defcustom org-tags-match-list-sublevels t
  1624. "Non-nil means list also sublevels of headlines matching tag search.
  1625. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  1626. the sublevels of a headline matching a tag search often also match
  1627. the same search. Listing all of them can create very long lists.
  1628. Setting this variable to nil causes subtrees of a match to be skipped.
  1629. This option is off by default, because inheritance in on. If you turn
  1630. inheritance off, you very likely want to turn this option on.
  1631. As a special case, if the tag search is restricted to TODO items, the
  1632. value of this variable is ignored and sublevels are always checked, to
  1633. make sure all corresponding TODO items find their way into the list."
  1634. :group 'org-tags
  1635. :type 'boolean)
  1636. (defvar org-tags-history nil
  1637. "History of minibuffer reads for tags.")
  1638. (defvar org-last-tags-completion-table nil
  1639. "The last used completion table for tags.")
  1640. (defvar org-after-tags-change-hook nil
  1641. "Hook that is run after the tags in a line have changed.")
  1642. (defgroup org-properties nil
  1643. "Options concerning properties in Org-mode."
  1644. :tag "Org Properties"
  1645. :group 'org)
  1646. (defcustom org-property-format "%-10s %s"
  1647. "How property key/value pairs should be formatted by `indent-line'.
  1648. When `indent-line' hits a property definition, it will format the line
  1649. according to this format, mainly to make sure that the values are
  1650. lined-up with respect to each other."
  1651. :group 'org-properties
  1652. :type 'string)
  1653. (defcustom org-use-property-inheritance nil
  1654. "Non-nil means, properties apply also for sublevels.
  1655. This setting is chiefly used during property searches. Turning it on can
  1656. cause significant overhead when doing a search, which is why it is not
  1657. on by default.
  1658. When nil, only the properties directly given in the current entry count.
  1659. When t, every property is inherited. The value may also be a list of
  1660. properties that should have inheritance, or a regular expression matching
  1661. properties that should be inherited.
  1662. However, note that some special properties use inheritance under special
  1663. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  1664. and the properties ending in \"_ALL\" when they are used as descriptor
  1665. for valid values of a property.
  1666. Note for programmers:
  1667. When querying an entry with `org-entry-get', you can control if inheritance
  1668. should be used. By default, `org-entry-get' looks only at the local
  1669. properties. You can request inheritance by setting the inherit argument
  1670. to t (to force inheritance) or to `selective' (to respect the setting
  1671. in this variable)."
  1672. :group 'org-properties
  1673. :type '(choice
  1674. (const :tag "Not" nil)
  1675. (const :tag "Always" t)
  1676. (repeat :tag "Specific properties" (string :tag "Property"))
  1677. (regexp :tag "Properties matched by regexp")))
  1678. (defun org-property-inherit-p (property)
  1679. "Check if PROPERTY is one that should be inherited."
  1680. (cond
  1681. ((eq org-use-property-inheritance t) t)
  1682. ((not org-use-property-inheritance) nil)
  1683. ((stringp org-use-property-inheritance)
  1684. (string-match org-use-property-inheritance property))
  1685. ((listp org-use-property-inheritance)
  1686. (member property org-use-property-inheritance))
  1687. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  1688. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  1689. "The default column format, if no other format has been defined.
  1690. This variable can be set on the per-file basis by inserting a line
  1691. #+COLUMNS: %25ITEM ....."
  1692. :group 'org-properties
  1693. :type 'string)
  1694. (defcustom org-columns-ellipses ".."
  1695. "The ellipses to be used when a field in column view is truncated.
  1696. When this is the empty string, as many characters as possible are shown,
  1697. but then there will be no visual indication that the field has been truncated.
  1698. When this is a string of length N, the last N characters of a truncated
  1699. field are replaced by this string. If the column is narrower than the
  1700. ellipses string, only part of the ellipses string will be shown."
  1701. :group 'org-properties
  1702. :type 'string)
  1703. (defcustom org-columns-modify-value-for-display-function nil
  1704. "Function that modifies values for display in column view.
  1705. For example, it can be used to cut out a certain part from a time stamp.
  1706. The function must take 2 argments:
  1707. column-title The tite of the column (*not* the property name)
  1708. value The value that should be modified.
  1709. The function should return the value that should be displayed,
  1710. or nil if the normal value should be used."
  1711. :group 'org-properties
  1712. :type 'function)
  1713. (defcustom org-effort-property "Effort"
  1714. "The property that is being used to keep track of effort estimates.
  1715. Effort estimates given in this property need to have the format H:MM."
  1716. :group 'org-properties
  1717. :group 'org-progress
  1718. :type '(string :tag "Property"))
  1719. (defconst org-global-properties-fixed
  1720. '(("VISIBILITY_ALL" . "folded children content all"))
  1721. "List of property/value pairs that can be inherited by any entry.
  1722. These are fixed values, for the preset properties.")
  1723. (defcustom org-global-properties nil
  1724. "List of property/value pairs that can be inherited by any entry.
  1725. You can set buffer-local values for this by adding lines like
  1726. #+PROPERTY: NAME VALUE"
  1727. :group 'org-properties
  1728. :type '(repeat
  1729. (cons (string :tag "Property")
  1730. (string :tag "Value"))))
  1731. (defvar org-file-properties nil
  1732. "List of property/value pairs that can be inherited by any entry.
  1733. Valid for the current buffer.
  1734. This variable is populated from #+PROPERTY lines.")
  1735. (make-variable-buffer-local 'org-file-properties)
  1736. (defgroup org-agenda nil
  1737. "Options concerning agenda views in Org-mode."
  1738. :tag "Org Agenda"
  1739. :group 'org)
  1740. (defvar org-category nil
  1741. "Variable used by org files to set a category for agenda display.
  1742. Such files should use a file variable to set it, for example
  1743. # -*- mode: org; org-category: \"ELisp\"
  1744. or contain a special line
  1745. #+CATEGORY: ELisp
  1746. If the file does not specify a category, then file's base name
  1747. is used instead.")
  1748. (make-variable-buffer-local 'org-category)
  1749. (put 'org-category 'safe-local-variable '(lambda (x) (or (symbolp x) (stringp x))))
  1750. (defcustom org-agenda-files nil
  1751. "The files to be used for agenda display.
  1752. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  1753. \\[org-remove-file]. You can also use customize to edit the list.
  1754. If an entry is a directory, all files in that directory that are matched by
  1755. `org-agenda-file-regexp' will be part of the file list.
  1756. If the value of the variable is not a list but a single file name, then
  1757. the list of agenda files is actually stored and maintained in that file, one
  1758. agenda file per line."
  1759. :group 'org-agenda
  1760. :type '(choice
  1761. (repeat :tag "List of files and directories" file)
  1762. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  1763. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  1764. "Regular expression to match files for `org-agenda-files'.
  1765. If any element in the list in that variable contains a directory instead
  1766. of a normal file, all files in that directory that are matched by this
  1767. regular expression will be included."
  1768. :group 'org-agenda
  1769. :type 'regexp)
  1770. (defcustom org-agenda-text-search-extra-files nil
  1771. "List of extra files to be searched by text search commands.
  1772. These files will be search in addition to the agenda files by the
  1773. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  1774. Note that these files will only be searched for text search commands,
  1775. not for the other agenda views like todo lists, tag searches or the weekly
  1776. agenda. This variable is intended to list notes and possibly archive files
  1777. that should also be searched by these two commands.
  1778. In fact, if the first element in the list is the symbol `agenda-archives',
  1779. than all archive files of all agenda files will be added to the search
  1780. scope."
  1781. :group 'org-agenda
  1782. :type '(set :greedy t
  1783. (const :tag "Agenda Archives" agenda-archives)
  1784. (repeat :inline t (file))))
  1785. (if (fboundp 'defvaralias)
  1786. (defvaralias 'org-agenda-multi-occur-extra-files
  1787. 'org-agenda-text-search-extra-files))
  1788. (defcustom org-agenda-skip-unavailable-files nil
  1789. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  1790. A nil value means to remove them, after a query, from the list."
  1791. :group 'org-agenda
  1792. :type 'boolean)
  1793. (defcustom org-calendar-to-agenda-key [?c]
  1794. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  1795. The command `org-calendar-goto-agenda' will be bound to this key. The
  1796. default is the character `c' because then `c' can be used to switch back and
  1797. forth between agenda and calendar."
  1798. :group 'org-agenda
  1799. :type 'sexp)
  1800. (defcustom org-calendar-agenda-action-key [?k]
  1801. "The key to be installed in `calendar-mode-map' for agenda-action.
  1802. The command `org-agenda-action' will be bound to this key. The
  1803. default is the character `k' because we use the same key in the agenda."
  1804. :group 'org-agenda
  1805. :type 'sexp)
  1806. (eval-after-load "calendar"
  1807. '(progn
  1808. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  1809. 'org-calendar-goto-agenda)
  1810. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  1811. 'org-agenda-action)))
  1812. (defgroup org-latex nil
  1813. "Options for embedding LaTeX code into Org-mode."
  1814. :tag "Org LaTeX"
  1815. :group 'org)
  1816. (defcustom org-format-latex-options
  1817. '(:foreground default :background default :scale 1.0
  1818. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  1819. :matchers ("begin" "$" "$$" "\\(" "\\["))
  1820. "Options for creating images from LaTeX fragments.
  1821. This is a property list with the following properties:
  1822. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  1823. `default' means use the foreground of the default face.
  1824. :background the background color, or \"Transparent\".
  1825. `default' means use the background of the default face.
  1826. :scale a scaling factor for the size of the images.
  1827. :html-foreground, :html-background, :html-scale
  1828. the same numbers for HTML export.
  1829. :matchers a list indicating which matchers should be used to
  1830. find LaTeX fragments. Valid members of this list are:
  1831. \"begin\" find environments
  1832. \"$\" find math expressions surrounded by $...$
  1833. \"$$\" find math expressions surrounded by $$....$$
  1834. \"\\(\" find math expressions surrounded by \\(...\\)
  1835. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  1836. :group 'org-latex
  1837. :type 'plist)
  1838. (defcustom org-format-latex-header "\\documentclass{article}
  1839. \\usepackage{fullpage} % do not remove
  1840. \\usepackage{amssymb}
  1841. \\usepackage[usenames]{color}
  1842. \\usepackage{amsmath}
  1843. \\usepackage{latexsym}
  1844. \\usepackage[mathscr]{eucal}
  1845. \\pagestyle{empty} % do not remove"
  1846. "The document header used for processing LaTeX fragments."
  1847. :group 'org-latex
  1848. :type 'string)
  1849. (defgroup org-font-lock nil
  1850. "Font-lock settings for highlighting in Org-mode."
  1851. :tag "Org Font Lock"
  1852. :group 'org)
  1853. (defcustom org-level-color-stars-only nil
  1854. "Non-nil means fontify only the stars in each headline.
  1855. When nil, the entire headline is fontified.
  1856. Changing it requires restart of `font-lock-mode' to become effective
  1857. also in regions already fontified."
  1858. :group 'org-font-lock
  1859. :type 'boolean)
  1860. (defcustom org-hide-leading-stars nil
  1861. "Non-nil means, hide the first N-1 stars in a headline.
  1862. This works by using the face `org-hide' for these stars. This
  1863. face is white for a light background, and black for a dark
  1864. background. You may have to customize the face `org-hide' to
  1865. make this work.
  1866. Changing it requires restart of `font-lock-mode' to become effective
  1867. also in regions already fontified.
  1868. You may also set this on a per-file basis by adding one of the following
  1869. lines to the buffer:
  1870. #+STARTUP: hidestars
  1871. #+STARTUP: showstars"
  1872. :group 'org-font-lock
  1873. :type 'boolean)
  1874. (defcustom org-fontify-done-headline nil
  1875. "Non-nil means, change the face of a headline if it is marked DONE.
  1876. Normally, only the TODO/DONE keyword indicates the state of a headline.
  1877. When this is non-nil, the headline after the keyword is set to the
  1878. `org-headline-done' as an additional indication."
  1879. :group 'org-font-lock
  1880. :type 'boolean)
  1881. (defcustom org-fontify-emphasized-text t
  1882. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  1883. Changing this variable requires a restart of Emacs to take effect."
  1884. :group 'org-font-lock
  1885. :type 'boolean)
  1886. (defcustom org-highlight-latex-fragments-and-specials nil
  1887. "Non-nil means, fontify what is treated specially by the exporters."
  1888. :group 'org-font-lock
  1889. :type 'boolean)
  1890. (defcustom org-hide-emphasis-markers nil
  1891. "Non-nil mean font-lock should hide the emphasis marker characters."
  1892. :group 'org-font-lock
  1893. :type 'boolean)
  1894. (defvar org-emph-re nil
  1895. "Regular expression for matching emphasis.")
  1896. (defvar org-verbatim-re nil
  1897. "Regular expression for matching verbatim text.")
  1898. (defvar org-emphasis-regexp-components) ; defined just below
  1899. (defvar org-emphasis-alist) ; defined just below
  1900. (defun org-set-emph-re (var val)
  1901. "Set variable and compute the emphasis regular expression."
  1902. (set var val)
  1903. (when (and (boundp 'org-emphasis-alist)
  1904. (boundp 'org-emphasis-regexp-components)
  1905. org-emphasis-alist org-emphasis-regexp-components)
  1906. (let* ((e org-emphasis-regexp-components)
  1907. (pre (car e))
  1908. (post (nth 1 e))
  1909. (border (nth 2 e))
  1910. (body (nth 3 e))
  1911. (nl (nth 4 e))
  1912. (stacked (and nil (nth 5 e))) ; stacked is no longer allowed, forced to nil
  1913. (body1 (concat body "*?"))
  1914. (markers (mapconcat 'car org-emphasis-alist ""))
  1915. (vmarkers (mapconcat
  1916. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  1917. org-emphasis-alist "")))
  1918. ;; make sure special characters appear at the right position in the class
  1919. (if (string-match "\\^" markers)
  1920. (setq markers (concat (replace-match "" t t markers) "^")))
  1921. (if (string-match "-" markers)
  1922. (setq markers (concat (replace-match "" t t markers) "-")))
  1923. (if (string-match "\\^" vmarkers)
  1924. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  1925. (if (string-match "-" vmarkers)
  1926. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  1927. (if (> nl 0)
  1928. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  1929. (int-to-string nl) "\\}")))
  1930. ;; Make the regexp
  1931. (setq org-emph-re
  1932. (concat "\\([" pre (if (and nil stacked) markers) "]\\|^\\)"
  1933. "\\("
  1934. "\\([" markers "]\\)"
  1935. "\\("
  1936. "[^" border "]\\|"
  1937. "[^" border (if (and nil stacked) markers) "]"
  1938. body1
  1939. "[^" border (if (and nil stacked) markers) "]"
  1940. "\\)"
  1941. "\\3\\)"
  1942. "\\([" post (if (and nil stacked) markers) "]\\|$\\)"))
  1943. (setq org-verbatim-re
  1944. (concat "\\([" pre "]\\|^\\)"
  1945. "\\("
  1946. "\\([" vmarkers "]\\)"
  1947. "\\("
  1948. "[^" border "]\\|"
  1949. "[^" border "]"
  1950. body1
  1951. "[^" border "]"
  1952. "\\)"
  1953. "\\3\\)"
  1954. "\\([" post "]\\|$\\)")))))
  1955. (defcustom org-emphasis-regexp-components
  1956. '(" \t('\"" "- \t.,:?;'\")" " \t\r\n,\"'" "." 1)
  1957. "Components used to build the regular expression for emphasis.
  1958. This is a list with 6 entries. Terminology: In an emphasis string
  1959. like \" *strong word* \", we call the initial space PREMATCH, the final
  1960. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  1961. and \"trong wor\" is the body. The different components in this variable
  1962. specify what is allowed/forbidden in each part:
  1963. pre Chars allowed as prematch. Beginning of line will be allowed too.
  1964. post Chars allowed as postmatch. End of line will be allowed too.
  1965. border The chars *forbidden* as border characters.
  1966. body-regexp A regexp like \".\" to match a body character. Don't use
  1967. non-shy groups here, and don't allow newline here.
  1968. newline The maximum number of newlines allowed in an emphasis exp.
  1969. Use customize to modify this, or restart Emacs after changing it."
  1970. :group 'org-font-lock
  1971. :set 'org-set-emph-re
  1972. :type '(list
  1973. (sexp :tag "Allowed chars in pre ")
  1974. (sexp :tag "Allowed chars in post ")
  1975. (sexp :tag "Forbidden chars in border ")
  1976. (sexp :tag "Regexp for body ")
  1977. (integer :tag "number of newlines allowed")
  1978. (option (boolean :tag "Please ignore this button"))))
  1979. (defcustom org-emphasis-alist
  1980. `(("*" bold "<b>" "</b>")
  1981. ("/" italic "<i>" "</i>")
  1982. ("_" underline "<u>" "</u>")
  1983. ("=" org-code "<code>" "</code>" verbatim)
  1984. ("~" org-verbatim "<code>" "</code>" verbatim)
  1985. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  1986. "<del>" "</del>")
  1987. )
  1988. "Special syntax for emphasized text.
  1989. Text starting and ending with a special character will be emphasized, for
  1990. example *bold*, _underlined_ and /italic/. This variable sets the marker
  1991. characters, the face to be used by font-lock for highlighting in Org-mode
  1992. Emacs buffers, and the HTML tags to be used for this.
  1993. Use customize to modify this, or restart Emacs after changing it."
  1994. :group 'org-font-lock
  1995. :set 'org-set-emph-re
  1996. :type '(repeat
  1997. (list
  1998. (string :tag "Marker character")
  1999. (choice
  2000. (face :tag "Font-lock-face")
  2001. (plist :tag "Face property list"))
  2002. (string :tag "HTML start tag")
  2003. (string :tag "HTML end tag")
  2004. (option (const verbatim)))))
  2005. ;;; Miscellaneous options
  2006. (defgroup org-completion nil
  2007. "Completion in Org-mode."
  2008. :tag "Org Completion"
  2009. :group 'org)
  2010. (defcustom org-completion-fallback-command 'hippie-expand
  2011. "The expansion command called by \\[org-complete] in normal context.
  2012. Normal means, no org-mode-specific context."
  2013. :group 'org-completion
  2014. :type 'function)
  2015. ;;; Functions and variables from ther packages
  2016. ;; Declared here to avoid compiler warnings
  2017. ;; XEmacs only
  2018. (defvar outline-mode-menu-heading)
  2019. (defvar outline-mode-menu-show)
  2020. (defvar outline-mode-menu-hide)
  2021. (defvar zmacs-regions) ; XEmacs regions
  2022. ;; Emacs only
  2023. (defvar mark-active)
  2024. ;; Various packages
  2025. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  2026. (declare-function calendar-forward-day "cal-move" (arg))
  2027. (declare-function calendar-goto-date "cal-move" (date))
  2028. (declare-function calendar-goto-today "cal-move" ())
  2029. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  2030. (defvar calc-embedded-close-formula)
  2031. (defvar calc-embedded-open-formula)
  2032. (declare-function cdlatex-tab "ext:cdlatex" ())
  2033. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  2034. (defvar font-lock-unfontify-region-function)
  2035. (declare-function iswitchb-mode "iswitchb" (&optional arg))
  2036. (declare-function iswitchb-read-buffer (prompt &optional default require-match start matches-set))
  2037. (defvar iswitchb-temp-buflist)
  2038. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  2039. (declare-function org-agenda-skip "org-agenda" ())
  2040. (declare-function org-format-agenda-item "org-agenda"
  2041. (extra txt &optional category tags dotime noprefix remove-re))
  2042. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  2043. (declare-function org-agenda-change-all-lines "org-agenda"
  2044. (newhead hdmarker &optional fixface))
  2045. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  2046. (declare-function org-agenda-maybe-redo "org-agenda" ())
  2047. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  2048. (beg end))
  2049. (declare-function parse-time-string "parse-time" (string))
  2050. (declare-function remember "remember" (&optional initial))
  2051. (declare-function remember-buffer-desc "remember" ())
  2052. (declare-function remember-finalize "remember" ())
  2053. (defvar remember-save-after-remembering)
  2054. (defvar remember-data-file)
  2055. (defvar remember-register)
  2056. (defvar remember-buffer)
  2057. (defvar remember-handler-functions)
  2058. (defvar remember-annotation-functions)
  2059. (defvar texmathp-why)
  2060. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  2061. (declare-function table--at-cell-p "table" (position &optional object at-column))
  2062. (defvar w3m-current-url)
  2063. (defvar w3m-current-title)
  2064. (defvar org-latex-regexps)
  2065. ;;; Autoload and prepare some org modules
  2066. ;; Some table stuff that needs to be defined here, because it is used
  2067. ;; by the functions setting up org-mode or checking for table context.
  2068. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  2069. "Detects an org-type or table-type table.")
  2070. (defconst org-table-line-regexp "^[ \t]*|"
  2071. "Detects an org-type table line.")
  2072. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  2073. "Detects an org-type table line.")
  2074. (defconst org-table-hline-regexp "^[ \t]*|-"
  2075. "Detects an org-type table hline.")
  2076. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  2077. "Detects a table-type table hline.")
  2078. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  2079. "Searching from within a table (any type) this finds the first line
  2080. outside the table.")
  2081. ;; Autoload the functions in org-table.el that are needed by functions here.
  2082. (eval-and-compile
  2083. (org-autoload "org-table"
  2084. '(org-table-align org-table-begin org-table-blank-field
  2085. org-table-convert org-table-convert-region org-table-copy-down
  2086. org-table-copy-region org-table-create
  2087. org-table-create-or-convert-from-region
  2088. org-table-create-with-table.el org-table-current-dline
  2089. org-table-cut-region org-table-delete-column org-table-edit-field
  2090. org-table-edit-formulas org-table-end org-table-eval-formula
  2091. org-table-export org-table-field-info
  2092. org-table-get-stored-formulas org-table-goto-column
  2093. org-table-hline-and-move org-table-import org-table-insert-column
  2094. org-table-insert-hline org-table-insert-row org-table-iterate
  2095. org-table-justify-field-maybe org-table-kill-row
  2096. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  2097. org-table-move-column org-table-move-column-left
  2098. org-table-move-column-right org-table-move-row
  2099. org-table-move-row-down org-table-move-row-up
  2100. org-table-next-field org-table-next-row org-table-paste-rectangle
  2101. org-table-previous-field org-table-recalculate
  2102. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  2103. org-table-toggle-coordinate-overlays
  2104. org-table-toggle-formula-debugger org-table-wrap-region
  2105. orgtbl-mode turn-on-orgtbl org-table-to-lisp)))
  2106. (defun org-at-table-p (&optional table-type)
  2107. "Return t if the cursor is inside an org-type table.
  2108. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  2109. (if org-enable-table-editor
  2110. (save-excursion
  2111. (beginning-of-line 1)
  2112. (looking-at (if table-type org-table-any-line-regexp
  2113. org-table-line-regexp)))
  2114. nil))
  2115. (defsubst org-table-p () (org-at-table-p))
  2116. (defun org-at-table.el-p ()
  2117. "Return t if and only if we are at a table.el table."
  2118. (and (org-at-table-p 'any)
  2119. (save-excursion
  2120. (goto-char (org-table-begin 'any))
  2121. (looking-at org-table1-hline-regexp))))
  2122. (defun org-table-recognize-table.el ()
  2123. "If there is a table.el table nearby, recognize it and move into it."
  2124. (if org-table-tab-recognizes-table.el
  2125. (if (org-at-table.el-p)
  2126. (progn
  2127. (beginning-of-line 1)
  2128. (if (looking-at org-table-dataline-regexp)
  2129. nil
  2130. (if (looking-at org-table1-hline-regexp)
  2131. (progn
  2132. (beginning-of-line 2)
  2133. (if (looking-at org-table-any-border-regexp)
  2134. (beginning-of-line -1)))))
  2135. (if (re-search-forward "|" (org-table-end t) t)
  2136. (progn
  2137. (require 'table)
  2138. (if (table--at-cell-p (point))
  2139. t
  2140. (message "recognizing table.el table...")
  2141. (table-recognize-table)
  2142. (message "recognizing table.el table...done")))
  2143. (error "This should not happen..."))
  2144. t)
  2145. nil)
  2146. nil))
  2147. (defun org-at-table-hline-p ()
  2148. "Return t if the cursor is inside a hline in a table."
  2149. (if org-enable-table-editor
  2150. (save-excursion
  2151. (beginning-of-line 1)
  2152. (looking-at org-table-hline-regexp))
  2153. nil))
  2154. (defvar org-table-clean-did-remove-column nil)
  2155. (defun org-table-map-tables (function)
  2156. "Apply FUNCTION to the start of all tables in the buffer."
  2157. (save-excursion
  2158. (save-restriction
  2159. (widen)
  2160. (goto-char (point-min))
  2161. (while (re-search-forward org-table-any-line-regexp nil t)
  2162. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  2163. (beginning-of-line 1)
  2164. (if (looking-at org-table-line-regexp)
  2165. (save-excursion (funcall function)))
  2166. (re-search-forward org-table-any-border-regexp nil 1))))
  2167. (message "Mapping tables: done"))
  2168. ;; Declare and autoload functions from org-exp.el
  2169. (declare-function org-default-export-plist "org-exp")
  2170. (declare-function org-infile-export-plist "org-exp")
  2171. (declare-function org-get-current-options "org-exp")
  2172. (eval-and-compile
  2173. (org-autoload "org-exp"
  2174. '(org-export org-export-as-ascii org-export-visible
  2175. org-insert-export-options-template org-export-as-html-and-open
  2176. org-export-as-html-batch org-export-as-html-to-buffer
  2177. org-replace-region-by-html org-export-region-as-html
  2178. org-export-as-html org-export-icalendar-this-file
  2179. org-export-icalendar-all-agenda-files
  2180. org-table-clean-before-export
  2181. org-export-icalendar-combine-agenda-files org-export-as-xoxo)))
  2182. ;; Declare and autoload functions from org-agenda.el
  2183. (eval-and-compile
  2184. (org-autoload "org-agenda"
  2185. '(org-agenda org-agenda-list org-search-view
  2186. org-todo-list org-tags-view org-agenda-list-stuck-projects
  2187. org-diary org-agenda-to-appt)))
  2188. ;; Autoload org-remember
  2189. (eval-and-compile
  2190. (org-autoload "org-remember"
  2191. '(org-remember-insinuate org-remember-annotation
  2192. org-remember-apply-template org-remember org-remember-handler)))
  2193. ;; Autoload org-clock.el
  2194. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  2195. (beg end))
  2196. (declare-function org-update-mode-line "org-clock" ())
  2197. (defvar org-clock-start-time)
  2198. (defvar org-clock-marker (make-marker)
  2199. "Marker recording the last clock-in.")
  2200. (eval-and-compile
  2201. (org-autoload
  2202. "org-clock"
  2203. '(org-clock-in org-clock-out org-clock-cancel
  2204. org-clock-goto org-clock-sum org-clock-display
  2205. org-remove-clock-overlays org-clock-report
  2206. org-clocktable-shift org-dblock-write:clocktable
  2207. org-get-clocktable)))
  2208. (defun org-clock-update-time-maybe ()
  2209. "If this is a CLOCK line, update it and return t.
  2210. Otherwise, return nil."
  2211. (interactive)
  2212. (save-excursion
  2213. (beginning-of-line 1)
  2214. (skip-chars-forward " \t")
  2215. (when (looking-at org-clock-string)
  2216. (let ((re (concat "[ \t]*" org-clock-string
  2217. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  2218. "\\([ \t]*=>.*\\)?\\)?"))
  2219. ts te h m s neg)
  2220. (cond
  2221. ((not (looking-at re))
  2222. nil)
  2223. ((not (match-end 2))
  2224. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2225. (> org-clock-marker (point))
  2226. (<= org-clock-marker (point-at-eol)))
  2227. ;; The clock is running here
  2228. (setq org-clock-start-time
  2229. (apply 'encode-time
  2230. (org-parse-time-string (match-string 1))))
  2231. (org-update-mode-line)))
  2232. (t
  2233. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  2234. (end-of-line 1)
  2235. (setq ts (match-string 1)
  2236. te (match-string 3))
  2237. (setq s (- (time-to-seconds
  2238. (apply 'encode-time (org-parse-time-string te)))
  2239. (time-to-seconds
  2240. (apply 'encode-time (org-parse-time-string ts))))
  2241. neg (< s 0)
  2242. s (abs s)
  2243. h (floor (/ s 3600))
  2244. s (- s (* 3600 h))
  2245. m (floor (/ s 60))
  2246. s (- s (* 60 s)))
  2247. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  2248. t))))))
  2249. (defun org-check-running-clock ()
  2250. "Check if the current buffer contains the running clock.
  2251. If yes, offer to stop it and to save the buffer with the changes."
  2252. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2253. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  2254. (buffer-name))))
  2255. (org-clock-out)
  2256. (when (y-or-n-p "Save changed buffer?")
  2257. (save-buffer))))
  2258. (defun org-clocktable-try-shift (dir n)
  2259. "Check if this line starts a clock table, if yes, shift the time block."
  2260. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  2261. (org-clocktable-shift dir n)))
  2262. ;; Autoload archiving code
  2263. ;; The stuff that is needed for cycling and tags has to be defined here.
  2264. (defgroup org-archive nil
  2265. "Options concerning archiving in Org-mode."
  2266. :tag "Org Archive"
  2267. :group 'org-structure)
  2268. (defcustom org-archive-location "%s_archive::"
  2269. "The location where subtrees should be archived.
  2270. Otherwise, the value of this variable is a string, consisting of two
  2271. parts, separated by a double-colon.
  2272. The first part is a file name - when omitted, archiving happens in the same
  2273. file. %s will be replaced by the current file name (without directory part).
  2274. Archiving to a different file is useful to keep archived entries from
  2275. contributing to the Org-mode Agenda.
  2276. The part after the double colon is a headline. The archived entries will be
  2277. filed under that headline. When omitted, the subtrees are simply filed away
  2278. at the end of the file, as top-level entries.
  2279. Here are a few examples:
  2280. \"%s_archive::\"
  2281. If the current file is Projects.org, archive in file
  2282. Projects.org_archive, as top-level trees. This is the default.
  2283. \"::* Archived Tasks\"
  2284. Archive in the current file, under the top-level headline
  2285. \"* Archived Tasks\".
  2286. \"~/org/archive.org::\"
  2287. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  2288. \"basement::** Finished Tasks\"
  2289. Archive in file ./basement (relative path), as level 3 trees
  2290. below the level 2 heading \"** Finished Tasks\".
  2291. You may set this option on a per-file basis by adding to the buffer a
  2292. line like
  2293. #+ARCHIVE: basement::** Finished Tasks
  2294. You may also define it locally for a subtree by setting an ARCHIVE property
  2295. in the entry. If such a property is found in an entry, or anywhere up
  2296. the hierarchy, it will be used."
  2297. :group 'org-archive
  2298. :type 'string)
  2299. (defcustom org-archive-tag "ARCHIVE"
  2300. "The tag that marks a subtree as archived.
  2301. An archived subtree does not open during visibility cycling, and does
  2302. not contribute to the agenda listings.
  2303. After changing this, font-lock must be restarted in the relevant buffers to
  2304. get the proper fontification."
  2305. :group 'org-archive
  2306. :group 'org-keywords
  2307. :type 'string)
  2308. (defcustom org-agenda-skip-archived-trees t
  2309. "Non-nil means, the agenda will skip any items located in archived trees.
  2310. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  2311. variable is no longer recommended, you should leave it at the value t.
  2312. Instead, use the key `v' to cycle the archives-mode in the agenda."
  2313. :group 'org-archive
  2314. :group 'org-agenda-skip
  2315. :type 'boolean)
  2316. (defcustom org-cycle-open-archived-trees nil
  2317. "Non-nil means, `org-cycle' will open archived trees.
  2318. An archived tree is a tree marked with the tag ARCHIVE.
  2319. When nil, archived trees will stay folded. You can still open them with
  2320. normal outline commands like `show-all', but not with the cycling commands."
  2321. :group 'org-archive
  2322. :group 'org-cycle
  2323. :type 'boolean)
  2324. (defcustom org-sparse-tree-open-archived-trees nil
  2325. "Non-nil means sparse tree construction shows matches in archived trees.
  2326. When nil, matches in these trees are highlighted, but the trees are kept in
  2327. collapsed state."
  2328. :group 'org-archive
  2329. :group 'org-sparse-trees
  2330. :type 'boolean)
  2331. (defun org-cycle-hide-archived-subtrees (state)
  2332. "Re-hide all archived subtrees after a visibility state change."
  2333. (when (and (not org-cycle-open-archived-trees)
  2334. (not (memq state '(overview folded))))
  2335. (save-excursion
  2336. (let* ((globalp (memq state '(contents all)))
  2337. (beg (if globalp (point-min) (point)))
  2338. (end (if globalp (point-max) (org-end-of-subtree t))))
  2339. (org-hide-archived-subtrees beg end)
  2340. (goto-char beg)
  2341. (if (looking-at (concat ".*:" org-archive-tag ":"))
  2342. (message "%s" (substitute-command-keys
  2343. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  2344. (defun org-force-cycle-archived ()
  2345. "Cycle subtree even if it is archived."
  2346. (interactive)
  2347. (setq this-command 'org-cycle)
  2348. (let ((org-cycle-open-archived-trees t))
  2349. (call-interactively 'org-cycle)))
  2350. (defun org-hide-archived-subtrees (beg end)
  2351. "Re-hide all archived subtrees after a visibility state change."
  2352. (save-excursion
  2353. (let* ((re (concat ":" org-archive-tag ":")))
  2354. (goto-char beg)
  2355. (while (re-search-forward re end t)
  2356. (and (org-on-heading-p) (hide-subtree))
  2357. (org-end-of-subtree t)))))
  2358. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  2359. (eval-and-compile
  2360. (org-autoload "org-archive"
  2361. '(org-add-archive-files org-archive-subtree
  2362. org-archive-to-archive-sibling org-toggle-archive-tag)))
  2363. ;; Autoload Column View Code
  2364. (declare-function org-columns-number-to-string "org-colview")
  2365. (declare-function org-columns-get-format-and-top-level "org-colview")
  2366. (declare-function org-columns-compute "org-colview")
  2367. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  2368. '(org-columns-number-to-string org-columns-get-format-and-top-level
  2369. org-columns-compute org-agenda-columns org-columns-remove-overlays
  2370. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  2371. ;; Autoload ID code
  2372. (org-autoload "org-id"
  2373. '(org-id-get-create org-id-new org-id-copy org-id-get
  2374. org-id-get-with-outline-path-completion
  2375. org-id-get-with-outline-drilling
  2376. org-id-goto org-id-find))
  2377. ;;; Variables for pre-computed regular expressions, all buffer local
  2378. (defvar org-drawer-regexp nil
  2379. "Matches first line of a hidden block.")
  2380. (make-variable-buffer-local 'org-drawer-regexp)
  2381. (defvar org-todo-regexp nil
  2382. "Matches any of the TODO state keywords.")
  2383. (make-variable-buffer-local 'org-todo-regexp)
  2384. (defvar org-not-done-regexp nil
  2385. "Matches any of the TODO state keywords except the last one.")
  2386. (make-variable-buffer-local 'org-not-done-regexp)
  2387. (defvar org-todo-line-regexp nil
  2388. "Matches a headline and puts TODO state into group 2 if present.")
  2389. (make-variable-buffer-local 'org-todo-line-regexp)
  2390. (defvar org-complex-heading-regexp nil
  2391. "Matches a headline and puts everything into groups:
  2392. group 1: the stars
  2393. group 2: The todo keyword, maybe
  2394. group 3: Priority cookie
  2395. group 4: True headline
  2396. group 5: Tags")
  2397. (make-variable-buffer-local 'org-complex-heading-regexp)
  2398. (defvar org-todo-line-tags-regexp nil
  2399. "Matches a headline and puts TODO state into group 2 if present.
  2400. Also put tags into group 4 if tags are present.")
  2401. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  2402. (defvar org-nl-done-regexp nil
  2403. "Matches newline followed by a headline with the DONE keyword.")
  2404. (make-variable-buffer-local 'org-nl-done-regexp)
  2405. (defvar org-looking-at-done-regexp nil
  2406. "Matches the DONE keyword a point.")
  2407. (make-variable-buffer-local 'org-looking-at-done-regexp)
  2408. (defvar org-ds-keyword-length 12
  2409. "Maximum length of the Deadline and SCHEDULED keywords.")
  2410. (make-variable-buffer-local 'org-ds-keyword-length)
  2411. (defvar org-deadline-regexp nil
  2412. "Matches the DEADLINE keyword.")
  2413. (make-variable-buffer-local 'org-deadline-regexp)
  2414. (defvar org-deadline-time-regexp nil
  2415. "Matches the DEADLINE keyword together with a time stamp.")
  2416. (make-variable-buffer-local 'org-deadline-time-regexp)
  2417. (defvar org-deadline-line-regexp nil
  2418. "Matches the DEADLINE keyword and the rest of the line.")
  2419. (make-variable-buffer-local 'org-deadline-line-regexp)
  2420. (defvar org-scheduled-regexp nil
  2421. "Matches the SCHEDULED keyword.")
  2422. (make-variable-buffer-local 'org-scheduled-regexp)
  2423. (defvar org-scheduled-time-regexp nil
  2424. "Matches the SCHEDULED keyword together with a time stamp.")
  2425. (make-variable-buffer-local 'org-scheduled-time-regexp)
  2426. (defvar org-closed-time-regexp nil
  2427. "Matches the CLOSED keyword together with a time stamp.")
  2428. (make-variable-buffer-local 'org-closed-time-regexp)
  2429. (defvar org-keyword-time-regexp nil
  2430. "Matches any of the 4 keywords, together with the time stamp.")
  2431. (make-variable-buffer-local 'org-keyword-time-regexp)
  2432. (defvar org-keyword-time-not-clock-regexp nil
  2433. "Matches any of the 3 keywords, together with the time stamp.")
  2434. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  2435. (defvar org-maybe-keyword-time-regexp nil
  2436. "Matches a timestamp, possibly preceeded by a keyword.")
  2437. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  2438. (defvar org-planning-or-clock-line-re nil
  2439. "Matches a line with planning or clock info.")
  2440. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  2441. (defconst org-plain-time-of-day-regexp
  2442. (concat
  2443. "\\(\\<[012]?[0-9]"
  2444. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2445. "\\(--?"
  2446. "\\(\\<[012]?[0-9]"
  2447. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2448. "\\)?")
  2449. "Regular expression to match a plain time or time range.
  2450. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2451. groups carry important information:
  2452. 0 the full match
  2453. 1 the first time, range or not
  2454. 8 the second time, if it is a range.")
  2455. (defconst org-plain-time-extension-regexp
  2456. (concat
  2457. "\\(\\<[012]?[0-9]"
  2458. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2459. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  2460. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  2461. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2462. groups carry important information:
  2463. 0 the full match
  2464. 7 hours of duration
  2465. 9 minutes of duration")
  2466. (defconst org-stamp-time-of-day-regexp
  2467. (concat
  2468. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  2469. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  2470. "\\(--?"
  2471. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  2472. "Regular expression to match a timestamp time or time range.
  2473. After a match, the following groups carry important information:
  2474. 0 the full match
  2475. 1 date plus weekday, for backreferencing to make sure both times on same day
  2476. 2 the first time, range or not
  2477. 4 the second time, if it is a range.")
  2478. (defconst org-startup-options
  2479. '(("fold" org-startup-folded t)
  2480. ("overview" org-startup-folded t)
  2481. ("nofold" org-startup-folded nil)
  2482. ("showall" org-startup-folded nil)
  2483. ("content" org-startup-folded content)
  2484. ("hidestars" org-hide-leading-stars t)
  2485. ("showstars" org-hide-leading-stars nil)
  2486. ("odd" org-odd-levels-only t)
  2487. ("oddeven" org-odd-levels-only nil)
  2488. ("align" org-startup-align-all-tables t)
  2489. ("noalign" org-startup-align-all-tables nil)
  2490. ("customtime" org-display-custom-times t)
  2491. ("logdone" org-log-done time)
  2492. ("lognotedone" org-log-done note)
  2493. ("nologdone" org-log-done nil)
  2494. ("lognoteclock-out" org-log-note-clock-out t)
  2495. ("nolognoteclock-out" org-log-note-clock-out nil)
  2496. ("logrepeat" org-log-repeat state)
  2497. ("lognoterepeat" org-log-repeat note)
  2498. ("nologrepeat" org-log-repeat nil)
  2499. ("constcgs" constants-unit-system cgs)
  2500. ("constSI" constants-unit-system SI))
  2501. "Variable associated with STARTUP options for org-mode.
  2502. Each element is a list of three items: The startup options as written
  2503. in the #+STARTUP line, the corresponding variable, and the value to
  2504. set this variable to if the option is found. An optional forth element PUSH
  2505. means to push this value onto the list in the variable.")
  2506. (defun org-set-regexps-and-options ()
  2507. "Precompute regular expressions for current buffer."
  2508. (when (org-mode-p)
  2509. (org-set-local 'org-todo-kwd-alist nil)
  2510. (org-set-local 'org-todo-key-alist nil)
  2511. (org-set-local 'org-todo-key-trigger nil)
  2512. (org-set-local 'org-todo-keywords-1 nil)
  2513. (org-set-local 'org-done-keywords nil)
  2514. (org-set-local 'org-todo-heads nil)
  2515. (org-set-local 'org-todo-sets nil)
  2516. (org-set-local 'org-todo-log-states nil)
  2517. (org-set-local 'org-file-properties nil)
  2518. (org-set-local 'org-file-tags nil)
  2519. (let ((re (org-make-options-regexp
  2520. '("CATEGORY" "SEQ_TODO" "TYP_TODO" "TODO" "COLUMNS"
  2521. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  2522. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE")))
  2523. (splitre "[ \t]+")
  2524. kwds kws0 kwsa key log value cat arch tags const links hw dws
  2525. tail sep kws1 prio props ftags drawers
  2526. ext-setup-or-nil setup-contents (start 0))
  2527. (save-excursion
  2528. (save-restriction
  2529. (widen)
  2530. (goto-char (point-min))
  2531. (while (or (and ext-setup-or-nil
  2532. (string-match re ext-setup-or-nil start)
  2533. (setq start (match-end 0)))
  2534. (and (setq ext-setup-or-nil nil start 0)
  2535. (re-search-forward re nil t)))
  2536. (setq key (upcase (match-string 1 ext-setup-or-nil))
  2537. value (org-match-string-no-properties 2 ext-setup-or-nil))
  2538. (cond
  2539. ((equal key "CATEGORY")
  2540. (if (string-match "[ \t]+$" value)
  2541. (setq value (replace-match "" t t value)))
  2542. (setq cat value))
  2543. ((member key '("SEQ_TODO" "TODO"))
  2544. (push (cons 'sequence (org-split-string value splitre)) kwds))
  2545. ((equal key "TYP_TODO")
  2546. (push (cons 'type (org-split-string value splitre)) kwds))
  2547. ((equal key "TAGS")
  2548. (setq tags (append tags (org-split-string value splitre))))
  2549. ((equal key "COLUMNS")
  2550. (org-set-local 'org-columns-default-format value))
  2551. ((equal key "LINK")
  2552. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  2553. (push (cons (match-string 1 value)
  2554. (org-trim (match-string 2 value)))
  2555. links)))
  2556. ((equal key "PRIORITIES")
  2557. (setq prio (org-split-string value " +")))
  2558. ((equal key "PROPERTY")
  2559. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  2560. (push (cons (match-string 1 value) (match-string 2 value))
  2561. props)))
  2562. ((equal key "FILETAGS")
  2563. (when (string-match "\\S-" value)
  2564. (setq ftags
  2565. (append
  2566. ftags
  2567. (apply 'append
  2568. (mapcar (lambda (x) (org-split-string x ":"))
  2569. (org-split-string value)))))))
  2570. ((equal key "DRAWERS")
  2571. (setq drawers (org-split-string value splitre)))
  2572. ((equal key "CONSTANTS")
  2573. (setq const (append const (org-split-string value splitre))))
  2574. ((equal key "STARTUP")
  2575. (let ((opts (org-split-string value splitre))
  2576. l var val)
  2577. (while (setq l (pop opts))
  2578. (when (setq l (assoc l org-startup-options))
  2579. (setq var (nth 1 l) val (nth 2 l))
  2580. (if (not (nth 3 l))
  2581. (set (make-local-variable var) val)
  2582. (if (not (listp (symbol-value var)))
  2583. (set (make-local-variable var) nil))
  2584. (set (make-local-variable var) (symbol-value var))
  2585. (add-to-list var val))))))
  2586. ((equal key "ARCHIVE")
  2587. (string-match " *$" value)
  2588. (setq arch (replace-match "" t t value))
  2589. (remove-text-properties 0 (length arch)
  2590. '(face t fontified t) arch))
  2591. ((equal key "SETUPFILE")
  2592. (setq setup-contents (org-file-contents
  2593. (expand-file-name
  2594. (org-remove-double-quotes value))
  2595. 'noerror))
  2596. (if (not ext-setup-or-nil)
  2597. (setq ext-setup-or-nil setup-contents start 0)
  2598. (setq ext-setup-or-nil
  2599. (concat (substring ext-setup-or-nil 0 start)
  2600. "\n" setup-contents "\n"
  2601. (substring ext-setup-or-nil start)))))
  2602. ))))
  2603. (when cat
  2604. (org-set-local 'org-category (intern cat))
  2605. (push (cons "CATEGORY" cat) props))
  2606. (when prio
  2607. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  2608. (setq prio (mapcar 'string-to-char prio))
  2609. (org-set-local 'org-highest-priority (nth 0 prio))
  2610. (org-set-local 'org-lowest-priority (nth 1 prio))
  2611. (org-set-local 'org-default-priority (nth 2 prio)))
  2612. (and props (org-set-local 'org-file-properties (nreverse props)))
  2613. (and ftags (org-set-local 'org-file-tags ftags))
  2614. (and drawers (org-set-local 'org-drawers drawers))
  2615. (and arch (org-set-local 'org-archive-location arch))
  2616. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  2617. ;; Process the TODO keywords
  2618. (unless kwds
  2619. ;; Use the global values as if they had been given locally.
  2620. (setq kwds (default-value 'org-todo-keywords))
  2621. (if (stringp (car kwds))
  2622. (setq kwds (list (cons org-todo-interpretation
  2623. (default-value 'org-todo-keywords)))))
  2624. (setq kwds (reverse kwds)))
  2625. (setq kwds (nreverse kwds))
  2626. (let (inter kws kw)
  2627. (while (setq kws (pop kwds))
  2628. (setq inter (pop kws) sep (member "|" kws)
  2629. kws0 (delete "|" (copy-sequence kws))
  2630. kwsa nil
  2631. kws1 (mapcar
  2632. (lambda (x)
  2633. ;; 1 2
  2634. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  2635. (progn
  2636. (setq kw (match-string 1 x)
  2637. key (and (match-end 2) (match-string 2 x))
  2638. log (org-extract-log-state-settings x))
  2639. (push (cons kw (and key (string-to-char key))) kwsa)
  2640. (and log (push log org-todo-log-states))
  2641. kw)
  2642. (error "Invalid TODO keyword %s" x)))
  2643. kws0)
  2644. kwsa (if kwsa (append '((:startgroup))
  2645. (nreverse kwsa)
  2646. '((:endgroup))))
  2647. hw (car kws1)
  2648. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  2649. tail (list inter hw (car dws) (org-last dws)))
  2650. (add-to-list 'org-todo-heads hw 'append)
  2651. (push kws1 org-todo-sets)
  2652. (setq org-done-keywords (append org-done-keywords dws nil))
  2653. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  2654. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  2655. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  2656. (setq org-todo-sets (nreverse org-todo-sets)
  2657. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  2658. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  2659. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  2660. ;; Process the constants
  2661. (when const
  2662. (let (e cst)
  2663. (while (setq e (pop const))
  2664. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  2665. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  2666. (setq org-table-formula-constants-local cst)))
  2667. ;; Process the tags.
  2668. (when tags
  2669. (let (e tgs)
  2670. (while (setq e (pop tags))
  2671. (cond
  2672. ((equal e "{") (push '(:startgroup) tgs))
  2673. ((equal e "}") (push '(:endgroup) tgs))
  2674. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  2675. (push (cons (match-string 1 e)
  2676. (string-to-char (match-string 2 e)))
  2677. tgs))
  2678. (t (push (list e) tgs))))
  2679. (org-set-local 'org-tag-alist nil)
  2680. (while (setq e (pop tgs))
  2681. (or (and (stringp (car e))
  2682. (assoc (car e) org-tag-alist))
  2683. (push e org-tag-alist)))))
  2684. ;; Compute the regular expressions and other local variables
  2685. (if (not org-done-keywords)
  2686. (setq org-done-keywords (list (org-last org-todo-keywords-1))))
  2687. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  2688. (length org-scheduled-string)
  2689. (length org-clock-string)
  2690. (length org-closed-string)))
  2691. org-drawer-regexp
  2692. (concat "^[ \t]*:\\("
  2693. (mapconcat 'regexp-quote org-drawers "\\|")
  2694. "\\):[ \t]*$")
  2695. org-not-done-keywords
  2696. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  2697. org-todo-regexp
  2698. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  2699. "\\|") "\\)\\>")
  2700. org-not-done-regexp
  2701. (concat "\\<\\("
  2702. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  2703. "\\)\\>")
  2704. org-todo-line-regexp
  2705. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  2706. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2707. "\\)\\>\\)?[ \t]*\\(.*\\)")
  2708. org-complex-heading-regexp
  2709. (concat "^\\(\\*+\\)\\(?:[ \t]+\\("
  2710. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2711. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  2712. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  2713. org-nl-done-regexp
  2714. (concat "\n\\*+[ \t]+"
  2715. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  2716. "\\)" "\\>")
  2717. org-todo-line-tags-regexp
  2718. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  2719. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2720. (org-re
  2721. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  2722. org-looking-at-done-regexp
  2723. (concat "^" "\\(?:"
  2724. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  2725. "\\>")
  2726. org-deadline-regexp (concat "\\<" org-deadline-string)
  2727. org-deadline-time-regexp
  2728. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  2729. org-deadline-line-regexp
  2730. (concat "\\<\\(" org-deadline-string "\\).*")
  2731. org-scheduled-regexp
  2732. (concat "\\<" org-scheduled-string)
  2733. org-scheduled-time-regexp
  2734. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  2735. org-closed-time-regexp
  2736. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  2737. org-keyword-time-regexp
  2738. (concat "\\<\\(" org-scheduled-string
  2739. "\\|" org-deadline-string
  2740. "\\|" org-closed-string
  2741. "\\|" org-clock-string "\\)"
  2742. " *[[<]\\([^]>]+\\)[]>]")
  2743. org-keyword-time-not-clock-regexp
  2744. (concat "\\<\\(" org-scheduled-string
  2745. "\\|" org-deadline-string
  2746. "\\|" org-closed-string
  2747. "\\)"
  2748. " *[[<]\\([^]>]+\\)[]>]")
  2749. org-maybe-keyword-time-regexp
  2750. (concat "\\(\\<\\(" org-scheduled-string
  2751. "\\|" org-deadline-string
  2752. "\\|" org-closed-string
  2753. "\\|" org-clock-string "\\)\\)?"
  2754. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  2755. org-planning-or-clock-line-re
  2756. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  2757. "\\|" org-deadline-string
  2758. "\\|" org-closed-string "\\|" org-clock-string
  2759. "\\)\\>\\)")
  2760. )
  2761. (org-compute-latex-and-specials-regexp)
  2762. (org-set-font-lock-defaults))))
  2763. (defun org-file-contents (file &optional noerror)
  2764. "Return the contents of FILE, as a string."
  2765. (if (or (not file)
  2766. (not (file-readable-p file)))
  2767. (if noerror
  2768. (progn
  2769. (message "Cannot read file %s" file)
  2770. (ding) (sit-for 2)
  2771. "")
  2772. (error "Cannot read file %s" file))
  2773. (with-temp-buffer
  2774. (insert-file-contents file)
  2775. (buffer-string))))
  2776. (defun org-extract-log-state-settings (x)
  2777. "Extract the log state setting from a TODO keyword string.
  2778. This will extract info from a string like \"WAIT(w@/!)\"."
  2779. (let (kw key log1 log2)
  2780. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  2781. (setq kw (match-string 1 x)
  2782. key (and (match-end 2) (match-string 2 x))
  2783. log1 (and (match-end 3) (match-string 3 x))
  2784. log2 (and (match-end 4) (match-string 4 x)))
  2785. (and (or log1 log2)
  2786. (list kw
  2787. (and log1 (if (equal log1 "!") 'time 'note))
  2788. (and log2 (if (equal log2 "!") 'time 'note)))))))
  2789. (defun org-remove-keyword-keys (list)
  2790. "Remove a pair of parenthesis at the end of each string in LIST."
  2791. (mapcar (lambda (x)
  2792. (if (string-match "(.*)$" x)
  2793. (substring x 0 (match-beginning 0))
  2794. x))
  2795. list))
  2796. ;; FIXME: this could be done much better, using second characters etc.
  2797. (defun org-assign-fast-keys (alist)
  2798. "Assign fast keys to a keyword-key alist.
  2799. Respect keys that are already there."
  2800. (let (new e k c c1 c2 (char ?a))
  2801. (while (setq e (pop alist))
  2802. (cond
  2803. ((equal e '(:startgroup)) (push e new))
  2804. ((equal e '(:endgroup)) (push e new))
  2805. (t
  2806. (setq k (car e) c2 nil)
  2807. (if (cdr e)
  2808. (setq c (cdr e))
  2809. ;; automatically assign a character.
  2810. (setq c1 (string-to-char
  2811. (downcase (substring
  2812. k (if (= (string-to-char k) ?@) 1 0)))))
  2813. (if (or (rassoc c1 new) (rassoc c1 alist))
  2814. (while (or (rassoc char new) (rassoc char alist))
  2815. (setq char (1+ char)))
  2816. (setq c2 c1))
  2817. (setq c (or c2 char)))
  2818. (push (cons k c) new))))
  2819. (nreverse new)))
  2820. ;;; Some variables used in various places
  2821. (defvar org-window-configuration nil
  2822. "Used in various places to store a window configuration.")
  2823. (defvar org-finish-function nil
  2824. "Function to be called when `C-c C-c' is used.
  2825. This is for getting out of special buffers like remember.")
  2826. ;; FIXME: Occasionally check by commenting these, to make sure
  2827. ;; no other functions uses these, forgetting to let-bind them.
  2828. (defvar entry)
  2829. (defvar state)
  2830. (defvar last-state)
  2831. (defvar date)
  2832. (defvar description)
  2833. ;; Defined somewhere in this file, but used before definition.
  2834. (defvar org-html-entities)
  2835. (defvar org-struct-menu)
  2836. (defvar org-org-menu)
  2837. (defvar org-tbl-menu)
  2838. (defvar org-agenda-keymap)
  2839. ;;;; Define the Org-mode
  2840. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  2841. (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."))
  2842. ;; We use a before-change function to check if a table might need
  2843. ;; an update.
  2844. (defvar org-table-may-need-update t
  2845. "Indicates that a table might need an update.
  2846. This variable is set by `org-before-change-function'.
  2847. `org-table-align' sets it back to nil.")
  2848. (defun org-before-change-function (beg end)
  2849. "Every change indicates that a table might need an update."
  2850. (setq org-table-may-need-update t))
  2851. (defvar org-mode-map)
  2852. (defvar org-mode-hook nil)
  2853. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  2854. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  2855. (defvar org-table-buffer-is-an nil)
  2856. (defconst org-outline-regexp "\\*+ ")
  2857. ;;;###autoload
  2858. (define-derived-mode org-mode outline-mode "Org"
  2859. "Outline-based notes management and organizer, alias
  2860. \"Carsten's outline-mode for keeping track of everything.\"
  2861. Org-mode develops organizational tasks around a NOTES file which
  2862. contains information about projects as plain text. Org-mode is
  2863. implemented on top of outline-mode, which is ideal to keep the content
  2864. of large files well structured. It supports ToDo items, deadlines and
  2865. time stamps, which magically appear in the diary listing of the Emacs
  2866. calendar. Tables are easily created with a built-in table editor.
  2867. Plain text URL-like links connect to websites, emails (VM), Usenet
  2868. messages (Gnus), BBDB entries, and any files related to the project.
  2869. For printing and sharing of notes, an Org-mode file (or a part of it)
  2870. can be exported as a structured ASCII or HTML file.
  2871. The following commands are available:
  2872. \\{org-mode-map}"
  2873. ;; Get rid of Outline menus, they are not needed
  2874. ;; Need to do this here because define-derived-mode sets up
  2875. ;; the keymap so late. Still, it is a waste to call this each time
  2876. ;; we switch another buffer into org-mode.
  2877. (if (featurep 'xemacs)
  2878. (when (boundp 'outline-mode-menu-heading)
  2879. ;; Assume this is Greg's port, it used easymenu
  2880. (easy-menu-remove outline-mode-menu-heading)
  2881. (easy-menu-remove outline-mode-menu-show)
  2882. (easy-menu-remove outline-mode-menu-hide))
  2883. (define-key org-mode-map [menu-bar headings] 'undefined)
  2884. (define-key org-mode-map [menu-bar hide] 'undefined)
  2885. (define-key org-mode-map [menu-bar show] 'undefined))
  2886. (org-load-modules-maybe)
  2887. (easy-menu-add org-org-menu)
  2888. (easy-menu-add org-tbl-menu)
  2889. (org-install-agenda-files-menu)
  2890. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  2891. (org-add-to-invisibility-spec '(org-cwidth))
  2892. (when (featurep 'xemacs)
  2893. (org-set-local 'line-move-ignore-invisible t))
  2894. (org-set-local 'outline-regexp org-outline-regexp)
  2895. (org-set-local 'outline-level 'org-outline-level)
  2896. (when (and org-ellipsis
  2897. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  2898. (fboundp 'make-glyph-code))
  2899. (unless org-display-table
  2900. (setq org-display-table (make-display-table)))
  2901. (set-display-table-slot
  2902. org-display-table 4
  2903. (vconcat (mapcar
  2904. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  2905. org-ellipsis)))
  2906. (if (stringp org-ellipsis) org-ellipsis "..."))))
  2907. (setq buffer-display-table org-display-table))
  2908. (org-set-regexps-and-options)
  2909. ;; Calc embedded
  2910. (org-set-local 'calc-embedded-open-mode "# ")
  2911. (modify-syntax-entry ?# "<")
  2912. (modify-syntax-entry ?@ "w")
  2913. (if org-startup-truncated (setq truncate-lines t))
  2914. (org-set-local 'font-lock-unfontify-region-function
  2915. 'org-unfontify-region)
  2916. ;; Activate before-change-function
  2917. (org-set-local 'org-table-may-need-update t)
  2918. (org-add-hook 'before-change-functions 'org-before-change-function nil
  2919. 'local)
  2920. ;; Check for running clock before killing a buffer
  2921. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  2922. ;; Paragraphs and auto-filling
  2923. (org-set-autofill-regexps)
  2924. (setq indent-line-function 'org-indent-line-function)
  2925. (org-update-radio-target-regexp)
  2926. ;; Comment characters
  2927. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  2928. (org-set-local 'comment-padding " ")
  2929. ;; Align options lines
  2930. (org-set-local
  2931. 'align-mode-rules-list
  2932. '((org-in-buffer-settings
  2933. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  2934. (modes . '(org-mode)))))
  2935. ;; Imenu
  2936. (org-set-local 'imenu-create-index-function
  2937. 'org-imenu-get-tree)
  2938. ;; Make isearch reveal context
  2939. (if (or (featurep 'xemacs)
  2940. (not (boundp 'outline-isearch-open-invisible-function)))
  2941. ;; Emacs 21 and XEmacs make use of the hook
  2942. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  2943. ;; Emacs 22 deals with this through a special variable
  2944. (org-set-local 'outline-isearch-open-invisible-function
  2945. (lambda (&rest ignore) (org-show-context 'isearch))))
  2946. ;; If empty file that did not turn on org-mode automatically, make it to.
  2947. (if (and org-insert-mode-line-in-empty-file
  2948. (interactive-p)
  2949. (= (point-min) (point-max)))
  2950. (insert "# -*- mode: org -*-\n\n"))
  2951. (unless org-inhibit-startup
  2952. (when org-startup-align-all-tables
  2953. (let ((bmp (buffer-modified-p)))
  2954. (org-table-map-tables 'org-table-align)
  2955. (set-buffer-modified-p bmp)))
  2956. (org-set-startup-visibility)))
  2957. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  2958. (defun org-current-time ()
  2959. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  2960. (if (> (car org-time-stamp-rounding-minutes) 1)
  2961. (let ((r (car org-time-stamp-rounding-minutes))
  2962. (time (decode-time)))
  2963. (apply 'encode-time
  2964. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  2965. (nthcdr 2 time))))
  2966. (current-time)))
  2967. ;;;; Font-Lock stuff, including the activators
  2968. (defvar org-mouse-map (make-sparse-keymap))
  2969. (org-defkey org-mouse-map
  2970. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  2971. (org-defkey org-mouse-map
  2972. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  2973. (when org-mouse-1-follows-link
  2974. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  2975. (when org-tab-follows-link
  2976. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  2977. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  2978. (when org-return-follows-link
  2979. (org-defkey org-mouse-map [(return)] 'org-open-at-point)
  2980. (org-defkey org-mouse-map "\C-m" 'org-open-at-point))
  2981. (require 'font-lock)
  2982. (defconst org-non-link-chars "]\t\n\r<>")
  2983. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  2984. "shell" "elisp"))
  2985. (defvar org-link-types-re nil
  2986. "Matches a link that has a url-like prefix like \"http:\"")
  2987. (defvar org-link-re-with-space nil
  2988. "Matches a link with spaces, optional angular brackets around it.")
  2989. (defvar org-link-re-with-space2 nil
  2990. "Matches a link with spaces, optional angular brackets around it.")
  2991. (defvar org-angle-link-re nil
  2992. "Matches link with angular brackets, spaces are allowed.")
  2993. (defvar org-plain-link-re nil
  2994. "Matches plain link, without spaces.")
  2995. (defvar org-bracket-link-regexp nil
  2996. "Matches a link in double brackets.")
  2997. (defvar org-bracket-link-analytic-regexp nil
  2998. "Regular expression used to analyze links.
  2999. Here is what the match groups contain after a match:
  3000. 1: http:
  3001. 2: http
  3002. 3: path
  3003. 4: [desc]
  3004. 5: desc")
  3005. (defvar org-any-link-re nil
  3006. "Regular expression matching any link.")
  3007. (defun org-make-link-regexps ()
  3008. "Update the link regular expressions.
  3009. This should be called after the variable `org-link-types' has changed."
  3010. (setq org-link-types-re
  3011. (concat
  3012. "\\`\\(" (mapconcat 'identity org-link-types "\\|") "\\):")
  3013. org-link-re-with-space
  3014. (concat
  3015. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3016. "\\([^" org-non-link-chars " ]"
  3017. "[^" org-non-link-chars "]*"
  3018. "[^" org-non-link-chars " ]\\)>?")
  3019. org-link-re-with-space2
  3020. (concat
  3021. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3022. "\\([^" org-non-link-chars " ]"
  3023. "[^]\t\n\r]*"
  3024. "[^" org-non-link-chars " ]\\)>?")
  3025. org-angle-link-re
  3026. (concat
  3027. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3028. "\\([^" org-non-link-chars " ]"
  3029. "[^" org-non-link-chars "]*"
  3030. "\\)>")
  3031. org-plain-link-re
  3032. (concat
  3033. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3034. "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  3035. org-bracket-link-regexp
  3036. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  3037. org-bracket-link-analytic-regexp
  3038. (concat
  3039. "\\[\\["
  3040. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  3041. "\\([^]]+\\)"
  3042. "\\]"
  3043. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3044. "\\]")
  3045. org-any-link-re
  3046. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  3047. org-angle-link-re "\\)\\|\\("
  3048. org-plain-link-re "\\)")))
  3049. (org-make-link-regexps)
  3050. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  3051. "Regular expression for fast time stamp matching.")
  3052. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  3053. "Regular expression for fast time stamp matching.")
  3054. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3055. "Regular expression matching time strings for analysis.
  3056. This one does not require the space after the date, so it can be used
  3057. on a string that terminates immediately after the date.")
  3058. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3059. "Regular expression matching time strings for analysis.")
  3060. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  3061. "Regular expression matching time stamps, with groups.")
  3062. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  3063. "Regular expression matching time stamps (also [..]), with groups.")
  3064. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  3065. "Regular expression matching a time stamp range.")
  3066. (defconst org-tr-regexp-both
  3067. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  3068. "Regular expression matching a time stamp range.")
  3069. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  3070. org-ts-regexp "\\)?")
  3071. "Regular expression matching a time stamp or time stamp range.")
  3072. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  3073. org-ts-regexp-both "\\)?")
  3074. "Regular expression matching a time stamp or time stamp range.
  3075. The time stamps may be either active or inactive.")
  3076. (defvar org-emph-face nil)
  3077. (defun org-do-emphasis-faces (limit)
  3078. "Run through the buffer and add overlays to links."
  3079. (let (rtn)
  3080. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  3081. (if (not (= (char-after (match-beginning 3))
  3082. (char-after (match-beginning 4))))
  3083. (progn
  3084. (setq rtn t)
  3085. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  3086. 'face
  3087. (nth 1 (assoc (match-string 3)
  3088. org-emphasis-alist)))
  3089. (add-text-properties (match-beginning 2) (match-end 2)
  3090. '(font-lock-multiline t))
  3091. (when org-hide-emphasis-markers
  3092. (add-text-properties (match-end 4) (match-beginning 5)
  3093. '(invisible org-link))
  3094. (add-text-properties (match-beginning 3) (match-end 3)
  3095. '(invisible org-link)))))
  3096. (backward-char 1))
  3097. rtn))
  3098. (defun org-emphasize (&optional char)
  3099. "Insert or change an emphasis, i.e. a font like bold or italic.
  3100. If there is an active region, change that region to a new emphasis.
  3101. If there is no region, just insert the marker characters and position
  3102. the cursor between them.
  3103. CHAR should be either the marker character, or the first character of the
  3104. HTML tag associated with that emphasis. If CHAR is a space, the means
  3105. to remove the emphasis of the selected region.
  3106. If char is not given (for example in an interactive call) it
  3107. will be prompted for."
  3108. (interactive)
  3109. (debug)
  3110. (let ((eal org-emphasis-alist) e det
  3111. (erc org-emphasis-regexp-components)
  3112. (prompt "")
  3113. (string "") beg end move tag c s)
  3114. (if (org-region-active-p)
  3115. (setq beg (region-beginning) end (region-end)
  3116. string (buffer-substring beg end))
  3117. (setq move t))
  3118. (while (setq e (pop eal))
  3119. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  3120. c (aref tag 0))
  3121. (push (cons c (string-to-char (car e))) det)
  3122. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  3123. (substring tag 1)))))
  3124. (setq det (nreverse det))
  3125. (unless char
  3126. (message "%s" (concat "Emphasis marker or tag:" prompt))
  3127. (setq char (read-char-exclusive)))
  3128. (setq char (or (cdr (assoc char det)) char))
  3129. (if (equal char ?\ )
  3130. (setq s "" move nil)
  3131. (unless (assoc (char-to-string char) org-emphasis-alist)
  3132. (error "No such emphasis marker: \"%c\"" char))
  3133. (setq s (char-to-string char)))
  3134. (while (and (> (length string) 1)
  3135. (equal (substring string 0 1) (substring string -1))
  3136. (assoc (substring string 0 1) org-emphasis-alist))
  3137. (setq string (substring string 1 -1)))
  3138. (setq string (concat s string s))
  3139. (if beg (delete-region beg end))
  3140. (unless (or (bolp)
  3141. (string-match (concat "[" (nth 0 erc) "\n]")
  3142. (char-to-string (char-before (point)))))
  3143. (insert " "))
  3144. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  3145. (char-to-string (char-after (point))))
  3146. (insert " ") (backward-char 1))
  3147. (insert string)
  3148. (and move (backward-char 1))))
  3149. (defconst org-nonsticky-props
  3150. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  3151. (defun org-activate-plain-links (limit)
  3152. "Run through the buffer and add overlays to links."
  3153. (catch 'exit
  3154. (let (f)
  3155. (while (re-search-forward org-plain-link-re limit t)
  3156. (setq f (get-text-property (match-beginning 0) 'face))
  3157. (if (or (eq f 'org-tag)
  3158. (and (listp f) (memq 'org-tag f)))
  3159. nil
  3160. (add-text-properties (match-beginning 0) (match-end 0)
  3161. (list 'mouse-face 'highlight
  3162. 'rear-nonsticky org-nonsticky-props
  3163. 'keymap org-mouse-map
  3164. ))
  3165. (throw 'exit t))))))
  3166. (defun org-activate-code (limit)
  3167. (if (re-search-forward "^[ \t]*\\(: .*\n?\\)" limit t)
  3168. (progn
  3169. (remove-text-properties (match-beginning 0) (match-end 0)
  3170. '(display t invisible t intangible t))
  3171. t)))
  3172. (defun org-activate-angle-links (limit)
  3173. "Run through the buffer and add overlays to links."
  3174. (if (re-search-forward org-angle-link-re limit t)
  3175. (progn
  3176. (add-text-properties (match-beginning 0) (match-end 0)
  3177. (list 'mouse-face 'highlight
  3178. 'rear-nonsticky org-nonsticky-props
  3179. 'keymap org-mouse-map
  3180. ))
  3181. t)))
  3182. (defun org-activate-bracket-links (limit)
  3183. "Run through the buffer and add overlays to bracketed links."
  3184. (if (re-search-forward org-bracket-link-regexp limit t)
  3185. (let* ((help (concat "LINK: "
  3186. (org-match-string-no-properties 1)))
  3187. ;; FIXME: above we should remove the escapes.
  3188. ;; but that requires another match, protecting match data,
  3189. ;; a lot of overhead for font-lock.
  3190. (ip (org-maybe-intangible
  3191. (list 'invisible 'org-link 'rear-nonsticky org-nonsticky-props
  3192. 'keymap org-mouse-map 'mouse-face 'highlight
  3193. 'font-lock-multiline t 'help-echo help)))
  3194. (vp (list 'rear-nonsticky org-nonsticky-props
  3195. 'keymap org-mouse-map 'mouse-face 'highlight
  3196. ' font-lock-multiline t 'help-echo help)))
  3197. ;; We need to remove the invisible property here. Table narrowing
  3198. ;; may have made some of this invisible.
  3199. (remove-text-properties (match-beginning 0) (match-end 0)
  3200. '(invisible nil))
  3201. (if (match-end 3)
  3202. (progn
  3203. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  3204. (add-text-properties (match-beginning 3) (match-end 3) vp)
  3205. (add-text-properties (match-end 3) (match-end 0) ip))
  3206. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  3207. (add-text-properties (match-beginning 1) (match-end 1) vp)
  3208. (add-text-properties (match-end 1) (match-end 0) ip))
  3209. t)))
  3210. (defun org-activate-dates (limit)
  3211. "Run through the buffer and add overlays to dates."
  3212. (if (re-search-forward org-tsr-regexp-both limit t)
  3213. (progn
  3214. (add-text-properties (match-beginning 0) (match-end 0)
  3215. (list 'mouse-face 'highlight
  3216. 'rear-nonsticky org-nonsticky-props
  3217. 'keymap org-mouse-map))
  3218. (when org-display-custom-times
  3219. (if (match-end 3)
  3220. (org-display-custom-time (match-beginning 3) (match-end 3)))
  3221. (org-display-custom-time (match-beginning 1) (match-end 1)))
  3222. t)))
  3223. (defvar org-target-link-regexp nil
  3224. "Regular expression matching radio targets in plain text.")
  3225. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  3226. "Regular expression matching a link target.")
  3227. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  3228. "Regular expression matching a radio target.")
  3229. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  3230. "Regular expression matching any target.")
  3231. (defun org-activate-target-links (limit)
  3232. "Run through the buffer and add overlays to target matches."
  3233. (when org-target-link-regexp
  3234. (let ((case-fold-search t))
  3235. (if (re-search-forward org-target-link-regexp limit t)
  3236. (progn
  3237. (add-text-properties (match-beginning 0) (match-end 0)
  3238. (list 'mouse-face 'highlight
  3239. 'rear-nonsticky org-nonsticky-props
  3240. 'keymap org-mouse-map
  3241. 'help-echo "Radio target link"
  3242. 'org-linked-text t))
  3243. t)))))
  3244. (defun org-update-radio-target-regexp ()
  3245. "Find all radio targets in this file and update the regular expression."
  3246. (interactive)
  3247. (when (memq 'radio org-activate-links)
  3248. (setq org-target-link-regexp
  3249. (org-make-target-link-regexp (org-all-targets 'radio)))
  3250. (org-restart-font-lock)))
  3251. (defun org-hide-wide-columns (limit)
  3252. (let (s e)
  3253. (setq s (text-property-any (point) (or limit (point-max))
  3254. 'org-cwidth t))
  3255. (when s
  3256. (setq e (next-single-property-change s 'org-cwidth))
  3257. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  3258. (goto-char e)
  3259. t)))
  3260. (defvar org-latex-and-specials-regexp nil
  3261. "Regular expression for highlighting export special stuff.")
  3262. (defvar org-match-substring-regexp)
  3263. (defvar org-match-substring-with-braces-regexp)
  3264. (defvar org-export-html-special-string-regexps)
  3265. (defun org-compute-latex-and-specials-regexp ()
  3266. "Compute regular expression for stuff treated specially by exporters."
  3267. (if (not org-highlight-latex-fragments-and-specials)
  3268. (org-set-local 'org-latex-and-specials-regexp nil)
  3269. (require 'org-exp)
  3270. (let*
  3271. ((matchers (plist-get org-format-latex-options :matchers))
  3272. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  3273. org-latex-regexps)))
  3274. (options (org-combine-plists (org-default-export-plist)
  3275. (org-infile-export-plist)))
  3276. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  3277. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  3278. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  3279. (org-export-html-expand (plist-get options :expand-quoted-html))
  3280. (org-export-with-special-strings (plist-get options :special-strings))
  3281. (re-sub
  3282. (cond
  3283. ((equal org-export-with-sub-superscripts '{})
  3284. (list org-match-substring-with-braces-regexp))
  3285. (org-export-with-sub-superscripts
  3286. (list org-match-substring-regexp))
  3287. (t nil)))
  3288. (re-latex
  3289. (if org-export-with-LaTeX-fragments
  3290. (mapcar (lambda (x) (nth 1 x)) latexs)))
  3291. (re-macros
  3292. (if org-export-with-TeX-macros
  3293. (list (concat "\\\\"
  3294. (regexp-opt
  3295. (append (mapcar 'car org-html-entities)
  3296. (if (boundp 'org-latex-entities)
  3297. org-latex-entities nil))
  3298. 'words))) ; FIXME
  3299. ))
  3300. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  3301. (re-special (if org-export-with-special-strings
  3302. (mapcar (lambda (x) (car x))
  3303. org-export-html-special-string-regexps)))
  3304. (re-rest
  3305. (delq nil
  3306. (list
  3307. (if org-export-html-expand "@<[^>\n]+>")
  3308. ))))
  3309. (org-set-local
  3310. 'org-latex-and-specials-regexp
  3311. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  3312. re-rest) "\\|")))))
  3313. (defun org-do-latex-and-special-faces (limit)
  3314. "Run through the buffer and add overlays to links."
  3315. (when org-latex-and-specials-regexp
  3316. (let (rtn d)
  3317. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  3318. limit t))
  3319. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  3320. 'face))
  3321. '(org-code org-verbatim underline)))
  3322. (progn
  3323. (setq rtn t
  3324. d (cond ((member (char-after (1+ (match-beginning 0)))
  3325. '(?_ ?^)) 1)
  3326. (t 0)))
  3327. (font-lock-prepend-text-property
  3328. (+ d (match-beginning 0)) (match-end 0)
  3329. 'face 'org-latex-and-export-specials)
  3330. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  3331. '(font-lock-multiline t)))))
  3332. rtn)))
  3333. (defun org-restart-font-lock ()
  3334. "Restart font-lock-mode, to force refontification."
  3335. (when (and (boundp 'font-lock-mode) font-lock-mode)
  3336. (font-lock-mode -1)
  3337. (font-lock-mode 1)))
  3338. (defun org-all-targets (&optional radio)
  3339. "Return a list of all targets in this file.
  3340. With optional argument RADIO, only find radio targets."
  3341. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  3342. rtn)
  3343. (save-excursion
  3344. (goto-char (point-min))
  3345. (while (re-search-forward re nil t)
  3346. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  3347. rtn)))
  3348. (defun org-make-target-link-regexp (targets)
  3349. "Make regular expression matching all strings in TARGETS.
  3350. The regular expression finds the targets also if there is a line break
  3351. between words."
  3352. (and targets
  3353. (concat
  3354. "\\<\\("
  3355. (mapconcat
  3356. (lambda (x)
  3357. (while (string-match " +" x)
  3358. (setq x (replace-match "\\s-+" t t x)))
  3359. x)
  3360. targets
  3361. "\\|")
  3362. "\\)\\>")))
  3363. (defun org-activate-tags (limit)
  3364. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  3365. (progn
  3366. (add-text-properties (match-beginning 1) (match-end 1)
  3367. (list 'mouse-face 'highlight
  3368. 'rear-nonsticky org-nonsticky-props
  3369. 'keymap org-mouse-map))
  3370. t)))
  3371. (defun org-outline-level ()
  3372. (save-excursion
  3373. (looking-at outline-regexp)
  3374. (if (match-beginning 1)
  3375. (+ (org-get-string-indentation (match-string 1)) 1000)
  3376. (1- (- (match-end 0) (match-beginning 0))))))
  3377. (defvar org-font-lock-keywords nil)
  3378. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  3379. "Regular expression matching a property line.")
  3380. (defvar org-font-lock-hook nil
  3381. "Functions to be called for special font lock stuff.")
  3382. (defun org-font-lock-hook (limit)
  3383. (run-hook-with-args 'org-font-lock-hook limit))
  3384. (defun org-set-font-lock-defaults ()
  3385. (let* ((em org-fontify-emphasized-text)
  3386. (lk org-activate-links)
  3387. (org-font-lock-extra-keywords
  3388. (list
  3389. ;; Call the hook
  3390. '(org-font-lock-hook)
  3391. ;; Headlines
  3392. '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
  3393. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  3394. ;; Table lines
  3395. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  3396. (1 'org-table t))
  3397. ;; Table internals
  3398. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  3399. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  3400. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  3401. ;; Drawers
  3402. (list org-drawer-regexp '(0 'org-special-keyword t))
  3403. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  3404. ;; Properties
  3405. (list org-property-re
  3406. '(1 'org-special-keyword t)
  3407. '(3 'org-property-value t))
  3408. (if org-format-transports-properties-p
  3409. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  3410. ;; Links
  3411. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  3412. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  3413. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  3414. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  3415. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  3416. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  3417. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  3418. '(org-hide-wide-columns (0 nil append))
  3419. ;; TODO lines
  3420. (list (concat "^\\*+[ \t]+" org-todo-regexp)
  3421. '(1 (org-get-todo-face 1) t))
  3422. ;; DONE
  3423. (if org-fontify-done-headline
  3424. (list (concat "^[*]+ +\\<\\("
  3425. (mapconcat 'regexp-quote org-done-keywords "\\|")
  3426. "\\)\\(.*\\)")
  3427. '(2 'org-headline-done t))
  3428. nil)
  3429. ;; Priorities
  3430. (list (concat "\\[#[A-Z0-9]\\]") '(0 'org-special-keyword t))
  3431. ;; Special keywords
  3432. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  3433. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  3434. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  3435. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  3436. ;; Emphasis
  3437. (if em
  3438. (if (featurep 'xemacs)
  3439. '(org-do-emphasis-faces (0 nil append))
  3440. '(org-do-emphasis-faces)))
  3441. ;; Checkboxes
  3442. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  3443. 2 'bold prepend)
  3444. (if org-provide-checkbox-statistics
  3445. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  3446. (0 (org-get-checkbox-statistics-face) t)))
  3447. ;; Description list items
  3448. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*? ::\\)"
  3449. 2 'bold prepend)
  3450. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  3451. '(1 'org-archived prepend))
  3452. ;; Specials
  3453. '(org-do-latex-and-special-faces)
  3454. ;; Code
  3455. '(org-activate-code (1 'org-code t))
  3456. ;; COMMENT
  3457. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  3458. "\\|" org-quote-string "\\)\\>")
  3459. '(1 'org-special-keyword t))
  3460. '("^#.*" (0 'font-lock-comment-face t))
  3461. )))
  3462. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  3463. ;; Now set the full font-lock-keywords
  3464. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  3465. (org-set-local 'font-lock-defaults
  3466. '(org-font-lock-keywords t nil nil backward-paragraph))
  3467. (kill-local-variable 'font-lock-keywords) nil))
  3468. (defvar org-m nil)
  3469. (defvar org-l nil)
  3470. (defvar org-f nil)
  3471. (defun org-get-level-face (n)
  3472. "Get the right face for match N in font-lock matching of healdines."
  3473. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  3474. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  3475. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  3476. (cond
  3477. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  3478. ((eq n 2) org-f)
  3479. (t (if org-level-color-stars-only nil org-f))))
  3480. (defun org-get-todo-face (kwd)
  3481. "Get the right face for a TODO keyword KWD.
  3482. If KWD is a number, get the corresponding match group."
  3483. (if (numberp kwd) (setq kwd (match-string kwd)))
  3484. (or (cdr (assoc kwd org-todo-keyword-faces))
  3485. (and (member kwd org-done-keywords) 'org-done)
  3486. 'org-todo))
  3487. (defun org-unfontify-region (beg end &optional maybe_loudly)
  3488. "Remove fontification and activation overlays from links."
  3489. (font-lock-default-unfontify-region beg end)
  3490. (let* ((buffer-undo-list t)
  3491. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  3492. (inhibit-modification-hooks t)
  3493. deactivate-mark buffer-file-name buffer-file-truename)
  3494. (remove-text-properties beg end
  3495. '(mouse-face t keymap t org-linked-text t
  3496. invisible t intangible t))))
  3497. ;;;; Visibility cycling, including org-goto and indirect buffer
  3498. ;;; Cycling
  3499. (defvar org-cycle-global-status nil)
  3500. (make-variable-buffer-local 'org-cycle-global-status)
  3501. (defvar org-cycle-subtree-status nil)
  3502. (make-variable-buffer-local 'org-cycle-subtree-status)
  3503. ;;;###autoload
  3504. (defun org-cycle (&optional arg)
  3505. "Visibility cycling for Org-mode.
  3506. - When this function is called with a prefix argument, rotate the entire
  3507. buffer through 3 states (global cycling)
  3508. 1. OVERVIEW: Show only top-level headlines.
  3509. 2. CONTENTS: Show all headlines of all levels, but no body text.
  3510. 3. SHOW ALL: Show everything.
  3511. When called with two C-u C-u prefixes, switch to the startup visibility,
  3512. determined by the variable `org-startup-folded', and by any VISIBILITY
  3513. properties in the buffer.
  3514. When called with three C-u C-u C-u prefixed, show the entire buffer,
  3515. including drawers.
  3516. - When point is at the beginning of a headline, rotate the subtree started
  3517. by this line through 3 different states (local cycling)
  3518. 1. FOLDED: Only the main headline is shown.
  3519. 2. CHILDREN: The main headline and the direct children are shown.
  3520. From this state, you can move to one of the children
  3521. and zoom in further.
  3522. 3. SUBTREE: Show the entire subtree, including body text.
  3523. - When there is a numeric prefix, go up to a heading with level ARG, do
  3524. a `show-subtree' and return to the previous cursor position. If ARG
  3525. is negative, go up that many levels.
  3526. - When point is not at the beginning of a headline, execute the global
  3527. binding for TAB, which is re-indenting the line. See the option
  3528. `org-cycle-emulate-tab' for details.
  3529. - Special case: if point is at the beginning of the buffer and there is
  3530. no headline in line 1, this function will act as if called with prefix arg.
  3531. But only if also the variable `org-cycle-global-at-bob' is t."
  3532. (interactive "P")
  3533. (org-load-modules-maybe)
  3534. (let* ((outline-regexp
  3535. (if (and (org-mode-p) org-cycle-include-plain-lists)
  3536. "\\(?:\\*+ \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"
  3537. outline-regexp))
  3538. (bob-special (and org-cycle-global-at-bob (bobp)
  3539. (not (looking-at outline-regexp))))
  3540. (org-cycle-hook
  3541. (if bob-special
  3542. (delq 'org-optimize-window-after-visibility-change
  3543. (copy-sequence org-cycle-hook))
  3544. org-cycle-hook))
  3545. (pos (point)))
  3546. (if (or bob-special (equal arg '(4)))
  3547. ;; special case: use global cycling
  3548. (setq arg t))
  3549. (cond
  3550. ((equal arg '(16))
  3551. (org-set-startup-visibility)
  3552. (message "Startup visibility, plus VISIBILITY properties"))
  3553. ((equal arg '(64))
  3554. (show-all)
  3555. (message "Entire buffer visible, including drawers"))
  3556. ((org-at-table-p 'any)
  3557. ;; Enter the table or move to the next field in the table
  3558. (or (org-table-recognize-table.el)
  3559. (progn
  3560. (if arg (org-table-edit-field t)
  3561. (org-table-justify-field-maybe)
  3562. (call-interactively 'org-table-next-field)))))
  3563. ((eq arg t) ;; Global cycling
  3564. (cond
  3565. ((and (eq last-command this-command)
  3566. (eq org-cycle-global-status 'overview))
  3567. ;; We just created the overview - now do table of contents
  3568. ;; This can be slow in very large buffers, so indicate action
  3569. (message "CONTENTS...")
  3570. (org-content)
  3571. (message "CONTENTS...done")
  3572. (setq org-cycle-global-status 'contents)
  3573. (run-hook-with-args 'org-cycle-hook 'contents))
  3574. ((and (eq last-command this-command)
  3575. (eq org-cycle-global-status 'contents))
  3576. ;; We just showed the table of contents - now show everything
  3577. (show-all)
  3578. (message "SHOW ALL")
  3579. (setq org-cycle-global-status 'all)
  3580. (run-hook-with-args 'org-cycle-hook 'all))
  3581. (t
  3582. ;; Default action: go to overview
  3583. (org-overview)
  3584. (message "OVERVIEW")
  3585. (setq org-cycle-global-status 'overview)
  3586. (run-hook-with-args 'org-cycle-hook 'overview))))
  3587. ((and org-drawers org-drawer-regexp
  3588. (save-excursion
  3589. (beginning-of-line 1)
  3590. (looking-at org-drawer-regexp)))
  3591. ;; Toggle block visibility
  3592. (org-flag-drawer
  3593. (not (get-char-property (match-end 0) 'invisible))))
  3594. ((integerp arg)
  3595. ;; Show-subtree, ARG levels up from here.
  3596. (save-excursion
  3597. (org-back-to-heading)
  3598. (outline-up-heading (if (< arg 0) (- arg)
  3599. (- (funcall outline-level) arg)))
  3600. (org-show-subtree)))
  3601. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  3602. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  3603. ;; At a heading: rotate between three different views
  3604. (org-back-to-heading)
  3605. (let ((goal-column 0) eoh eol eos)
  3606. ;; First, some boundaries
  3607. (save-excursion
  3608. (org-back-to-heading)
  3609. (save-excursion
  3610. (beginning-of-line 2)
  3611. (while (and (not (eobp)) ;; this is like `next-line'
  3612. (get-char-property (1- (point)) 'invisible))
  3613. (beginning-of-line 2)) (setq eol (point)))
  3614. (outline-end-of-heading) (setq eoh (point))
  3615. (org-end-of-subtree t)
  3616. (unless (eobp)
  3617. (skip-chars-forward " \t\n")
  3618. (beginning-of-line 1) ; in case this is an item
  3619. )
  3620. (setq eos (1- (point))))
  3621. ;; Find out what to do next and set `this-command'
  3622. (cond
  3623. ((= eos eoh)
  3624. ;; Nothing is hidden behind this heading
  3625. (message "EMPTY ENTRY")
  3626. (setq org-cycle-subtree-status nil)
  3627. (save-excursion
  3628. (goto-char eos)
  3629. (outline-next-heading)
  3630. (if (org-invisible-p) (org-flag-heading nil))))
  3631. ((or (>= eol eos)
  3632. (not (string-match "\\S-" (buffer-substring eol eos))))
  3633. ;; Entire subtree is hidden in one line: open it
  3634. (org-show-entry)
  3635. (show-children)
  3636. (message "CHILDREN")
  3637. (save-excursion
  3638. (goto-char eos)
  3639. (outline-next-heading)
  3640. (if (org-invisible-p) (org-flag-heading nil)))
  3641. (setq org-cycle-subtree-status 'children)
  3642. (run-hook-with-args 'org-cycle-hook 'children))
  3643. ((and (eq last-command this-command)
  3644. (eq org-cycle-subtree-status 'children))
  3645. ;; We just showed the children, now show everything.
  3646. (org-show-subtree)
  3647. (message "SUBTREE")
  3648. (setq org-cycle-subtree-status 'subtree)
  3649. (run-hook-with-args 'org-cycle-hook 'subtree))
  3650. (t
  3651. ;; Default action: hide the subtree.
  3652. (hide-subtree)
  3653. (message "FOLDED")
  3654. (setq org-cycle-subtree-status 'folded)
  3655. (run-hook-with-args 'org-cycle-hook 'folded)))))
  3656. ;; TAB emulation and template completion
  3657. (buffer-read-only (org-back-to-heading))
  3658. ((org-try-structure-completion))
  3659. ((org-try-cdlatex-tab))
  3660. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  3661. (or (not (bolp))
  3662. (not (looking-at outline-regexp))))
  3663. (call-interactively (global-key-binding "\t")))
  3664. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  3665. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  3666. (or (and (eq org-cycle-emulate-tab 'white)
  3667. (= (match-end 0) (point-at-eol)))
  3668. (and (eq org-cycle-emulate-tab 'whitestart)
  3669. (>= (match-end 0) pos))))
  3670. t
  3671. (eq org-cycle-emulate-tab t))
  3672. (call-interactively (global-key-binding "\t")))
  3673. (t (save-excursion
  3674. (org-back-to-heading)
  3675. (org-cycle))))))
  3676. ;;;###autoload
  3677. (defun org-global-cycle (&optional arg)
  3678. "Cycle the global visibility. For details see `org-cycle'.
  3679. With C-u prefix arg, switch to startup visibility.
  3680. With a numeric prefix, show all headlines up to that level."
  3681. (interactive "P")
  3682. (let ((org-cycle-include-plain-lists
  3683. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  3684. (cond
  3685. ((integerp arg)
  3686. (show-all)
  3687. (hide-sublevels arg)
  3688. (setq org-cycle-global-status 'contents))
  3689. ((equal arg '(4))
  3690. (org-set-startup-visibility)
  3691. (message "Startup visibility, plus VISIBILITY properties."))
  3692. (t
  3693. (org-cycle '(4))))))
  3694. (defun org-set-startup-visibility ()
  3695. "Set the visibility required by startup options and properties."
  3696. (cond
  3697. ((eq org-startup-folded t)
  3698. (org-cycle '(4)))
  3699. ((eq org-startup-folded 'content)
  3700. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  3701. (org-cycle '(4)) (org-cycle '(4)))))
  3702. (org-set-visibility-according-to-property 'no-cleanup)
  3703. (org-cycle-hide-archived-subtrees 'all)
  3704. (org-cycle-hide-drawers 'all)
  3705. (org-cycle-show-empty-lines 'all))
  3706. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  3707. "Switch subtree visibilities according to :VISIBILITY: property."
  3708. (interactive)
  3709. (let (state)
  3710. (save-excursion
  3711. (goto-char (point-min))
  3712. (while (re-search-forward
  3713. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  3714. nil t)
  3715. (setq state (match-string 1))
  3716. (save-excursion
  3717. (org-back-to-heading t)
  3718. (hide-subtree)
  3719. (org-reveal)
  3720. (cond
  3721. ((equal state '("fold" "folded"))
  3722. (hide-subtree))
  3723. ((equal state "children")
  3724. (org-show-hidden-entry)
  3725. (show-children))
  3726. ((equal state "content")
  3727. (save-excursion
  3728. (save-restriction
  3729. (org-narrow-to-subtree)
  3730. (org-content))))
  3731. ((member state '("all" "showall"))
  3732. (show-subtree)))))
  3733. (unless no-cleanup
  3734. (org-cycle-hide-archived-subtrees 'all)
  3735. (org-cycle-hide-drawers 'all)
  3736. (org-cycle-show-empty-lines 'all)))))
  3737. (defun org-overview ()
  3738. "Switch to overview mode, shoing only top-level headlines.
  3739. Really, this shows all headlines with level equal or greater than the level
  3740. of the first headline in the buffer. This is important, because if the
  3741. first headline is not level one, then (hide-sublevels 1) gives confusing
  3742. results."
  3743. (interactive)
  3744. (let ((level (save-excursion
  3745. (goto-char (point-min))
  3746. (if (re-search-forward (concat "^" outline-regexp) nil t)
  3747. (progn
  3748. (goto-char (match-beginning 0))
  3749. (funcall outline-level))))))
  3750. (and level (hide-sublevels level))))
  3751. (defun org-content (&optional arg)
  3752. "Show all headlines in the buffer, like a table of contents.
  3753. With numerical argument N, show content up to level N."
  3754. (interactive "P")
  3755. (save-excursion
  3756. ;; Visit all headings and show their offspring
  3757. (and (integerp arg) (org-overview))
  3758. (goto-char (point-max))
  3759. (catch 'exit
  3760. (while (and (progn (condition-case nil
  3761. (outline-previous-visible-heading 1)
  3762. (error (goto-char (point-min))))
  3763. t)
  3764. (looking-at outline-regexp))
  3765. (if (integerp arg)
  3766. (show-children (1- arg))
  3767. (show-branches))
  3768. (if (bobp) (throw 'exit nil))))))
  3769. (defun org-optimize-window-after-visibility-change (state)
  3770. "Adjust the window after a change in outline visibility.
  3771. This function is the default value of the hook `org-cycle-hook'."
  3772. (when (get-buffer-window (current-buffer))
  3773. (cond
  3774. ; ((eq state 'overview) (org-first-headline-recenter 1))
  3775. ; ((eq state 'overview) (org-beginning-of-line))
  3776. ((eq state 'content) nil)
  3777. ((eq state 'all) nil)
  3778. ((eq state 'folded) nil)
  3779. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  3780. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  3781. (defun org-compact-display-after-subtree-move ()
  3782. (let (beg end)
  3783. (save-excursion
  3784. (if (org-up-heading-safe)
  3785. (progn
  3786. (hide-subtree)
  3787. (show-entry)
  3788. (show-children)
  3789. (org-cycle-show-empty-lines 'children)
  3790. (org-cycle-hide-drawers 'children))
  3791. (org-overview)))))
  3792. (defun org-cycle-show-empty-lines (state)
  3793. "Show empty lines above all visible headlines.
  3794. The region to be covered depends on STATE when called through
  3795. `org-cycle-hook'. Lisp program can use t for STATE to get the
  3796. entire buffer covered. Note that an empty line is only shown if there
  3797. are at least `org-cycle-separator-lines' empty lines before the headeline."
  3798. (when (> org-cycle-separator-lines 0)
  3799. (save-excursion
  3800. (let* ((n org-cycle-separator-lines)
  3801. (re (cond
  3802. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  3803. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  3804. (t (let ((ns (number-to-string (- n 2))))
  3805. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  3806. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  3807. beg end)
  3808. (cond
  3809. ((memq state '(overview contents t))
  3810. (setq beg (point-min) end (point-max)))
  3811. ((memq state '(children folded))
  3812. (setq beg (point) end (progn (org-end-of-subtree t t)
  3813. (beginning-of-line 2)
  3814. (point)))))
  3815. (when beg
  3816. (goto-char beg)
  3817. (while (re-search-forward re end t)
  3818. (if (not (get-char-property (match-end 1) 'invisible))
  3819. (outline-flag-region
  3820. (match-beginning 1) (match-end 1) nil)))))))
  3821. ;; Never hide empty lines at the end of the file.
  3822. (save-excursion
  3823. (goto-char (point-max))
  3824. (outline-previous-heading)
  3825. (outline-end-of-heading)
  3826. (if (and (looking-at "[ \t\n]+")
  3827. (= (match-end 0) (point-max)))
  3828. (outline-flag-region (point) (match-end 0) nil))))
  3829. (defun org-show-empty-lines-in-parent ()
  3830. "Move to the parent and re-show empty lines before visible headlines."
  3831. (save-excursion
  3832. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  3833. (org-cycle-show-empty-lines context))))
  3834. (defun org-cycle-hide-drawers (state)
  3835. "Re-hide all drawers after a visibility state change."
  3836. (when (and (org-mode-p)
  3837. (not (memq state '(overview folded))))
  3838. (save-excursion
  3839. (let* ((globalp (memq state '(contents all)))
  3840. (beg (if globalp (point-min) (point)))
  3841. (end (if globalp (point-max) (org-end-of-subtree t))))
  3842. (goto-char beg)
  3843. (while (re-search-forward org-drawer-regexp end t)
  3844. (org-flag-drawer t))))))
  3845. (defun org-flag-drawer (flag)
  3846. (save-excursion
  3847. (beginning-of-line 1)
  3848. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  3849. (let ((b (match-end 0))
  3850. (outline-regexp org-outline-regexp))
  3851. (if (re-search-forward
  3852. "^[ \t]*:END:"
  3853. (save-excursion (outline-next-heading) (point)) t)
  3854. (outline-flag-region b (point-at-eol) flag)
  3855. (error ":END: line missing"))))))
  3856. (defun org-subtree-end-visible-p ()
  3857. "Is the end of the current subtree visible?"
  3858. (pos-visible-in-window-p
  3859. (save-excursion (org-end-of-subtree t) (point))))
  3860. (defun org-first-headline-recenter (&optional N)
  3861. "Move cursor to the first headline and recenter the headline.
  3862. Optional argument N means, put the headline into the Nth line of the window."
  3863. (goto-char (point-min))
  3864. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  3865. (beginning-of-line)
  3866. (recenter (prefix-numeric-value N))))
  3867. ;;; Org-goto
  3868. (defvar org-goto-window-configuration nil)
  3869. (defvar org-goto-marker nil)
  3870. (defvar org-goto-map
  3871. (let ((map (make-sparse-keymap)))
  3872. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  3873. (while (setq cmd (pop cmds))
  3874. (substitute-key-definition cmd cmd map global-map)))
  3875. (suppress-keymap map)
  3876. (org-defkey map "\C-m" 'org-goto-ret)
  3877. (org-defkey map [(return)] 'org-goto-ret)
  3878. (org-defkey map [(left)] 'org-goto-left)
  3879. (org-defkey map [(right)] 'org-goto-right)
  3880. (org-defkey map [(control ?g)] 'org-goto-quit)
  3881. (org-defkey map "\C-i" 'org-cycle)
  3882. (org-defkey map [(tab)] 'org-cycle)
  3883. (org-defkey map [(down)] 'outline-next-visible-heading)
  3884. (org-defkey map [(up)] 'outline-previous-visible-heading)
  3885. (if org-goto-auto-isearch
  3886. (if (fboundp 'define-key-after)
  3887. (define-key-after map [t] 'org-goto-local-auto-isearch)
  3888. nil)
  3889. (org-defkey map "q" 'org-goto-quit)
  3890. (org-defkey map "n" 'outline-next-visible-heading)
  3891. (org-defkey map "p" 'outline-previous-visible-heading)
  3892. (org-defkey map "f" 'outline-forward-same-level)
  3893. (org-defkey map "b" 'outline-backward-same-level)
  3894. (org-defkey map "u" 'outline-up-heading))
  3895. (org-defkey map "/" 'org-occur)
  3896. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  3897. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  3898. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  3899. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  3900. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  3901. map))
  3902. (defconst org-goto-help
  3903. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  3904. RET=jump to location [Q]uit and return to previous location
  3905. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  3906. (defvar org-goto-start-pos) ; dynamically scoped parameter
  3907. ;; FIXME: Docstring doe not mention both interfaces
  3908. (defun org-goto (&optional alternative-interface)
  3909. "Look up a different location in the current file, keeping current visibility.
  3910. When you want look-up or go to a different location in a document, the
  3911. fastest way is often to fold the entire buffer and then dive into the tree.
  3912. This method has the disadvantage, that the previous location will be folded,
  3913. which may not be what you want.
  3914. This command works around this by showing a copy of the current buffer
  3915. in an indirect buffer, in overview mode. You can dive into the tree in
  3916. that copy, use org-occur and incremental search to find a location.
  3917. When pressing RET or `Q', the command returns to the original buffer in
  3918. which the visibility is still unchanged. After RET is will also jump to
  3919. the location selected in the indirect buffer and expose the
  3920. the headline hierarchy above."
  3921. (interactive "P")
  3922. (let* ((org-refile-targets '((nil . (:maxlevel . 10))))
  3923. (org-refile-use-outline-path t)
  3924. (interface
  3925. (if (not alternative-interface)
  3926. org-goto-interface
  3927. (if (eq org-goto-interface 'outline)
  3928. 'outline-path-completion
  3929. 'outline)))
  3930. (org-goto-start-pos (point))
  3931. (selected-point
  3932. (if (eq interface 'outline)
  3933. (car (org-get-location (current-buffer) org-goto-help))
  3934. (nth 3 (org-refile-get-location "Goto: ")))))
  3935. (if selected-point
  3936. (progn
  3937. (org-mark-ring-push org-goto-start-pos)
  3938. (goto-char selected-point)
  3939. (if (or (org-invisible-p) (org-invisible-p2))
  3940. (org-show-context 'org-goto)))
  3941. (message "Quit"))))
  3942. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  3943. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  3944. (defvar org-goto-local-auto-isearch-map) ; defined below
  3945. (defun org-get-location (buf help)
  3946. "Let the user select a location in the Org-mode buffer BUF.
  3947. This function uses a recursive edit. It returns the selected position
  3948. or nil."
  3949. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  3950. (isearch-hide-immediately nil)
  3951. (isearch-search-fun-function
  3952. (lambda () 'org-goto-local-search-headings))
  3953. (org-goto-selected-point org-goto-exit-command))
  3954. (save-excursion
  3955. (save-window-excursion
  3956. (delete-other-windows)
  3957. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  3958. (switch-to-buffer
  3959. (condition-case nil
  3960. (make-indirect-buffer (current-buffer) "*org-goto*")
  3961. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  3962. (with-output-to-temp-buffer "*Help*"
  3963. (princ help))
  3964. (shrink-window-if-larger-than-buffer (get-buffer-window "*Help*"))
  3965. (setq buffer-read-only nil)
  3966. (let ((org-startup-truncated t)
  3967. (org-startup-folded nil)
  3968. (org-startup-align-all-tables nil))
  3969. (org-mode)
  3970. (org-overview))
  3971. (setq buffer-read-only t)
  3972. (if (and (boundp 'org-goto-start-pos)
  3973. (integer-or-marker-p org-goto-start-pos))
  3974. (let ((org-show-hierarchy-above t)
  3975. (org-show-siblings t)
  3976. (org-show-following-heading t))
  3977. (goto-char org-goto-start-pos)
  3978. (and (org-invisible-p) (org-show-context)))
  3979. (goto-char (point-min)))
  3980. (org-beginning-of-line)
  3981. (message "Select location and press RET")
  3982. (use-local-map org-goto-map)
  3983. (recursive-edit)
  3984. ))
  3985. (kill-buffer "*org-goto*")
  3986. (cons org-goto-selected-point org-goto-exit-command)))
  3987. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  3988. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  3989. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  3990. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  3991. (defun org-goto-local-search-headings (string bound noerror)
  3992. "Search and make sure that any matches are in headlines."
  3993. (catch 'return
  3994. (while (if isearch-forward
  3995. (search-forward string bound noerror)
  3996. (search-backward string bound noerror))
  3997. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  3998. (and (member :headline context)
  3999. (not (member :tags context))))
  4000. (throw 'return (point))))))
  4001. (defun org-goto-local-auto-isearch ()
  4002. "Start isearch."
  4003. (interactive)
  4004. (goto-char (point-min))
  4005. (let ((keys (this-command-keys)))
  4006. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  4007. (isearch-mode t)
  4008. (isearch-process-search-char (string-to-char keys)))))
  4009. (defun org-goto-ret (&optional arg)
  4010. "Finish `org-goto' by going to the new location."
  4011. (interactive "P")
  4012. (setq org-goto-selected-point (point)
  4013. org-goto-exit-command 'return)
  4014. (throw 'exit nil))
  4015. (defun org-goto-left ()
  4016. "Finish `org-goto' by going to the new location."
  4017. (interactive)
  4018. (if (org-on-heading-p)
  4019. (progn
  4020. (beginning-of-line 1)
  4021. (setq org-goto-selected-point (point)
  4022. org-goto-exit-command 'left)
  4023. (throw 'exit nil))
  4024. (error "Not on a heading")))
  4025. (defun org-goto-right ()
  4026. "Finish `org-goto' by going to the new location."
  4027. (interactive)
  4028. (if (org-on-heading-p)
  4029. (progn
  4030. (setq org-goto-selected-point (point)
  4031. org-goto-exit-command 'right)
  4032. (throw 'exit nil))
  4033. (error "Not on a heading")))
  4034. (defun org-goto-quit ()
  4035. "Finish `org-goto' without cursor motion."
  4036. (interactive)
  4037. (setq org-goto-selected-point nil)
  4038. (setq org-goto-exit-command 'quit)
  4039. (throw 'exit nil))
  4040. ;;; Indirect buffer display of subtrees
  4041. (defvar org-indirect-dedicated-frame nil
  4042. "This is the frame being used for indirect tree display.")
  4043. (defvar org-last-indirect-buffer nil)
  4044. (defun org-tree-to-indirect-buffer (&optional arg)
  4045. "Create indirect buffer and narrow it to current subtree.
  4046. With numerical prefix ARG, go up to this level and then take that tree.
  4047. If ARG is negative, go up that many levels.
  4048. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  4049. indirect buffer previously made with this command, to avoid proliferation of
  4050. indirect buffers. However, when you call the command with a `C-u' prefix, or
  4051. when `org-indirect-buffer-display' is `new-frame', the last buffer
  4052. is kept so that you can work with several indirect buffers at the same time.
  4053. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  4054. requests that a new frame be made for the new buffer, so that the dedicated
  4055. frame is not changed."
  4056. (interactive "P")
  4057. (let ((cbuf (current-buffer))
  4058. (cwin (selected-window))
  4059. (pos (point))
  4060. beg end level heading ibuf)
  4061. (save-excursion
  4062. (org-back-to-heading t)
  4063. (when (numberp arg)
  4064. (setq level (org-outline-level))
  4065. (if (< arg 0) (setq arg (+ level arg)))
  4066. (while (> (setq level (org-outline-level)) arg)
  4067. (outline-up-heading 1 t)))
  4068. (setq beg (point)
  4069. heading (org-get-heading))
  4070. (org-end-of-subtree t) (setq end (point)))
  4071. (if (and (buffer-live-p org-last-indirect-buffer)
  4072. (not (eq org-indirect-buffer-display 'new-frame))
  4073. (not arg))
  4074. (kill-buffer org-last-indirect-buffer))
  4075. (setq ibuf (org-get-indirect-buffer cbuf)
  4076. org-last-indirect-buffer ibuf)
  4077. (cond
  4078. ((or (eq org-indirect-buffer-display 'new-frame)
  4079. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  4080. (select-frame (make-frame))
  4081. (delete-other-windows)
  4082. (switch-to-buffer ibuf)
  4083. (org-set-frame-title heading))
  4084. ((eq org-indirect-buffer-display 'dedicated-frame)
  4085. (raise-frame
  4086. (select-frame (or (and org-indirect-dedicated-frame
  4087. (frame-live-p org-indirect-dedicated-frame)
  4088. org-indirect-dedicated-frame)
  4089. (setq org-indirect-dedicated-frame (make-frame)))))
  4090. (delete-other-windows)
  4091. (switch-to-buffer ibuf)
  4092. (org-set-frame-title (concat "Indirect: " heading)))
  4093. ((eq org-indirect-buffer-display 'current-window)
  4094. (switch-to-buffer ibuf))
  4095. ((eq org-indirect-buffer-display 'other-window)
  4096. (pop-to-buffer ibuf))
  4097. (t (error "Invalid value.")))
  4098. (if (featurep 'xemacs)
  4099. (save-excursion (org-mode) (turn-on-font-lock)))
  4100. (narrow-to-region beg end)
  4101. (show-all)
  4102. (goto-char pos)
  4103. (and (window-live-p cwin) (select-window cwin))))
  4104. (defun org-get-indirect-buffer (&optional buffer)
  4105. (setq buffer (or buffer (current-buffer)))
  4106. (let ((n 1) (base (buffer-name buffer)) bname)
  4107. (while (buffer-live-p
  4108. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  4109. (setq n (1+ n)))
  4110. (condition-case nil
  4111. (make-indirect-buffer buffer bname 'clone)
  4112. (error (make-indirect-buffer buffer bname)))))
  4113. (defun org-set-frame-title (title)
  4114. "Set the title of the current frame to the string TITLE."
  4115. ;; FIXME: how to name a single frame in XEmacs???
  4116. (unless (featurep 'xemacs)
  4117. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  4118. ;;;; Structure editing
  4119. ;;; Inserting headlines
  4120. (defun org-insert-heading (&optional force-heading)
  4121. "Insert a new heading or item with same depth at point.
  4122. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  4123. If point is at the beginning of a headline, insert a sibling before the
  4124. current headline. If point is not at the beginning, do not split the line,
  4125. but create the new hedline after the current line."
  4126. (interactive "P")
  4127. (if (= (buffer-size) 0)
  4128. (insert "\n* ")
  4129. (when (or force-heading (not (org-insert-item)))
  4130. (let* ((head (save-excursion
  4131. (condition-case nil
  4132. (progn
  4133. (org-back-to-heading)
  4134. (match-string 0))
  4135. (error "*"))))
  4136. (blank (cdr (assq 'heading org-blank-before-new-entry)))
  4137. pos hide-previous)
  4138. (cond
  4139. ((and (org-on-heading-p) (bolp)
  4140. (or (bobp)
  4141. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  4142. ;; insert before the current line
  4143. (open-line (if blank 2 1)))
  4144. ((and (bolp)
  4145. (or (bobp)
  4146. (save-excursion
  4147. (backward-char 1) (not (org-invisible-p)))))
  4148. ;; insert right here
  4149. nil)
  4150. (t
  4151. ;; in the middle of the line
  4152. (save-excursion
  4153. (end-of-line)
  4154. (setq hide-previous (org-invisible-p)))
  4155. (org-show-entry)
  4156. (let ((split
  4157. (org-get-alist-option org-M-RET-may-split-line 'headline))
  4158. tags pos)
  4159. (cond
  4160. (org-insert-heading-respect-content
  4161. (org-end-of-subtree nil t)
  4162. (open-line 1))
  4163. ((org-on-heading-p)
  4164. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  4165. (setq tags (and (match-end 2) (match-string 2)))
  4166. (and (match-end 1)
  4167. (delete-region (match-beginning 1) (match-end 1)))
  4168. (setq pos (point-at-bol))
  4169. (or split (end-of-line 1))
  4170. (delete-horizontal-space)
  4171. (newline (if blank 2 1))
  4172. (when tags
  4173. (save-excursion
  4174. (goto-char pos)
  4175. (end-of-line 1)
  4176. (insert " " tags)
  4177. (org-set-tags nil 'align))))
  4178. (t
  4179. (or split (end-of-line 1))
  4180. (newline (if blank 2 1)))))))
  4181. (insert head) (just-one-space)
  4182. (setq pos (point))
  4183. (end-of-line 1)
  4184. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  4185. (when (and org-insert-heading-respect-content hide-previous)
  4186. (save-excursion
  4187. (outline-previous-visible-heading 1)
  4188. (hide-entry)))
  4189. (run-hooks 'org-insert-heading-hook)))))
  4190. (defun org-get-heading (&optional no-tags)
  4191. "Return the heading of the current entry, without the stars."
  4192. (save-excursion
  4193. (org-back-to-heading t)
  4194. (if (looking-at
  4195. (if no-tags
  4196. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  4197. "\\*+[ \t]+\\([^\r\n]*\\)"))
  4198. (match-string 1) "")))
  4199. (defun org-insert-heading-after-current ()
  4200. "Insert a new heading with same level as current, after current subtree."
  4201. (interactive)
  4202. (org-back-to-heading)
  4203. (org-insert-heading)
  4204. (org-move-subtree-down)
  4205. (end-of-line 1))
  4206. (defun org-insert-heading-respect-content ()
  4207. (interactive)
  4208. (let ((org-insert-heading-respect-content t))
  4209. (org-insert-heading t)))
  4210. (defun org-insert-todo-heading-respect-content (&optional force-state)
  4211. (interactive "P")
  4212. (let ((org-insert-heading-respect-content t))
  4213. (org-insert-todo-heading force-state t)))
  4214. (defun org-insert-todo-heading (arg &optional force-heading)
  4215. "Insert a new heading with the same level and TODO state as current heading.
  4216. If the heading has no TODO state, or if the state is DONE, use the first
  4217. state (TODO by default). Also with prefix arg, force first state."
  4218. (interactive "P")
  4219. (when (or force-heading (not (org-insert-item 'checkbox)))
  4220. (org-insert-heading force-heading)
  4221. (save-excursion
  4222. (org-back-to-heading)
  4223. (outline-previous-heading)
  4224. (looking-at org-todo-line-regexp))
  4225. (if (or arg
  4226. (not (match-beginning 2))
  4227. (member (match-string 2) org-done-keywords))
  4228. (insert (car org-todo-keywords-1) " ")
  4229. (insert (match-string 2) " "))
  4230. (when org-provide-todo-statistics
  4231. (org-update-parent-todo-statistics))))
  4232. (defun org-insert-subheading (arg)
  4233. "Insert a new subheading and demote it.
  4234. Works for outline headings and for plain lists alike."
  4235. (interactive "P")
  4236. (org-insert-heading arg)
  4237. (cond
  4238. ((org-on-heading-p) (org-do-demote))
  4239. ((org-at-item-p) (org-indent-item 1))))
  4240. (defun org-insert-todo-subheading (arg)
  4241. "Insert a new subheading with TODO keyword or checkbox and demote it.
  4242. Works for outline headings and for plain lists alike."
  4243. (interactive "P")
  4244. (org-insert-todo-heading arg)
  4245. (cond
  4246. ((org-on-heading-p) (org-do-demote))
  4247. ((org-at-item-p) (org-indent-item 1))))
  4248. ;;; Promotion and Demotion
  4249. (defun org-promote-subtree ()
  4250. "Promote the entire subtree.
  4251. See also `org-promote'."
  4252. (interactive)
  4253. (save-excursion
  4254. (org-map-tree 'org-promote))
  4255. (org-fix-position-after-promote))
  4256. (defun org-demote-subtree ()
  4257. "Demote the entire subtree. See `org-demote'.
  4258. See also `org-promote'."
  4259. (interactive)
  4260. (save-excursion
  4261. (org-map-tree 'org-demote))
  4262. (org-fix-position-after-promote))
  4263. (defun org-do-promote ()
  4264. "Promote the current heading higher up the tree.
  4265. If the region is active in `transient-mark-mode', promote all headings
  4266. in the region."
  4267. (interactive)
  4268. (save-excursion
  4269. (if (org-region-active-p)
  4270. (org-map-region 'org-promote (region-beginning) (region-end))
  4271. (org-promote)))
  4272. (org-fix-position-after-promote))
  4273. (defun org-do-demote ()
  4274. "Demote the current heading lower down the tree.
  4275. If the region is active in `transient-mark-mode', demote all headings
  4276. in the region."
  4277. (interactive)
  4278. (save-excursion
  4279. (if (org-region-active-p)
  4280. (org-map-region 'org-demote (region-beginning) (region-end))
  4281. (org-demote)))
  4282. (org-fix-position-after-promote))
  4283. (defun org-fix-position-after-promote ()
  4284. "Make sure that after pro/demotion cursor position is right."
  4285. (let ((pos (point)))
  4286. (when (save-excursion
  4287. (beginning-of-line 1)
  4288. (looking-at org-todo-line-regexp)
  4289. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  4290. (cond ((eobp) (insert " "))
  4291. ((eolp) (insert " "))
  4292. ((equal (char-after) ?\ ) (forward-char 1))))))
  4293. (defun org-reduced-level (l)
  4294. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  4295. (defun org-get-valid-level (level &optional change)
  4296. "Rectify a level change under the influence of `org-odd-levels-only'
  4297. LEVEL is a current level, CHANGE is by how much the level should be
  4298. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  4299. even level numbers will become the next higher odd number."
  4300. (if org-odd-levels-only
  4301. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  4302. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  4303. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  4304. (max 1 (+ level change))))
  4305. (if (boundp 'define-obsolete-function-alias)
  4306. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  4307. (define-obsolete-function-alias 'org-get-legal-level
  4308. 'org-get-valid-level)
  4309. (define-obsolete-function-alias 'org-get-legal-level
  4310. 'org-get-valid-level "23.1")))
  4311. (defun org-promote ()
  4312. "Promote the current heading higher up the tree.
  4313. If the region is active in `transient-mark-mode', promote all headings
  4314. in the region."
  4315. (org-back-to-heading t)
  4316. (let* ((level (save-match-data (funcall outline-level)))
  4317. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  4318. (diff (abs (- level (length up-head) -1))))
  4319. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  4320. (replace-match up-head nil t)
  4321. ;; Fixup tag positioning
  4322. (and org-auto-align-tags (org-set-tags nil t))
  4323. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  4324. (defun org-demote ()
  4325. "Demote the current heading lower down the tree.
  4326. If the region is active in `transient-mark-mode', demote all headings
  4327. in the region."
  4328. (org-back-to-heading t)
  4329. (let* ((level (save-match-data (funcall outline-level)))
  4330. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  4331. (diff (abs (- level (length down-head) -1))))
  4332. (replace-match down-head nil t)
  4333. ;; Fixup tag positioning
  4334. (and org-auto-align-tags (org-set-tags nil t))
  4335. (if org-adapt-indentation (org-fixup-indentation diff))))
  4336. (defun org-map-tree (fun)
  4337. "Call FUN for every heading underneath the current one."
  4338. (org-back-to-heading)
  4339. (let ((level (funcall outline-level)))
  4340. (save-excursion
  4341. (funcall fun)
  4342. (while (and (progn
  4343. (outline-next-heading)
  4344. (> (funcall outline-level) level))
  4345. (not (eobp)))
  4346. (funcall fun)))))
  4347. (defun org-map-region (fun beg end)
  4348. "Call FUN for every heading between BEG and END."
  4349. (let ((org-ignore-region t))
  4350. (save-excursion
  4351. (setq end (copy-marker end))
  4352. (goto-char beg)
  4353. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  4354. (< (point) end))
  4355. (funcall fun))
  4356. (while (and (progn
  4357. (outline-next-heading)
  4358. (< (point) end))
  4359. (not (eobp)))
  4360. (funcall fun)))))
  4361. (defun org-fixup-indentation (diff)
  4362. "Change the indentation in the current entry by DIFF
  4363. However, if any line in the current entry has no indentation, or if it
  4364. would end up with no indentation after the change, nothing at all is done."
  4365. (save-excursion
  4366. (let ((end (save-excursion (outline-next-heading)
  4367. (point-marker)))
  4368. (prohibit (if (> diff 0)
  4369. "^\\S-"
  4370. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  4371. col)
  4372. (unless (save-excursion (end-of-line 1)
  4373. (re-search-forward prohibit end t))
  4374. (while (and (< (point) end)
  4375. (re-search-forward "^[ \t]+" end t))
  4376. (goto-char (match-end 0))
  4377. (setq col (current-column))
  4378. (if (< diff 0) (replace-match ""))
  4379. (indent-to (+ diff col))))
  4380. (move-marker end nil))))
  4381. (defun org-convert-to-odd-levels ()
  4382. "Convert an org-mode file with all levels allowed to one with odd levels.
  4383. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  4384. level 5 etc."
  4385. (interactive)
  4386. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  4387. (let ((org-odd-levels-only nil) n)
  4388. (save-excursion
  4389. (goto-char (point-min))
  4390. (while (re-search-forward "^\\*\\*+ " nil t)
  4391. (setq n (- (length (match-string 0)) 2))
  4392. (while (>= (setq n (1- n)) 0)
  4393. (org-demote))
  4394. (end-of-line 1))))))
  4395. (defun org-convert-to-oddeven-levels ()
  4396. "Convert an org-mode file with only odd levels to one with odd and even levels.
  4397. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  4398. section with an even level, conversion would destroy the structure of the file. An error
  4399. is signaled in this case."
  4400. (interactive)
  4401. (goto-char (point-min))
  4402. ;; First check if there are no even levels
  4403. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  4404. (org-show-context t)
  4405. (error "Not all levels are odd in this file. Conversion not possible."))
  4406. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  4407. (let ((org-odd-levels-only nil) n)
  4408. (save-excursion
  4409. (goto-char (point-min))
  4410. (while (re-search-forward "^\\*\\*+ " nil t)
  4411. (setq n (/ (1- (length (match-string 0))) 2))
  4412. (while (>= (setq n (1- n)) 0)
  4413. (org-promote))
  4414. (end-of-line 1))))))
  4415. (defun org-tr-level (n)
  4416. "Make N odd if required."
  4417. (if org-odd-levels-only (1+ (/ n 2)) n))
  4418. ;;; Vertical tree motion, cutting and pasting of subtrees
  4419. (defun org-move-subtree-up (&optional arg)
  4420. "Move the current subtree up past ARG headlines of the same level."
  4421. (interactive "p")
  4422. (org-move-subtree-down (- (prefix-numeric-value arg))))
  4423. (defun org-move-subtree-down (&optional arg)
  4424. "Move the current subtree down past ARG headlines of the same level."
  4425. (interactive "p")
  4426. (setq arg (prefix-numeric-value arg))
  4427. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  4428. 'outline-get-last-sibling))
  4429. (ins-point (make-marker))
  4430. (cnt (abs arg))
  4431. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  4432. ;; Select the tree
  4433. (org-back-to-heading)
  4434. (setq beg0 (point))
  4435. (save-excursion
  4436. (setq ne-beg (org-back-over-empty-lines))
  4437. (setq beg (point)))
  4438. (save-match-data
  4439. (save-excursion (outline-end-of-heading)
  4440. (setq folded (org-invisible-p)))
  4441. (outline-end-of-subtree))
  4442. (outline-next-heading)
  4443. (setq ne-end (org-back-over-empty-lines))
  4444. (setq end (point))
  4445. (goto-char beg0)
  4446. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  4447. ;; include less whitespace
  4448. (save-excursion
  4449. (goto-char beg)
  4450. (forward-line (- ne-beg ne-end))
  4451. (setq beg (point))))
  4452. ;; Find insertion point, with error handling
  4453. (while (> cnt 0)
  4454. (or (and (funcall movfunc) (looking-at outline-regexp))
  4455. (progn (goto-char beg0)
  4456. (error "Cannot move past superior level or buffer limit")))
  4457. (setq cnt (1- cnt)))
  4458. (if (> arg 0)
  4459. ;; Moving forward - still need to move over subtree
  4460. (progn (org-end-of-subtree t t)
  4461. (save-excursion
  4462. (org-back-over-empty-lines)
  4463. (or (bolp) (newline)))))
  4464. (setq ne-ins (org-back-over-empty-lines))
  4465. (move-marker ins-point (point))
  4466. (setq txt (buffer-substring beg end))
  4467. (org-save-markers-in-region beg end)
  4468. (delete-region beg end)
  4469. (outline-flag-region (1- beg) beg nil)
  4470. (outline-flag-region (1- (point)) (point) nil)
  4471. (let ((bbb (point)))
  4472. (insert-before-markers txt)
  4473. (org-reinstall-markers-in-region bbb)
  4474. (move-marker ins-point bbb))
  4475. (or (bolp) (insert "\n"))
  4476. (setq ins-end (point))
  4477. (goto-char ins-point)
  4478. (org-skip-whitespace)
  4479. (when (and (< arg 0)
  4480. (org-first-sibling-p)
  4481. (> ne-ins ne-beg))
  4482. ;; Move whitespace back to beginning
  4483. (save-excursion
  4484. (goto-char ins-end)
  4485. (let ((kill-whole-line t))
  4486. (kill-line (- ne-ins ne-beg)) (point)))
  4487. (insert (make-string (- ne-ins ne-beg) ?\n)))
  4488. (move-marker ins-point nil)
  4489. (org-compact-display-after-subtree-move)
  4490. (org-show-empty-lines-in-parent)
  4491. (unless folded
  4492. (org-show-entry)
  4493. (show-children)
  4494. (org-cycle-hide-drawers 'children))))
  4495. (defvar org-subtree-clip ""
  4496. "Clipboard for cut and paste of subtrees.
  4497. This is actually only a copy of the kill, because we use the normal kill
  4498. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  4499. (defvar org-subtree-clip-folded nil
  4500. "Was the last copied subtree folded?
  4501. This is used to fold the tree back after pasting.")
  4502. (defun org-cut-subtree (&optional n)
  4503. "Cut the current subtree into the clipboard.
  4504. With prefix arg N, cut this many sequential subtrees.
  4505. This is a short-hand for marking the subtree and then cutting it."
  4506. (interactive "p")
  4507. (org-copy-subtree n 'cut))
  4508. (defun org-copy-subtree (&optional n cut force-store-markers)
  4509. "Cut the current subtree into the clipboard.
  4510. With prefix arg N, cut this many sequential subtrees.
  4511. This is a short-hand for marking the subtree and then copying it.
  4512. If CUT is non-nil, actually cut the subtree.
  4513. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  4514. of some markers in the region, even if CUT is non-nil. This is
  4515. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  4516. (interactive "p")
  4517. (let (beg end folded (beg0 (point)))
  4518. (if (interactive-p)
  4519. (org-back-to-heading nil) ; take what looks like a subtree
  4520. (org-back-to-heading t)) ; take what is really there
  4521. (org-back-over-empty-lines)
  4522. (setq beg (point))
  4523. (skip-chars-forward " \t\r\n")
  4524. (save-match-data
  4525. (save-excursion (outline-end-of-heading)
  4526. (setq folded (org-invisible-p)))
  4527. (condition-case nil
  4528. (outline-forward-same-level (1- n))
  4529. (error nil))
  4530. (org-end-of-subtree t t))
  4531. (org-back-over-empty-lines)
  4532. (setq end (point))
  4533. (goto-char beg0)
  4534. (when (> end beg)
  4535. (setq org-subtree-clip-folded folded)
  4536. (when (or cut force-store-markers)
  4537. (org-save-markers-in-region beg end))
  4538. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  4539. (setq org-subtree-clip (current-kill 0))
  4540. (message "%s: Subtree(s) with %d characters"
  4541. (if cut "Cut" "Copied")
  4542. (length org-subtree-clip)))))
  4543. (defun org-paste-subtree (&optional level tree for-yank)
  4544. "Paste the clipboard as a subtree, with modification of headline level.
  4545. The entire subtree is promoted or demoted in order to match a new headline
  4546. level.
  4547. If the cursor is at the beginning of a headline, the same level as
  4548. that headline is used to paste the tree
  4549. If not, the new level is derived from the *visible* headings
  4550. before and after the insertion point, and taken to be the inferior headline
  4551. level of the two. So if the previous visible heading is level 3 and the
  4552. next is level 4 (or vice versa), level 4 will be used for insertion.
  4553. This makes sure that the subtree remains an independent subtree and does
  4554. not swallow low level entries.
  4555. You can also force a different level, either by using a numeric prefix
  4556. argument, or by inserting the heading marker by hand. For example, if the
  4557. cursor is after \"*****\", then the tree will be shifted to level 5.
  4558. If optional TREE is given, use this text instead of the kill ring.
  4559. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  4560. move back over whitespace before inserting, and move point to the end of
  4561. the inserted text when done."
  4562. (interactive "P")
  4563. (unless (org-kill-is-subtree-p tree)
  4564. (error "%s"
  4565. (substitute-command-keys
  4566. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  4567. (let* ((visp (not (org-invisible-p)))
  4568. (txt (or tree (and kill-ring (current-kill 0))))
  4569. (^re (concat "^\\(" outline-regexp "\\)"))
  4570. (re (concat "\\(" outline-regexp "\\)"))
  4571. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  4572. (old-level (if (string-match ^re txt)
  4573. (- (match-end 0) (match-beginning 0) 1)
  4574. -1))
  4575. (force-level (cond (level (prefix-numeric-value level))
  4576. ((and (looking-at "[ \t]*$")
  4577. (string-match
  4578. ^re_ (buffer-substring
  4579. (point-at-bol) (point))))
  4580. (- (match-end 1) (match-beginning 1)))
  4581. ((and (bolp)
  4582. (looking-at org-outline-regexp))
  4583. (- (match-end 0) (point) 1))
  4584. (t nil)))
  4585. (previous-level (save-excursion
  4586. (condition-case nil
  4587. (progn
  4588. (outline-previous-visible-heading 1)
  4589. (if (looking-at re)
  4590. (- (match-end 0) (match-beginning 0) 1)
  4591. 1))
  4592. (error 1))))
  4593. (next-level (save-excursion
  4594. (condition-case nil
  4595. (progn
  4596. (or (looking-at outline-regexp)
  4597. (outline-next-visible-heading 1))
  4598. (if (looking-at re)
  4599. (- (match-end 0) (match-beginning 0) 1)
  4600. 1))
  4601. (error 1))))
  4602. (new-level (or force-level (max previous-level next-level)))
  4603. (shift (if (or (= old-level -1)
  4604. (= new-level -1)
  4605. (= old-level new-level))
  4606. 0
  4607. (- new-level old-level)))
  4608. (delta (if (> shift 0) -1 1))
  4609. (func (if (> shift 0) 'org-demote 'org-promote))
  4610. (org-odd-levels-only nil)
  4611. beg end newend)
  4612. ;; Remove the forced level indicator
  4613. (if force-level
  4614. (delete-region (point-at-bol) (point)))
  4615. ;; Paste
  4616. (beginning-of-line 1)
  4617. (unless for-yank (org-back-over-empty-lines))
  4618. (setq beg (point))
  4619. (insert-before-markers txt)
  4620. (unless (string-match "\n\\'" txt) (insert "\n"))
  4621. (setq newend (point))
  4622. (org-reinstall-markers-in-region beg)
  4623. (setq end (point))
  4624. (goto-char beg)
  4625. (skip-chars-forward " \t\n\r")
  4626. (setq beg (point))
  4627. (if (and (org-invisible-p) visp)
  4628. (save-excursion (outline-show-heading)))
  4629. ;; Shift if necessary
  4630. (unless (= shift 0)
  4631. (save-restriction
  4632. (narrow-to-region beg end)
  4633. (while (not (= shift 0))
  4634. (org-map-region func (point-min) (point-max))
  4635. (setq shift (+ delta shift)))
  4636. (goto-char (point-min))
  4637. (setq newend (point-max))))
  4638. (when (or (interactive-p) for-yank)
  4639. (message "Clipboard pasted as level %d subtree" new-level))
  4640. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  4641. kill-ring
  4642. (eq org-subtree-clip (current-kill 0))
  4643. org-subtree-clip-folded)
  4644. ;; The tree was folded before it was killed/copied
  4645. (hide-subtree))
  4646. (and for-yank (goto-char newend))))
  4647. (defun org-kill-is-subtree-p (&optional txt)
  4648. "Check if the current kill is an outline subtree, or a set of trees.
  4649. Returns nil if kill does not start with a headline, or if the first
  4650. headline level is not the largest headline level in the tree.
  4651. So this will actually accept several entries of equal levels as well,
  4652. which is OK for `org-paste-subtree'.
  4653. If optional TXT is given, check this string instead of the current kill."
  4654. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  4655. (start-level (and kill
  4656. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  4657. org-outline-regexp "\\)")
  4658. kill)
  4659. (- (match-end 2) (match-beginning 2) 1)))
  4660. (re (concat "^" org-outline-regexp))
  4661. (start (1+ (or (match-beginning 2) -1))))
  4662. (if (not start-level)
  4663. (progn
  4664. nil) ;; does not even start with a heading
  4665. (catch 'exit
  4666. (while (setq start (string-match re kill (1+ start)))
  4667. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  4668. (throw 'exit nil)))
  4669. t))))
  4670. (defvar org-markers-to-move nil
  4671. "Markers that should be moved with a cut-and-paste operation.
  4672. Those markers are stored together with their positions relative to
  4673. the start of the region.")
  4674. (defun org-save-markers-in-region (beg end)
  4675. "Check markers in region.
  4676. If these markers are between BEG and END, record their position relative
  4677. to BEG, so that after moving the block of text, we can put the markers back
  4678. into place.
  4679. This function gets called just before an entry or tree gets cut from the
  4680. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  4681. called immediately, to move the markers with the entries."
  4682. (setq org-markers-to-move nil)
  4683. (when (featurep 'org-clock)
  4684. (org-clock-save-markers-for-cut-and-paste beg end))
  4685. (when (featurep 'org-agenda)
  4686. (org-agenda-save-markers-for-cut-and-paste beg end)))
  4687. (defun org-check-and-save-marker (marker beg end)
  4688. "Check if MARKER is between BEG and END.
  4689. If yes, remember the marker and the distance to BEG."
  4690. (when (and (marker-buffer marker)
  4691. (equal (marker-buffer marker) (current-buffer)))
  4692. (if (and (>= marker beg) (< marker end))
  4693. (push (cons marker (- marker beg)) org-markers-to-move))))
  4694. (defun org-reinstall-markers-in-region (beg)
  4695. "Move all remembered markers to their position relative to BEG."
  4696. (mapc (lambda (x)
  4697. (move-marker (car x) (+ beg (cdr x))))
  4698. org-markers-to-move)
  4699. (setq org-markers-to-move nil))
  4700. (defun org-narrow-to-subtree ()
  4701. "Narrow buffer to the current subtree."
  4702. (interactive)
  4703. (save-excursion
  4704. (save-match-data
  4705. (narrow-to-region
  4706. (progn (org-back-to-heading) (point))
  4707. (progn (org-end-of-subtree t) (point))))))
  4708. ;;; Outline Sorting
  4709. (defun org-sort (with-case)
  4710. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  4711. Optional argument WITH-CASE means sort case-sensitively."
  4712. (interactive "P")
  4713. (if (org-at-table-p)
  4714. (org-call-with-arg 'org-table-sort-lines with-case)
  4715. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  4716. (defun org-sort-remove-invisible (s)
  4717. (remove-text-properties 0 (length s) org-rm-props s)
  4718. (while (string-match org-bracket-link-regexp s)
  4719. (setq s (replace-match (if (match-end 2)
  4720. (match-string 3 s)
  4721. (match-string 1 s)) t t s)))
  4722. s)
  4723. (defvar org-priority-regexp) ; defined later in the file
  4724. (defun org-sort-entries-or-items (&optional with-case sorting-type getkey-func property)
  4725. "Sort entries on a certain level of an outline tree.
  4726. If there is an active region, the entries in the region are sorted.
  4727. Else, if the cursor is before the first entry, sort the top-level items.
  4728. Else, the children of the entry at point are sorted.
  4729. Sorting can be alphabetically, numerically, and by date/time as given by
  4730. the first time stamp in the entry. The command prompts for the sorting
  4731. type unless it has been given to the function through the SORTING-TYPE
  4732. argument, which needs to a character, any of (?n ?N ?a ?A ?t ?T ?p ?P ?f ?F).
  4733. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  4734. called with point at the beginning of the record. It must return either
  4735. a string or a number that should serve as the sorting key for that record.
  4736. Comparing entries ignores case by default. However, with an optional argument
  4737. WITH-CASE, the sorting considers case as well."
  4738. (interactive "P")
  4739. (let ((case-func (if with-case 'identity 'downcase))
  4740. start beg end stars re re2
  4741. txt what tmp plain-list-p)
  4742. ;; Find beginning and end of region to sort
  4743. (cond
  4744. ((org-region-active-p)
  4745. ;; we will sort the region
  4746. (setq end (region-end)
  4747. what "region")
  4748. (goto-char (region-beginning))
  4749. (if (not (org-on-heading-p)) (outline-next-heading))
  4750. (setq start (point)))
  4751. ((org-at-item-p)
  4752. ;; we will sort this plain list
  4753. (org-beginning-of-item-list) (setq start (point))
  4754. (org-end-of-item-list) (setq end (point))
  4755. (goto-char start)
  4756. (setq plain-list-p t
  4757. what "plain list"))
  4758. ((or (org-on-heading-p)
  4759. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  4760. ;; we will sort the children of the current headline
  4761. (org-back-to-heading)
  4762. (setq start (point)
  4763. end (progn (org-end-of-subtree t t)
  4764. (org-back-over-empty-lines)
  4765. (point))
  4766. what "children")
  4767. (goto-char start)
  4768. (show-subtree)
  4769. (outline-next-heading))
  4770. (t
  4771. ;; we will sort the top-level entries in this file
  4772. (goto-char (point-min))
  4773. (or (org-on-heading-p) (outline-next-heading))
  4774. (setq start (point) end (point-max) what "top-level")
  4775. (goto-char start)
  4776. (show-all)))
  4777. (setq beg (point))
  4778. (if (>= beg end) (error "Nothing to sort"))
  4779. (unless plain-list-p
  4780. (looking-at "\\(\\*+\\)")
  4781. (setq stars (match-string 1)
  4782. re (concat "^" (regexp-quote stars) " +")
  4783. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  4784. txt (buffer-substring beg end))
  4785. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  4786. (if (and (not (equal stars "*")) (string-match re2 txt))
  4787. (error "Region to sort contains a level above the first entry")))
  4788. (unless sorting-type
  4789. (message
  4790. (if plain-list-p
  4791. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  4792. "Sort %s: [a]lpha [n]umeric [t]ime [p]riority p[r]operty todo[o]rder [f]unc A/N/T/P/O/F means reversed:")
  4793. what)
  4794. (setq sorting-type (read-char-exclusive))
  4795. (and (= (downcase sorting-type) ?f)
  4796. (setq getkey-func
  4797. (completing-read "Sort using function: "
  4798. obarray 'fboundp t nil nil))
  4799. (setq getkey-func (intern getkey-func)))
  4800. (and (= (downcase sorting-type) ?r)
  4801. (setq property
  4802. (completing-read "Property: "
  4803. (mapcar 'list (org-buffer-property-keys t))
  4804. nil t))))
  4805. (message "Sorting entries...")
  4806. (save-restriction
  4807. (narrow-to-region start end)
  4808. (let ((dcst (downcase sorting-type))
  4809. (now (current-time)))
  4810. (sort-subr
  4811. (/= dcst sorting-type)
  4812. ;; This function moves to the beginning character of the "record" to
  4813. ;; be sorted.
  4814. (if plain-list-p
  4815. (lambda nil
  4816. (if (org-at-item-p) t (goto-char (point-max))))
  4817. (lambda nil
  4818. (if (re-search-forward re nil t)
  4819. (goto-char (match-beginning 0))
  4820. (goto-char (point-max)))))
  4821. ;; This function moves to the last character of the "record" being
  4822. ;; sorted.
  4823. (if plain-list-p
  4824. 'org-end-of-item
  4825. (lambda nil
  4826. (save-match-data
  4827. (condition-case nil
  4828. (outline-forward-same-level 1)
  4829. (error
  4830. (goto-char (point-max)))))))
  4831. ;; This function returns the value that gets sorted against.
  4832. (if plain-list-p
  4833. (lambda nil
  4834. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  4835. (cond
  4836. ((= dcst ?n)
  4837. (string-to-number (buffer-substring (match-end 0)
  4838. (point-at-eol))))
  4839. ((= dcst ?a)
  4840. (buffer-substring (match-end 0) (point-at-eol)))
  4841. ((= dcst ?t)
  4842. (if (re-search-forward org-ts-regexp
  4843. (point-at-eol) t)
  4844. (org-time-string-to-time (match-string 0))
  4845. now))
  4846. ((= dcst ?f)
  4847. (if getkey-func
  4848. (progn
  4849. (setq tmp (funcall getkey-func))
  4850. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  4851. tmp)
  4852. (error "Invalid key function `%s'" getkey-func)))
  4853. (t (error "Invalid sorting type `%c'" sorting-type)))))
  4854. (lambda nil
  4855. (cond
  4856. ((= dcst ?n)
  4857. (if (looking-at org-complex-heading-regexp)
  4858. (string-to-number (match-string 4))
  4859. nil))
  4860. ((= dcst ?a)
  4861. (if (looking-at org-complex-heading-regexp)
  4862. (funcall case-func (match-string 4))
  4863. nil))
  4864. ((= dcst ?t)
  4865. (if (re-search-forward org-ts-regexp
  4866. (save-excursion
  4867. (forward-line 2)
  4868. (point)) t)
  4869. (org-time-string-to-time (match-string 0))
  4870. now))
  4871. ((= dcst ?p)
  4872. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  4873. (string-to-char (match-string 2))
  4874. org-default-priority))
  4875. ((= dcst ?r)
  4876. (or (org-entry-get nil property) ""))
  4877. ((= dcst ?o)
  4878. (if (looking-at org-complex-heading-regexp)
  4879. (- 9999 (length (member (match-string 2)
  4880. org-todo-keywords-1)))))
  4881. ((= dcst ?f)
  4882. (if getkey-func
  4883. (progn
  4884. (setq tmp (funcall getkey-func))
  4885. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  4886. tmp)
  4887. (error "Invalid key function `%s'" getkey-func)))
  4888. (t (error "Invalid sorting type `%c'" sorting-type)))))
  4889. nil
  4890. (cond
  4891. ((= dcst ?a) 'string<)
  4892. ((= dcst ?t) 'time-less-p)
  4893. (t nil)))))
  4894. (message "Sorting entries...done")))
  4895. (defun org-do-sort (table what &optional with-case sorting-type)
  4896. "Sort TABLE of WHAT according to SORTING-TYPE.
  4897. The user will be prompted for the SORTING-TYPE if the call to this
  4898. function does not specify it. WHAT is only for the prompt, to indicate
  4899. what is being sorted. The sorting key will be extracted from
  4900. the car of the elements of the table.
  4901. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  4902. (unless sorting-type
  4903. (message
  4904. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  4905. what)
  4906. (setq sorting-type (read-char-exclusive)))
  4907. (let ((dcst (downcase sorting-type))
  4908. extractfun comparefun)
  4909. ;; Define the appropriate functions
  4910. (cond
  4911. ((= dcst ?n)
  4912. (setq extractfun 'string-to-number
  4913. comparefun (if (= dcst sorting-type) '< '>)))
  4914. ((= dcst ?a)
  4915. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  4916. (lambda(x) (downcase (org-sort-remove-invisible x))))
  4917. comparefun (if (= dcst sorting-type)
  4918. 'string<
  4919. (lambda (a b) (and (not (string< a b))
  4920. (not (string= a b)))))))
  4921. ((= dcst ?t)
  4922. (setq extractfun
  4923. (lambda (x)
  4924. (if (string-match org-ts-regexp x)
  4925. (time-to-seconds
  4926. (org-time-string-to-time (match-string 0 x)))
  4927. 0))
  4928. comparefun (if (= dcst sorting-type) '< '>)))
  4929. (t (error "Invalid sorting type `%c'" sorting-type)))
  4930. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  4931. table)
  4932. (lambda (a b) (funcall comparefun (car a) (car b))))))
  4933. ;;; Editing source examples
  4934. (defvar org-exit-edit-mode-map (make-sparse-keymap))
  4935. (define-key org-exit-edit-mode-map "\C-c'" 'org-edit-src-exit)
  4936. (defvar org-edit-src-force-single-line nil)
  4937. (defvar org-edit-src-from-org-mode nil)
  4938. (defvar org-edit-src-picture nil)
  4939. (define-minor-mode org-exit-edit-mode
  4940. "Minor mode installing a single key binding, \"C-c '\" to exit special edit.")
  4941. (defun org-edit-src-code ()
  4942. "Edit the source code example at point.
  4943. An indirect buffer is created, and that buffer is then narrowed to the
  4944. example at point and switched to the correct language mode. When done,
  4945. exit by killing the buffer with \\[org-edit-src-exit]."
  4946. (interactive)
  4947. (let ((line (org-current-line))
  4948. (case-fold-search t)
  4949. (msg (substitute-command-keys
  4950. "Edit, then exit with C-c ' (C-c and single quote)"))
  4951. (info (org-edit-src-find-region-and-lang))
  4952. (org-mode-p (eq major-mode 'org-mode))
  4953. beg end lang lang-f single)
  4954. (if (not info)
  4955. nil
  4956. (setq beg (nth 0 info)
  4957. end (nth 1 info)
  4958. lang (nth 2 info)
  4959. single (nth 3 info)
  4960. lang-f (intern (concat lang "-mode")))
  4961. (unless (functionp lang-f)
  4962. (error "No such language mode: %s" lang-f))
  4963. (goto-line line)
  4964. (if (get-buffer "*Org Edit Src Example*")
  4965. (kill-buffer "*Org Edit Src Example*"))
  4966. (switch-to-buffer (make-indirect-buffer (current-buffer)
  4967. "*Org Edit Src Example*"))
  4968. (narrow-to-region beg end)
  4969. (remove-text-properties beg end '(display nil invisible nil
  4970. intangible nil))
  4971. (let ((org-inhibit-startup t))
  4972. (funcall lang-f))
  4973. (set (make-local-variable 'org-edit-src-force-single-line) single)
  4974. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  4975. (when org-mode-p
  4976. (goto-char (point-min))
  4977. (while (re-search-forward "^," nil t)
  4978. (replace-match "")))
  4979. (goto-line line)
  4980. (org-exit-edit-mode)
  4981. (org-set-local 'header-line-format msg)
  4982. (message "%s" msg)
  4983. t)))
  4984. (defun org-edit-fixed-width-region ()
  4985. "Edit the fixed-width ascii drawing at point.
  4986. This must be a region where each line starts with ca colon followed by
  4987. a space character.
  4988. An indirect buffer is created, and that buffer is then narrowed to the
  4989. example at point and switched to artist-mode. When done,
  4990. exit by killing the buffer with \\[org-edit-src-exit]."
  4991. (interactive)
  4992. (let ((line (org-current-line))
  4993. (case-fold-search t)
  4994. (msg (substitute-command-keys
  4995. "Edit, then exit with C-c ' (C-c and single quote)"))
  4996. (org-mode-p (eq major-mode 'org-mode))
  4997. beg end lang lang-f)
  4998. (beginning-of-line 1)
  4999. (if (looking-at "[ \t]*[^:\n \t]")
  5000. nil
  5001. (if (looking-at "[ \t]*\\(\n\\|\\'\\)]")
  5002. (setq beg (point) end (match-end 0))
  5003. (save-excursion
  5004. (if (re-search-backward "^[ \t]*[^:]" nil 'move)
  5005. (setq beg (point-at-bol 2))
  5006. (setq beg (point))))
  5007. (save-excursion
  5008. (if (re-search-forward "^[ \t]*[^:]" nil 'move)
  5009. (setq end (1- (match-beginning 0)))
  5010. (setq end (point))))
  5011. (goto-line line)
  5012. (if (get-buffer "*Org Edit Picture*")
  5013. (kill-buffer "*Org Edit Picture*"))
  5014. (switch-to-buffer (make-indirect-buffer (current-buffer)
  5015. "*Org Edit Picture*"))
  5016. (narrow-to-region beg end)
  5017. (remove-text-properties beg end '(display nil invisible nil
  5018. intangible nil))
  5019. (when (fboundp 'font-lock-unfontify-region)
  5020. (font-lock-unfontify-region (point-min) (point-max)))
  5021. (cond
  5022. ((eq org-edit-fixed-width-region-mode 'artist-mode)
  5023. (fundamental-mode)
  5024. (artist-mode 1))
  5025. (t (funcall org-edit-fixed-width-region-mode)))
  5026. (set (make-local-variable 'org-edit-src-force-single-line) nil)
  5027. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  5028. (set (make-local-variable 'org-edit-src-picture) t)
  5029. (goto-char (point-min))
  5030. (while (re-search-forward "^[ \t]*: " nil t)
  5031. (replace-match ""))
  5032. (goto-line line)
  5033. (org-exit-edit-mode)
  5034. (org-set-local 'header-line-format msg)
  5035. (message "%s" msg)
  5036. t))))
  5037. (defun org-edit-src-find-region-and-lang ()
  5038. "Find the region and language for a local edit.
  5039. Return a list with beginning and end of the region, a string representing
  5040. the language, a switch telling of the content should be in a single line."
  5041. (let ((re-list
  5042. (append
  5043. org-edit-src-region-extra
  5044. '(
  5045. ("<src\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</src>" lang)
  5046. ("<literal\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</literal>" style)
  5047. ("<example>[ \t]*\n?" "\n?[ \t]*</example>" "fundamental")
  5048. ("<lisp>[ \t]*\n?" "\n?[ \t]*</lisp>" "emacs-lisp")
  5049. ("<perl>[ \t]*\n?" "\n?[ \t]*</perl>" "perl")
  5050. ("<python>[ \t]*\n?" "\n?[ \t]*</python>" "python")
  5051. ("<ruby>[ \t]*\n?" "\n?[ \t]*</ruby>" "ruby")
  5052. ("^#\\+begin_src\\( \\([^ \t\n]+\\)\\)?.*\n" "\n#\\+end_src" 2)
  5053. ("^#\\+begin_example.*\n" "\n#\\+end_example" "fundamental")
  5054. ("^#\\+html:" "\n" "html" single-line)
  5055. ("^#\\+begin_html.*\n" "\n#\\+end_html" "html")
  5056. ("^#\\+begin_latex.*\n" "\n#\\+end_latex" "latex")
  5057. ("^#\\+latex:" "\n" "latex" single-line)
  5058. ("^#\\+begin_ascii.*\n" "\n#\\+end_ascii" "fundamental")
  5059. ("^#\\+ascii:" "\n" "ascii" single-line)
  5060. )))
  5061. (pos (point))
  5062. re re1 re2 single beg end lang)
  5063. (catch 'exit
  5064. (while (setq entry (pop re-list))
  5065. (setq re1 (car entry) re2 (nth 1 entry) lang (nth 2 entry)
  5066. single (nth 3 entry))
  5067. (save-excursion
  5068. (if (or (looking-at re1)
  5069. (re-search-backward re1 nil t))
  5070. (progn
  5071. (setq beg (match-end 0) lang (org-edit-src-get-lang lang))
  5072. (if (and (re-search-forward re2 nil t)
  5073. (>= (match-end 0) pos))
  5074. (throw 'exit (list beg (match-beginning 0) lang single))))
  5075. (if (or (looking-at re2)
  5076. (re-search-forward re2 nil t))
  5077. (progn
  5078. (setq end (match-beginning 0))
  5079. (if (and (re-search-backward re1 nil t)
  5080. (<= (match-beginning 0) pos))
  5081. (throw 'exit
  5082. (list (match-end 0) end
  5083. (org-edit-src-get-lang lang) single)))))))))))
  5084. (defun org-edit-src-get-lang (lang)
  5085. "Extract the src language."
  5086. (let ((m (match-string 0)))
  5087. (cond
  5088. ((stringp lang) lang)
  5089. ((integerp lang) (match-string lang))
  5090. ((and (eq lang 'lang)
  5091. (string-match "\\<lang=\"\\([^ \t\n\"]+\\)\"" m))
  5092. (match-string 1 m))
  5093. ((and (eq lang 'style)
  5094. (string-match "\\<style=\"\\([^ \t\n\"]+\\)\"" m))
  5095. (match-string 1 m))
  5096. (t "fundamental"))))
  5097. (defun org-edit-src-exit ()
  5098. "Exit special edit and protect problematic lines."
  5099. (interactive)
  5100. (unless (buffer-base-buffer (current-buffer))
  5101. (error "This is not an indirect buffer, something is wrong..."))
  5102. (unless (> (point-min) 1)
  5103. (error "This buffer is not narrowed, something is wrong..."))
  5104. (goto-char (point-min))
  5105. (if (looking-at "[ \t\n]*\n") (replace-match ""))
  5106. (if (re-search-forward "\n[ \t\n]*\\'" nil t) (replace-match ""))
  5107. (when (org-bound-and-true-p org-edit-src-force-single-line)
  5108. (goto-char (point-min))
  5109. (while (re-search-forward "\n" nil t)
  5110. (replace-match " "))
  5111. (goto-char (point-min))
  5112. (if (looking-at "\\s-*") (replace-match " "))
  5113. (if (re-search-forward "\\s-+\\'" nil t)
  5114. (replace-match "")))
  5115. (when (org-bound-and-true-p org-edit-src-from-org-mode)
  5116. (goto-char (point-min))
  5117. (while (re-search-forward (if (org-mode-p) "^\\(.\\)" "^\\([*#]\\)") nil t)
  5118. (replace-match ",\\1"))
  5119. (when font-lock-mode
  5120. (font-lock-unfontify-region (point-min) (point-max)))
  5121. (put-text-property (point-min) (point-max) 'font-lock-fontified t))
  5122. (when (org-bound-and-true-p org-edit-src-picture)
  5123. (goto-char (point-min))
  5124. (while (re-search-forward "^" nil t)
  5125. (replace-match ": "))
  5126. (when font-lock-mode
  5127. (font-lock-unfontify-region (point-min) (point-max)))
  5128. (put-text-property (point-min) (point-max) 'font-lock-fontified t))
  5129. (kill-buffer (current-buffer))
  5130. (and (org-mode-p) (org-restart-font-lock)))
  5131. ;;; The orgstruct minor mode
  5132. ;; Define a minor mode which can be used in other modes in order to
  5133. ;; integrate the org-mode structure editing commands.
  5134. ;; This is really a hack, because the org-mode structure commands use
  5135. ;; keys which normally belong to the major mode. Here is how it
  5136. ;; works: The minor mode defines all the keys necessary to operate the
  5137. ;; structure commands, but wraps the commands into a function which
  5138. ;; tests if the cursor is currently at a headline or a plain list
  5139. ;; item. If that is the case, the structure command is used,
  5140. ;; temporarily setting many Org-mode variables like regular
  5141. ;; expressions for filling etc. However, when any of those keys is
  5142. ;; used at a different location, function uses `key-binding' to look
  5143. ;; up if the key has an associated command in another currently active
  5144. ;; keymap (minor modes, major mode, global), and executes that
  5145. ;; command. There might be problems if any of the keys is otherwise
  5146. ;; used as a prefix key.
  5147. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  5148. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  5149. ;; addresses this by checking explicitly for both bindings.
  5150. (defvar orgstruct-mode-map (make-sparse-keymap)
  5151. "Keymap for the minor `orgstruct-mode'.")
  5152. (defvar org-local-vars nil
  5153. "List of local variables, for use by `orgstruct-mode'")
  5154. ;;;###autoload
  5155. (define-minor-mode orgstruct-mode
  5156. "Toggle the minor more `orgstruct-mode'.
  5157. This mode is for using Org-mode structure commands in other modes.
  5158. The following key behave as if Org-mode was active, if the cursor
  5159. is on a headline, or on a plain list item (both in the definition
  5160. of Org-mode).
  5161. M-up Move entry/item up
  5162. M-down Move entry/item down
  5163. M-left Promote
  5164. M-right Demote
  5165. M-S-up Move entry/item up
  5166. M-S-down Move entry/item down
  5167. M-S-left Promote subtree
  5168. M-S-right Demote subtree
  5169. M-q Fill paragraph and items like in Org-mode
  5170. C-c ^ Sort entries
  5171. C-c - Cycle list bullet
  5172. TAB Cycle item visibility
  5173. M-RET Insert new heading/item
  5174. S-M-RET Insert new TODO heading / Chekbox item
  5175. C-c C-c Set tags / toggle checkbox"
  5176. nil " OrgStruct" nil
  5177. (org-load-modules-maybe)
  5178. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  5179. ;;;###autoload
  5180. (defun turn-on-orgstruct ()
  5181. "Unconditionally turn on `orgstruct-mode'."
  5182. (orgstruct-mode 1))
  5183. ;;;###autoload
  5184. (defun turn-on-orgstruct++ ()
  5185. "Unconditionally turn on `orgstruct-mode', and force org-mode indentations.
  5186. In addition to setting orgstruct-mode, this also exports all indentation and
  5187. autofilling variables from org-mode into the buffer. Note that turning
  5188. off orgstruct-mode will *not* remove these additional settings."
  5189. (orgstruct-mode 1)
  5190. (let (var val)
  5191. (mapc
  5192. (lambda (x)
  5193. (when (string-match
  5194. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5195. (symbol-name (car x)))
  5196. (setq var (car x) val (nth 1 x))
  5197. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  5198. org-local-vars)))
  5199. (defun orgstruct-error ()
  5200. "Error when there is no default binding for a structure key."
  5201. (interactive)
  5202. (error "This key has no function outside structure elements"))
  5203. (defun orgstruct-setup ()
  5204. "Setup orgstruct keymaps."
  5205. (let ((nfunc 0)
  5206. (bindings
  5207. (list
  5208. '([(meta up)] org-metaup)
  5209. '([(meta down)] org-metadown)
  5210. '([(meta left)] org-metaleft)
  5211. '([(meta right)] org-metaright)
  5212. '([(meta shift up)] org-shiftmetaup)
  5213. '([(meta shift down)] org-shiftmetadown)
  5214. '([(meta shift left)] org-shiftmetaleft)
  5215. '([(meta shift right)] org-shiftmetaright)
  5216. '([(shift up)] org-shiftup)
  5217. '([(shift down)] org-shiftdown)
  5218. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  5219. '("\M-q" fill-paragraph)
  5220. '("\C-c^" org-sort)
  5221. '("\C-c-" org-cycle-list-bullet)))
  5222. elt key fun cmd)
  5223. (while (setq elt (pop bindings))
  5224. (setq nfunc (1+ nfunc))
  5225. (setq key (org-key (car elt))
  5226. fun (nth 1 elt)
  5227. cmd (orgstruct-make-binding fun nfunc key))
  5228. (org-defkey orgstruct-mode-map key cmd))
  5229. ;; Special treatment needed for TAB and RET
  5230. (org-defkey orgstruct-mode-map [(tab)]
  5231. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  5232. (org-defkey orgstruct-mode-map "\C-i"
  5233. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  5234. (org-defkey orgstruct-mode-map "\M-\C-m"
  5235. (orgstruct-make-binding 'org-insert-heading 105
  5236. "\M-\C-m" [(meta return)]))
  5237. (org-defkey orgstruct-mode-map [(meta return)]
  5238. (orgstruct-make-binding 'org-insert-heading 106
  5239. [(meta return)] "\M-\C-m"))
  5240. (org-defkey orgstruct-mode-map [(shift meta return)]
  5241. (orgstruct-make-binding 'org-insert-todo-heading 107
  5242. [(meta return)] "\M-\C-m"))
  5243. (unless org-local-vars
  5244. (setq org-local-vars (org-get-local-variables)))
  5245. t))
  5246. (defun orgstruct-make-binding (fun n &rest keys)
  5247. "Create a function for binding in the structure minor mode.
  5248. FUN is the command to call inside a table. N is used to create a unique
  5249. command name. KEYS are keys that should be checked in for a command
  5250. to execute outside of tables."
  5251. (eval
  5252. (list 'defun
  5253. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  5254. '(arg)
  5255. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  5256. "Outside of structure, run the binding of `"
  5257. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  5258. "'.")
  5259. '(interactive "p")
  5260. (list 'if
  5261. '(org-context-p 'headline 'item)
  5262. (list 'org-run-like-in-org-mode (list 'quote fun))
  5263. (list 'let '(orgstruct-mode)
  5264. (list 'call-interactively
  5265. (append '(or)
  5266. (mapcar (lambda (k)
  5267. (list 'key-binding k))
  5268. keys)
  5269. '('orgstruct-error))))))))
  5270. (defun org-context-p (&rest contexts)
  5271. "Check if local context is any of CONTEXTS.
  5272. Possible values in the list of contexts are `table', `headline', and `item'."
  5273. (let ((pos (point)))
  5274. (goto-char (point-at-bol))
  5275. (prog1 (or (and (memq 'table contexts)
  5276. (looking-at "[ \t]*|"))
  5277. (and (memq 'headline contexts)
  5278. ;;????????? (looking-at "\\*+"))
  5279. (looking-at outline-regexp))
  5280. (and (memq 'item contexts)
  5281. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)")))
  5282. (goto-char pos))))
  5283. (defun org-get-local-variables ()
  5284. "Return a list of all local variables in an org-mode buffer."
  5285. (let (varlist)
  5286. (with-current-buffer (get-buffer-create "*Org tmp*")
  5287. (erase-buffer)
  5288. (org-mode)
  5289. (setq varlist (buffer-local-variables)))
  5290. (kill-buffer "*Org tmp*")
  5291. (delq nil
  5292. (mapcar
  5293. (lambda (x)
  5294. (setq x
  5295. (if (symbolp x)
  5296. (list x)
  5297. (list (car x) (list 'quote (cdr x)))))
  5298. (if (string-match
  5299. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5300. (symbol-name (car x)))
  5301. x nil))
  5302. varlist))))
  5303. ;;;###autoload
  5304. (defun org-run-like-in-org-mode (cmd)
  5305. (org-load-modules-maybe)
  5306. (unless org-local-vars
  5307. (setq org-local-vars (org-get-local-variables)))
  5308. (eval (list 'let org-local-vars
  5309. (list 'call-interactively (list 'quote cmd)))))
  5310. ;;;; Archiving
  5311. (defun org-get-category (&optional pos)
  5312. "Get the category applying to position POS."
  5313. (get-text-property (or pos (point)) 'org-category))
  5314. (defun org-refresh-category-properties ()
  5315. "Refresh category text properties in the buffer."
  5316. (let ((def-cat (cond
  5317. ((null org-category)
  5318. (if buffer-file-name
  5319. (file-name-sans-extension
  5320. (file-name-nondirectory buffer-file-name))
  5321. "???"))
  5322. ((symbolp org-category) (symbol-name org-category))
  5323. (t org-category)))
  5324. beg end cat pos optionp)
  5325. (org-unmodified
  5326. (save-excursion
  5327. (save-restriction
  5328. (widen)
  5329. (goto-char (point-min))
  5330. (put-text-property (point) (point-max) 'org-category def-cat)
  5331. (while (re-search-forward
  5332. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  5333. (setq pos (match-end 0)
  5334. optionp (equal (char-after (match-beginning 0)) ?#)
  5335. cat (org-trim (match-string 2)))
  5336. (if optionp
  5337. (setq beg (point-at-bol) end (point-max))
  5338. (org-back-to-heading t)
  5339. (setq beg (point) end (org-end-of-subtree t t)))
  5340. (put-text-property beg end 'org-category cat)
  5341. (goto-char pos)))))))
  5342. ;;;; Link Stuff
  5343. ;;; Link abbreviations
  5344. (defun org-link-expand-abbrev (link)
  5345. "Apply replacements as defined in `org-link-abbrev-alist."
  5346. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  5347. (let* ((key (match-string 1 link))
  5348. (as (or (assoc key org-link-abbrev-alist-local)
  5349. (assoc key org-link-abbrev-alist)))
  5350. (tag (and (match-end 2) (match-string 3 link)))
  5351. rpl)
  5352. (if (not as)
  5353. link
  5354. (setq rpl (cdr as))
  5355. (cond
  5356. ((symbolp rpl) (funcall rpl tag))
  5357. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  5358. (t (concat rpl tag)))))
  5359. link))
  5360. ;;; Storing and inserting links
  5361. (defvar org-insert-link-history nil
  5362. "Minibuffer history for links inserted with `org-insert-link'.")
  5363. (defvar org-stored-links nil
  5364. "Contains the links stored with `org-store-link'.")
  5365. (defvar org-store-link-plist nil
  5366. "Plist with info about the most recently link created with `org-store-link'.")
  5367. (defvar org-link-protocols nil
  5368. "Link protocols added to Org-mode using `org-add-link-type'.")
  5369. (defvar org-store-link-functions nil
  5370. "List of functions that are called to create and store a link.
  5371. Each function will be called in turn until one returns a non-nil
  5372. value. Each function should check if it is responsible for creating
  5373. this link (for example by looking at the major mode).
  5374. If not, it must exit and return nil.
  5375. If yes, it should return a non-nil value after a calling
  5376. `org-store-link-props' with a list of properties and values.
  5377. Special properties are:
  5378. :type The link prefix. like \"http\". This must be given.
  5379. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  5380. This is obligatory as well.
  5381. :description Optional default description for the second pair
  5382. of brackets in an Org-mode link. The user can still change
  5383. this when inserting this link into an Org-mode buffer.
  5384. In addition to these, any additional properties can be specified
  5385. and then used in remember templates.")
  5386. (defun org-add-link-type (type &optional follow export)
  5387. "Add TYPE to the list of `org-link-types'.
  5388. Re-compute all regular expressions depending on `org-link-types'
  5389. FOLLOW and EXPORT are two functions.
  5390. FOLLOW should take the link path as the single argument and do whatever
  5391. is necessary to follow the link, for example find a file or display
  5392. a mail message.
  5393. EXPORT should format the link path for export to one of the export formats.
  5394. It should be a function accepting three arguments:
  5395. path the path of the link, the text after the prefix (like \"http:\")
  5396. desc the description of the link, if any, nil if there was no descripton
  5397. format the export format, a symbol like `html' or `latex'.
  5398. The function may use the FORMAT information to return different values
  5399. depending on the format. The return value will be put literally into
  5400. the exported file.
  5401. Org-mode has a built-in default for exporting links. If you are happy with
  5402. this default, there is no need to define an export function for the link
  5403. type. For a simple example of an export function, see `org-bbdb.el'."
  5404. (add-to-list 'org-link-types type t)
  5405. (org-make-link-regexps)
  5406. (if (assoc type org-link-protocols)
  5407. (setcdr (assoc type org-link-protocols) (list follow export))
  5408. (push (list type follow export) org-link-protocols)))
  5409. ;;;###autoload
  5410. (defun org-store-link (arg)
  5411. "\\<org-mode-map>Store an org-link to the current location.
  5412. This link is added to `org-stored-links' and can later be inserted
  5413. into an org-buffer with \\[org-insert-link].
  5414. For some link types, a prefix arg is interpreted:
  5415. For links to usenet articles, arg negates `org-usenet-links-prefer-google'.
  5416. For file links, arg negates `org-context-in-file-links'."
  5417. (interactive "P")
  5418. (org-load-modules-maybe)
  5419. (setq org-store-link-plist nil) ; reset
  5420. (let (link cpltxt desc description search txt)
  5421. (cond
  5422. ((run-hook-with-args-until-success 'org-store-link-functions)
  5423. (setq link (plist-get org-store-link-plist :link)
  5424. desc (or (plist-get org-store-link-plist :description) link)))
  5425. ((eq major-mode 'calendar-mode)
  5426. (let ((cd (calendar-cursor-to-date)))
  5427. (setq link
  5428. (format-time-string
  5429. (car org-time-stamp-formats)
  5430. (apply 'encode-time
  5431. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  5432. nil nil nil))))
  5433. (org-store-link-props :type "calendar" :date cd)))
  5434. ((eq major-mode 'w3-mode)
  5435. (setq cpltxt (url-view-url t)
  5436. link (org-make-link cpltxt))
  5437. (org-store-link-props :type "w3" :url (url-view-url t)))
  5438. ((eq major-mode 'w3m-mode)
  5439. (setq cpltxt (or w3m-current-title w3m-current-url)
  5440. link (org-make-link w3m-current-url))
  5441. (org-store-link-props :type "w3m" :url (url-view-url t)))
  5442. ((setq search (run-hook-with-args-until-success
  5443. 'org-create-file-search-functions))
  5444. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  5445. "::" search))
  5446. (setq cpltxt (or description link)))
  5447. ((eq major-mode 'image-mode)
  5448. (setq cpltxt (concat "file:"
  5449. (abbreviate-file-name buffer-file-name))
  5450. link (org-make-link cpltxt))
  5451. (org-store-link-props :type "image" :file buffer-file-name))
  5452. ((eq major-mode 'dired-mode)
  5453. ;; link to the file in the current line
  5454. (setq cpltxt (concat "file:"
  5455. (abbreviate-file-name
  5456. (expand-file-name
  5457. (dired-get-filename nil t))))
  5458. link (org-make-link cpltxt)))
  5459. ((and buffer-file-name (org-mode-p))
  5460. ;; Just link to current headline
  5461. (setq cpltxt (concat "file:"
  5462. (abbreviate-file-name buffer-file-name)))
  5463. ;; Add a context search string
  5464. (when (org-xor org-context-in-file-links arg)
  5465. ;; Check if we are on a target
  5466. (if (org-in-regexp "<<\\(.*?\\)>>")
  5467. (setq cpltxt (concat cpltxt "::" (match-string 1)))
  5468. (setq txt (cond
  5469. ((org-on-heading-p) nil)
  5470. ((org-region-active-p)
  5471. (buffer-substring (region-beginning) (region-end)))
  5472. (t nil)))
  5473. (when (or (null txt) (string-match "\\S-" txt))
  5474. (setq cpltxt
  5475. (concat cpltxt "::"
  5476. (condition-case nil
  5477. (org-make-org-heading-search-string txt)
  5478. (error "")))
  5479. desc "NONE"))))
  5480. (if (string-match "::\\'" cpltxt)
  5481. (setq cpltxt (substring cpltxt 0 -2)))
  5482. (setq link (org-make-link cpltxt)))
  5483. ((buffer-file-name (buffer-base-buffer))
  5484. ;; Just link to this file here.
  5485. (setq cpltxt (concat "file:"
  5486. (abbreviate-file-name
  5487. (buffer-file-name (buffer-base-buffer)))))
  5488. ;; Add a context string
  5489. (when (org-xor org-context-in-file-links arg)
  5490. (setq txt (if (org-region-active-p)
  5491. (buffer-substring (region-beginning) (region-end))
  5492. (buffer-substring (point-at-bol) (point-at-eol))))
  5493. ;; Only use search option if there is some text.
  5494. (when (string-match "\\S-" txt)
  5495. (setq cpltxt
  5496. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  5497. desc "NONE")))
  5498. (setq link (org-make-link cpltxt)))
  5499. ((interactive-p)
  5500. (error "Cannot link to a buffer which is not visiting a file"))
  5501. (t (setq link nil)))
  5502. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  5503. (setq link (or link cpltxt)
  5504. desc (or desc cpltxt))
  5505. (if (equal desc "NONE") (setq desc nil))
  5506. (if (and (interactive-p) link)
  5507. (progn
  5508. (setq org-stored-links
  5509. (cons (list link desc) org-stored-links))
  5510. (message "Stored: %s" (or desc link)))
  5511. (and link (org-make-link-string link desc)))))
  5512. (defun org-store-link-props (&rest plist)
  5513. "Store link properties, extract names and addresses."
  5514. (let (x adr)
  5515. (when (setq x (plist-get plist :from))
  5516. (setq adr (mail-extract-address-components x))
  5517. (setq plist (plist-put plist :fromname (car adr)))
  5518. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  5519. (when (setq x (plist-get plist :to))
  5520. (setq adr (mail-extract-address-components x))
  5521. (setq plist (plist-put plist :toname (car adr)))
  5522. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  5523. (let ((from (plist-get plist :from))
  5524. (to (plist-get plist :to)))
  5525. (when (and from to org-from-is-user-regexp)
  5526. (setq plist
  5527. (plist-put plist :fromto
  5528. (if (string-match org-from-is-user-regexp from)
  5529. (concat "to %t")
  5530. (concat "from %f"))))))
  5531. (setq org-store-link-plist plist))
  5532. (defun org-add-link-props (&rest plist)
  5533. "Add these properties to the link property list."
  5534. (let (key value)
  5535. (while plist
  5536. (setq key (pop plist) value (pop plist))
  5537. (setq org-store-link-plist
  5538. (plist-put org-store-link-plist key value)))))
  5539. (defun org-email-link-description (&optional fmt)
  5540. "Return the description part of an email link.
  5541. This takes information from `org-store-link-plist' and formats it
  5542. according to FMT (default from `org-email-link-description-format')."
  5543. (setq fmt (or fmt org-email-link-description-format))
  5544. (let* ((p org-store-link-plist)
  5545. (to (plist-get p :toaddress))
  5546. (from (plist-get p :fromaddress))
  5547. (table
  5548. (list
  5549. (cons "%c" (plist-get p :fromto))
  5550. (cons "%F" (plist-get p :from))
  5551. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  5552. (cons "%T" (plist-get p :to))
  5553. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  5554. (cons "%s" (plist-get p :subject))
  5555. (cons "%m" (plist-get p :message-id)))))
  5556. (when (string-match "%c" fmt)
  5557. ;; Check if the user wrote this message
  5558. (if (and org-from-is-user-regexp from to
  5559. (save-match-data (string-match org-from-is-user-regexp from)))
  5560. (setq fmt (replace-match "to %t" t t fmt))
  5561. (setq fmt (replace-match "from %f" t t fmt))))
  5562. (org-replace-escapes fmt table)))
  5563. (defun org-make-org-heading-search-string (&optional string heading)
  5564. "Make search string for STRING or current headline."
  5565. (interactive)
  5566. (let ((s (or string (org-get-heading))))
  5567. (unless (and string (not heading))
  5568. ;; We are using a headline, clean up garbage in there.
  5569. (if (string-match org-todo-regexp s)
  5570. (setq s (replace-match "" t t s)))
  5571. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  5572. (setq s (replace-match "" t t s)))
  5573. (setq s (org-trim s))
  5574. (if (string-match (concat "^\\(" org-quote-string "\\|"
  5575. org-comment-string "\\)") s)
  5576. (setq s (replace-match "" t t s)))
  5577. (while (string-match org-ts-regexp s)
  5578. (setq s (replace-match "" t t s))))
  5579. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  5580. (setq s (replace-match " " t t s)))
  5581. (or string (setq s (concat "*" s))) ; Add * for headlines
  5582. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  5583. (defun org-make-link (&rest strings)
  5584. "Concatenate STRINGS."
  5585. (apply 'concat strings))
  5586. (defun org-make-link-string (link &optional description)
  5587. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  5588. (unless (string-match "\\S-" link)
  5589. (error "Empty link"))
  5590. (when (stringp description)
  5591. ;; Remove brackets from the description, they are fatal.
  5592. (while (string-match "\\[" description)
  5593. (setq description (replace-match "{" t t description)))
  5594. (while (string-match "\\]" description)
  5595. (setq description (replace-match "}" t t description))))
  5596. (when (equal (org-link-escape link) description)
  5597. ;; No description needed, it is identical
  5598. (setq description nil))
  5599. (when (and (not description)
  5600. (not (equal link (org-link-escape link))))
  5601. (setq description (org-extract-attributes link)))
  5602. (concat "[[" (org-link-escape link) "]"
  5603. (if description (concat "[" description "]") "")
  5604. "]"))
  5605. (defconst org-link-escape-chars
  5606. '((?\ . "%20")
  5607. (?\[ . "%5B")
  5608. (?\] . "%5D")
  5609. (?\340 . "%E0") ; `a
  5610. (?\342 . "%E2") ; ^a
  5611. (?\347 . "%E7") ; ,c
  5612. (?\350 . "%E8") ; `e
  5613. (?\351 . "%E9") ; 'e
  5614. (?\352 . "%EA") ; ^e
  5615. (?\356 . "%EE") ; ^i
  5616. (?\364 . "%F4") ; ^o
  5617. (?\371 . "%F9") ; `u
  5618. (?\373 . "%FB") ; ^u
  5619. (?\; . "%3B")
  5620. (?? . "%3F")
  5621. (?= . "%3D")
  5622. (?+ . "%2B")
  5623. )
  5624. "Association list of escapes for some characters problematic in links.
  5625. This is the list that is used for internal purposes.")
  5626. (defconst org-link-escape-chars-browser
  5627. '((?\ . "%20")) ; 32 for the SPC char
  5628. "Association list of escapes for some characters problematic in links.
  5629. This is the list that is used before handing over to the browser.")
  5630. (defun org-link-escape (text &optional table)
  5631. "Escape charaters in TEXT that are problematic for links."
  5632. (setq table (or table org-link-escape-chars))
  5633. (when text
  5634. (let ((re (mapconcat (lambda (x) (regexp-quote
  5635. (char-to-string (car x))))
  5636. table "\\|")))
  5637. (while (string-match re text)
  5638. (setq text
  5639. (replace-match
  5640. (cdr (assoc (string-to-char (match-string 0 text))
  5641. table))
  5642. t t text)))
  5643. text)))
  5644. (defun org-link-unescape (text &optional table)
  5645. "Reverse the action of `org-link-escape'."
  5646. (setq table (or table org-link-escape-chars))
  5647. (when text
  5648. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  5649. table "\\|")))
  5650. (while (string-match re text)
  5651. (setq text
  5652. (replace-match
  5653. (char-to-string (car (rassoc (match-string 0 text) table)))
  5654. t t text)))
  5655. text)))
  5656. (defun org-xor (a b)
  5657. "Exclusive or."
  5658. (if a (not b) b))
  5659. (defun org-get-header (header)
  5660. "Find a header field in the current buffer."
  5661. (save-excursion
  5662. (goto-char (point-min))
  5663. (let ((case-fold-search t) s)
  5664. (cond
  5665. ((eq header 'from)
  5666. (if (re-search-forward "^From:\\s-+\\(.*\\)" nil t)
  5667. (setq s (match-string 1)))
  5668. (while (string-match "\"" s)
  5669. (setq s (replace-match "" t t s)))
  5670. (if (string-match "[<(].*" s)
  5671. (setq s (replace-match "" t t s))))
  5672. ((eq header 'message-id)
  5673. (if (re-search-forward "^message-id:\\s-+\\(.*\\)" nil t)
  5674. (setq s (match-string 1))))
  5675. ((eq header 'subject)
  5676. (if (re-search-forward "^subject:\\s-+\\(.*\\)" nil t)
  5677. (setq s (match-string 1)))))
  5678. (if (string-match "\\`[ \t\]+" s) (setq s (replace-match "" t t s)))
  5679. (if (string-match "[ \t\]+\\'" s) (setq s (replace-match "" t t s)))
  5680. s)))
  5681. (defun org-fixup-message-id-for-http (s)
  5682. "Replace special characters in a message id, so it can be used in an http query."
  5683. (while (string-match "<" s)
  5684. (setq s (replace-match "%3C" t t s)))
  5685. (while (string-match ">" s)
  5686. (setq s (replace-match "%3E" t t s)))
  5687. (while (string-match "@" s)
  5688. (setq s (replace-match "%40" t t s)))
  5689. s)
  5690. ;;;###autoload
  5691. (defun org-insert-link-global ()
  5692. "Insert a link like Org-mode does.
  5693. This command can be called in any mode to insert a link in Org-mode syntax."
  5694. (interactive)
  5695. (org-load-modules-maybe)
  5696. (org-run-like-in-org-mode 'org-insert-link))
  5697. (defun org-insert-link (&optional complete-file link-location)
  5698. "Insert a link. At the prompt, enter the link.
  5699. Completion can be used to select a link previously stored with
  5700. `org-store-link'. When the empty string is entered (i.e. if you just
  5701. press RET at the prompt), the link defaults to the most recently
  5702. stored link. As SPC triggers completion in the minibuffer, you need to
  5703. use M-SPC or C-q SPC to force the insertion of a space character.
  5704. You will also be prompted for a description, and if one is given, it will
  5705. be displayed in the buffer instead of the link.
  5706. If there is already a link at point, this command will allow you to edit link
  5707. and description parts.
  5708. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  5709. be selected using completion. The path to the file will be relative to the
  5710. current directory if the file is in the current directory or a subdirectory.
  5711. Otherwise, the link will be the absolute path as completed in the minibuffer
  5712. \(i.e. normally ~/path/to/file).
  5713. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  5714. the current directory or below. With three \\[universal-argument] prefixes, negate the meaning
  5715. of `org-keep-stored-link-after-insertion'.
  5716. If `org-make-link-description-function' is non-nil, this function will be
  5717. called with the link target, and the result will be the default
  5718. link description.
  5719. If the LINK-LOCATION parameter is non-nil, this value will be
  5720. used as the link location instead of reading one interactively."
  5721. (interactive "P")
  5722. (let* ((wcf (current-window-configuration))
  5723. (region (if (org-region-active-p)
  5724. (buffer-substring (region-beginning) (region-end))))
  5725. (remove (and region (list (region-beginning) (region-end))))
  5726. (desc region)
  5727. tmphist ; byte-compile incorrectly complains about this
  5728. (link link-location)
  5729. entry file)
  5730. (cond
  5731. (link-location) ; specified by arg, just use it.
  5732. ((org-in-regexp org-bracket-link-regexp 1)
  5733. ;; We do have a link at point, and we are going to edit it.
  5734. (setq remove (list (match-beginning 0) (match-end 0)))
  5735. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  5736. (setq link (read-string "Link: "
  5737. (org-link-unescape
  5738. (org-match-string-no-properties 1)))))
  5739. ((or (org-in-regexp org-angle-link-re)
  5740. (org-in-regexp org-plain-link-re))
  5741. ;; Convert to bracket link
  5742. (setq remove (list (match-beginning 0) (match-end 0))
  5743. link (read-string "Link: "
  5744. (org-remove-angle-brackets (match-string 0)))))
  5745. ((equal complete-file '(4))
  5746. ;; Completing read for file names.
  5747. (setq file (read-file-name "File: "))
  5748. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  5749. (pwd1 (file-name-as-directory (abbreviate-file-name
  5750. (expand-file-name ".")))))
  5751. (cond
  5752. ((equal complete-file '(16))
  5753. (setq link (org-make-link
  5754. "file:"
  5755. (abbreviate-file-name (expand-file-name file)))))
  5756. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  5757. (setq link (org-make-link "file:" (match-string 1 file))))
  5758. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  5759. (expand-file-name file))
  5760. (setq link (org-make-link
  5761. "file:" (match-string 1 (expand-file-name file)))))
  5762. (t (setq link (org-make-link "file:" file))))))
  5763. (t
  5764. ;; Read link, with completion for stored links.
  5765. (with-output-to-temp-buffer "*Org Links*"
  5766. (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
  5767. (when org-stored-links
  5768. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  5769. (princ (mapconcat
  5770. (lambda (x)
  5771. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  5772. (reverse org-stored-links) "\n"))))
  5773. (let ((cw (selected-window)))
  5774. (select-window (get-buffer-window "*Org Links*"))
  5775. (shrink-window-if-larger-than-buffer)
  5776. (setq truncate-lines t)
  5777. (select-window cw))
  5778. ;; Fake a link history, containing the stored links.
  5779. (setq tmphist (append (mapcar 'car org-stored-links)
  5780. org-insert-link-history))
  5781. (unwind-protect
  5782. (setq link (org-completing-read
  5783. "Link: "
  5784. (append
  5785. (mapcar (lambda (x) (list (concat (car x) ":")))
  5786. (append org-link-abbrev-alist-local org-link-abbrev-alist))
  5787. (mapcar (lambda (x) (list (concat x ":")))
  5788. org-link-types))
  5789. nil nil nil
  5790. 'tmphist
  5791. (or (car (car org-stored-links)))))
  5792. (set-window-configuration wcf)
  5793. (kill-buffer "*Org Links*"))
  5794. (setq entry (assoc link org-stored-links))
  5795. (or entry (push link org-insert-link-history))
  5796. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  5797. (not org-keep-stored-link-after-insertion))
  5798. (setq org-stored-links (delq (assoc link org-stored-links)
  5799. org-stored-links)))
  5800. (setq desc (or desc (nth 1 entry)))))
  5801. (if (string-match org-plain-link-re link)
  5802. ;; URL-like link, normalize the use of angular brackets.
  5803. (setq link (org-make-link (org-remove-angle-brackets link))))
  5804. ;; Check if we are linking to the current file with a search option
  5805. ;; If yes, simplify the link by using only the search option.
  5806. (when (and buffer-file-name
  5807. (string-match "\\<file:\\(.+?\\)::\\([^>]+\\)" link))
  5808. (let* ((path (match-string 1 link))
  5809. (case-fold-search nil)
  5810. (search (match-string 2 link)))
  5811. (save-match-data
  5812. (if (equal (file-truename buffer-file-name) (file-truename path))
  5813. ;; We are linking to this same file, with a search option
  5814. (setq link search)))))
  5815. ;; Check if we can/should use a relative path. If yes, simplify the link
  5816. (when (string-match "\\<file:\\(.*\\)" link)
  5817. (let* ((path (match-string 1 link))
  5818. (origpath path)
  5819. (case-fold-search nil))
  5820. (cond
  5821. ((eq org-link-file-path-type 'absolute)
  5822. (setq path (abbreviate-file-name (expand-file-name path))))
  5823. ((eq org-link-file-path-type 'noabbrev)
  5824. (setq path (expand-file-name path)))
  5825. ((eq org-link-file-path-type 'relative)
  5826. (setq path (file-relative-name path)))
  5827. (t
  5828. (save-match-data
  5829. (if (string-match (concat "^" (regexp-quote
  5830. (file-name-as-directory
  5831. (expand-file-name "."))))
  5832. (expand-file-name path))
  5833. ;; We are linking a file with relative path name.
  5834. (setq path (substring (expand-file-name path)
  5835. (match-end 0)))))))
  5836. (setq link (concat "file:" path))
  5837. (if (equal desc origpath)
  5838. (setq desc path))))
  5839. (if org-make-link-description-function
  5840. (setq desc (funcall org-make-link-description-function link desc)))
  5841. (setq desc (read-string "Description: " desc))
  5842. (unless (string-match "\\S-" desc) (setq desc nil))
  5843. (if remove (apply 'delete-region remove))
  5844. (insert (org-make-link-string link desc))))
  5845. (defun org-completing-read (&rest args)
  5846. (let ((minibuffer-local-completion-map
  5847. (copy-keymap minibuffer-local-completion-map)))
  5848. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  5849. (apply 'completing-read args)))
  5850. (defun org-extract-attributes (s)
  5851. "Extract the attributes cookie from a string and set as text property."
  5852. (let (a attr (start 0) key value)
  5853. (save-match-data
  5854. (when (string-match "{{\\([^}]+\\)}}$" s)
  5855. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  5856. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  5857. (setq key (match-string 1 a) value (match-string 2 a)
  5858. start (match-end 0)
  5859. attr (plist-put attr (intern key) value))))
  5860. (org-add-props s nil 'org-attributes attr))
  5861. s))
  5862. (defun org-attributes-to-string (plist)
  5863. "Format a property list into an HTML attribute list."
  5864. (let ((s "") key value)
  5865. (while plist
  5866. (setq key (pop plist) value (pop plist))
  5867. (setq s (concat s " "(symbol-name key) "=\"" value "\"")))
  5868. s))
  5869. ;;; Opening/following a link
  5870. (defvar org-link-search-failed nil)
  5871. (defun org-next-link ()
  5872. "Move forward to the next link.
  5873. If the link is in hidden text, expose it."
  5874. (interactive)
  5875. (when (and org-link-search-failed (eq this-command last-command))
  5876. (goto-char (point-min))
  5877. (message "Link search wrapped back to beginning of buffer"))
  5878. (setq org-link-search-failed nil)
  5879. (let* ((pos (point))
  5880. (ct (org-context))
  5881. (a (assoc :link ct)))
  5882. (if a (goto-char (nth 2 a)))
  5883. (if (re-search-forward org-any-link-re nil t)
  5884. (progn
  5885. (goto-char (match-beginning 0))
  5886. (if (org-invisible-p) (org-show-context)))
  5887. (goto-char pos)
  5888. (setq org-link-search-failed t)
  5889. (error "No further link found"))))
  5890. (defun org-previous-link ()
  5891. "Move backward to the previous link.
  5892. If the link is in hidden text, expose it."
  5893. (interactive)
  5894. (when (and org-link-search-failed (eq this-command last-command))
  5895. (goto-char (point-max))
  5896. (message "Link search wrapped back to end of buffer"))
  5897. (setq org-link-search-failed nil)
  5898. (let* ((pos (point))
  5899. (ct (org-context))
  5900. (a (assoc :link ct)))
  5901. (if a (goto-char (nth 1 a)))
  5902. (if (re-search-backward org-any-link-re nil t)
  5903. (progn
  5904. (goto-char (match-beginning 0))
  5905. (if (org-invisible-p) (org-show-context)))
  5906. (goto-char pos)
  5907. (setq org-link-search-failed t)
  5908. (error "No further link found"))))
  5909. (defun org-find-file-at-mouse (ev)
  5910. "Open file link or URL at mouse."
  5911. (interactive "e")
  5912. (mouse-set-point ev)
  5913. (org-open-at-point 'in-emacs))
  5914. (defun org-open-at-mouse (ev)
  5915. "Open file link or URL at mouse."
  5916. (interactive "e")
  5917. (mouse-set-point ev)
  5918. (org-open-at-point))
  5919. (defvar org-window-config-before-follow-link nil
  5920. "The window configuration before following a link.
  5921. This is saved in case the need arises to restore it.")
  5922. (defvar org-open-link-marker (make-marker)
  5923. "Marker pointing to the location where `org-open-at-point; was called.")
  5924. ;;;###autoload
  5925. (defun org-open-at-point-global ()
  5926. "Follow a link like Org-mode does.
  5927. This command can be called in any mode to follow a link that has
  5928. Org-mode syntax."
  5929. (interactive)
  5930. (org-run-like-in-org-mode 'org-open-at-point))
  5931. ;;;###autoload
  5932. (defun org-open-link-from-string (s &optional arg)
  5933. "Open a link in the string S, as if it was in Org-mode."
  5934. (interactive "sLink: \nP")
  5935. (with-temp-buffer
  5936. (let ((org-inhibit-startup t))
  5937. (org-mode)
  5938. (insert s)
  5939. (goto-char (point-min))
  5940. (org-open-at-point arg))))
  5941. (defun org-open-at-point (&optional in-emacs)
  5942. "Open link at or after point.
  5943. If there is no link at point, this function will search forward up to
  5944. the end of the current subtree.
  5945. Normally, files will be opened by an appropriate application. If the
  5946. optional argument IN-EMACS is non-nil, Emacs will visit the file."
  5947. (interactive "P")
  5948. (org-load-modules-maybe)
  5949. (move-marker org-open-link-marker (point))
  5950. (setq org-window-config-before-follow-link (current-window-configuration))
  5951. (org-remove-occur-highlights nil nil t)
  5952. (if (org-at-timestamp-p t)
  5953. (org-follow-timestamp-link)
  5954. (let (type path link line search (pos (point)))
  5955. (catch 'match
  5956. (save-excursion
  5957. (skip-chars-forward "^]\n\r")
  5958. (when (org-in-regexp org-bracket-link-regexp)
  5959. (setq link (org-extract-attributes
  5960. (org-link-unescape (org-match-string-no-properties 1))))
  5961. (while (string-match " *\n *" link)
  5962. (setq link (replace-match " " t t link)))
  5963. (setq link (org-link-expand-abbrev link))
  5964. (cond
  5965. ((or (file-name-absolute-p link)
  5966. (string-match "^\\.\\.?/" link))
  5967. (setq type "file" path link))
  5968. ((string-match org-link-re-with-space2 link)
  5969. (setq type (match-string 1 link) path (match-string 2 link)))
  5970. (t (setq type "thisfile" path link)))
  5971. (throw 'match t)))
  5972. (when (get-text-property (point) 'org-linked-text)
  5973. (setq type "thisfile"
  5974. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  5975. (1+ (point)) (point))
  5976. path (buffer-substring
  5977. (previous-single-property-change pos 'org-linked-text)
  5978. (next-single-property-change pos 'org-linked-text)))
  5979. (throw 'match t))
  5980. (save-excursion
  5981. (when (or (org-in-regexp org-angle-link-re)
  5982. (org-in-regexp org-plain-link-re))
  5983. (setq type (match-string 1) path (match-string 2))
  5984. (throw 'match t)))
  5985. (when (org-in-regexp "\\<\\([^><\n]+\\)\\>")
  5986. (setq type "tree-match"
  5987. path (match-string 1))
  5988. (throw 'match t))
  5989. (save-excursion
  5990. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  5991. (setq type "tags"
  5992. path (match-string 1))
  5993. (while (string-match ":" path)
  5994. (setq path (replace-match "+" t t path)))
  5995. (throw 'match t))))
  5996. (unless path
  5997. (error "No link found"))
  5998. ;; Remove any trailing spaces in path
  5999. (if (string-match " +\\'" path)
  6000. (setq path (replace-match "" t t path)))
  6001. (cond
  6002. ((assoc type org-link-protocols)
  6003. (funcall (nth 1 (assoc type org-link-protocols)) path))
  6004. ((equal type "mailto")
  6005. (let ((cmd (car org-link-mailto-program))
  6006. (args (cdr org-link-mailto-program)) args1
  6007. (address path) (subject "") a)
  6008. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  6009. (setq address (match-string 1 path)
  6010. subject (org-link-escape (match-string 2 path))))
  6011. (while args
  6012. (cond
  6013. ((not (stringp (car args))) (push (pop args) args1))
  6014. (t (setq a (pop args))
  6015. (if (string-match "%a" a)
  6016. (setq a (replace-match address t t a)))
  6017. (if (string-match "%s" a)
  6018. (setq a (replace-match subject t t a)))
  6019. (push a args1))))
  6020. (apply cmd (nreverse args1))))
  6021. ((member type '("http" "https" "ftp" "news"))
  6022. (browse-url (concat type ":" (org-link-escape
  6023. path org-link-escape-chars-browser))))
  6024. ((member type '("message"))
  6025. (browse-url (concat type ":" path)))
  6026. ((string= type "tags")
  6027. (org-tags-view in-emacs path))
  6028. ((string= type "thisfile")
  6029. (if in-emacs
  6030. (switch-to-buffer-other-window
  6031. (org-get-buffer-for-internal-link (current-buffer)))
  6032. (org-mark-ring-push))
  6033. (let ((cmd `(org-link-search
  6034. ,path
  6035. ,(cond ((equal in-emacs '(4)) 'occur)
  6036. ((equal in-emacs '(16)) 'org-occur)
  6037. (t nil))
  6038. ,pos)))
  6039. (condition-case nil (eval cmd)
  6040. (error (progn (widen) (eval cmd))))))
  6041. ((string= type "tree-match")
  6042. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  6043. ((string= type "file")
  6044. (if (string-match "::\\([0-9]+\\)\\'" path)
  6045. (setq line (string-to-number (match-string 1 path))
  6046. path (substring path 0 (match-beginning 0)))
  6047. (if (string-match "::\\(.+\\)\\'" path)
  6048. (setq search (match-string 1 path)
  6049. path (substring path 0 (match-beginning 0)))))
  6050. (if (string-match "[*?{]" (file-name-nondirectory path))
  6051. (dired path)
  6052. (org-open-file path in-emacs line search)))
  6053. ((string= type "news")
  6054. (require 'org-gnus)
  6055. (org-gnus-follow-link path))
  6056. ((string= type "shell")
  6057. (let ((cmd path))
  6058. (if (or (not org-confirm-shell-link-function)
  6059. (funcall org-confirm-shell-link-function
  6060. (format "Execute \"%s\" in shell? "
  6061. (org-add-props cmd nil
  6062. 'face 'org-warning))))
  6063. (progn
  6064. (message "Executing %s" cmd)
  6065. (shell-command cmd))
  6066. (error "Abort"))))
  6067. ((string= type "elisp")
  6068. (let ((cmd path))
  6069. (if (or (not org-confirm-elisp-link-function)
  6070. (funcall org-confirm-elisp-link-function
  6071. (format "Execute \"%s\" as elisp? "
  6072. (org-add-props cmd nil
  6073. 'face 'org-warning))))
  6074. (message "%s => %s" cmd (eval (read cmd)))
  6075. (error "Abort"))))
  6076. (t
  6077. (browse-url-at-point)))))
  6078. (move-marker org-open-link-marker nil)
  6079. (run-hook-with-args 'org-follow-link-hook))
  6080. ;;;; Time estimates
  6081. (defun org-get-effort (&optional pom)
  6082. "Get the effort estimate for the current entry."
  6083. (org-entry-get pom org-effort-property))
  6084. ;;; File search
  6085. (defvar org-create-file-search-functions nil
  6086. "List of functions to construct the right search string for a file link.
  6087. These functions are called in turn with point at the location to
  6088. which the link should point.
  6089. A function in the hook should first test if it would like to
  6090. handle this file type, for example by checking the major-mode or
  6091. the file extension. If it decides not to handle this file, it
  6092. should just return nil to give other functions a chance. If it
  6093. does handle the file, it must return the search string to be used
  6094. when following the link. The search string will be part of the
  6095. file link, given after a double colon, and `org-open-at-point'
  6096. will automatically search for it. If special measures must be
  6097. taken to make the search successful, another function should be
  6098. added to the companion hook `org-execute-file-search-functions',
  6099. which see.
  6100. A function in this hook may also use `setq' to set the variable
  6101. `description' to provide a suggestion for the descriptive text to
  6102. be used for this link when it gets inserted into an Org-mode
  6103. buffer with \\[org-insert-link].")
  6104. (defvar org-execute-file-search-functions nil
  6105. "List of functions to execute a file search triggered by a link.
  6106. Functions added to this hook must accept a single argument, the
  6107. search string that was part of the file link, the part after the
  6108. double colon. The function must first check if it would like to
  6109. handle this search, for example by checking the major-mode or the
  6110. file extension. If it decides not to handle this search, it
  6111. should just return nil to give other functions a chance. If it
  6112. does handle the search, it must return a non-nil value to keep
  6113. other functions from trying.
  6114. Each function can access the current prefix argument through the
  6115. variable `current-prefix-argument'. Note that a single prefix is
  6116. used to force opening a link in Emacs, so it may be good to only
  6117. use a numeric or double prefix to guide the search function.
  6118. In case this is needed, a function in this hook can also restore
  6119. the window configuration before `org-open-at-point' was called using:
  6120. (set-window-configuration org-window-config-before-follow-link)")
  6121. (defun org-link-search (s &optional type avoid-pos)
  6122. "Search for a link search option.
  6123. If S is surrounded by forward slashes, it is interpreted as a
  6124. regular expression. In org-mode files, this will create an `org-occur'
  6125. sparse tree. In ordinary files, `occur' will be used to list matches.
  6126. If the current buffer is in `dired-mode', grep will be used to search
  6127. in all files. If AVOID-POS is given, ignore matches near that position."
  6128. (let ((case-fold-search t)
  6129. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  6130. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  6131. (append '(("") (" ") ("\t") ("\n"))
  6132. org-emphasis-alist)
  6133. "\\|") "\\)"))
  6134. (pos (point))
  6135. (pre nil) (post nil)
  6136. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  6137. (cond
  6138. ;; First check if there are any special
  6139. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  6140. ;; Now try the builtin stuff
  6141. ((save-excursion
  6142. (goto-char (point-min))
  6143. (and
  6144. (re-search-forward
  6145. (concat "<<" (regexp-quote s0) ">>") nil t)
  6146. (setq type 'dedicated
  6147. pos (match-beginning 0))))
  6148. ;; There is an exact target for this
  6149. (goto-char pos))
  6150. ((string-match "^/\\(.*\\)/$" s)
  6151. ;; A regular expression
  6152. (cond
  6153. ((org-mode-p)
  6154. (org-occur (match-string 1 s)))
  6155. ;;((eq major-mode 'dired-mode)
  6156. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  6157. (t (org-do-occur (match-string 1 s)))))
  6158. (t
  6159. ;; A normal search strings
  6160. (when (equal (string-to-char s) ?*)
  6161. ;; Anchor on headlines, post may include tags.
  6162. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  6163. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  6164. s (substring s 1)))
  6165. (remove-text-properties
  6166. 0 (length s)
  6167. '(face nil mouse-face nil keymap nil fontified nil) s)
  6168. ;; Make a series of regular expressions to find a match
  6169. (setq words (org-split-string s "[ \n\r\t]+")
  6170. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  6171. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  6172. "\\)" markers)
  6173. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  6174. re2a (concat "[ \t\r\n]" re2a_)
  6175. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  6176. re4 (concat "[^a-zA-Z_]" re4_)
  6177. re1 (concat pre re2 post)
  6178. re3 (concat pre (if pre re4_ re4) post)
  6179. re5 (concat pre ".*" re4)
  6180. re2 (concat pre re2)
  6181. re2a (concat pre (if pre re2a_ re2a))
  6182. re4 (concat pre (if pre re4_ re4))
  6183. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  6184. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  6185. re5 "\\)"
  6186. ))
  6187. (cond
  6188. ((eq type 'org-occur) (org-occur reall))
  6189. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  6190. (t (goto-char (point-min))
  6191. (setq type 'fuzzy)
  6192. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  6193. (org-search-not-self 1 re1 nil t)
  6194. (org-search-not-self 1 re2 nil t)
  6195. (org-search-not-self 1 re2a nil t)
  6196. (org-search-not-self 1 re3 nil t)
  6197. (org-search-not-self 1 re4 nil t)
  6198. (org-search-not-self 1 re5 nil t)
  6199. )
  6200. (goto-char (match-beginning 1))
  6201. (goto-char pos)
  6202. (error "No match")))))
  6203. (t
  6204. ;; Normal string-search
  6205. (goto-char (point-min))
  6206. (if (search-forward s nil t)
  6207. (goto-char (match-beginning 0))
  6208. (error "No match"))))
  6209. (and (org-mode-p) (org-show-context 'link-search))
  6210. type))
  6211. (defun org-search-not-self (group &rest args)
  6212. "Execute `re-search-forward', but only accept matches that do not
  6213. enclose the position of `org-open-link-marker'."
  6214. (let ((m org-open-link-marker))
  6215. (catch 'exit
  6216. (while (apply 're-search-forward args)
  6217. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  6218. (goto-char (match-end group))
  6219. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  6220. (> (match-beginning 0) (marker-position m))
  6221. (< (match-end 0) (marker-position m)))
  6222. (save-match-data
  6223. (or (not (org-in-regexp
  6224. org-bracket-link-analytic-regexp 1))
  6225. (not (match-end 4)) ; no description
  6226. (and (<= (match-beginning 4) (point))
  6227. (>= (match-end 4) (point))))))
  6228. (throw 'exit (point))))))))
  6229. (defun org-get-buffer-for-internal-link (buffer)
  6230. "Return a buffer to be used for displaying the link target of internal links."
  6231. (cond
  6232. ((not org-display-internal-link-with-indirect-buffer)
  6233. buffer)
  6234. ((string-match "(Clone)$" (buffer-name buffer))
  6235. (message "Buffer is already a clone, not making another one")
  6236. ;; we also do not modify visibility in this case
  6237. buffer)
  6238. (t ; make a new indirect buffer for displaying the link
  6239. (let* ((bn (buffer-name buffer))
  6240. (ibn (concat bn "(Clone)"))
  6241. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  6242. (with-current-buffer ib (org-overview))
  6243. ib))))
  6244. (defun org-do-occur (regexp &optional cleanup)
  6245. "Call the Emacs command `occur'.
  6246. If CLEANUP is non-nil, remove the printout of the regular expression
  6247. in the *Occur* buffer. This is useful if the regex is long and not useful
  6248. to read."
  6249. (occur regexp)
  6250. (when cleanup
  6251. (let ((cwin (selected-window)) win beg end)
  6252. (when (setq win (get-buffer-window "*Occur*"))
  6253. (select-window win))
  6254. (goto-char (point-min))
  6255. (when (re-search-forward "match[a-z]+" nil t)
  6256. (setq beg (match-end 0))
  6257. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  6258. (setq end (1- (match-beginning 0)))))
  6259. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  6260. (goto-char (point-min))
  6261. (select-window cwin))))
  6262. ;;; The mark ring for links jumps
  6263. (defvar org-mark-ring nil
  6264. "Mark ring for positions before jumps in Org-mode.")
  6265. (defvar org-mark-ring-last-goto nil
  6266. "Last position in the mark ring used to go back.")
  6267. ;; Fill and close the ring
  6268. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  6269. (loop for i from 1 to org-mark-ring-length do
  6270. (push (make-marker) org-mark-ring))
  6271. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  6272. org-mark-ring)
  6273. (defun org-mark-ring-push (&optional pos buffer)
  6274. "Put the current position or POS into the mark ring and rotate it."
  6275. (interactive)
  6276. (setq pos (or pos (point)))
  6277. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  6278. (move-marker (car org-mark-ring)
  6279. (or pos (point))
  6280. (or buffer (current-buffer)))
  6281. (message "%s"
  6282. (substitute-command-keys
  6283. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  6284. (defun org-mark-ring-goto (&optional n)
  6285. "Jump to the previous position in the mark ring.
  6286. With prefix arg N, jump back that many stored positions. When
  6287. called several times in succession, walk through the entire ring.
  6288. Org-mode commands jumping to a different position in the current file,
  6289. or to another Org-mode file, automatically push the old position
  6290. onto the ring."
  6291. (interactive "p")
  6292. (let (p m)
  6293. (if (eq last-command this-command)
  6294. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  6295. (setq p org-mark-ring))
  6296. (setq org-mark-ring-last-goto p)
  6297. (setq m (car p))
  6298. (switch-to-buffer (marker-buffer m))
  6299. (goto-char m)
  6300. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  6301. (defun org-remove-angle-brackets (s)
  6302. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  6303. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  6304. s)
  6305. (defun org-add-angle-brackets (s)
  6306. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  6307. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  6308. s)
  6309. (defun org-remove-double-quotes (s)
  6310. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  6311. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  6312. s)
  6313. ;;; Following specific links
  6314. (defun org-follow-timestamp-link ()
  6315. (cond
  6316. ((org-at-date-range-p t)
  6317. (let ((org-agenda-start-on-weekday)
  6318. (t1 (match-string 1))
  6319. (t2 (match-string 2)))
  6320. (setq t1 (time-to-days (org-time-string-to-time t1))
  6321. t2 (time-to-days (org-time-string-to-time t2)))
  6322. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  6323. ((org-at-timestamp-p t)
  6324. (org-agenda-list nil (time-to-days (org-time-string-to-time
  6325. (substring (match-string 1) 0 10)))
  6326. 1))
  6327. (t (error "This should not happen"))))
  6328. ;;; Following file links
  6329. (defvar org-wait nil)
  6330. (defun org-open-file (path &optional in-emacs line search)
  6331. "Open the file at PATH.
  6332. First, this expands any special file name abbreviations. Then the
  6333. configuration variable `org-file-apps' is checked if it contains an
  6334. entry for this file type, and if yes, the corresponding command is launched.
  6335. If no application is found, Emacs simply visits the file.
  6336. With optional argument IN-EMACS, Emacs will visit the file.
  6337. Optional LINE specifies a line to go to, optional SEARCH a string to
  6338. search for. If LINE or SEARCH is given, the file will always be
  6339. opened in Emacs.
  6340. If the file does not exist, an error is thrown."
  6341. (setq in-emacs (or in-emacs line search))
  6342. (let* ((file (if (equal path "")
  6343. buffer-file-name
  6344. (substitute-in-file-name (expand-file-name path))))
  6345. (apps (append org-file-apps (org-default-apps)))
  6346. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  6347. (dirp (if remp nil (file-directory-p file)))
  6348. (file (if (and dirp org-open-directory-means-index-dot-org)
  6349. (concat (file-name-as-directory file) "index.org")
  6350. file))
  6351. (a-m-a-p (assq 'auto-mode apps))
  6352. (dfile (downcase file))
  6353. (old-buffer (current-buffer))
  6354. (old-pos (point))
  6355. (old-mode major-mode)
  6356. ext cmd)
  6357. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  6358. (setq ext (match-string 1 dfile))
  6359. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  6360. (setq ext (match-string 1 dfile))))
  6361. (if in-emacs
  6362. (setq cmd 'emacs)
  6363. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  6364. (and dirp (cdr (assoc 'directory apps)))
  6365. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  6366. 'string-match)
  6367. (cdr (assoc ext apps))
  6368. (cdr (assoc t apps)))))
  6369. (when (eq cmd 'default)
  6370. (setq cmd (cdr (assoc t apps))))
  6371. (when (eq cmd 'mailcap)
  6372. (require 'mailcap)
  6373. (mailcap-parse-mailcaps)
  6374. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  6375. (command (mailcap-mime-info mime-type)))
  6376. (if (stringp command)
  6377. (setq cmd command)
  6378. (setq cmd 'emacs))))
  6379. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  6380. (not (file-exists-p file))
  6381. (not org-open-non-existing-files))
  6382. (error "No such file: %s" file))
  6383. (cond
  6384. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  6385. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  6386. (while (string-match "['\"]%s['\"]" cmd)
  6387. (setq cmd (replace-match "%s" t t cmd)))
  6388. (while (string-match "%s" cmd)
  6389. (setq cmd (replace-match
  6390. (save-match-data
  6391. (shell-quote-argument
  6392. (convert-standard-filename file)))
  6393. t t cmd)))
  6394. (save-window-excursion
  6395. (start-process-shell-command cmd nil cmd)
  6396. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  6397. ))
  6398. ((or (stringp cmd)
  6399. (eq cmd 'emacs))
  6400. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  6401. (widen)
  6402. (if line (goto-line line)
  6403. (if search (org-link-search search))))
  6404. ((consp cmd)
  6405. (let ((file (convert-standard-filename file)))
  6406. (eval cmd)))
  6407. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  6408. (and (org-mode-p) (eq old-mode 'org-mode)
  6409. (or (not (equal old-buffer (current-buffer)))
  6410. (not (equal old-pos (point))))
  6411. (org-mark-ring-push old-pos old-buffer))))
  6412. (defun org-default-apps ()
  6413. "Return the default applications for this operating system."
  6414. (cond
  6415. ((eq system-type 'darwin)
  6416. org-file-apps-defaults-macosx)
  6417. ((eq system-type 'windows-nt)
  6418. org-file-apps-defaults-windowsnt)
  6419. (t org-file-apps-defaults-gnu)))
  6420. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  6421. "Convert extensions to regular expressions in the cars of LIST.
  6422. Also, weed out any non-string entries, because the return value is used
  6423. only for regexp matching.
  6424. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  6425. point to the symbol `emacs', indicating that the file should
  6426. be opened in Emacs."
  6427. (append
  6428. (delq nil
  6429. (mapcar (lambda (x)
  6430. (if (not (stringp (car x)))
  6431. nil
  6432. (if (string-match "\\W" (car x))
  6433. x
  6434. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  6435. list))
  6436. (if add-auto-mode
  6437. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  6438. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  6439. (defun org-file-remote-p (file)
  6440. "Test whether FILE specifies a location on a remote system.
  6441. Return non-nil if the location is indeed remote.
  6442. For example, the filename \"/user@host:/foo\" specifies a location
  6443. on the system \"/user@host:\"."
  6444. (cond ((fboundp 'file-remote-p)
  6445. (file-remote-p file))
  6446. ((fboundp 'tramp-handle-file-remote-p)
  6447. (tramp-handle-file-remote-p file))
  6448. ((and (boundp 'ange-ftp-name-format)
  6449. (string-match (car ange-ftp-name-format) file))
  6450. t)
  6451. (t nil)))
  6452. ;;;; Refiling
  6453. (defun org-get-org-file ()
  6454. "Read a filename, with default directory `org-directory'."
  6455. (let ((default (or org-default-notes-file remember-data-file)))
  6456. (read-file-name (format "File name [%s]: " default)
  6457. (file-name-as-directory org-directory)
  6458. default)))
  6459. (defun org-notes-order-reversed-p ()
  6460. "Check if the current file should receive notes in reversed order."
  6461. (cond
  6462. ((not org-reverse-note-order) nil)
  6463. ((eq t org-reverse-note-order) t)
  6464. ((not (listp org-reverse-note-order)) nil)
  6465. (t (catch 'exit
  6466. (let ((all org-reverse-note-order)
  6467. entry)
  6468. (while (setq entry (pop all))
  6469. (if (string-match (car entry) buffer-file-name)
  6470. (throw 'exit (cdr entry))))
  6471. nil)))))
  6472. (defvar org-refile-target-table nil
  6473. "The list of refile targets, created by `org-refile'.")
  6474. (defvar org-agenda-new-buffers nil
  6475. "Buffers created to visit agenda files.")
  6476. (defun org-get-refile-targets (&optional default-buffer)
  6477. "Produce a table with refile targets."
  6478. (let ((entries (or org-refile-targets '((nil . (:level . 1)))))
  6479. targets txt re files f desc descre)
  6480. (with-current-buffer (or default-buffer (current-buffer))
  6481. (while (setq entry (pop entries))
  6482. (setq files (car entry) desc (cdr entry))
  6483. (cond
  6484. ((null files) (setq files (list (current-buffer))))
  6485. ((eq files 'org-agenda-files)
  6486. (setq files (org-agenda-files 'unrestricted)))
  6487. ((and (symbolp files) (fboundp files))
  6488. (setq files (funcall files)))
  6489. ((and (symbolp files) (boundp files))
  6490. (setq files (symbol-value files))))
  6491. (if (stringp files) (setq files (list files)))
  6492. (cond
  6493. ((eq (car desc) :tag)
  6494. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  6495. ((eq (car desc) :todo)
  6496. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  6497. ((eq (car desc) :regexp)
  6498. (setq descre (cdr desc)))
  6499. ((eq (car desc) :level)
  6500. (setq descre (concat "^\\*\\{" (number-to-string
  6501. (if org-odd-levels-only
  6502. (1- (* 2 (cdr desc)))
  6503. (cdr desc)))
  6504. "\\}[ \t]")))
  6505. ((eq (car desc) :maxlevel)
  6506. (setq descre (concat "^\\*\\{1," (number-to-string
  6507. (if org-odd-levels-only
  6508. (1- (* 2 (cdr desc)))
  6509. (cdr desc)))
  6510. "\\}[ \t]")))
  6511. (t (error "Bad refiling target description %s" desc)))
  6512. (while (setq f (pop files))
  6513. (save-excursion
  6514. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  6515. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  6516. (save-excursion
  6517. (save-restriction
  6518. (widen)
  6519. (goto-char (point-min))
  6520. (while (re-search-forward descre nil t)
  6521. (goto-char (point-at-bol))
  6522. (when (looking-at org-complex-heading-regexp)
  6523. (setq txt (org-link-display-format (match-string 4))
  6524. re (concat "^" (regexp-quote
  6525. (buffer-substring (match-beginning 1)
  6526. (match-end 4)))))
  6527. (if (match-end 5) (setq re (concat re "[ \t]+"
  6528. (regexp-quote
  6529. (match-string 5)))))
  6530. (setq re (concat re "[ \t]*$"))
  6531. (when org-refile-use-outline-path
  6532. (setq txt (mapconcat 'org-protect-slash
  6533. (append
  6534. (if (eq org-refile-use-outline-path 'file)
  6535. (list (file-name-nondirectory
  6536. (buffer-file-name (buffer-base-buffer))))
  6537. (if (eq org-refile-use-outline-path 'full-file-path)
  6538. (list (buffer-file-name (buffer-base-buffer)))))
  6539. (org-get-outline-path)
  6540. (list txt))
  6541. "/")))
  6542. (push (list txt f re (point)) targets))
  6543. (goto-char (point-at-eol))))))))
  6544. (nreverse targets))))
  6545. (defun org-protect-slash (s)
  6546. (while (string-match "/" s)
  6547. (setq s (replace-match "\\" t t s)))
  6548. s)
  6549. (defun org-get-outline-path ()
  6550. "Return the outline path to the current entry, as a list."
  6551. (let (rtn)
  6552. (save-excursion
  6553. (while (org-up-heading-safe)
  6554. (when (looking-at org-complex-heading-regexp)
  6555. (push (org-match-string-no-properties 4) rtn)))
  6556. rtn)))
  6557. (defvar org-refile-history nil
  6558. "History for refiling operations.")
  6559. (defun org-refile (&optional goto default-buffer)
  6560. "Move the entry at point to another heading.
  6561. The list of target headings is compiled using the information in
  6562. `org-refile-targets', which see. This list is created before each use
  6563. and will therefore always be up-to-date.
  6564. At the target location, the entry is filed as a subitem of the target heading.
  6565. Depending on `org-reverse-note-order', the new subitem will either be the
  6566. first or the last subitem.
  6567. With prefix arg GOTO, the command will only visit the target location,
  6568. not actually move anything.
  6569. With a double prefix `C-u C-u', go to the location where the last refiling
  6570. operation has put the subtree."
  6571. (interactive "P")
  6572. (let* ((cbuf (current-buffer))
  6573. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  6574. pos it nbuf file re level reversed)
  6575. (if (equal goto '(16))
  6576. (org-refile-goto-last-stored)
  6577. (when (setq it (org-refile-get-location
  6578. (if goto "Goto: " "Refile to: ") default-buffer))
  6579. (setq file (nth 1 it)
  6580. re (nth 2 it)
  6581. pos (nth 3 it))
  6582. (setq nbuf (or (find-buffer-visiting file)
  6583. (find-file-noselect file)))
  6584. (if goto
  6585. (progn
  6586. (switch-to-buffer nbuf)
  6587. (goto-char pos)
  6588. (org-show-context 'org-goto))
  6589. (org-copy-subtree 1 nil t)
  6590. (save-excursion
  6591. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  6592. (find-file-noselect file))))
  6593. (setq reversed (org-notes-order-reversed-p))
  6594. (save-excursion
  6595. (save-restriction
  6596. (widen)
  6597. (goto-char pos)
  6598. (looking-at outline-regexp)
  6599. (setq level (org-get-valid-level (funcall outline-level) 1))
  6600. (goto-char
  6601. (if reversed
  6602. (or (outline-next-heading) (point-max))
  6603. (or (save-excursion (outline-get-next-sibling))
  6604. (org-end-of-subtree t t)
  6605. (point-max))))
  6606. (if (not (bolp)) (newline))
  6607. (bookmark-set "org-refile-last-stored")
  6608. (org-paste-subtree level))))
  6609. (org-cut-subtree)
  6610. (setq org-markers-to-move nil)
  6611. (message "Entry refiled to \"%s\"" (car it)))))))
  6612. (defun org-refile-goto-last-stored ()
  6613. "Go to the location where the last refile was stored."
  6614. (interactive)
  6615. (bookmark-jump "org-refile-last-stored")
  6616. (message "This is the location of the last refile"))
  6617. (defun org-refile-get-location (&optional prompt default-buffer)
  6618. "Prompt the user for a refile location, using PROMPT."
  6619. (let ((org-refile-targets org-refile-targets)
  6620. (org-refile-use-outline-path org-refile-use-outline-path))
  6621. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  6622. (unless org-refile-target-table
  6623. (error "No refile targets"))
  6624. (let* ((cbuf (current-buffer))
  6625. (cfunc (if org-refile-use-outline-path
  6626. 'org-olpath-completing-read
  6627. 'completing-read))
  6628. (extra (if org-refile-use-outline-path "/" ""))
  6629. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  6630. (fname (and filename (file-truename filename)))
  6631. (tbl (mapcar
  6632. (lambda (x)
  6633. (if (not (equal fname (file-truename (nth 1 x))))
  6634. (cons (concat (car x) extra " ("
  6635. (file-name-nondirectory (nth 1 x)) ")")
  6636. (cdr x))
  6637. (cons (concat (car x) extra) (cdr x))))
  6638. org-refile-target-table))
  6639. (completion-ignore-case t))
  6640. (assoc (funcall cfunc prompt tbl nil t nil 'org-refile-history)
  6641. tbl)))
  6642. (defun org-olpath-completing-read (prompt collection &rest args)
  6643. "Read an outline path like a file name."
  6644. (let ((thetable collection))
  6645. (apply
  6646. 'completing-read prompt
  6647. (lambda (string predicate &optional flag)
  6648. (let (rtn r s f (l (length string)))
  6649. (cond
  6650. ((eq flag nil)
  6651. ;; try completion
  6652. (try-completion string thetable))
  6653. ((eq flag t)
  6654. ;; all-completions
  6655. (setq rtn (all-completions string thetable predicate))
  6656. (mapcar
  6657. (lambda (x)
  6658. (setq r (substring x l))
  6659. (if (string-match " ([^)]*)$" x)
  6660. (setq f (match-string 0 x))
  6661. (setq f ""))
  6662. (if (string-match "/" r)
  6663. (concat string (substring r 0 (match-end 0)) f)
  6664. x))
  6665. rtn))
  6666. ((eq flag 'lambda)
  6667. ;; exact match?
  6668. (assoc string thetable)))
  6669. ))
  6670. args)))
  6671. ;;;; Dynamic blocks
  6672. (defun org-find-dblock (name)
  6673. "Find the first dynamic block with name NAME in the buffer.
  6674. If not found, stay at current position and return nil."
  6675. (let (pos)
  6676. (save-excursion
  6677. (goto-char (point-min))
  6678. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  6679. nil t)
  6680. (match-beginning 0))))
  6681. (if pos (goto-char pos))
  6682. pos))
  6683. (defconst org-dblock-start-re
  6684. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  6685. "Matches the startline of a dynamic block, with parameters.")
  6686. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  6687. "Matches the end of a dyhamic block.")
  6688. (defun org-create-dblock (plist)
  6689. "Create a dynamic block section, with parameters taken from PLIST.
  6690. PLIST must containe a :name entry which is used as name of the block."
  6691. (unless (bolp) (newline))
  6692. (let ((name (plist-get plist :name)))
  6693. (insert "#+BEGIN: " name)
  6694. (while plist
  6695. (if (eq (car plist) :name)
  6696. (setq plist (cddr plist))
  6697. (insert " " (prin1-to-string (pop plist)))))
  6698. (insert "\n\n#+END:\n")
  6699. (beginning-of-line -2)))
  6700. (defun org-prepare-dblock ()
  6701. "Prepare dynamic block for refresh.
  6702. This empties the block, puts the cursor at the insert position and returns
  6703. the property list including an extra property :name with the block name."
  6704. (unless (looking-at org-dblock-start-re)
  6705. (error "Not at a dynamic block"))
  6706. (let* ((begdel (1+ (match-end 0)))
  6707. (name (org-no-properties (match-string 1)))
  6708. (params (append (list :name name)
  6709. (read (concat "(" (match-string 3) ")")))))
  6710. (unless (re-search-forward org-dblock-end-re nil t)
  6711. (error "Dynamic block not terminated"))
  6712. (setq params
  6713. (append params
  6714. (list :content (buffer-substring
  6715. begdel (match-beginning 0)))))
  6716. (delete-region begdel (match-beginning 0))
  6717. (goto-char begdel)
  6718. (open-line 1)
  6719. params))
  6720. (defun org-map-dblocks (&optional command)
  6721. "Apply COMMAND to all dynamic blocks in the current buffer.
  6722. If COMMAND is not given, use `org-update-dblock'."
  6723. (let ((cmd (or command 'org-update-dblock))
  6724. pos)
  6725. (save-excursion
  6726. (goto-char (point-min))
  6727. (while (re-search-forward org-dblock-start-re nil t)
  6728. (goto-char (setq pos (match-beginning 0)))
  6729. (condition-case nil
  6730. (funcall cmd)
  6731. (error (message "Error during update of dynamic block")))
  6732. (goto-char pos)
  6733. (unless (re-search-forward org-dblock-end-re nil t)
  6734. (error "Dynamic block not terminated"))))))
  6735. (defun org-dblock-update (&optional arg)
  6736. "User command for updating dynamic blocks.
  6737. Update the dynamic block at point. With prefix ARG, update all dynamic
  6738. blocks in the buffer."
  6739. (interactive "P")
  6740. (if arg
  6741. (org-update-all-dblocks)
  6742. (or (looking-at org-dblock-start-re)
  6743. (org-beginning-of-dblock))
  6744. (org-update-dblock)))
  6745. (defun org-update-dblock ()
  6746. "Update the dynamic block at point
  6747. This means to empty the block, parse for parameters and then call
  6748. the correct writing function."
  6749. (save-window-excursion
  6750. (let* ((pos (point))
  6751. (line (org-current-line))
  6752. (params (org-prepare-dblock))
  6753. (name (plist-get params :name))
  6754. (cmd (intern (concat "org-dblock-write:" name))))
  6755. (message "Updating dynamic block `%s' at line %d..." name line)
  6756. (funcall cmd params)
  6757. (message "Updating dynamic block `%s' at line %d...done" name line)
  6758. (goto-char pos))))
  6759. (defun org-beginning-of-dblock ()
  6760. "Find the beginning of the dynamic block at point.
  6761. Error if there is no scuh block at point."
  6762. (let ((pos (point))
  6763. beg)
  6764. (end-of-line 1)
  6765. (if (and (re-search-backward org-dblock-start-re nil t)
  6766. (setq beg (match-beginning 0))
  6767. (re-search-forward org-dblock-end-re nil t)
  6768. (> (match-end 0) pos))
  6769. (goto-char beg)
  6770. (goto-char pos)
  6771. (error "Not in a dynamic block"))))
  6772. (defun org-update-all-dblocks ()
  6773. "Update all dynamic blocks in the buffer.
  6774. This function can be used in a hook."
  6775. (when (org-mode-p)
  6776. (org-map-dblocks 'org-update-dblock)))
  6777. ;;;; Completion
  6778. (defconst org-additional-option-like-keywords
  6779. '("BEGIN_HTML" "BEGIN_LaTeX" "END_HTML" "END_LaTeX"
  6780. "ORGTBL" "HTML:" "LaTeX:" "BEGIN:" "END:" "TBLFM"
  6781. "BEGIN_EXAMPLE" "END_EXAMPLE"
  6782. "BEGIN_QUOTE" "END_QUOTE"
  6783. "BEGIN_VERSE" "END_VERSE"
  6784. "BEGIN_SRC" "END_SRC"))
  6785. (defcustom org-structure-template-alist
  6786. '(
  6787. ("s" "#+begin_src ?\n\n#+end_src"
  6788. "<src lang=\"?\">\n\n</src>")
  6789. ("e" "#+begin_example\n?\n#+end_example"
  6790. "<example>\n?\n</example>")
  6791. ("q" "#+begin_quote\n?\n#+end_quote"
  6792. "<quote>\n?\n</quote>")
  6793. ("v" "#+begin_verse\n?\n#+end_verse"
  6794. "<verse>\n?\n/verse>")
  6795. ("l" "#+begin_latex\n?\n#+end_latex"
  6796. "<literal style=\"latex\">\n?\n</literal>")
  6797. ("L" "#+latex: "
  6798. "<literal style=\"latex\">?</literal>")
  6799. ("h" "#+begin_html\n?\n#+end_html"
  6800. "<literal style=\"html\">\n?\n</literal>")
  6801. ("H" "#+html: "
  6802. "<literal style=\"html\">?</literal>")
  6803. ("a" "#+begin_ascii\n?\n#+end_ascii")
  6804. ("A" "#+ascii: ")
  6805. ("i" "#+include %file ?"
  6806. "<include file=%file markup=\"?\">")
  6807. )
  6808. "Structure completion elements.
  6809. This is a list of abbreviation keys and values. The value gets inserted
  6810. it you type @samp{.} followed by the key and then the completion key,
  6811. usually `M-TAB'. %file will be replaced by a file name after prompting
  6812. for the file uning completion.
  6813. There are two templates for each key, the first uses the original Org syntax,
  6814. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  6815. the default when the /org-mtags.el/ module has been loaded. See also the
  6816. variable `org-mtags-prefere-muse-templates'.
  6817. This is an experimental feature, it is undecided if it is going to stay in."
  6818. :group 'org-completion
  6819. :type '(repeat
  6820. (string :tag "Key")
  6821. (string :tag "Template")
  6822. (string :tag "Muse Template")))
  6823. (defun org-try-structure-completion ()
  6824. "Try to complete a structure template before point.
  6825. This looks for strings like \"<e\" on an otherwise empty line and
  6826. expands them."
  6827. (let ((l (buffer-substring (point-at-bol) (point)))
  6828. a)
  6829. (when (and (looking-at "[ \t]*$")
  6830. (string-match "^[ \t]*<\\([a-z]+\\)$"l)
  6831. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  6832. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  6833. (match-beginning 1)) a)
  6834. t)))
  6835. (defun org-complete-expand-structure-template (start cell)
  6836. "Expand a structure template."
  6837. (let* ((musep (org-bound-and-true-p org-mtags-prefere-muse-templates))
  6838. (rpl (nth (if musep 2 1) cell)))
  6839. (delete-region start (point))
  6840. (when (string-match "\\`#\\+" rpl)
  6841. (cond
  6842. ((bolp))
  6843. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  6844. (delete-region (point-at-bol) (point)))
  6845. (t (newline))))
  6846. (setq start (point))
  6847. (if (string-match "%file" rpl)
  6848. (setq rpl (replace-match
  6849. (concat
  6850. "\""
  6851. (save-match-data
  6852. (abbreviate-file-name (read-file-name "Include file: ")))
  6853. "\"")
  6854. t t rpl)))
  6855. (insert rpl)
  6856. (if (re-search-backward "\\?" start t) (delete-char 1))))
  6857. (defun org-complete (&optional arg)
  6858. "Perform completion on word at point.
  6859. At the beginning of a headline, this completes TODO keywords as given in
  6860. `org-todo-keywords'.
  6861. If the current word is preceded by a backslash, completes the TeX symbols
  6862. that are supported for HTML support.
  6863. If the current word is preceded by \"#+\", completes special words for
  6864. setting file options.
  6865. In the line after \"#+STARTUP:, complete valid keywords.\"
  6866. At all other locations, this simply calls the value of
  6867. `org-completion-fallback-command'."
  6868. (interactive "P")
  6869. (org-without-partial-completion
  6870. (catch 'exit
  6871. (let* ((a nil)
  6872. (end (point))
  6873. (beg1 (save-excursion
  6874. (skip-chars-backward (org-re "[:alnum:]_@"))
  6875. (point)))
  6876. (beg (save-excursion
  6877. (skip-chars-backward "a-zA-Z0-9_:$")
  6878. (point)))
  6879. (confirm (lambda (x) (stringp (car x))))
  6880. (searchhead (equal (char-before beg) ?*))
  6881. (struct
  6882. (when (and (member (char-before beg1) '(?. ?<))
  6883. (setq a (assoc (buffer-substring beg1 (point))
  6884. org-structure-template-alist)))
  6885. (org-complete-expand-structure-template (1- beg1) a)
  6886. (throw 'exit t)))
  6887. (tag (and (equal (char-before beg1) ?:)
  6888. (equal (char-after (point-at-bol)) ?*)))
  6889. (prop (and (equal (char-before beg1) ?:)
  6890. (not (equal (char-after (point-at-bol)) ?*))))
  6891. (texp (equal (char-before beg) ?\\))
  6892. (link (equal (char-before beg) ?\[))
  6893. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  6894. beg)
  6895. "#+"))
  6896. (startup (string-match "^#\\+STARTUP:.*"
  6897. (buffer-substring (point-at-bol) (point))))
  6898. (completion-ignore-case opt)
  6899. (type nil)
  6900. (tbl nil)
  6901. (table (cond
  6902. (opt
  6903. (setq type :opt)
  6904. (require 'org-exp)
  6905. (append
  6906. (mapcar
  6907. (lambda (x)
  6908. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  6909. (cons (match-string 2 x) (match-string 1 x)))
  6910. (org-split-string (org-get-current-options) "\n"))
  6911. (mapcar 'list org-additional-option-like-keywords)))
  6912. (startup
  6913. (setq type :startup)
  6914. org-startup-options)
  6915. (link (append org-link-abbrev-alist-local
  6916. org-link-abbrev-alist))
  6917. (texp
  6918. (setq type :tex)
  6919. org-html-entities)
  6920. ((string-match "\\`\\*+[ \t]+\\'"
  6921. (buffer-substring (point-at-bol) beg))
  6922. (setq type :todo)
  6923. (mapcar 'list org-todo-keywords-1))
  6924. (searchhead
  6925. (setq type :searchhead)
  6926. (save-excursion
  6927. (goto-char (point-min))
  6928. (while (re-search-forward org-todo-line-regexp nil t)
  6929. (push (list
  6930. (org-make-org-heading-search-string
  6931. (match-string 3) t))
  6932. tbl)))
  6933. tbl)
  6934. (tag (setq type :tag beg beg1)
  6935. (or org-tag-alist (org-get-buffer-tags)))
  6936. (prop (setq type :prop beg beg1)
  6937. (mapcar 'list (org-buffer-property-keys nil t t)))
  6938. (t (progn
  6939. (call-interactively org-completion-fallback-command)
  6940. (throw 'exit nil)))))
  6941. (pattern (buffer-substring-no-properties beg end))
  6942. (completion (try-completion pattern table confirm)))
  6943. (cond ((eq completion t)
  6944. (if (not (assoc (upcase pattern) table))
  6945. (message "Already complete")
  6946. (if (and (equal type :opt)
  6947. (not (member (car (assoc (upcase pattern) table))
  6948. org-additional-option-like-keywords)))
  6949. (insert (substring (cdr (assoc (upcase pattern) table))
  6950. (length pattern)))
  6951. (if (memq type '(:tag :prop)) (insert ":")))))
  6952. ((null completion)
  6953. (message "Can't find completion for \"%s\"" pattern)
  6954. (ding))
  6955. ((not (string= pattern completion))
  6956. (delete-region beg end)
  6957. (if (string-match " +$" completion)
  6958. (setq completion (replace-match "" t t completion)))
  6959. (insert completion)
  6960. (if (get-buffer-window "*Completions*")
  6961. (delete-window (get-buffer-window "*Completions*")))
  6962. (if (assoc completion table)
  6963. (if (eq type :todo) (insert " ")
  6964. (if (memq type '(:tag :prop)) (insert ":"))))
  6965. (if (and (equal type :opt) (assoc completion table))
  6966. (message "%s" (substitute-command-keys
  6967. "Press \\[org-complete] again to insert example settings"))))
  6968. (t
  6969. (message "Making completion list...")
  6970. (let ((list (sort (all-completions pattern table confirm)
  6971. 'string<)))
  6972. (with-output-to-temp-buffer "*Completions*"
  6973. (condition-case nil
  6974. ;; Protection needed for XEmacs and emacs 21
  6975. (display-completion-list list pattern)
  6976. (error (display-completion-list list)))))
  6977. (message "Making completion list...%s" "done")))))))
  6978. ;;;; TODO, DEADLINE, Comments
  6979. (defun org-toggle-comment ()
  6980. "Change the COMMENT state of an entry."
  6981. (interactive)
  6982. (save-excursion
  6983. (org-back-to-heading)
  6984. (let (case-fold-search)
  6985. (if (looking-at (concat outline-regexp
  6986. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  6987. (replace-match "" t t nil 1)
  6988. (if (looking-at outline-regexp)
  6989. (progn
  6990. (goto-char (match-end 0))
  6991. (insert org-comment-string " ")))))))
  6992. (defvar org-last-todo-state-is-todo nil
  6993. "This is non-nil when the last TODO state change led to a TODO state.
  6994. If the last change removed the TODO tag or switched to DONE, then
  6995. this is nil.")
  6996. (defvar org-setting-tags nil) ; dynamically skiped
  6997. (defun org-parse-local-options (string var)
  6998. "Parse STRING for startup setting relevant for variable VAR."
  6999. (let ((rtn (symbol-value var))
  7000. e opts)
  7001. (save-match-data
  7002. (if (or (not string) (not (string-match "\\S-" string)))
  7003. rtn
  7004. (setq opts (delq nil (mapcar (lambda (x)
  7005. (setq e (assoc x org-startup-options))
  7006. (if (eq (nth 1 e) var) e nil))
  7007. (org-split-string string "[ \t]+"))))
  7008. (if (not opts)
  7009. rtn
  7010. (setq rtn nil)
  7011. (while (setq e (pop opts))
  7012. (if (not (nth 3 e))
  7013. (setq rtn (nth 2 e))
  7014. (if (not (listp rtn)) (setq rtn nil))
  7015. (push (nth 2 e) rtn)))
  7016. rtn)))))
  7017. (defvar org-blocker-hook nil
  7018. "Hook for functions that are allowed to block a state change.
  7019. Each function gets as its single argument a property list, see
  7020. `org-trigger-hook' for more information about this list.
  7021. If any of the functions in this hook returns nil, the state change
  7022. is blocked.")
  7023. (defvar org-trigger-hook nil
  7024. "Hook for functions that are triggered by a state change.
  7025. Each function gets as its single argument a property list with at least
  7026. the following elements:
  7027. (:type type-of-change :position pos-at-entry-start
  7028. :from old-state :to new-state)
  7029. Depending on the type, more properties may be present.
  7030. This mechanism is currently implemented for:
  7031. TODO state changes
  7032. ------------------
  7033. :type todo-state-change
  7034. :from previous state (keyword as a string), or nil
  7035. :to new state (keyword as a string), or nil")
  7036. (defun org-todo (&optional arg)
  7037. "Change the TODO state of an item.
  7038. The state of an item is given by a keyword at the start of the heading,
  7039. like
  7040. *** TODO Write paper
  7041. *** DONE Call mom
  7042. The different keywords are specified in the variable `org-todo-keywords'.
  7043. By default the available states are \"TODO\" and \"DONE\".
  7044. So for this example: when the item starts with TODO, it is changed to DONE.
  7045. When it starts with DONE, the DONE is removed. And when neither TODO nor
  7046. DONE are present, add TODO at the beginning of the heading.
  7047. With C-u prefix arg, use completion to determine the new state.
  7048. With numeric prefix arg, switch to that state.
  7049. For calling through lisp, arg is also interpreted in the following way:
  7050. 'none -> empty state
  7051. \"\"(empty string) -> switch to empty state
  7052. 'done -> switch to DONE
  7053. 'nextset -> switch to the next set of keywords
  7054. 'previousset -> switch to the previous set of keywords
  7055. \"WAITING\" -> switch to the specified keyword, but only if it
  7056. really is a member of `org-todo-keywords'."
  7057. (interactive "P")
  7058. (save-excursion
  7059. (catch 'exit
  7060. (org-back-to-heading)
  7061. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  7062. (or (looking-at (concat " +" org-todo-regexp " *"))
  7063. (looking-at " *"))
  7064. (let* ((match-data (match-data))
  7065. (startpos (point-at-bol))
  7066. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  7067. (org-log-done org-log-done)
  7068. (org-log-repeat org-log-repeat)
  7069. (org-todo-log-states org-todo-log-states)
  7070. (this (match-string 1))
  7071. (hl-pos (match-beginning 0))
  7072. (head (org-get-todo-sequence-head this))
  7073. (ass (assoc head org-todo-kwd-alist))
  7074. (interpret (nth 1 ass))
  7075. (done-word (nth 3 ass))
  7076. (final-done-word (nth 4 ass))
  7077. (last-state (or this ""))
  7078. (completion-ignore-case t)
  7079. (member (member this org-todo-keywords-1))
  7080. (tail (cdr member))
  7081. (state (cond
  7082. ((and org-todo-key-trigger
  7083. (or (and (equal arg '(4)) (eq org-use-fast-todo-selection 'prefix))
  7084. (and (not arg) org-use-fast-todo-selection
  7085. (not (eq org-use-fast-todo-selection 'prefix)))))
  7086. ;; Use fast selection
  7087. (org-fast-todo-selection))
  7088. ((and (equal arg '(4))
  7089. (or (not org-use-fast-todo-selection)
  7090. (not org-todo-key-trigger)))
  7091. ;; Read a state with completion
  7092. (completing-read "State: " (mapcar (lambda(x) (list x))
  7093. org-todo-keywords-1)
  7094. nil t))
  7095. ((eq arg 'right)
  7096. (if this
  7097. (if tail (car tail) nil)
  7098. (car org-todo-keywords-1)))
  7099. ((eq arg 'left)
  7100. (if (equal member org-todo-keywords-1)
  7101. nil
  7102. (if this
  7103. (nth (- (length org-todo-keywords-1) (length tail) 2)
  7104. org-todo-keywords-1)
  7105. (org-last org-todo-keywords-1))))
  7106. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  7107. (setq arg nil))) ; hack to fall back to cycling
  7108. (arg
  7109. ;; user or caller requests a specific state
  7110. (cond
  7111. ((equal arg "") nil)
  7112. ((eq arg 'none) nil)
  7113. ((eq arg 'done) (or done-word (car org-done-keywords)))
  7114. ((eq arg 'nextset)
  7115. (or (car (cdr (member head org-todo-heads)))
  7116. (car org-todo-heads)))
  7117. ((eq arg 'previousset)
  7118. (let ((org-todo-heads (reverse org-todo-heads)))
  7119. (or (car (cdr (member head org-todo-heads)))
  7120. (car org-todo-heads))))
  7121. ((car (member arg org-todo-keywords-1)))
  7122. ((nth (1- (prefix-numeric-value arg))
  7123. org-todo-keywords-1))))
  7124. ((null member) (or head (car org-todo-keywords-1)))
  7125. ((equal this final-done-word) nil) ;; -> make empty
  7126. ((null tail) nil) ;; -> first entry
  7127. ((eq interpret 'sequence)
  7128. (car tail))
  7129. ((memq interpret '(type priority))
  7130. (if (eq this-command last-command)
  7131. (car tail)
  7132. (if (> (length tail) 0)
  7133. (or done-word (car org-done-keywords))
  7134. nil)))
  7135. (t nil)))
  7136. (next (if state (concat " " state " ") " "))
  7137. (change-plist (list :type 'todo-state-change :from this :to state
  7138. :position startpos))
  7139. dolog now-done-p)
  7140. (when org-blocker-hook
  7141. (unless (save-excursion
  7142. (save-match-data
  7143. (run-hook-with-args-until-failure
  7144. 'org-blocker-hook change-plist)))
  7145. (if (interactive-p)
  7146. (error "TODO state change from %s to %s blocked" this state)
  7147. ;; fail silently
  7148. (message "TODO state change from %s to %s blocked" this state)
  7149. (throw 'exit nil))))
  7150. (store-match-data match-data)
  7151. (replace-match next t t)
  7152. (unless (pos-visible-in-window-p hl-pos)
  7153. (message "TODO state changed to %s" (org-trim next)))
  7154. (unless head
  7155. (setq head (org-get-todo-sequence-head state)
  7156. ass (assoc head org-todo-kwd-alist)
  7157. interpret (nth 1 ass)
  7158. done-word (nth 3 ass)
  7159. final-done-word (nth 4 ass)))
  7160. (when (memq arg '(nextset previousset))
  7161. (message "Keyword-Set %d/%d: %s"
  7162. (- (length org-todo-sets) -1
  7163. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  7164. (length org-todo-sets)
  7165. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  7166. (setq org-last-todo-state-is-todo
  7167. (not (member state org-done-keywords)))
  7168. (setq now-done-p (and (member state org-done-keywords)
  7169. (not (member this org-done-keywords))))
  7170. (and logging (org-local-logging logging))
  7171. (when (and (or org-todo-log-states org-log-done)
  7172. (not (memq arg '(nextset previousset))))
  7173. ;; we need to look at recording a time and note
  7174. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  7175. (nth 2 (assoc this org-todo-log-states))))
  7176. (when (and state
  7177. (member state org-not-done-keywords)
  7178. (not (member this org-not-done-keywords)))
  7179. ;; This is now a todo state and was not one before
  7180. ;; If there was a CLOSED time stamp, get rid of it.
  7181. (org-add-planning-info nil nil 'closed))
  7182. (when (and now-done-p org-log-done)
  7183. ;; It is now done, and it was not done before
  7184. (org-add-planning-info 'closed (org-current-time))
  7185. (if (and (not dolog) (eq 'note org-log-done))
  7186. (org-add-log-setup 'done state 'findpos 'note)))
  7187. (when (and state dolog)
  7188. ;; This is a non-nil state, and we need to log it
  7189. (org-add-log-setup 'state state 'findpos dolog)))
  7190. ;; Fixup tag positioning
  7191. (org-todo-trigger-tag-changes state)
  7192. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  7193. (when org-provide-todo-statistics
  7194. (org-update-parent-todo-statistics))
  7195. (run-hooks 'org-after-todo-state-change-hook)
  7196. (if (and arg (not (member state org-done-keywords)))
  7197. (setq head (org-get-todo-sequence-head state)))
  7198. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  7199. ;; Do we need to trigger a repeat?
  7200. (when now-done-p (org-auto-repeat-maybe state))
  7201. ;; Fixup cursor location if close to the keyword
  7202. (if (and (outline-on-heading-p)
  7203. (not (bolp))
  7204. (save-excursion (beginning-of-line 1)
  7205. (looking-at org-todo-line-regexp))
  7206. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  7207. (progn
  7208. (goto-char (or (match-end 2) (match-end 1)))
  7209. (just-one-space)))
  7210. (when org-trigger-hook
  7211. (save-excursion
  7212. (run-hook-with-args 'org-trigger-hook change-plist)))))))
  7213. (defun org-update-parent-todo-statistics ()
  7214. "Update any statistics cookie in the parent of the current headline."
  7215. (interactive)
  7216. (let ((box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  7217. level (cnt-all 0) (cnt-done 0) is-percent kwd)
  7218. (catch 'exit
  7219. (save-excursion
  7220. (setq level (org-up-heading-safe))
  7221. (unless (and level
  7222. (re-search-forward box-re (point-at-eol) t))
  7223. (throw 'exit nil))
  7224. (setq is-percent (match-end 2))
  7225. (save-match-data
  7226. (unless (outline-next-heading) (throw 'exit nil))
  7227. (while (looking-at org-todo-line-regexp)
  7228. (setq kwd (match-string 2))
  7229. (and kwd (setq cnt-all (1+ cnt-all)))
  7230. (and (member kwd org-done-keywords)
  7231. (setq cnt-done (1+ cnt-done)))
  7232. (condition-case nil
  7233. (org-forward-same-level 1)
  7234. (error (end-of-line 1)))))
  7235. (replace-match
  7236. (if is-percent
  7237. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  7238. (format "[%d/%d]" cnt-done cnt-all)))
  7239. (run-hook-with-args 'org-after-todo-statistics-hook
  7240. cnt-done (- cnt-all cnt-done))))))
  7241. (defvar org-after-todo-statistics-hook nil
  7242. "Hook that is called after a TODO statistics cookie has been updated.
  7243. Each function is called with two arguments: the number of not-done entries
  7244. and the number of done entries.
  7245. For example, the following function, when added to this hook, will switch
  7246. an entry to DONE when all children are done, and back to TODO when new
  7247. entries are set to a TODO status. Note that this hook is only called
  7248. when there is a statistics cookie in the headline!
  7249. (defun org-summary-todo (n-done n-not-done)
  7250. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  7251. (let (org-log-done org-log-states) ; turn off logging
  7252. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  7253. ")
  7254. (defun org-todo-trigger-tag-changes (state)
  7255. "Apply the changes defined in `org-todo-state-tags-triggers'."
  7256. (let ((l org-todo-state-tags-triggers)
  7257. changes)
  7258. (when (or (not state) (equal state ""))
  7259. (setq changes (append changes (cdr (assoc "" l)))))
  7260. (when (and (stringp state) (> (length state) 0))
  7261. (setq changes (append changes (cdr (assoc state l)))))
  7262. (when (member state org-not-done-keywords)
  7263. (setq changes (append changes (cdr (assoc 'todo l)))))
  7264. (when (member state org-done-keywords)
  7265. (setq changes (append changes (cdr (assoc 'done l)))))
  7266. (dolist (c changes)
  7267. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  7268. (defun org-local-logging (value)
  7269. "Get logging settings from a property VALUE."
  7270. (let* (words w a)
  7271. ;; directly set the variables, they are already local.
  7272. (setq org-log-done nil
  7273. org-log-repeat nil
  7274. org-todo-log-states nil)
  7275. (setq words (org-split-string value))
  7276. (while (setq w (pop words))
  7277. (cond
  7278. ((setq a (assoc w org-startup-options))
  7279. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  7280. (set (nth 1 a) (nth 2 a))))
  7281. ((setq a (org-extract-log-state-settings w))
  7282. (and (member (car a) org-todo-keywords-1)
  7283. (push a org-todo-log-states)))))))
  7284. (defun org-get-todo-sequence-head (kwd)
  7285. "Return the head of the TODO sequence to which KWD belongs.
  7286. If KWD is not set, check if there is a text property remembering the
  7287. right sequence."
  7288. (let (p)
  7289. (cond
  7290. ((not kwd)
  7291. (or (get-text-property (point-at-bol) 'org-todo-head)
  7292. (progn
  7293. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  7294. nil (point-at-eol)))
  7295. (get-text-property p 'org-todo-head))))
  7296. ((not (member kwd org-todo-keywords-1))
  7297. (car org-todo-keywords-1))
  7298. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  7299. (defun org-fast-todo-selection ()
  7300. "Fast TODO keyword selection with single keys.
  7301. Returns the new TODO keyword, or nil if no state change should occur."
  7302. (let* ((fulltable org-todo-key-alist)
  7303. (done-keywords org-done-keywords) ;; needed for the faces.
  7304. (maxlen (apply 'max (mapcar
  7305. (lambda (x)
  7306. (if (stringp (car x)) (string-width (car x)) 0))
  7307. fulltable)))
  7308. (expert nil)
  7309. (fwidth (+ maxlen 3 1 3))
  7310. (ncol (/ (- (window-width) 4) fwidth))
  7311. tg cnt e c tbl
  7312. groups ingroup)
  7313. (save-window-excursion
  7314. (if expert
  7315. (set-buffer (get-buffer-create " *Org todo*"))
  7316. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  7317. (erase-buffer)
  7318. (org-set-local 'org-done-keywords done-keywords)
  7319. (setq tbl fulltable cnt 0)
  7320. (while (setq e (pop tbl))
  7321. (cond
  7322. ((equal e '(:startgroup))
  7323. (push '() groups) (setq ingroup t)
  7324. (when (not (= cnt 0))
  7325. (setq cnt 0)
  7326. (insert "\n"))
  7327. (insert "{ "))
  7328. ((equal e '(:endgroup))
  7329. (setq ingroup nil cnt 0)
  7330. (insert "}\n"))
  7331. (t
  7332. (setq tg (car e) c (cdr e))
  7333. (if ingroup (push tg (car groups)))
  7334. (setq tg (org-add-props tg nil 'face
  7335. (org-get-todo-face tg)))
  7336. (if (and (= cnt 0) (not ingroup)) (insert " "))
  7337. (insert "[" c "] " tg (make-string
  7338. (- fwidth 4 (length tg)) ?\ ))
  7339. (when (= (setq cnt (1+ cnt)) ncol)
  7340. (insert "\n")
  7341. (if ingroup (insert " "))
  7342. (setq cnt 0)))))
  7343. (insert "\n")
  7344. (goto-char (point-min))
  7345. (if (and (not expert) (fboundp 'fit-window-to-buffer))
  7346. (fit-window-to-buffer))
  7347. (message "[a-z..]:Set [SPC]:clear")
  7348. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  7349. (cond
  7350. ((or (= c ?\C-g)
  7351. (and (= c ?q) (not (rassoc c fulltable))))
  7352. (setq quit-flag t))
  7353. ((= c ?\ ) nil)
  7354. ((setq e (rassoc c fulltable) tg (car e))
  7355. tg)
  7356. (t (setq quit-flag t))))))
  7357. (defun org-entry-is-todo-p ()
  7358. (member (org-get-todo-state) org-not-done-keywords))
  7359. (defun org-entry-is-done-p ()
  7360. (member (org-get-todo-state) org-done-keywords))
  7361. (defun org-get-todo-state ()
  7362. (save-excursion
  7363. (org-back-to-heading t)
  7364. (and (looking-at org-todo-line-regexp)
  7365. (match-end 2)
  7366. (match-string 2))))
  7367. (defun org-at-date-range-p (&optional inactive-ok)
  7368. "Is the cursor inside a date range?"
  7369. (interactive)
  7370. (save-excursion
  7371. (catch 'exit
  7372. (let ((pos (point)))
  7373. (skip-chars-backward "^[<\r\n")
  7374. (skip-chars-backward "<[")
  7375. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  7376. (>= (match-end 0) pos)
  7377. (throw 'exit t))
  7378. (skip-chars-backward "^<[\r\n")
  7379. (skip-chars-backward "<[")
  7380. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  7381. (>= (match-end 0) pos)
  7382. (throw 'exit t)))
  7383. nil)))
  7384. (defun org-get-repeat ()
  7385. "Check if there is a deadline/schedule with repeater in this entry."
  7386. (save-match-data
  7387. (save-excursion
  7388. (org-back-to-heading t)
  7389. (if (re-search-forward
  7390. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  7391. (match-string 1)))))
  7392. (defvar org-last-changed-timestamp)
  7393. (defvar org-last-inserted-timestamp)
  7394. (defvar org-log-post-message)
  7395. (defvar org-log-note-purpose)
  7396. (defvar org-log-note-how)
  7397. (defvar org-log-note-extra)
  7398. (defun org-auto-repeat-maybe (done-word)
  7399. "Check if the current headline contains a repeated deadline/schedule.
  7400. If yes, set TODO state back to what it was and change the base date
  7401. of repeating deadline/scheduled time stamps to new date.
  7402. This function is run automatically after each state change to a DONE state."
  7403. ;; last-state is dynamically scoped into this function
  7404. (let* ((repeat (org-get-repeat))
  7405. (aa (assoc last-state org-todo-kwd-alist))
  7406. (interpret (nth 1 aa))
  7407. (head (nth 2 aa))
  7408. (whata '(("d" . day) ("m" . month) ("y" . year)))
  7409. (msg "Entry repeats: ")
  7410. (org-log-done nil)
  7411. (org-todo-log-states nil)
  7412. (nshiftmax 10) (nshift 0)
  7413. re type n what ts mb0 time)
  7414. (when repeat
  7415. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  7416. (org-todo (if (eq interpret 'type) last-state head))
  7417. (when org-log-repeat
  7418. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  7419. (memq 'org-add-log-note post-command-hook))
  7420. ;; OK, we are already setup for some record
  7421. (if (eq org-log-repeat 'note)
  7422. ;; make sure we take a note, not only a time stamp
  7423. (setq org-log-note-how 'note))
  7424. ;; Set up for taking a record
  7425. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  7426. 'findpos org-log-repeat)))
  7427. (org-back-to-heading t)
  7428. (org-add-planning-info nil nil 'closed)
  7429. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  7430. org-deadline-time-regexp "\\)\\|\\("
  7431. org-ts-regexp "\\)"))
  7432. (while (re-search-forward
  7433. re (save-excursion (outline-next-heading) (point)) t)
  7434. (setq type (if (match-end 1) org-scheduled-string
  7435. (if (match-end 3) org-deadline-string "Plain:"))
  7436. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0)))
  7437. mb0 (match-beginning 0))
  7438. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  7439. (setq n (string-to-number (match-string 2 ts))
  7440. what (match-string 3 ts))
  7441. (if (equal what "w") (setq n (* n 7) what "d"))
  7442. ;; Preparation, see if we need to modify the start date for the change
  7443. (when (match-end 1)
  7444. (setq time (save-match-data (org-time-string-to-time ts)))
  7445. (cond
  7446. ((equal (match-string 1 ts) ".")
  7447. ;; Shift starting date to today
  7448. (org-timestamp-change
  7449. (- (time-to-days (current-time)) (time-to-days time))
  7450. 'day))
  7451. ((equal (match-string 1 ts) "+")
  7452. (while (or (= nshift 0)
  7453. (<= (time-to-days time) (time-to-days (current-time))))
  7454. (when (= (incf nshift) nshiftmax)
  7455. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  7456. (error "Abort")))
  7457. (org-timestamp-change n (cdr (assoc what whata)))
  7458. (org-at-timestamp-p t)
  7459. (setq ts (match-string 1))
  7460. (setq time (save-match-data (org-time-string-to-time ts))))
  7461. (org-timestamp-change (- n) (cdr (assoc what whata)))
  7462. ;; rematch, so that we have everything in place for the real shift
  7463. (org-at-timestamp-p t)
  7464. (setq ts (match-string 1))
  7465. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  7466. (org-timestamp-change n (cdr (assoc what whata)))
  7467. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  7468. (setq org-log-post-message msg)
  7469. (message "%s" msg))))
  7470. (defun org-show-todo-tree (arg)
  7471. "Make a compact tree which shows all headlines marked with TODO.
  7472. The tree will show the lines where the regexp matches, and all higher
  7473. headlines above the match.
  7474. With a \\[universal-argument] prefix, also show the DONE entries.
  7475. With a numeric prefix N, construct a sparse tree for the Nth element
  7476. of `org-todo-keywords-1'."
  7477. (interactive "P")
  7478. (let ((case-fold-search nil)
  7479. (kwd-re
  7480. (cond ((null arg) org-not-done-regexp)
  7481. ((equal arg '(4))
  7482. (let ((kwd (completing-read "Keyword (or KWD1|KWD2|...): "
  7483. (mapcar 'list org-todo-keywords-1))))
  7484. (concat "\\("
  7485. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  7486. "\\)\\>")))
  7487. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  7488. (regexp-quote (nth (1- (prefix-numeric-value arg))
  7489. org-todo-keywords-1)))
  7490. (t (error "Invalid prefix argument: %s" arg)))))
  7491. (message "%d TODO entries found"
  7492. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  7493. (defun org-deadline (&optional remove time)
  7494. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  7495. With argument REMOVE, remove any deadline from the item.
  7496. When TIME is set, it should be an internal time specification, and the
  7497. scheduling will use the corresponding date."
  7498. (interactive "P")
  7499. (if remove
  7500. (progn
  7501. (org-remove-timestamp-with-keyword org-deadline-string)
  7502. (message "Item no longer has a deadline."))
  7503. (if (org-get-repeat)
  7504. (error "Cannot change deadline on task with repeater, please do that by hand")
  7505. (org-add-planning-info 'deadline time 'closed)
  7506. (message "Deadline on %s" org-last-inserted-timestamp))))
  7507. (defun org-schedule (&optional remove time)
  7508. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  7509. With argument REMOVE, remove any scheduling date from the item.
  7510. When TIME is set, it should be an internal time specification, and the
  7511. scheduling will use the corresponding date."
  7512. (interactive "P")
  7513. (if remove
  7514. (progn
  7515. (org-remove-timestamp-with-keyword org-scheduled-string)
  7516. (message "Item is no longer scheduled."))
  7517. (if (org-get-repeat)
  7518. (error "Cannot reschedule task with repeater, please do that by hand")
  7519. (org-add-planning-info 'scheduled time 'closed)
  7520. (message "Scheduled to %s" org-last-inserted-timestamp))))
  7521. (defun org-remove-timestamp-with-keyword (keyword)
  7522. "Remove all time stamps with KEYWORD in the current entry."
  7523. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  7524. beg)
  7525. (save-excursion
  7526. (org-back-to-heading t)
  7527. (setq beg (point))
  7528. (org-end-of-subtree t t)
  7529. (while (re-search-backward re beg t)
  7530. (replace-match "")
  7531. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  7532. (equal (char-before) ?\ ))
  7533. (backward-delete-char 1)
  7534. (if (string-match "^[ \t]*$" (buffer-substring
  7535. (point-at-bol) (point-at-eol)))
  7536. (delete-region (point-at-bol)
  7537. (min (point-max) (1+ (point-at-eol))))))))))
  7538. (defun org-add-planning-info (what &optional time &rest remove)
  7539. "Insert new timestamp with keyword in the line directly after the headline.
  7540. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  7541. If non is given, the user is prompted for a date.
  7542. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  7543. be removed."
  7544. (interactive)
  7545. (let (org-time-was-given org-end-time-was-given ts
  7546. end default-time default-input)
  7547. (when (and (not time) (memq what '(scheduled deadline)))
  7548. ;; Try to get a default date/time from existing timestamp
  7549. (save-excursion
  7550. (org-back-to-heading t)
  7551. (setq end (save-excursion (outline-next-heading) (point)))
  7552. (when (re-search-forward (if (eq what 'scheduled)
  7553. org-scheduled-time-regexp
  7554. org-deadline-time-regexp)
  7555. end t)
  7556. (setq ts (match-string 1)
  7557. default-time
  7558. (apply 'encode-time (org-parse-time-string ts))
  7559. default-input (and ts (org-get-compact-tod ts))))))
  7560. (when what
  7561. ;; If necessary, get the time from the user
  7562. (setq time (or time (org-read-date nil 'to-time nil nil
  7563. default-time default-input))))
  7564. (when (and org-insert-labeled-timestamps-at-point
  7565. (member what '(scheduled deadline)))
  7566. (insert
  7567. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  7568. (org-insert-time-stamp time org-time-was-given
  7569. nil nil nil (list org-end-time-was-given))
  7570. (setq what nil))
  7571. (save-excursion
  7572. (save-restriction
  7573. (let (col list elt ts buffer-invisibility-spec)
  7574. (org-back-to-heading t)
  7575. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  7576. (goto-char (match-end 1))
  7577. (setq col (current-column))
  7578. (goto-char (match-end 0))
  7579. (if (eobp) (insert "\n") (forward-char 1))
  7580. (if (and (not (looking-at outline-regexp))
  7581. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  7582. "[^\r\n]*"))
  7583. (not (equal (match-string 1) org-clock-string)))
  7584. (narrow-to-region (match-beginning 0) (match-end 0))
  7585. (insert-before-markers "\n")
  7586. (backward-char 1)
  7587. (narrow-to-region (point) (point))
  7588. (and org-adapt-indentation (org-indent-to-column col)))
  7589. ;; Check if we have to remove something.
  7590. (setq list (cons what remove))
  7591. (while list
  7592. (setq elt (pop list))
  7593. (goto-char (point-min))
  7594. (when (or (and (eq elt 'scheduled)
  7595. (re-search-forward org-scheduled-time-regexp nil t))
  7596. (and (eq elt 'deadline)
  7597. (re-search-forward org-deadline-time-regexp nil t))
  7598. (and (eq elt 'closed)
  7599. (re-search-forward org-closed-time-regexp nil t)))
  7600. (replace-match "")
  7601. (if (looking-at "--+<[^>]+>") (replace-match ""))
  7602. (if (looking-at " +") (replace-match ""))))
  7603. (goto-char (point-max))
  7604. (when what
  7605. (insert
  7606. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  7607. (cond ((eq what 'scheduled) org-scheduled-string)
  7608. ((eq what 'deadline) org-deadline-string)
  7609. ((eq what 'closed) org-closed-string))
  7610. " ")
  7611. (setq ts (org-insert-time-stamp
  7612. time
  7613. (or org-time-was-given
  7614. (and (eq what 'closed) org-log-done-with-time))
  7615. (eq what 'closed)
  7616. nil nil (list org-end-time-was-given)))
  7617. (end-of-line 1))
  7618. (goto-char (point-min))
  7619. (widen)
  7620. (if (and (looking-at "[ \t]+\n")
  7621. (equal (char-before) ?\n))
  7622. (delete-region (1- (point)) (point-at-eol)))
  7623. ts)))))
  7624. (defvar org-log-note-marker (make-marker))
  7625. (defvar org-log-note-purpose nil)
  7626. (defvar org-log-note-state nil)
  7627. (defvar org-log-note-how nil)
  7628. (defvar org-log-note-extra nil)
  7629. (defvar org-log-note-window-configuration nil)
  7630. (defvar org-log-note-return-to (make-marker))
  7631. (defvar org-log-post-message nil
  7632. "Message to be displayed after a log note has been stored.
  7633. The auto-repeater uses this.")
  7634. (defun org-add-note ()
  7635. "Add a note to the current entry.
  7636. This is done in the same way as adding a state change note."
  7637. (interactive)
  7638. (org-add-log-setup 'note nil 'findpos nil))
  7639. (defvar org-property-end-re)
  7640. (defun org-add-log-setup (&optional purpose state findpos how &optional extra)
  7641. "Set up the post command hook to take a note.
  7642. If this is about to TODO state change, the new state is expected in STATE.
  7643. When FINDPOS is non-nil, find the correct position for the note in
  7644. the current entry. If not, assume that it can be inserted at point.
  7645. HOW is an indicator what kind of note should be created.
  7646. EXTRA is additional text that will be inserted into the notes buffer."
  7647. (save-restriction
  7648. (save-excursion
  7649. (when findpos
  7650. (org-back-to-heading t)
  7651. (narrow-to-region (point) (save-excursion
  7652. (outline-next-heading) (point)))
  7653. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  7654. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  7655. "[^\r\n]*\\)?"))
  7656. (goto-char (match-end 0))
  7657. (when (and org-log-state-notes-insert-after-drawers
  7658. (save-excursion
  7659. (forward-line) (looking-at org-drawer-regexp)))
  7660. (progn (forward-line)
  7661. (while (looking-at org-drawer-regexp)
  7662. (goto-char (match-end 0))
  7663. (re-search-forward org-property-end-re (point-max) t)
  7664. (forward-line))
  7665. (forward-line -1)))
  7666. (unless org-log-states-order-reversed
  7667. (and (= (char-after) ?\n) (forward-char 1))
  7668. (org-skip-over-state-notes)
  7669. (skip-chars-backward " \t\n\r")))
  7670. (move-marker org-log-note-marker (point))
  7671. (setq org-log-note-purpose purpose
  7672. org-log-note-state state
  7673. org-log-note-how how
  7674. org-log-note-extra extra)
  7675. (add-hook 'post-command-hook 'org-add-log-note 'append))))
  7676. (defun org-skip-over-state-notes ()
  7677. "Skip past the list of State notes in an entry."
  7678. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  7679. (while (looking-at "[ \t]*- State")
  7680. (condition-case nil
  7681. (org-next-item)
  7682. (error (org-end-of-item)))))
  7683. (defun org-add-log-note (&optional purpose)
  7684. "Pop up a window for taking a note, and add this note later at point."
  7685. (remove-hook 'post-command-hook 'org-add-log-note)
  7686. (setq org-log-note-window-configuration (current-window-configuration))
  7687. (delete-other-windows)
  7688. (move-marker org-log-note-return-to (point))
  7689. (switch-to-buffer (marker-buffer org-log-note-marker))
  7690. (goto-char org-log-note-marker)
  7691. (org-switch-to-buffer-other-window "*Org Note*")
  7692. (erase-buffer)
  7693. (if (memq org-log-note-how '(time state))
  7694. (let (current-prefix-arg) (org-store-log-note))
  7695. (let ((org-inhibit-startup t)) (org-mode))
  7696. (insert (format "# Insert note for %s.
  7697. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  7698. (cond
  7699. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  7700. ((eq org-log-note-purpose 'done) "closed todo item")
  7701. ((eq org-log-note-purpose 'state)
  7702. (format "state change to \"%s\"" org-log-note-state))
  7703. ((eq org-log-note-purpose 'note)
  7704. "this entry")
  7705. (t (error "This should not happen")))))
  7706. (if org-log-note-extra (insert org-log-note-extra))
  7707. (org-set-local 'org-finish-function 'org-store-log-note)))
  7708. (defvar org-note-abort nil) ; dynamically scoped
  7709. (defun org-store-log-note ()
  7710. "Finish taking a log note, and insert it to where it belongs."
  7711. (let ((txt (buffer-string))
  7712. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  7713. lines ind)
  7714. (kill-buffer (current-buffer))
  7715. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  7716. (setq txt (replace-match "" t t txt)))
  7717. (if (string-match "\\s-+\\'" txt)
  7718. (setq txt (replace-match "" t t txt)))
  7719. (setq lines (org-split-string txt "\n"))
  7720. (when (and note (string-match "\\S-" note))
  7721. (setq note
  7722. (org-replace-escapes
  7723. note
  7724. (list (cons "%u" (user-login-name))
  7725. (cons "%U" user-full-name)
  7726. (cons "%t" (format-time-string
  7727. (org-time-stamp-format 'long 'inactive)
  7728. (current-time)))
  7729. (cons "%s" (if org-log-note-state
  7730. (concat "\"" org-log-note-state "\"")
  7731. "")))))
  7732. (if lines (setq note (concat note " \\\\")))
  7733. (push note lines))
  7734. (when (or current-prefix-arg org-note-abort) (setq lines nil))
  7735. (when lines
  7736. (save-excursion
  7737. (set-buffer (marker-buffer org-log-note-marker))
  7738. (save-excursion
  7739. (goto-char org-log-note-marker)
  7740. (move-marker org-log-note-marker nil)
  7741. (end-of-line 1)
  7742. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  7743. (indent-relative nil)
  7744. (insert "- " (pop lines))
  7745. (org-indent-line-function)
  7746. (beginning-of-line 1)
  7747. (looking-at "[ \t]*")
  7748. (setq ind (concat (match-string 0) " "))
  7749. (end-of-line 1)
  7750. (while lines (insert "\n" ind (pop lines)))))))
  7751. (set-window-configuration org-log-note-window-configuration)
  7752. (with-current-buffer (marker-buffer org-log-note-return-to)
  7753. (goto-char org-log-note-return-to))
  7754. (move-marker org-log-note-return-to nil)
  7755. (and org-log-post-message (message "%s" org-log-post-message)))
  7756. (defun org-sparse-tree (&optional arg)
  7757. "Create a sparse tree, prompt for the details.
  7758. This command can create sparse trees. You first need to select the type
  7759. of match used to create the tree:
  7760. t Show entries with a specific TODO keyword.
  7761. T Show entries selected by a tags match.
  7762. p Enter a property name and its value (both with completion on existing
  7763. names/values) and show entries with that property.
  7764. r Show entries matching a regular expression
  7765. d Show deadlines due within `org-deadline-warning-days'."
  7766. (interactive "P")
  7767. (let (ans kwd value)
  7768. (message "Sparse tree: [/]regexp [t]odo-kwd [T]ag [p]roperty [d]eadlines [b]efore-date")
  7769. (setq ans (read-char-exclusive))
  7770. (cond
  7771. ((equal ans ?d)
  7772. (call-interactively 'org-check-deadlines))
  7773. ((equal ans ?b)
  7774. (call-interactively 'org-check-before-date))
  7775. ((equal ans ?t)
  7776. (org-show-todo-tree '(4)))
  7777. ((equal ans ?T)
  7778. (call-interactively 'org-tags-sparse-tree))
  7779. ((member ans '(?p ?P))
  7780. (setq kwd (completing-read "Property: "
  7781. (mapcar 'list (org-buffer-property-keys))))
  7782. (setq value (completing-read "Value: "
  7783. (mapcar 'list (org-property-values kwd))))
  7784. (unless (string-match "\\`{.*}\\'" value)
  7785. (setq value (concat "\"" value "\"")))
  7786. (org-tags-sparse-tree arg (concat kwd "=" value)))
  7787. ((member ans '(?r ?R ?/))
  7788. (call-interactively 'org-occur))
  7789. (t (error "No such sparse tree command \"%c\"" ans)))))
  7790. (defvar org-occur-highlights nil
  7791. "List of overlays used for occur matches.")
  7792. (make-variable-buffer-local 'org-occur-highlights)
  7793. (defvar org-occur-parameters nil
  7794. "Parameters of the active org-occur calls.
  7795. This is a list, each call to org-occur pushes as cons cell,
  7796. containing the regular expression and the callback, onto the list.
  7797. The list can contain several entries if `org-occur' has been called
  7798. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  7799. will only contain one set of parameters. When the highlights are
  7800. removed (for example with `C-c C-c', or with the next edit (depending
  7801. on `org-remove-highlights-with-change'), this variable is emptied
  7802. as well.")
  7803. (make-variable-buffer-local 'org-occur-parameters)
  7804. (defun org-occur (regexp &optional keep-previous callback)
  7805. "Make a compact tree which shows all matches of REGEXP.
  7806. The tree will show the lines where the regexp matches, and all higher
  7807. headlines above the match. It will also show the heading after the match,
  7808. to make sure editing the matching entry is easy.
  7809. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  7810. call to `org-occur' will be kept, to allow stacking of calls to this
  7811. command.
  7812. If CALLBACK is non-nil, it is a function which is called to confirm
  7813. that the match should indeed be shown."
  7814. (interactive "sRegexp: \nP")
  7815. (unless keep-previous
  7816. (org-remove-occur-highlights nil nil t))
  7817. (push (cons regexp callback) org-occur-parameters)
  7818. (let ((cnt 0))
  7819. (save-excursion
  7820. (goto-char (point-min))
  7821. (if (or (not keep-previous) ; do not want to keep
  7822. (not org-occur-highlights)) ; no previous matches
  7823. ;; hide everything
  7824. (org-overview))
  7825. (while (re-search-forward regexp nil t)
  7826. (when (or (not callback)
  7827. (save-match-data (funcall callback)))
  7828. (setq cnt (1+ cnt))
  7829. (when org-highlight-sparse-tree-matches
  7830. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  7831. (org-show-context 'occur-tree))))
  7832. (when org-remove-highlights-with-change
  7833. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  7834. nil 'local))
  7835. (unless org-sparse-tree-open-archived-trees
  7836. (org-hide-archived-subtrees (point-min) (point-max)))
  7837. (run-hooks 'org-occur-hook)
  7838. (if (interactive-p)
  7839. (message "%d match(es) for regexp %s" cnt regexp))
  7840. cnt))
  7841. (defun org-show-context (&optional key)
  7842. "Make sure point and context and visible.
  7843. How much context is shown depends upon the variables
  7844. `org-show-hierarchy-above', `org-show-following-heading'. and
  7845. `org-show-siblings'."
  7846. (let ((heading-p (org-on-heading-p t))
  7847. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  7848. (following-p (org-get-alist-option org-show-following-heading key))
  7849. (entry-p (org-get-alist-option org-show-entry-below key))
  7850. (siblings-p (org-get-alist-option org-show-siblings key)))
  7851. (catch 'exit
  7852. ;; Show heading or entry text
  7853. (if (and heading-p (not entry-p))
  7854. (org-flag-heading nil) ; only show the heading
  7855. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  7856. (org-show-hidden-entry))) ; show entire entry
  7857. (when following-p
  7858. ;; Show next sibling, or heading below text
  7859. (save-excursion
  7860. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  7861. (org-flag-heading nil))))
  7862. (when siblings-p (org-show-siblings))
  7863. (when hierarchy-p
  7864. ;; show all higher headings, possibly with siblings
  7865. (save-excursion
  7866. (while (and (condition-case nil
  7867. (progn (org-up-heading-all 1) t)
  7868. (error nil))
  7869. (not (bobp)))
  7870. (org-flag-heading nil)
  7871. (when siblings-p (org-show-siblings))))))))
  7872. (defun org-reveal (&optional siblings)
  7873. "Show current entry, hierarchy above it, and the following headline.
  7874. This can be used to show a consistent set of context around locations
  7875. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  7876. not t for the search context.
  7877. With optional argument SIBLINGS, on each level of the hierarchy all
  7878. siblings are shown. This repairs the tree structure to what it would
  7879. look like when opened with hierarchical calls to `org-cycle'."
  7880. (interactive "P")
  7881. (let ((org-show-hierarchy-above t)
  7882. (org-show-following-heading t)
  7883. (org-show-siblings (if siblings t org-show-siblings)))
  7884. (org-show-context nil)))
  7885. (defun org-highlight-new-match (beg end)
  7886. "Highlight from BEG to END and mark the highlight is an occur headline."
  7887. (let ((ov (org-make-overlay beg end)))
  7888. (org-overlay-put ov 'face 'secondary-selection)
  7889. (push ov org-occur-highlights)))
  7890. (defun org-remove-occur-highlights (&optional beg end noremove)
  7891. "Remove the occur highlights from the buffer.
  7892. BEG and END are ignored. If NOREMOVE is nil, remove this function
  7893. from the `before-change-functions' in the current buffer."
  7894. (interactive)
  7895. (unless org-inhibit-highlight-removal
  7896. (mapc 'org-delete-overlay org-occur-highlights)
  7897. (setq org-occur-highlights nil)
  7898. (setq org-occur-parameters nil)
  7899. (unless noremove
  7900. (remove-hook 'before-change-functions
  7901. 'org-remove-occur-highlights 'local))))
  7902. ;;;; Priorities
  7903. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  7904. "Regular expression matching the priority indicator.")
  7905. (defvar org-remove-priority-next-time nil)
  7906. (defun org-priority-up ()
  7907. "Increase the priority of the current item."
  7908. (interactive)
  7909. (org-priority 'up))
  7910. (defun org-priority-down ()
  7911. "Decrease the priority of the current item."
  7912. (interactive)
  7913. (org-priority 'down))
  7914. (defun org-priority (&optional action)
  7915. "Change the priority of an item by ARG.
  7916. ACTION can be `set', `up', `down', or a character."
  7917. (interactive)
  7918. (setq action (or action 'set))
  7919. (let (current new news have remove)
  7920. (save-excursion
  7921. (org-back-to-heading)
  7922. (if (looking-at org-priority-regexp)
  7923. (setq current (string-to-char (match-string 2))
  7924. have t)
  7925. (setq current org-default-priority))
  7926. (cond
  7927. ((or (eq action 'set)
  7928. (if (featurep 'xemacs) (characterp action) (integerp action)))
  7929. (if (not (eq action 'set))
  7930. (setq new action)
  7931. (message "Priority %c-%c, SPC to remove: "
  7932. org-highest-priority org-lowest-priority)
  7933. (setq new (read-char-exclusive)))
  7934. (if (and (= (upcase org-highest-priority) org-highest-priority)
  7935. (= (upcase org-lowest-priority) org-lowest-priority))
  7936. (setq new (upcase new)))
  7937. (cond ((equal new ?\ ) (setq remove t))
  7938. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  7939. (error "Priority must be between `%c' and `%c'"
  7940. org-highest-priority org-lowest-priority))))
  7941. ((eq action 'up)
  7942. (if (and (not have) (eq last-command this-command))
  7943. (setq new org-lowest-priority)
  7944. (setq new (if (and org-priority-start-cycle-with-default (not have))
  7945. org-default-priority (1- current)))))
  7946. ((eq action 'down)
  7947. (if (and (not have) (eq last-command this-command))
  7948. (setq new org-highest-priority)
  7949. (setq new (if (and org-priority-start-cycle-with-default (not have))
  7950. org-default-priority (1+ current)))))
  7951. (t (error "Invalid action")))
  7952. (if (or (< (upcase new) org-highest-priority)
  7953. (> (upcase new) org-lowest-priority))
  7954. (setq remove t))
  7955. (setq news (format "%c" new))
  7956. (if have
  7957. (if remove
  7958. (replace-match "" t t nil 1)
  7959. (replace-match news t t nil 2))
  7960. (if remove
  7961. (error "No priority cookie found in line")
  7962. (looking-at org-todo-line-regexp)
  7963. (if (match-end 2)
  7964. (progn
  7965. (goto-char (match-end 2))
  7966. (insert " [#" news "]"))
  7967. (goto-char (match-beginning 3))
  7968. (insert "[#" news "] ")))))
  7969. (org-preserve-lc (org-set-tags nil 'align))
  7970. (if remove
  7971. (message "Priority removed")
  7972. (message "Priority of current item set to %s" news))))
  7973. (defun org-get-priority (s)
  7974. "Find priority cookie and return priority."
  7975. (save-match-data
  7976. (if (not (string-match org-priority-regexp s))
  7977. (* 1000 (- org-lowest-priority org-default-priority))
  7978. (* 1000 (- org-lowest-priority
  7979. (string-to-char (match-string 2 s)))))))
  7980. ;;;; Tags
  7981. (defvar org-agenda-archives-mode)
  7982. (defun org-scan-tags (action matcher &optional todo-only)
  7983. "Scan headline tags with inheritance and produce output ACTION.
  7984. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  7985. or `agenda' to produce an entry list for an agenda view. It can also be
  7986. a Lisp form or a function that should be called at each matched headline, in
  7987. this case the return value is a list of all return values from these calls.
  7988. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  7989. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  7990. only lines with a TODO keyword are included in the output."
  7991. (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
  7992. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  7993. (org-re
  7994. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  7995. (props (list 'face 'default
  7996. 'done-face 'org-done
  7997. 'undone-face 'default
  7998. 'mouse-face 'highlight
  7999. 'org-not-done-regexp org-not-done-regexp
  8000. 'org-todo-regexp org-todo-regexp
  8001. 'keymap org-agenda-keymap
  8002. 'help-echo
  8003. (format "mouse-2 or RET jump to org file %s"
  8004. (abbreviate-file-name
  8005. (or (buffer-file-name (buffer-base-buffer))
  8006. (buffer-name (buffer-base-buffer)))))))
  8007. (case-fold-search nil)
  8008. lspos tags tags-list
  8009. (tags-alist (list (cons 0 (mapcar 'downcase org-file-tags))))
  8010. (llast 0) rtn rtn1 level category i txt
  8011. todo marker entry priority)
  8012. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  8013. (setq action (list 'lambda nil action)))
  8014. (save-excursion
  8015. (goto-char (point-min))
  8016. (when (eq action 'sparse-tree)
  8017. (org-overview)
  8018. (org-remove-occur-highlights))
  8019. (while (re-search-forward re nil t)
  8020. (catch :skip
  8021. (setq todo (if (match-end 1) (match-string 2))
  8022. tags (if (match-end 4) (match-string 4)))
  8023. (goto-char (setq lspos (1+ (match-beginning 0))))
  8024. (setq level (org-reduced-level (funcall outline-level))
  8025. category (org-get-category))
  8026. (setq i llast llast level)
  8027. ;; remove tag lists from same and sublevels
  8028. (while (>= i level)
  8029. (when (setq entry (assoc i tags-alist))
  8030. (setq tags-alist (delete entry tags-alist)))
  8031. (setq i (1- i)))
  8032. ;; add the next tags
  8033. (when tags
  8034. (setq tags (mapcar 'downcase (org-split-string tags ":"))
  8035. tags-alist
  8036. (cons (cons level tags) tags-alist)))
  8037. ;; compile tags for current headline
  8038. (setq tags-list
  8039. (if org-use-tag-inheritance
  8040. (apply 'append (mapcar 'cdr tags-alist))
  8041. tags))
  8042. (when (and tags org-use-tag-inheritance
  8043. (not (eq t org-use-tag-inheritance)))
  8044. ;; selective inheritance, remove uninherited ones
  8045. (setcdr (car tags-alist)
  8046. (org-remove-uniherited-tags (cdar tags-alist))))
  8047. (when (and (or (not todo-only) (member todo org-not-done-keywords))
  8048. (let ((case-fold-search t)) (eval matcher))
  8049. (or
  8050. (not (member org-archive-tag tags-list))
  8051. ;; we have an archive tag, should we use this anyway?
  8052. (or (not org-agenda-skip-archived-trees)
  8053. (and (eq action 'agenda) org-agenda-archives-mode))))
  8054. (unless (eq action 'sparse-tree) (org-agenda-skip))
  8055. ;; select this headline
  8056. (cond
  8057. ((eq action 'sparse-tree)
  8058. (and org-highlight-sparse-tree-matches
  8059. (org-get-heading) (match-end 0)
  8060. (org-highlight-new-match
  8061. (match-beginning 0) (match-beginning 1)))
  8062. (org-show-context 'tags-tree))
  8063. ((eq action 'agenda)
  8064. (setq txt (org-format-agenda-item
  8065. ""
  8066. (concat
  8067. (if org-tags-match-list-sublevels
  8068. (make-string (1- level) ?.) "")
  8069. (org-get-heading))
  8070. category tags-list)
  8071. priority (org-get-priority txt))
  8072. (goto-char lspos)
  8073. (setq marker (org-agenda-new-marker))
  8074. (org-add-props txt props
  8075. 'org-marker marker 'org-hd-marker marker 'org-category category
  8076. 'priority priority 'type "tagsmatch")
  8077. (push txt rtn))
  8078. ((functionp action)
  8079. (save-excursion
  8080. (setq rtn1 (funcall action))
  8081. (push rtn1 rtn))
  8082. (goto-char (point-at-eol)))
  8083. (t (error "Invalid action")))
  8084. ;; if we are to skip sublevels, jump to end of subtree
  8085. (or org-tags-match-list-sublevels (org-end-of-subtree t))))))
  8086. (when (and (eq action 'sparse-tree)
  8087. (not org-sparse-tree-open-archived-trees))
  8088. (org-hide-archived-subtrees (point-min) (point-max)))
  8089. (nreverse rtn)))
  8090. (defun org-remove-uniherited-tags (tags)
  8091. "Remove all tags that are not inherited from the list TAGS."
  8092. (cond
  8093. ((eq org-use-tag-inheritance t) tags)
  8094. ((not org-use-tag-inheritance) nil)
  8095. ((stringp org-use-tag-inheritance)
  8096. (delq nil (mapcar
  8097. (lambda (x) (if (string-match org-use-tag-inheritance x) x nil))
  8098. tags)))
  8099. ((listp org-use-tag-inheritance)
  8100. (delq nil (mapcar
  8101. (lambda (x) (if (member x org-use-tag-inheritance) x nil))
  8102. tags)))))
  8103. (defvar todo-only) ;; dynamically scoped
  8104. (defun org-tags-sparse-tree (&optional todo-only match)
  8105. "Create a sparse tree according to tags string MATCH.
  8106. MATCH can contain positive and negative selection of tags, like
  8107. \"+WORK+URGENT-WITHBOSS\".
  8108. If optional argument TODO_ONLY is non-nil, only select lines that are
  8109. also TODO lines."
  8110. (interactive "P")
  8111. (org-prepare-agenda-buffers (list (current-buffer)))
  8112. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  8113. (defvar org-cached-props nil)
  8114. (defun org-cached-entry-get (pom property)
  8115. (if (or (eq t org-use-property-inheritance)
  8116. (and (stringp org-use-property-inheritance)
  8117. (string-match org-use-property-inheritance property))
  8118. (and (listp org-use-property-inheritance)
  8119. (member property org-use-property-inheritance)))
  8120. ;; Caching is not possible, check it directly
  8121. (org-entry-get pom property 'inherit)
  8122. ;; Get all properties, so that we can do complicated checks easily
  8123. (cdr (assoc property (or org-cached-props
  8124. (setq org-cached-props
  8125. (org-entry-properties pom)))))))
  8126. (defun org-global-tags-completion-table (&optional files)
  8127. "Return the list of all tags in all agenda buffer/files."
  8128. (save-excursion
  8129. (org-uniquify
  8130. (delq nil
  8131. (apply 'append
  8132. (mapcar
  8133. (lambda (file)
  8134. (set-buffer (find-file-noselect file))
  8135. (append (org-get-buffer-tags)
  8136. (mapcar (lambda (x) (if (stringp (car-safe x))
  8137. (list (car-safe x)) nil))
  8138. org-tag-alist)))
  8139. (if (and files (car files))
  8140. files
  8141. (org-agenda-files))))))))
  8142. (defun org-make-tags-matcher (match)
  8143. "Create the TAGS//TODO matcher form for the selection string MATCH."
  8144. ;; todo-only is scoped dynamically into this function, and the function
  8145. ;; may change it it the matcher asksk for it.
  8146. (unless match
  8147. ;; Get a new match request, with completion
  8148. (let ((org-last-tags-completion-table
  8149. (org-global-tags-completion-table)))
  8150. (setq match (completing-read
  8151. "Match: " 'org-tags-completion-function nil nil nil
  8152. 'org-tags-history))))
  8153. ;; Parse the string and create a lisp form
  8154. (let ((match0 match)
  8155. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
  8156. minus tag mm
  8157. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  8158. orterms term orlist re-p str-p level-p level-op time-p
  8159. prop-p pn pv po cat-p gv)
  8160. (if (string-match "/+" match)
  8161. ;; match contains also a todo-matching request
  8162. (progn
  8163. (setq tagsmatch (substring match 0 (match-beginning 0))
  8164. todomatch (substring match (match-end 0)))
  8165. (if (string-match "^!" todomatch)
  8166. (setq todo-only t todomatch (substring todomatch 1)))
  8167. (if (string-match "^\\s-*$" todomatch)
  8168. (setq todomatch nil)))
  8169. ;; only matching tags
  8170. (setq tagsmatch match todomatch nil))
  8171. ;; Make the tags matcher
  8172. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  8173. (setq tagsmatcher t)
  8174. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  8175. (while (setq term (pop orterms))
  8176. (while (and (equal (substring term -1) "\\") orterms)
  8177. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  8178. (while (string-match re term)
  8179. (setq minus (and (match-end 1)
  8180. (equal (match-string 1 term) "-"))
  8181. tag (match-string 2 term)
  8182. re-p (equal (string-to-char tag) ?{)
  8183. level-p (match-end 4)
  8184. prop-p (match-end 5)
  8185. mm (cond
  8186. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  8187. (level-p
  8188. (setq level-op (org-op-to-function (match-string 3 term)))
  8189. `(,level-op level ,(string-to-number
  8190. (match-string 4 term))))
  8191. (prop-p
  8192. (setq pn (match-string 5 term)
  8193. po (match-string 6 term)
  8194. pv (match-string 7 term)
  8195. cat-p (equal pn "CATEGORY")
  8196. re-p (equal (string-to-char pv) ?{)
  8197. str-p (equal (string-to-char pv) ?\")
  8198. time-p (save-match-data
  8199. (string-match "^\"[[<].*[]>]\"$" pv))
  8200. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  8201. (if time-p (setq pv (org-matcher-time pv)))
  8202. (setq po (org-op-to-function po (if time-p 'time str-p)))
  8203. (if (equal pn "CATEGORY")
  8204. (setq gv '(get-text-property (point) 'org-category))
  8205. (setq gv `(org-cached-entry-get nil ,pn)))
  8206. (if re-p
  8207. (if (eq po 'org<>)
  8208. `(not (string-match ,pv (or ,gv "")))
  8209. `(string-match ,pv (or ,gv "")))
  8210. (if str-p
  8211. `(,po (or ,gv "") ,pv)
  8212. `(,po (string-to-number (or ,gv ""))
  8213. ,(string-to-number pv) ))))
  8214. (t `(member ,(downcase tag) tags-list)))
  8215. mm (if minus (list 'not mm) mm)
  8216. term (substring term (match-end 0)))
  8217. (push mm tagsmatcher))
  8218. (push (if (> (length tagsmatcher) 1)
  8219. (cons 'and tagsmatcher)
  8220. (car tagsmatcher))
  8221. orlist)
  8222. (setq tagsmatcher nil))
  8223. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  8224. (setq tagsmatcher
  8225. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  8226. ;; Make the todo matcher
  8227. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  8228. (setq todomatcher t)
  8229. (setq orterms (org-split-string todomatch "|") orlist nil)
  8230. (while (setq term (pop orterms))
  8231. (while (string-match re term)
  8232. (setq minus (and (match-end 1)
  8233. (equal (match-string 1 term) "-"))
  8234. kwd (match-string 2 term)
  8235. re-p (equal (string-to-char kwd) ?{)
  8236. term (substring term (match-end 0))
  8237. mm (if re-p
  8238. `(string-match ,(substring kwd 1 -1) todo)
  8239. (list 'equal 'todo kwd))
  8240. mm (if minus (list 'not mm) mm))
  8241. (push mm todomatcher))
  8242. (push (if (> (length todomatcher) 1)
  8243. (cons 'and todomatcher)
  8244. (car todomatcher))
  8245. orlist)
  8246. (setq todomatcher nil))
  8247. (setq todomatcher (if (> (length orlist) 1)
  8248. (cons 'or orlist) (car orlist))))
  8249. ;; Return the string and lisp forms of the matcher
  8250. (setq matcher (if todomatcher
  8251. (list 'and tagsmatcher todomatcher)
  8252. tagsmatcher))
  8253. (cons match0 matcher)))
  8254. (defun org-op-to-function (op &optional stringp)
  8255. "Turn an operator into the appropriate function."
  8256. (setq op
  8257. (cond
  8258. ((equal op "<" ) '(< string< org-time<))
  8259. ((equal op ">" ) '(> org-string> org-time>))
  8260. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  8261. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  8262. ((member op '("=" "==")) '(= string= org-time=))
  8263. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  8264. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  8265. (defun org<> (a b) (not (= a b)))
  8266. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  8267. (defun org-string>= (a b) (not (string< a b)))
  8268. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  8269. (defun org-string<> (a b) (not (string= a b)))
  8270. (defun org-time= (a b) (= (org-2ft a) (org-2ft b)))
  8271. (defun org-time< (a b) (< (org-2ft a) (org-2ft b)))
  8272. (defun org-time<= (a b) (<= (org-2ft a) (org-2ft b)))
  8273. (defun org-time> (a b) (> (org-2ft a) (org-2ft b)))
  8274. (defun org-time>= (a b) (>= (org-2ft a) (org-2ft b)))
  8275. (defun org-time<> (a b) (org<> (org-2ft a) (org-2ft b)))
  8276. (defun org-2ft (s)
  8277. "Convert S to a floating point time.
  8278. If S is already a number, just return it. If it is a string, parse
  8279. it as a time string and apply `float-time' to it. f S is nil, just return 0."
  8280. (cond
  8281. ((numberp s) s)
  8282. ((stringp s)
  8283. (condition-case nil
  8284. (float-time (apply 'encode-time (org-parse-time-string s)))
  8285. (error 0.)))
  8286. (t 0.)))
  8287. (defun org-matcher-time (s)
  8288. (cond
  8289. ((equal s "<now>") (float-time))
  8290. ((equal s "<today>")
  8291. (float-time (append '(0 0 0) (nthcdr 3 (decode-time)))))
  8292. (t (org-2ft s))))
  8293. (defun org-match-any-p (re list)
  8294. "Does re match any element of list?"
  8295. (setq list (mapcar (lambda (x) (string-match re x)) list))
  8296. (delq nil list))
  8297. (defvar org-add-colon-after-tag-completion nil) ;; dynamically skoped param
  8298. (defvar org-tags-overlay (org-make-overlay 1 1))
  8299. (org-detach-overlay org-tags-overlay)
  8300. (defun org-get-local-tags-at (&optional pos)
  8301. "Get a list of tags defined in the current headline."
  8302. (org-get-tags-at pos 'local))
  8303. (defun org-get-local-tags ()
  8304. "Get a list of tags defined in the current headline."
  8305. (org-get-tags-at nil 'local))
  8306. (defun org-get-tags-at (&optional pos local)
  8307. "Get a list of all headline tags applicable at POS.
  8308. POS defaults to point. If tags are inherited, the list contains
  8309. the targets in the same sequence as the headlines appear, i.e.
  8310. the tags of the current headline come last.
  8311. When LOCAL is non-nil, only return tags from the current headline,
  8312. ignore inherited ones."
  8313. (interactive)
  8314. (let (tags ltags lastpos parent)
  8315. (save-excursion
  8316. (save-restriction
  8317. (widen)
  8318. (goto-char (or pos (point)))
  8319. (save-match-data
  8320. (catch 'done
  8321. (condition-case nil
  8322. (progn
  8323. (org-back-to-heading t)
  8324. (while (not (equal lastpos (point)))
  8325. (setq lastpos (point))
  8326. (when (looking-at (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  8327. (setq ltags (org-split-string
  8328. (org-match-string-no-properties 1) ":"))
  8329. (setq tags (append
  8330. (if parent
  8331. (org-remove-uniherited-tags ltags)
  8332. ltags)
  8333. tags)))
  8334. (or org-use-tag-inheritance (throw 'done t))
  8335. (if local (throw 'done t))
  8336. (org-up-heading-all 1)
  8337. (setq parent t)))
  8338. (error nil)))))
  8339. (append (org-remove-uniherited-tags org-file-tags) tags))))
  8340. (defun org-toggle-tag (tag &optional onoff)
  8341. "Toggle the tag TAG for the current line.
  8342. If ONOFF is `on' or `off', don't toggle but set to this state."
  8343. (unless (org-on-heading-p t) (error "Not on headling"))
  8344. (let (res current)
  8345. (save-excursion
  8346. (beginning-of-line)
  8347. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  8348. (point-at-eol) t)
  8349. (progn
  8350. (setq current (match-string 1))
  8351. (replace-match ""))
  8352. (setq current ""))
  8353. (setq current (nreverse (org-split-string current ":")))
  8354. (cond
  8355. ((eq onoff 'on)
  8356. (setq res t)
  8357. (or (member tag current) (push tag current)))
  8358. ((eq onoff 'off)
  8359. (or (not (member tag current)) (setq current (delete tag current))))
  8360. (t (if (member tag current)
  8361. (setq current (delete tag current))
  8362. (setq res t)
  8363. (push tag current))))
  8364. (end-of-line 1)
  8365. (if current
  8366. (progn
  8367. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  8368. (org-set-tags nil t))
  8369. (delete-horizontal-space))
  8370. (run-hooks 'org-after-tags-change-hook))
  8371. res))
  8372. (defun org-align-tags-here (to-col)
  8373. ;; Assumes that this is a headline
  8374. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  8375. (beginning-of-line 1)
  8376. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  8377. (< pos (match-beginning 2)))
  8378. (progn
  8379. (setq tags-l (- (match-end 2) (match-beginning 2)))
  8380. (goto-char (match-beginning 1))
  8381. (insert " ")
  8382. (delete-region (point) (1+ (match-beginning 2)))
  8383. (setq ncol (max (1+ (current-column))
  8384. (1+ col)
  8385. (if (> to-col 0)
  8386. to-col
  8387. (- (abs to-col) tags-l))))
  8388. (setq p (point))
  8389. (insert (make-string (- ncol (current-column)) ?\ ))
  8390. (setq ncol (current-column))
  8391. (when indent-tabs-mode (tabify p (point-at-eol)))
  8392. (org-move-to-column (min ncol col) t))
  8393. (goto-char pos))))
  8394. (defun org-set-tags-command (&optional arg just-align)
  8395. "Call the set-tags command for the current entry."
  8396. (interactive "P")
  8397. (if (org-on-heading-p)
  8398. (org-set-tags arg just-align)
  8399. (save-excursion
  8400. (org-back-to-heading t)
  8401. (org-set-tags arg just-align))))
  8402. (defun org-set-tags (&optional arg just-align)
  8403. "Set the tags for the current headline.
  8404. With prefix ARG, realign all tags in headings in the current buffer."
  8405. (interactive "P")
  8406. (let* ((re (concat "^" outline-regexp))
  8407. (current (org-get-tags-string))
  8408. (col (current-column))
  8409. (org-setting-tags t)
  8410. table current-tags inherited-tags ; computed below when needed
  8411. tags p0 c0 c1 rpl)
  8412. (if arg
  8413. (save-excursion
  8414. (goto-char (point-min))
  8415. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  8416. (while (re-search-forward re nil t)
  8417. (org-set-tags nil t)
  8418. (end-of-line 1)))
  8419. (message "All tags realigned to column %d" org-tags-column))
  8420. (if just-align
  8421. (setq tags current)
  8422. ;; Get a new set of tags from the user
  8423. (save-excursion
  8424. (setq table (or org-tag-alist (org-get-buffer-tags))
  8425. org-last-tags-completion-table table
  8426. current-tags (org-split-string current ":")
  8427. inherited-tags (nreverse
  8428. (nthcdr (length current-tags)
  8429. (nreverse (org-get-tags-at))))
  8430. tags
  8431. (if (or (eq t org-use-fast-tag-selection)
  8432. (and org-use-fast-tag-selection
  8433. (delq nil (mapcar 'cdr table))))
  8434. (org-fast-tag-selection
  8435. current-tags inherited-tags table
  8436. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  8437. (let ((org-add-colon-after-tag-completion t))
  8438. (org-trim
  8439. (org-without-partial-completion
  8440. (completing-read "Tags: " 'org-tags-completion-function
  8441. nil nil current 'org-tags-history)))))))
  8442. (while (string-match "[-+&]+" tags)
  8443. ;; No boolean logic, just a list
  8444. (setq tags (replace-match ":" t t tags))))
  8445. (if (string-match "\\`[\t ]*\\'" tags)
  8446. (setq tags "")
  8447. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  8448. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  8449. ;; Insert new tags at the correct column
  8450. (beginning-of-line 1)
  8451. (cond
  8452. ((and (equal current "") (equal tags "")))
  8453. ((re-search-forward
  8454. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  8455. (point-at-eol) t)
  8456. (if (equal tags "")
  8457. (setq rpl "")
  8458. (goto-char (match-beginning 0))
  8459. (setq c0 (current-column) p0 (point)
  8460. c1 (max (1+ c0) (if (> org-tags-column 0)
  8461. org-tags-column
  8462. (- (- org-tags-column) (length tags))))
  8463. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  8464. (replace-match rpl t t)
  8465. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  8466. tags)
  8467. (t (error "Tags alignment failed")))
  8468. (org-move-to-column col)
  8469. (unless just-align
  8470. (run-hooks 'org-after-tags-change-hook)))))
  8471. (defun org-change-tag-in-region (beg end tag off)
  8472. "Add or remove TAG for each entry in the region.
  8473. This works in the agenda, and also in an org-mode buffer."
  8474. (interactive
  8475. (list (region-beginning) (region-end)
  8476. (let ((org-last-tags-completion-table
  8477. (if (org-mode-p)
  8478. (org-get-buffer-tags)
  8479. (org-global-tags-completion-table))))
  8480. (completing-read
  8481. "Tag: " 'org-tags-completion-function nil nil nil
  8482. 'org-tags-history))
  8483. (progn
  8484. (message "[s]et or [r]emove? ")
  8485. (equal (read-char-exclusive) ?r))))
  8486. (if (fboundp 'deactivate-mark) (deactivate-mark))
  8487. (let ((agendap (equal major-mode 'org-agenda-mode))
  8488. l1 l2 m buf pos newhead (cnt 0))
  8489. (goto-char end)
  8490. (setq l2 (1- (org-current-line)))
  8491. (goto-char beg)
  8492. (setq l1 (org-current-line))
  8493. (loop for l from l1 to l2 do
  8494. (goto-line l)
  8495. (setq m (get-text-property (point) 'org-hd-marker))
  8496. (when (or (and (org-mode-p) (org-on-heading-p))
  8497. (and agendap m))
  8498. (setq buf (if agendap (marker-buffer m) (current-buffer))
  8499. pos (if agendap m (point)))
  8500. (with-current-buffer buf
  8501. (save-excursion
  8502. (save-restriction
  8503. (goto-char pos)
  8504. (setq cnt (1+ cnt))
  8505. (org-toggle-tag tag (if off 'off 'on))
  8506. (setq newhead (org-get-heading)))))
  8507. (and agendap (org-agenda-change-all-lines newhead m))))
  8508. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  8509. (defun org-tags-completion-function (string predicate &optional flag)
  8510. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  8511. (confirm (lambda (x) (stringp (car x)))))
  8512. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  8513. (setq s1 (match-string 1 string)
  8514. s2 (match-string 2 string))
  8515. (setq s1 "" s2 string))
  8516. (cond
  8517. ((eq flag nil)
  8518. ;; try completion
  8519. (setq rtn (try-completion s2 ctable confirm))
  8520. (if (stringp rtn)
  8521. (setq rtn
  8522. (concat s1 s2 (substring rtn (length s2))
  8523. (if (and org-add-colon-after-tag-completion
  8524. (assoc rtn ctable))
  8525. ":" ""))))
  8526. rtn)
  8527. ((eq flag t)
  8528. ;; all-completions
  8529. (all-completions s2 ctable confirm)
  8530. )
  8531. ((eq flag 'lambda)
  8532. ;; exact match?
  8533. (assoc s2 ctable)))
  8534. ))
  8535. (defun org-fast-tag-insert (kwd tags face &optional end)
  8536. "Insert KDW, and the TAGS, the latter with face FACE. Also inser END."
  8537. (insert (format "%-12s" (concat kwd ":"))
  8538. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  8539. (or end "")))
  8540. (defun org-fast-tag-show-exit (flag)
  8541. (save-excursion
  8542. (goto-line 3)
  8543. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  8544. (replace-match ""))
  8545. (when flag
  8546. (end-of-line 1)
  8547. (org-move-to-column (- (window-width) 19) t)
  8548. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  8549. (defun org-set-current-tags-overlay (current prefix)
  8550. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  8551. (if (featurep 'xemacs)
  8552. (org-overlay-display org-tags-overlay (concat prefix s)
  8553. 'secondary-selection)
  8554. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  8555. (org-overlay-display org-tags-overlay (concat prefix s)))))
  8556. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  8557. "Fast tag selection with single keys.
  8558. CURRENT is the current list of tags in the headline, INHERITED is the
  8559. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  8560. possibly with grouping information. TODO-TABLE is a similar table with
  8561. TODO keywords, should these have keys assigned to them.
  8562. If the keys are nil, a-z are automatically assigned.
  8563. Returns the new tags string, or nil to not change the current settings."
  8564. (let* ((fulltable (append table todo-table))
  8565. (maxlen (apply 'max (mapcar
  8566. (lambda (x)
  8567. (if (stringp (car x)) (string-width (car x)) 0))
  8568. fulltable)))
  8569. (buf (current-buffer))
  8570. (expert (eq org-fast-tag-selection-single-key 'expert))
  8571. (buffer-tags nil)
  8572. (fwidth (+ maxlen 3 1 3))
  8573. (ncol (/ (- (window-width) 4) fwidth))
  8574. (i-face 'org-done)
  8575. (c-face 'org-todo)
  8576. tg cnt e c char c1 c2 ntable tbl rtn
  8577. ov-start ov-end ov-prefix
  8578. (exit-after-next org-fast-tag-selection-single-key)
  8579. (done-keywords org-done-keywords)
  8580. groups ingroup)
  8581. (save-excursion
  8582. (beginning-of-line 1)
  8583. (if (looking-at
  8584. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  8585. (setq ov-start (match-beginning 1)
  8586. ov-end (match-end 1)
  8587. ov-prefix "")
  8588. (setq ov-start (1- (point-at-eol))
  8589. ov-end (1+ ov-start))
  8590. (skip-chars-forward "^\n\r")
  8591. (setq ov-prefix
  8592. (concat
  8593. (buffer-substring (1- (point)) (point))
  8594. (if (> (current-column) org-tags-column)
  8595. " "
  8596. (make-string (- org-tags-column (current-column)) ?\ ))))))
  8597. (org-move-overlay org-tags-overlay ov-start ov-end)
  8598. (save-window-excursion
  8599. (if expert
  8600. (set-buffer (get-buffer-create " *Org tags*"))
  8601. (delete-other-windows)
  8602. (split-window-vertically)
  8603. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  8604. (erase-buffer)
  8605. (org-set-local 'org-done-keywords done-keywords)
  8606. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  8607. (org-fast-tag-insert "Current" current c-face "\n\n")
  8608. (org-fast-tag-show-exit exit-after-next)
  8609. (org-set-current-tags-overlay current ov-prefix)
  8610. (setq tbl fulltable char ?a cnt 0)
  8611. (while (setq e (pop tbl))
  8612. (cond
  8613. ((equal e '(:startgroup))
  8614. (push '() groups) (setq ingroup t)
  8615. (when (not (= cnt 0))
  8616. (setq cnt 0)
  8617. (insert "\n"))
  8618. (insert "{ "))
  8619. ((equal e '(:endgroup))
  8620. (setq ingroup nil cnt 0)
  8621. (insert "}\n"))
  8622. (t
  8623. (setq tg (car e) c2 nil)
  8624. (if (cdr e)
  8625. (setq c (cdr e))
  8626. ;; automatically assign a character.
  8627. (setq c1 (string-to-char
  8628. (downcase (substring
  8629. tg (if (= (string-to-char tg) ?@) 1 0)))))
  8630. (if (or (rassoc c1 ntable) (rassoc c1 table))
  8631. (while (or (rassoc char ntable) (rassoc char table))
  8632. (setq char (1+ char)))
  8633. (setq c2 c1))
  8634. (setq c (or c2 char)))
  8635. (if ingroup (push tg (car groups)))
  8636. (setq tg (org-add-props tg nil 'face
  8637. (cond
  8638. ((not (assoc tg table))
  8639. (org-get-todo-face tg))
  8640. ((member tg current) c-face)
  8641. ((member tg inherited) i-face)
  8642. (t nil))))
  8643. (if (and (= cnt 0) (not ingroup)) (insert " "))
  8644. (insert "[" c "] " tg (make-string
  8645. (- fwidth 4 (length tg)) ?\ ))
  8646. (push (cons tg c) ntable)
  8647. (when (= (setq cnt (1+ cnt)) ncol)
  8648. (insert "\n")
  8649. (if ingroup (insert " "))
  8650. (setq cnt 0)))))
  8651. (setq ntable (nreverse ntable))
  8652. (insert "\n")
  8653. (goto-char (point-min))
  8654. (if (and (not expert) (fboundp 'fit-window-to-buffer))
  8655. (fit-window-to-buffer))
  8656. (setq rtn
  8657. (catch 'exit
  8658. (while t
  8659. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  8660. (if groups " [!] no groups" " [!]groups")
  8661. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  8662. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  8663. (cond
  8664. ((= c ?\r) (throw 'exit t))
  8665. ((= c ?!)
  8666. (setq groups (not groups))
  8667. (goto-char (point-min))
  8668. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  8669. ((= c ?\C-c)
  8670. (if (not expert)
  8671. (org-fast-tag-show-exit
  8672. (setq exit-after-next (not exit-after-next)))
  8673. (setq expert nil)
  8674. (delete-other-windows)
  8675. (split-window-vertically)
  8676. (org-switch-to-buffer-other-window " *Org tags*")
  8677. (and (fboundp 'fit-window-to-buffer)
  8678. (fit-window-to-buffer))))
  8679. ((or (= c ?\C-g)
  8680. (and (= c ?q) (not (rassoc c ntable))))
  8681. (org-detach-overlay org-tags-overlay)
  8682. (setq quit-flag t))
  8683. ((= c ?\ )
  8684. (setq current nil)
  8685. (if exit-after-next (setq exit-after-next 'now)))
  8686. ((= c ?\t)
  8687. (condition-case nil
  8688. (setq tg (completing-read
  8689. "Tag: "
  8690. (or buffer-tags
  8691. (with-current-buffer buf
  8692. (org-get-buffer-tags)))))
  8693. (quit (setq tg "")))
  8694. (when (string-match "\\S-" tg)
  8695. (add-to-list 'buffer-tags (list tg))
  8696. (if (member tg current)
  8697. (setq current (delete tg current))
  8698. (push tg current)))
  8699. (if exit-after-next (setq exit-after-next 'now)))
  8700. ((setq e (rassoc c todo-table) tg (car e))
  8701. (with-current-buffer buf
  8702. (save-excursion (org-todo tg)))
  8703. (if exit-after-next (setq exit-after-next 'now)))
  8704. ((setq e (rassoc c ntable) tg (car e))
  8705. (if (member tg current)
  8706. (setq current (delete tg current))
  8707. (loop for g in groups do
  8708. (if (member tg g)
  8709. (mapc (lambda (x)
  8710. (setq current (delete x current)))
  8711. g)))
  8712. (push tg current))
  8713. (if exit-after-next (setq exit-after-next 'now))))
  8714. ;; Create a sorted list
  8715. (setq current
  8716. (sort current
  8717. (lambda (a b)
  8718. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  8719. (if (eq exit-after-next 'now) (throw 'exit t))
  8720. (goto-char (point-min))
  8721. (beginning-of-line 2)
  8722. (delete-region (point) (point-at-eol))
  8723. (org-fast-tag-insert "Current" current c-face)
  8724. (org-set-current-tags-overlay current ov-prefix)
  8725. (while (re-search-forward
  8726. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  8727. (setq tg (match-string 1))
  8728. (add-text-properties
  8729. (match-beginning 1) (match-end 1)
  8730. (list 'face
  8731. (cond
  8732. ((member tg current) c-face)
  8733. ((member tg inherited) i-face)
  8734. (t (get-text-property (match-beginning 1) 'face))))))
  8735. (goto-char (point-min)))))
  8736. (org-detach-overlay org-tags-overlay)
  8737. (if rtn
  8738. (mapconcat 'identity current ":")
  8739. nil))))
  8740. (defun org-get-tags-string ()
  8741. "Get the TAGS string in the current headline."
  8742. (unless (org-on-heading-p t)
  8743. (error "Not on a heading"))
  8744. (save-excursion
  8745. (beginning-of-line 1)
  8746. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  8747. (org-match-string-no-properties 1)
  8748. "")))
  8749. (defun org-get-tags ()
  8750. "Get the list of tags specified in the current headline."
  8751. (org-split-string (org-get-tags-string) ":"))
  8752. (defun org-get-buffer-tags ()
  8753. "Get a table of all tags used in the buffer, for completion."
  8754. (let (tags)
  8755. (save-excursion
  8756. (goto-char (point-min))
  8757. (while (re-search-forward
  8758. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  8759. (when (equal (char-after (point-at-bol 0)) ?*)
  8760. (mapc (lambda (x) (add-to-list 'tags x))
  8761. (org-split-string (org-match-string-no-properties 1) ":")))))
  8762. (mapcar 'list tags)))
  8763. ;;;; The mapping API
  8764. ;;;###autoload
  8765. (defun org-map-entries (func &optional match scope &rest skip)
  8766. "Call FUNC at each headline selected by MATCH in SCOPE.
  8767. FUNC is a function or a lisp form. The function will be called without
  8768. arguments, with the cursor positioned at the beginning of the headline.
  8769. The return values of all calls to the function will be collected and
  8770. returned as a list.
  8771. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  8772. Only headlines that are matched by this query will be considered during
  8773. the iteration. When MATCH is nil or t, all headlines will be
  8774. visited by the iteration.
  8775. SCOPE determines the scope of this command. It can be any of:
  8776. nil The current buffer, respecting the restriction if any
  8777. tree The subtree started with the entry at point
  8778. file The current buffer, without restriction
  8779. file-with-archives
  8780. The current buffer, and any archives associated with it
  8781. agenda All agenda files
  8782. agenda-with-archives
  8783. All agenda files with any archive files associated with them
  8784. \(file1 file2 ...)
  8785. If this is a list, all files in the list will be scanned
  8786. The remaining args are treated as settings for the skipping facilities of
  8787. the scanner. The following items can be given here:
  8788. archive skip trees with the archive tag.
  8789. comment skip trees with the COMMENT keyword
  8790. function or Emacs Lisp form:
  8791. will be used as value for `org-agenda-skip-function', so whenever
  8792. the the function returns t, FUNC will not be called for that
  8793. entry and search will continue from the point where the
  8794. function leaves it."
  8795. (let* ((org-agenda-archives-mode nil) ; just to make sure
  8796. (org-agenda-skip-archived-trees (memq 'archive skip))
  8797. (org-agenda-skip-comment-trees (memq 'comment skip))
  8798. (org-agenda-skip-function
  8799. (car (org-delete-all '(comment archive) skip)))
  8800. (org-tags-match-list-sublevels t)
  8801. matcher pos file
  8802. org-todo-keywords-for-agenda
  8803. org-done-keywords-for-agenda
  8804. org-todo-keyword-alist-for-agenda
  8805. org-tag-alist-for-agenda)
  8806. (cond
  8807. ((eq match t) (setq matcher t))
  8808. ((eq match nil) (setq matcher t))
  8809. (t (setq matcher (if match (org-make-tags-matcher match) t))))
  8810. (when (eq scope 'tree)
  8811. (org-back-to-heading t)
  8812. (org-narrow-to-subtree)
  8813. (setq scope nil))
  8814. (if (not scope)
  8815. (progn
  8816. (org-prepare-agenda-buffers
  8817. (list (buffer-file-name (current-buffer))))
  8818. (org-scan-tags func matcher))
  8819. ;; Get the right scope
  8820. (setq pos (point))
  8821. (cond
  8822. ((and scope (listp scope) (symbolp (car scope)))
  8823. (setq scope (eval scope)))
  8824. ((eq scope 'agenda)
  8825. (setq scope (org-agenda-files t)))
  8826. ((eq scope 'agenda-with-archives)
  8827. (setq scope (org-agenda-files t))
  8828. (setq scope (org-add-archive-files scope)))
  8829. ((eq scope 'file)
  8830. (setq scope (list (buffer-file-name))))
  8831. ((eq scope 'file-with-archives)
  8832. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  8833. (org-prepare-agenda-buffers scope)
  8834. (while (setq file (pop scope))
  8835. (with-current-buffer (org-find-base-buffer-visiting file)
  8836. (save-excursion
  8837. (save-restriction
  8838. (widen)
  8839. (goto-char (point-min))
  8840. (org-scan-tags func matcher))))))))
  8841. ;;;; Properties
  8842. ;;; Setting and retrieving properties
  8843. (defconst org-special-properties
  8844. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "PRIORITY"
  8845. "TIMESTAMP" "TIMESTAMP_IA")
  8846. "The special properties valid in Org-mode.
  8847. These are properties that are not defined in the property drawer,
  8848. but in some other way.")
  8849. (defconst org-default-properties
  8850. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION"
  8851. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  8852. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  8853. "EXPORT_FILE_NAME" "EXPORT_TITLE")
  8854. "Some properties that are used by Org-mode for various purposes.
  8855. Being in this list makes sure that they are offered for completion.")
  8856. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  8857. "Regular expression matching the first line of a property drawer.")
  8858. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  8859. "Regular expression matching the first line of a property drawer.")
  8860. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  8861. "Regular expression matching the first line of a property drawer.")
  8862. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  8863. "Regular expression matching the first line of a property drawer.")
  8864. (defconst org-property-drawer-re
  8865. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  8866. org-property-end-re "\\)\n?")
  8867. "Matches an entire property drawer.")
  8868. (defconst org-clock-drawer-re
  8869. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  8870. org-property-end-re "\\)\n?")
  8871. "Matches an entire clock drawer.")
  8872. (defun org-property-action ()
  8873. "Do an action on properties."
  8874. (interactive)
  8875. (let (c)
  8876. (org-at-property-p)
  8877. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  8878. (setq c (read-char-exclusive))
  8879. (cond
  8880. ((equal c ?s)
  8881. (call-interactively 'org-set-property))
  8882. ((equal c ?d)
  8883. (call-interactively 'org-delete-property))
  8884. ((equal c ?D)
  8885. (call-interactively 'org-delete-property-globally))
  8886. ((equal c ?c)
  8887. (call-interactively 'org-compute-property-at-point))
  8888. (t (error "No such property action %c" c)))))
  8889. (defun org-at-property-p ()
  8890. "Is the cursor in a property line?"
  8891. ;; FIXME: Does not check if we are actually in the drawer.
  8892. ;; FIXME: also returns true on any drawers.....
  8893. ;; This is used by C-c C-c for property action.
  8894. (save-excursion
  8895. (beginning-of-line 1)
  8896. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  8897. (defun org-get-property-block (&optional beg end force)
  8898. "Return the (beg . end) range of the body of the property drawer.
  8899. BEG and END can be beginning and end of subtree, if not given
  8900. they will be found.
  8901. If the drawer does not exist and FORCE is non-nil, create the drawer."
  8902. (catch 'exit
  8903. (save-excursion
  8904. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  8905. (end (or end (progn (outline-next-heading) (point)))))
  8906. (goto-char beg)
  8907. (if (re-search-forward org-property-start-re end t)
  8908. (setq beg (1+ (match-end 0)))
  8909. (if force
  8910. (save-excursion
  8911. (org-insert-property-drawer)
  8912. (setq end (progn (outline-next-heading) (point))))
  8913. (throw 'exit nil))
  8914. (goto-char beg)
  8915. (if (re-search-forward org-property-start-re end t)
  8916. (setq beg (1+ (match-end 0)))))
  8917. (if (re-search-forward org-property-end-re end t)
  8918. (setq end (match-beginning 0))
  8919. (or force (throw 'exit nil))
  8920. (goto-char beg)
  8921. (setq end beg)
  8922. (org-indent-line-function)
  8923. (insert ":END:\n"))
  8924. (cons beg end)))))
  8925. (defun org-entry-properties (&optional pom which)
  8926. "Get all properties of the entry at point-or-marker POM.
  8927. This includes the TODO keyword, the tags, time strings for deadline,
  8928. scheduled, and clocking, and any additional properties defined in the
  8929. entry. The return value is an alist, keys may occur multiple times
  8930. if the property key was used several times.
  8931. POM may also be nil, in which case the current entry is used.
  8932. If WHICH is nil or `all', get all properties. If WHICH is
  8933. `special' or `standard', only get that subclass."
  8934. (setq which (or which 'all))
  8935. (org-with-point-at pom
  8936. (let ((clockstr (substring org-clock-string 0 -1))
  8937. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  8938. beg end range props sum-props key value string clocksum)
  8939. (save-excursion
  8940. (when (condition-case nil (org-back-to-heading t) (error nil))
  8941. (setq beg (point))
  8942. (setq sum-props (get-text-property (point) 'org-summaries))
  8943. (setq clocksum (get-text-property (point) :org-clock-minutes))
  8944. (outline-next-heading)
  8945. (setq end (point))
  8946. (when (memq which '(all special))
  8947. ;; Get the special properties, like TODO and tags
  8948. (goto-char beg)
  8949. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  8950. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  8951. (when (looking-at org-priority-regexp)
  8952. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  8953. (when (and (setq value (org-get-tags-string))
  8954. (string-match "\\S-" value))
  8955. (push (cons "TAGS" value) props))
  8956. (when (setq value (org-get-tags-at))
  8957. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  8958. props))
  8959. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  8960. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  8961. string (if (equal key clockstr)
  8962. (org-no-properties
  8963. (org-trim
  8964. (buffer-substring
  8965. (match-beginning 3) (goto-char (point-at-eol)))))
  8966. (substring (org-match-string-no-properties 3) 1 -1)))
  8967. (unless key
  8968. (if (= (char-after (match-beginning 3)) ?\[)
  8969. (setq key "TIMESTAMP_IA")
  8970. (setq key "TIMESTAMP")))
  8971. (when (or (equal key clockstr) (not (assoc key props)))
  8972. (push (cons key string) props)))
  8973. )
  8974. (when (memq which '(all standard))
  8975. ;; Get the standard properties, like :PORP: ...
  8976. (setq range (org-get-property-block beg end))
  8977. (when range
  8978. (goto-char (car range))
  8979. (while (re-search-forward
  8980. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  8981. (cdr range) t)
  8982. (setq key (org-match-string-no-properties 1)
  8983. value (org-trim (or (org-match-string-no-properties 2) "")))
  8984. (unless (member key excluded)
  8985. (push (cons key (or value "")) props)))))
  8986. (if clocksum
  8987. (push (cons "CLOCKSUM"
  8988. (org-columns-number-to-string (/ (float clocksum) 60.)
  8989. 'add_times))
  8990. props))
  8991. (unless (assoc "CATEGORY" props)
  8992. (setq value (or (org-get-category)
  8993. (progn (org-refresh-category-properties)
  8994. (org-get-category))))
  8995. (push (cons "CATEGORY" value) props))
  8996. (append sum-props (nreverse props)))))))
  8997. (defun org-entry-get (pom property &optional inherit)
  8998. "Get value of PROPERTY for entry at point-or-marker POM.
  8999. If INHERIT is non-nil and the entry does not have the property,
  9000. then also check higher levels of the hierarchy.
  9001. If INHERIT is the symbol `selective', use inheritance only if the setting
  9002. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  9003. If the property is present but empty, the return value is the empty string.
  9004. If the property is not present at all, nil is returned."
  9005. (org-with-point-at pom
  9006. (if (and inherit (if (eq inherit 'selective)
  9007. (org-property-inherit-p property)
  9008. t))
  9009. (org-entry-get-with-inheritance property)
  9010. (if (member property org-special-properties)
  9011. ;; We need a special property. Use brute force, get all properties.
  9012. (cdr (assoc property (org-entry-properties nil 'special)))
  9013. (let ((range (org-get-property-block)))
  9014. (if (and range
  9015. (goto-char (car range))
  9016. (re-search-forward
  9017. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)?")
  9018. (cdr range) t))
  9019. ;; Found the property, return it.
  9020. (if (match-end 1)
  9021. (org-match-string-no-properties 1)
  9022. "")))))))
  9023. (defun org-property-or-variable-value (var &optional inherit)
  9024. "Check if there is a property fixing the value of VAR.
  9025. If yes, return this value. If not, return the current value of the variable."
  9026. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  9027. (if (and prop (stringp prop) (string-match "\\S-" prop))
  9028. (read prop)
  9029. (symbol-value var))))
  9030. (defun org-entry-delete (pom property)
  9031. "Delete the property PROPERTY from entry at point-or-marker POM."
  9032. (org-with-point-at pom
  9033. (if (member property org-special-properties)
  9034. nil ; cannot delete these properties.
  9035. (let ((range (org-get-property-block)))
  9036. (if (and range
  9037. (goto-char (car range))
  9038. (re-search-forward
  9039. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)")
  9040. (cdr range) t))
  9041. (progn
  9042. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  9043. t)
  9044. nil)))))
  9045. ;; Multi-values properties are properties that contain multiple values
  9046. ;; These values are assumed to be single words, separated by whitespace.
  9047. (defun org-entry-add-to-multivalued-property (pom property value)
  9048. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  9049. (let* ((old (org-entry-get pom property))
  9050. (values (and old (org-split-string old "[ \t]"))))
  9051. (setq value (org-entry-protect-space value))
  9052. (unless (member value values)
  9053. (setq values (cons value values))
  9054. (org-entry-put pom property
  9055. (mapconcat 'identity values " ")))))
  9056. (defun org-entry-remove-from-multivalued-property (pom property value)
  9057. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  9058. (let* ((old (org-entry-get pom property))
  9059. (values (and old (org-split-string old "[ \t]"))))
  9060. (setq value (org-entry-protect-space value))
  9061. (when (member value values)
  9062. (setq values (delete value values))
  9063. (org-entry-put pom property
  9064. (mapconcat 'identity values " ")))))
  9065. (defun org-entry-member-in-multivalued-property (pom property value)
  9066. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  9067. (let* ((old (org-entry-get pom property))
  9068. (values (and old (org-split-string old "[ \t]"))))
  9069. (setq value (org-entry-protect-space value))
  9070. (member value values)))
  9071. (defun org-entry-get-multivalued-property (pom property)
  9072. "Return a list of values in a multivalued property."
  9073. (let* ((value (org-entry-get pom property))
  9074. (values (and value (org-split-string value "[ \t]"))))
  9075. (mapcar 'org-entry-restore-space values)))
  9076. (defun org-entry-put-multivalued-property (pom property &rest values)
  9077. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  9078. VALUES should be a list of strings. Spaces will be protected."
  9079. (org-entry-put pom property
  9080. (mapconcat 'org-entry-protect-space values " "))
  9081. (let* ((value (org-entry-get pom property))
  9082. (values (and value (org-split-string value "[ \t]"))))
  9083. (mapcar 'org-entry-restore-space values)))
  9084. (defun org-entry-protect-space (s)
  9085. "Protect spaces and newline in string S."
  9086. (while (string-match " " s)
  9087. (setq s (replace-match "%20" t t s)))
  9088. (while (string-match "\n" s)
  9089. (setq s (replace-match "%0A" t t s)))
  9090. s)
  9091. (defun org-entry-restore-space (s)
  9092. "Restore spaces and newline in string S."
  9093. (while (string-match "%20" s)
  9094. (setq s (replace-match " " t t s)))
  9095. (while (string-match "%0A" s)
  9096. (setq s (replace-match "\n" t t s)))
  9097. s)
  9098. (defvar org-entry-property-inherited-from (make-marker)
  9099. "Marker pointing to the entry from where a proerty was inherited.
  9100. Each call to `org-entry-get-with-inheritance' will set this marker to the
  9101. location of the entry where the inheriance search matched. If there was
  9102. no match, the marker will point nowhere.
  9103. Note that also `org-entry-get' calls this function, if the INHERIT flag
  9104. is set.")
  9105. (defun org-entry-get-with-inheritance (property)
  9106. "Get entry property, and search higher levels if not present."
  9107. (move-marker org-entry-property-inherited-from nil)
  9108. (let (tmp)
  9109. (save-excursion
  9110. (save-restriction
  9111. (widen)
  9112. (catch 'ex
  9113. (while t
  9114. (when (setq tmp (org-entry-get nil property))
  9115. (org-back-to-heading t)
  9116. (move-marker org-entry-property-inherited-from (point))
  9117. (throw 'ex tmp))
  9118. (or (org-up-heading-safe) (throw 'ex nil)))))
  9119. (or tmp
  9120. (cdr (assoc property org-file-properties))
  9121. (cdr (assoc property org-global-properties))
  9122. (cdr (assoc property org-global-properties-fixed))))))
  9123. (defun org-entry-put (pom property value)
  9124. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  9125. (org-with-point-at pom
  9126. (org-back-to-heading t)
  9127. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  9128. range)
  9129. (cond
  9130. ((equal property "TODO")
  9131. (when (and (stringp value) (string-match "\\S-" value)
  9132. (not (member value org-todo-keywords-1)))
  9133. (error "\"%s\" is not a valid TODO state" value))
  9134. (if (or (not value)
  9135. (not (string-match "\\S-" value)))
  9136. (setq value 'none))
  9137. (org-todo value)
  9138. (org-set-tags nil 'align))
  9139. ((equal property "PRIORITY")
  9140. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  9141. (string-to-char value) ?\ ))
  9142. (org-set-tags nil 'align))
  9143. ((equal property "SCHEDULED")
  9144. (if (re-search-forward org-scheduled-time-regexp end t)
  9145. (cond
  9146. ((eq value 'earlier) (org-timestamp-change -1 'day))
  9147. ((eq value 'later) (org-timestamp-change 1 'day))
  9148. (t (call-interactively 'org-schedule)))
  9149. (call-interactively 'org-schedule)))
  9150. ((equal property "DEADLINE")
  9151. (if (re-search-forward org-deadline-time-regexp end t)
  9152. (cond
  9153. ((eq value 'earlier) (org-timestamp-change -1 'day))
  9154. ((eq value 'later) (org-timestamp-change 1 'day))
  9155. (t (call-interactively 'org-deadline)))
  9156. (call-interactively 'org-deadline)))
  9157. ((member property org-special-properties)
  9158. (error "The %s property can not yet be set with `org-entry-put'"
  9159. property))
  9160. (t ; a non-special property
  9161. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  9162. (setq range (org-get-property-block beg end 'force))
  9163. (goto-char (car range))
  9164. (if (re-search-forward
  9165. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  9166. (progn
  9167. (delete-region (match-beginning 1) (match-end 1))
  9168. (goto-char (match-beginning 1)))
  9169. (goto-char (cdr range))
  9170. (insert "\n")
  9171. (backward-char 1)
  9172. (org-indent-line-function)
  9173. (insert ":" property ":"))
  9174. (and value (insert " " value))
  9175. (org-indent-line-function)))))))
  9176. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  9177. "Get all property keys in the current buffer.
  9178. With INCLUDE-SPECIALS, also list the special properties that relect things
  9179. like tags and TODO state.
  9180. With INCLUDE-DEFAULTS, also include properties that has special meaning
  9181. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  9182. With INCLUDE-COLUMNS, also include property names given in COLUMN
  9183. formats in the current buffer."
  9184. (let (rtn range cfmt cols s p)
  9185. (save-excursion
  9186. (save-restriction
  9187. (widen)
  9188. (goto-char (point-min))
  9189. (while (re-search-forward org-property-start-re nil t)
  9190. (setq range (org-get-property-block))
  9191. (goto-char (car range))
  9192. (while (re-search-forward
  9193. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  9194. (cdr range) t)
  9195. (add-to-list 'rtn (org-match-string-no-properties 1)))
  9196. (outline-next-heading))))
  9197. (when include-specials
  9198. (setq rtn (append org-special-properties rtn)))
  9199. (when include-defaults
  9200. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties))
  9201. (when include-columns
  9202. (save-excursion
  9203. (save-restriction
  9204. (widen)
  9205. (goto-char (point-min))
  9206. (while (re-search-forward
  9207. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  9208. nil t)
  9209. (setq cfmt (match-string 2) s 0)
  9210. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  9211. cfmt s)
  9212. (setq s (match-end 0)
  9213. p (match-string 1 cfmt))
  9214. (unless (or (equal p "ITEM")
  9215. (member p org-special-properties))
  9216. (add-to-list 'rtn (match-string 1 cfmt))))))))
  9217. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  9218. (defun org-property-values (key)
  9219. "Return a list of all values of property KEY."
  9220. (save-excursion
  9221. (save-restriction
  9222. (widen)
  9223. (goto-char (point-min))
  9224. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  9225. values)
  9226. (while (re-search-forward re nil t)
  9227. (add-to-list 'values (org-trim (match-string 1))))
  9228. (delete "" values)))))
  9229. (defun org-insert-property-drawer ()
  9230. "Insert a property drawer into the current entry."
  9231. (interactive)
  9232. (org-back-to-heading t)
  9233. (looking-at outline-regexp)
  9234. (let ((indent (- (match-end 0)(match-beginning 0)))
  9235. (beg (point))
  9236. (re (concat "^[ \t]*" org-keyword-time-regexp))
  9237. end hiddenp)
  9238. (outline-next-heading)
  9239. (setq end (point))
  9240. (goto-char beg)
  9241. (while (re-search-forward re end t))
  9242. (setq hiddenp (org-invisible-p))
  9243. (end-of-line 1)
  9244. (and (equal (char-after) ?\n) (forward-char 1))
  9245. (while (looking-at "^[ \t]*\\(:CLOCK:\\|CLOCK\\|:END:\\)")
  9246. (beginning-of-line 2))
  9247. (org-skip-over-state-notes)
  9248. (skip-chars-backward " \t\n\r")
  9249. (if (eq (char-before) ?*) (forward-char 1))
  9250. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  9251. (beginning-of-line 0)
  9252. (org-indent-to-column indent)
  9253. (beginning-of-line 2)
  9254. (org-indent-to-column indent)
  9255. (beginning-of-line 0)
  9256. (if hiddenp
  9257. (save-excursion
  9258. (org-back-to-heading t)
  9259. (hide-entry))
  9260. (org-flag-drawer t))))
  9261. (defun org-set-property (property value)
  9262. "In the current entry, set PROPERTY to VALUE.
  9263. When called interactively, this will prompt for a property name, offering
  9264. completion on existing and default properties. And then it will prompt
  9265. for a value, offering competion either on allowed values (via an inherited
  9266. xxx_ALL property) or on existing values in other instances of this property
  9267. in the current file."
  9268. (interactive
  9269. (let* ((completion-ignore-case t)
  9270. (keys (org-buffer-property-keys nil t t))
  9271. (prop0 (completing-read "Property: " (mapcar 'list keys)))
  9272. (prop (if (member prop0 keys)
  9273. prop0
  9274. (or (cdr (assoc (downcase prop0)
  9275. (mapcar (lambda (x) (cons (downcase x) x))
  9276. keys)))
  9277. prop0)))
  9278. (cur (org-entry-get nil prop))
  9279. (allowed (org-property-get-allowed-values nil prop 'table))
  9280. (existing (mapcar 'list (org-property-values prop)))
  9281. (val (if allowed
  9282. (org-completing-read "Value: " allowed nil 'req-match)
  9283. (org-completing-read
  9284. (concat "Value" (if (and cur (string-match "\\S-" cur))
  9285. (concat "[" cur "]") "")
  9286. ": ")
  9287. existing nil nil "" nil cur))))
  9288. (list prop (if (equal val "") cur val))))
  9289. (unless (equal (org-entry-get nil property) value)
  9290. (org-entry-put nil property value)))
  9291. (defun org-delete-property (property)
  9292. "In the current entry, delete PROPERTY."
  9293. (interactive
  9294. (let* ((completion-ignore-case t)
  9295. (prop (completing-read
  9296. "Property: " (org-entry-properties nil 'standard))))
  9297. (list prop)))
  9298. (message "Property %s %s" property
  9299. (if (org-entry-delete nil property)
  9300. "deleted"
  9301. "was not present in the entry")))
  9302. (defun org-delete-property-globally (property)
  9303. "Remove PROPERTY globally, from all entries."
  9304. (interactive
  9305. (let* ((completion-ignore-case t)
  9306. (prop (completing-read
  9307. "Globally remove property: "
  9308. (mapcar 'list (org-buffer-property-keys)))))
  9309. (list prop)))
  9310. (save-excursion
  9311. (save-restriction
  9312. (widen)
  9313. (goto-char (point-min))
  9314. (let ((cnt 0))
  9315. (while (re-search-forward
  9316. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  9317. nil t)
  9318. (setq cnt (1+ cnt))
  9319. (replace-match ""))
  9320. (message "Property \"%s\" removed from %d entries" property cnt)))))
  9321. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  9322. (defun org-compute-property-at-point ()
  9323. "Compute the property at point.
  9324. This looks for an enclosing column format, extracts the operator and
  9325. then applies it to the proerty in the column format's scope."
  9326. (interactive)
  9327. (unless (org-at-property-p)
  9328. (error "Not at a property"))
  9329. (let ((prop (org-match-string-no-properties 2)))
  9330. (org-columns-get-format-and-top-level)
  9331. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  9332. (error "No operator defined for property %s" prop))
  9333. (org-columns-compute prop)))
  9334. (defun org-property-get-allowed-values (pom property &optional table)
  9335. "Get allowed values for the property PROPERTY.
  9336. When TABLE is non-nil, return an alist that can directly be used for
  9337. completion."
  9338. (let (vals)
  9339. (cond
  9340. ((equal property "TODO")
  9341. (setq vals (org-with-point-at pom
  9342. (append org-todo-keywords-1 '("")))))
  9343. ((equal property "PRIORITY")
  9344. (let ((n org-lowest-priority))
  9345. (while (>= n org-highest-priority)
  9346. (push (char-to-string n) vals)
  9347. (setq n (1- n)))))
  9348. ((member property org-special-properties))
  9349. (t
  9350. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  9351. (when (and vals (string-match "\\S-" vals))
  9352. (setq vals (car (read-from-string (concat "(" vals ")"))))
  9353. (setq vals (mapcar (lambda (x)
  9354. (cond ((stringp x) x)
  9355. ((numberp x) (number-to-string x))
  9356. ((symbolp x) (symbol-name x))
  9357. (t "???")))
  9358. vals)))))
  9359. (if table (mapcar 'list vals) vals)))
  9360. (defun org-property-previous-allowed-value (&optional previous)
  9361. "Switch to the next allowed value for this property."
  9362. (interactive)
  9363. (org-property-next-allowed-value t))
  9364. (defun org-property-next-allowed-value (&optional previous)
  9365. "Switch to the next allowed value for this property."
  9366. (interactive)
  9367. (unless (org-at-property-p)
  9368. (error "Not at a property"))
  9369. (let* ((key (match-string 2))
  9370. (value (match-string 3))
  9371. (allowed (or (org-property-get-allowed-values (point) key)
  9372. (and (member value '("[ ]" "[-]" "[X]"))
  9373. '("[ ]" "[X]"))))
  9374. nval)
  9375. (unless allowed
  9376. (error "Allowed values for this property have not been defined"))
  9377. (if previous (setq allowed (reverse allowed)))
  9378. (if (member value allowed)
  9379. (setq nval (car (cdr (member value allowed)))))
  9380. (setq nval (or nval (car allowed)))
  9381. (if (equal nval value)
  9382. (error "Only one allowed value for this property"))
  9383. (org-at-property-p)
  9384. (replace-match (concat " :" key ": " nval) t t)
  9385. (org-indent-line-function)
  9386. (beginning-of-line 1)
  9387. (skip-chars-forward " \t")))
  9388. (defun org-find-entry-with-id (ident)
  9389. "Locate the entry that contains the ID property with exact value IDENT.
  9390. IDENT can be a string, a symbol or a number, this function will search for
  9391. the string representation of it.
  9392. Return the position where this entry starts, or nil if there is no such entry."
  9393. (let ((id (cond
  9394. ((stringp ident) ident)
  9395. ((symbol-name ident) (symbol-name ident))
  9396. ((numberp ident) (number-to-string ident))
  9397. (t (error "IDENT %s must be a string, symbol or number" ident))))
  9398. (case-fold-search nil))
  9399. (save-excursion
  9400. (save-restriction
  9401. (widen)
  9402. (goto-char (point-min))
  9403. (when (re-search-forward
  9404. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  9405. nil t)
  9406. (org-back-to-heading)
  9407. (point))))))
  9408. ;;;; Timestamps
  9409. (defvar org-last-changed-timestamp nil)
  9410. (defvar org-last-inserted-timestamp nil
  9411. "The last time stamp inserted with `org-insert-time-stamp'.")
  9412. (defvar org-time-was-given) ; dynamically scoped parameter
  9413. (defvar org-end-time-was-given) ; dynamically scoped parameter
  9414. (defvar org-ts-what) ; dynamically scoped parameter
  9415. (defun org-time-stamp (arg &optional inactive)
  9416. "Prompt for a date/time and insert a time stamp.
  9417. If the user specifies a time like HH:MM, or if this command is called
  9418. with a prefix argument, the time stamp will contain date and time.
  9419. Otherwise, only the date will be included. All parts of a date not
  9420. specified by the user will be filled in from the current date/time.
  9421. So if you press just return without typing anything, the time stamp
  9422. will represent the current date/time. If there is already a timestamp
  9423. at the cursor, it will be modified."
  9424. (interactive "P")
  9425. (let* ((ts nil)
  9426. (default-time
  9427. ;; Default time is either today, or, when entering a range,
  9428. ;; the range start.
  9429. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  9430. (save-excursion
  9431. (re-search-backward
  9432. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  9433. (- (point) 20) t)))
  9434. (apply 'encode-time (org-parse-time-string (match-string 1)))
  9435. (current-time)))
  9436. (default-input (and ts (org-get-compact-tod ts)))
  9437. org-time-was-given org-end-time-was-given time)
  9438. (cond
  9439. ((and (org-at-timestamp-p t)
  9440. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  9441. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  9442. (insert "--")
  9443. (setq time (let ((this-command this-command))
  9444. (org-read-date arg 'totime nil nil
  9445. default-time default-input)))
  9446. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  9447. ((org-at-timestamp-p t)
  9448. (setq time (let ((this-command this-command))
  9449. (org-read-date arg 'totime nil nil default-time default-input)))
  9450. (when (org-at-timestamp-p t) ; just to get the match data
  9451. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  9452. (replace-match "")
  9453. (setq org-last-changed-timestamp
  9454. (org-insert-time-stamp
  9455. time (or org-time-was-given arg)
  9456. inactive nil nil (list org-end-time-was-given))))
  9457. (message "Timestamp updated"))
  9458. (t
  9459. (setq time (let ((this-command this-command))
  9460. (org-read-date arg 'totime nil nil default-time default-input)))
  9461. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  9462. nil nil (list org-end-time-was-given))))))
  9463. ;; FIXME: can we use this for something else, like computing time differences?
  9464. (defun org-get-compact-tod (s)
  9465. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  9466. (let* ((t1 (match-string 1 s))
  9467. (h1 (string-to-number (match-string 2 s)))
  9468. (m1 (string-to-number (match-string 3 s)))
  9469. (t2 (and (match-end 4) (match-string 5 s)))
  9470. (h2 (and t2 (string-to-number (match-string 6 s))))
  9471. (m2 (and t2 (string-to-number (match-string 7 s))))
  9472. dh dm)
  9473. (if (not t2)
  9474. t1
  9475. (setq dh (- h2 h1) dm (- m2 m1))
  9476. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  9477. (concat t1 "+" (number-to-string dh)
  9478. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  9479. (defun org-time-stamp-inactive (&optional arg)
  9480. "Insert an inactive time stamp.
  9481. An inactive time stamp is enclosed in square brackets instead of angle
  9482. brackets. It is inactive in the sense that it does not trigger agenda entries,
  9483. does not link to the calendar and cannot be changed with the S-cursor keys.
  9484. So these are more for recording a certain time/date."
  9485. (interactive "P")
  9486. (org-time-stamp arg 'inactive))
  9487. (defvar org-date-ovl (org-make-overlay 1 1))
  9488. (org-overlay-put org-date-ovl 'face 'org-warning)
  9489. (org-detach-overlay org-date-ovl)
  9490. (defvar org-ans1) ; dynamically scoped parameter
  9491. (defvar org-ans2) ; dynamically scoped parameter
  9492. (defvar org-plain-time-of-day-regexp) ; defined below
  9493. (defvar org-overriding-default-time nil) ; dynamically scoped
  9494. (defvar org-read-date-overlay nil)
  9495. (defvar org-dcst nil) ; dynamically scoped
  9496. (defun org-read-date (&optional with-time to-time from-string prompt
  9497. default-time default-input)
  9498. "Read a date, possibly a time, and make things smooth for the user.
  9499. The prompt will suggest to enter an ISO date, but you can also enter anything
  9500. which will at least partially be understood by `parse-time-string'.
  9501. Unrecognized parts of the date will default to the current day, month, year,
  9502. hour and minute. If this command is called to replace a timestamp at point,
  9503. of to enter the second timestamp of a range, the default time is taken from the
  9504. existing stamp. For example,
  9505. 3-2-5 --> 2003-02-05
  9506. feb 15 --> currentyear-02-15
  9507. sep 12 9 --> 2009-09-12
  9508. 12:45 --> today 12:45
  9509. 22 sept 0:34 --> currentyear-09-22 0:34
  9510. 12 --> currentyear-currentmonth-12
  9511. Fri --> nearest Friday (today or later)
  9512. etc.
  9513. Furthermore you can specify a relative date by giving, as the *first* thing
  9514. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  9515. change in days weeks, months, years.
  9516. With a single plus or minus, the date is relative to today. With a double
  9517. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  9518. +4d --> four days from today
  9519. +4 --> same as above
  9520. +2w --> two weeks from today
  9521. ++5 --> five days from default date
  9522. The function understands only English month and weekday abbreviations,
  9523. but this can be configured with the variables `parse-time-months' and
  9524. `parse-time-weekdays'.
  9525. While prompting, a calendar is popped up - you can also select the
  9526. date with the mouse (button 1). The calendar shows a period of three
  9527. months. To scroll it to other months, use the keys `>' and `<'.
  9528. If you don't like the calendar, turn it off with
  9529. \(setq org-read-date-popup-calendar nil)
  9530. With optional argument TO-TIME, the date will immediately be converted
  9531. to an internal time.
  9532. With an optional argument WITH-TIME, the prompt will suggest to also
  9533. insert a time. Note that when WITH-TIME is not set, you can still
  9534. enter a time, and this function will inform the calling routine about
  9535. this change. The calling routine may then choose to change the format
  9536. used to insert the time stamp into the buffer to include the time.
  9537. With optional argument FROM-STRING, read from this string instead from
  9538. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  9539. the time/date that is used for everything that is not specified by the
  9540. user."
  9541. (require 'parse-time)
  9542. (let* ((org-time-stamp-rounding-minutes
  9543. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  9544. (org-dcst org-display-custom-times)
  9545. (ct (org-current-time))
  9546. (def (or org-overriding-default-time default-time ct))
  9547. (defdecode (decode-time def))
  9548. (dummy (progn
  9549. (when (< (nth 2 defdecode) org-extend-today-until)
  9550. (setcar (nthcdr 2 defdecode) -1)
  9551. (setcar (nthcdr 1 defdecode) 59)
  9552. (setq def (apply 'encode-time defdecode)
  9553. defdecode (decode-time def)))))
  9554. (calendar-move-hook nil)
  9555. (calendar-view-diary-initially-flag nil)
  9556. (view-diary-entries-initially nil)
  9557. (calendar-view-holidays-initially-flag nil)
  9558. (view-calendar-holidays-initially nil)
  9559. (timestr (format-time-string
  9560. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  9561. (prompt (concat (if prompt (concat prompt " ") "")
  9562. (format "Date+time [%s]: " timestr)))
  9563. ans (org-ans0 "") org-ans1 org-ans2 final)
  9564. (cond
  9565. (from-string (setq ans from-string))
  9566. (org-read-date-popup-calendar
  9567. (save-excursion
  9568. (save-window-excursion
  9569. (calendar)
  9570. (calendar-forward-day (- (time-to-days def)
  9571. (calendar-absolute-from-gregorian
  9572. (calendar-current-date))))
  9573. (org-eval-in-calendar nil t)
  9574. (let* ((old-map (current-local-map))
  9575. (map (copy-keymap calendar-mode-map))
  9576. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  9577. (org-defkey map (kbd "RET") 'org-calendar-select)
  9578. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  9579. 'org-calendar-select-mouse)
  9580. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  9581. 'org-calendar-select-mouse)
  9582. (org-defkey minibuffer-local-map [(meta shift left)]
  9583. (lambda () (interactive)
  9584. (org-eval-in-calendar '(calendar-backward-month 1))))
  9585. (org-defkey minibuffer-local-map [(meta shift right)]
  9586. (lambda () (interactive)
  9587. (org-eval-in-calendar '(calendar-forward-month 1))))
  9588. (org-defkey minibuffer-local-map [(meta shift up)]
  9589. (lambda () (interactive)
  9590. (org-eval-in-calendar '(calendar-backward-year 1))))
  9591. (org-defkey minibuffer-local-map [(meta shift down)]
  9592. (lambda () (interactive)
  9593. (org-eval-in-calendar '(calendar-forward-year 1))))
  9594. (org-defkey minibuffer-local-map [(shift up)]
  9595. (lambda () (interactive)
  9596. (org-eval-in-calendar '(calendar-backward-week 1))))
  9597. (org-defkey minibuffer-local-map [(shift down)]
  9598. (lambda () (interactive)
  9599. (org-eval-in-calendar '(calendar-forward-week 1))))
  9600. (org-defkey minibuffer-local-map [(shift left)]
  9601. (lambda () (interactive)
  9602. (org-eval-in-calendar '(calendar-backward-day 1))))
  9603. (org-defkey minibuffer-local-map [(shift right)]
  9604. (lambda () (interactive)
  9605. (org-eval-in-calendar '(calendar-forward-day 1))))
  9606. (org-defkey minibuffer-local-map ">"
  9607. (lambda () (interactive)
  9608. (org-eval-in-calendar '(scroll-calendar-left 1))))
  9609. (org-defkey minibuffer-local-map "<"
  9610. (lambda () (interactive)
  9611. (org-eval-in-calendar '(scroll-calendar-right 1))))
  9612. (unwind-protect
  9613. (progn
  9614. (use-local-map map)
  9615. (add-hook 'post-command-hook 'org-read-date-display)
  9616. (setq org-ans0 (read-string prompt default-input nil nil))
  9617. ;; org-ans0: from prompt
  9618. ;; org-ans1: from mouse click
  9619. ;; org-ans2: from calendar motion
  9620. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  9621. (remove-hook 'post-command-hook 'org-read-date-display)
  9622. (use-local-map old-map)
  9623. (when org-read-date-overlay
  9624. (org-delete-overlay org-read-date-overlay)
  9625. (setq org-read-date-overlay nil)))))))
  9626. (t ; Naked prompt only
  9627. (unwind-protect
  9628. (setq ans (read-string prompt default-input nil timestr))
  9629. (when org-read-date-overlay
  9630. (org-delete-overlay org-read-date-overlay)
  9631. (setq org-read-date-overlay nil)))))
  9632. (setq final (org-read-date-analyze ans def defdecode))
  9633. (if to-time
  9634. (apply 'encode-time final)
  9635. (if (and (boundp 'org-time-was-given) org-time-was-given)
  9636. (format "%04d-%02d-%02d %02d:%02d"
  9637. (nth 5 final) (nth 4 final) (nth 3 final)
  9638. (nth 2 final) (nth 1 final))
  9639. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  9640. (defvar def)
  9641. (defvar defdecode)
  9642. (defvar with-time)
  9643. (defun org-read-date-display ()
  9644. "Display the currrent date prompt interpretation in the minibuffer."
  9645. (when org-read-date-display-live
  9646. (when org-read-date-overlay
  9647. (org-delete-overlay org-read-date-overlay))
  9648. (let ((p (point)))
  9649. (end-of-line 1)
  9650. (while (not (equal (buffer-substring
  9651. (max (point-min) (- (point) 4)) (point))
  9652. " "))
  9653. (insert " "))
  9654. (goto-char p))
  9655. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  9656. " " (or org-ans1 org-ans2)))
  9657. (org-end-time-was-given nil)
  9658. (f (org-read-date-analyze ans def defdecode))
  9659. (fmts (if org-dcst
  9660. org-time-stamp-custom-formats
  9661. org-time-stamp-formats))
  9662. (fmt (if (or with-time
  9663. (and (boundp 'org-time-was-given) org-time-was-given))
  9664. (cdr fmts)
  9665. (car fmts)))
  9666. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  9667. (when (and org-end-time-was-given
  9668. (string-match org-plain-time-of-day-regexp txt))
  9669. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  9670. org-end-time-was-given
  9671. (substring txt (match-end 0)))))
  9672. (setq org-read-date-overlay
  9673. (org-make-overlay (1- (point-at-eol)) (point-at-eol)))
  9674. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  9675. (defun org-read-date-analyze (ans def defdecode)
  9676. "Analyze the combined answer of the date prompt."
  9677. ;; FIXME: cleanup and comment
  9678. (let (delta deltan deltaw deltadef year month day
  9679. hour minute second wday pm h2 m2 tl wday1
  9680. iso-year iso-weekday iso-week iso-year iso-date)
  9681. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  9682. (setq ans "+0"))
  9683. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  9684. (setq ans (replace-match "" t t ans)
  9685. deltan (car delta)
  9686. deltaw (nth 1 delta)
  9687. deltadef (nth 2 delta)))
  9688. ;; Check if there is an iso week date in there
  9689. ;; If yes, sore the info and ostpone interpreting it until the rest
  9690. ;; of the parsing is done
  9691. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  9692. (setq iso-year (if (match-end 1) (org-small-year-to-year (string-to-number (match-string 1 ans))))
  9693. iso-weekday (if (match-end 3) (string-to-number (match-string 3 ans)))
  9694. iso-week (string-to-number (match-string 2 ans)))
  9695. (setq ans (replace-match "" t t ans)))
  9696. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  9697. (when (string-match
  9698. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  9699. (setq year (if (match-end 2)
  9700. (string-to-number (match-string 2 ans))
  9701. (string-to-number (format-time-string "%Y")))
  9702. month (string-to-number (match-string 3 ans))
  9703. day (string-to-number (match-string 4 ans)))
  9704. (if (< year 100) (setq year (+ 2000 year)))
  9705. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  9706. t nil ans)))
  9707. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  9708. ;; If there is a time with am/pm, and *no* time without it, we convert
  9709. ;; so that matching will be successful.
  9710. (loop for i from 1 to 2 do ; twice, for end time as well
  9711. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  9712. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  9713. (setq hour (string-to-number (match-string 1 ans))
  9714. minute (if (match-end 3)
  9715. (string-to-number (match-string 3 ans))
  9716. 0)
  9717. pm (equal ?p
  9718. (string-to-char (downcase (match-string 4 ans)))))
  9719. (if (and (= hour 12) (not pm))
  9720. (setq hour 0)
  9721. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  9722. (setq ans (replace-match (format "%02d:%02d" hour minute)
  9723. t t ans))))
  9724. ;; Check if a time range is given as a duration
  9725. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  9726. (setq hour (string-to-number (match-string 1 ans))
  9727. h2 (+ hour (string-to-number (match-string 3 ans)))
  9728. minute (string-to-number (match-string 2 ans))
  9729. m2 (+ minute (if (match-end 5) (string-to-number
  9730. (match-string 5 ans))0)))
  9731. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  9732. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  9733. t t ans)))
  9734. ;; Check if there is a time range
  9735. (when (boundp 'org-end-time-was-given)
  9736. (setq org-time-was-given nil)
  9737. (when (and (string-match org-plain-time-of-day-regexp ans)
  9738. (match-end 8))
  9739. (setq org-end-time-was-given (match-string 8 ans))
  9740. (setq ans (concat (substring ans 0 (match-beginning 7))
  9741. (substring ans (match-end 7))))))
  9742. (setq tl (parse-time-string ans)
  9743. day (or (nth 3 tl) (nth 3 defdecode))
  9744. month (or (nth 4 tl)
  9745. (if (and org-read-date-prefer-future
  9746. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  9747. (1+ (nth 4 defdecode))
  9748. (nth 4 defdecode)))
  9749. year (or (nth 5 tl)
  9750. (if (and org-read-date-prefer-future
  9751. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  9752. (1+ (nth 5 defdecode))
  9753. (nth 5 defdecode)))
  9754. hour (or (nth 2 tl) (nth 2 defdecode))
  9755. minute (or (nth 1 tl) (nth 1 defdecode))
  9756. second (or (nth 0 tl) 0)
  9757. wday (nth 6 tl))
  9758. ;; Special date definitions below
  9759. (cond
  9760. (iso-week
  9761. ;; There was an iso week
  9762. (setq year (or iso-year year)
  9763. day (or iso-weekday wday 1)
  9764. wday nil ; to make sure that the trigger below does not match
  9765. iso-date (calendar-gregorian-from-absolute
  9766. (calendar-absolute-from-iso
  9767. (list iso-week day year))))
  9768. ; FIXME: Should we also push ISO weeks into the future?
  9769. ; (when (and org-read-date-prefer-future
  9770. ; (not iso-year)
  9771. ; (< (calendar-absolute-from-gregorian iso-date)
  9772. ; (time-to-days (current-time))))
  9773. ; (setq year (1+ year)
  9774. ; iso-date (calendar-gregorian-from-absolute
  9775. ; (calendar-absolute-from-iso
  9776. ; (list iso-week day year)))))
  9777. (setq month (car iso-date)
  9778. year (nth 2 iso-date)
  9779. day (nth 1 iso-date)))
  9780. (deltan
  9781. (unless deltadef
  9782. (let ((now (decode-time (current-time))))
  9783. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  9784. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  9785. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  9786. ((equal deltaw "m") (setq month (+ month deltan)))
  9787. ((equal deltaw "y") (setq year (+ year deltan)))))
  9788. ((and wday (not (nth 3 tl)))
  9789. ;; Weekday was given, but no day, so pick that day in the week
  9790. ;; on or after the derived date.
  9791. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  9792. (unless (equal wday wday1)
  9793. (setq day (+ day (% (- wday wday1 -7) 7))))))
  9794. (if (and (boundp 'org-time-was-given)
  9795. (nth 2 tl))
  9796. (setq org-time-was-given t))
  9797. (if (< year 100) (setq year (+ 2000 year)))
  9798. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  9799. (list second minute hour day month year)))
  9800. (defvar parse-time-weekdays)
  9801. (defun org-read-date-get-relative (s today default)
  9802. "Check string S for special relative date string.
  9803. TODAY and DEFAULT are internal times, for today and for a default.
  9804. Return shift list (N what def-flag)
  9805. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  9806. N is the number of WHATs to shift.
  9807. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  9808. the DEFAULT date rather than TODAY."
  9809. (when (and
  9810. (string-match
  9811. (concat
  9812. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  9813. "\\([0-9]+\\)?"
  9814. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  9815. "\\([ \t]\\|$\\)") s)
  9816. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  9817. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  9818. (string-to-char (substring (match-string 1 s) -1))
  9819. ?+))
  9820. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  9821. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  9822. (what (if (match-end 3) (match-string 3 s) "d"))
  9823. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  9824. (date (if rel default today))
  9825. (wday (nth 6 (decode-time date)))
  9826. delta)
  9827. (if wday1
  9828. (progn
  9829. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  9830. (if (= dir ?-) (setq delta (- delta 7)))
  9831. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  9832. (list delta "d" rel))
  9833. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  9834. (defun org-eval-in-calendar (form &optional keepdate)
  9835. "Eval FORM in the calendar window and return to current window.
  9836. Also, store the cursor date in variable org-ans2."
  9837. (let ((sw (selected-window)))
  9838. (select-window (get-buffer-window "*Calendar*"))
  9839. (eval form)
  9840. (when (and (not keepdate) (calendar-cursor-to-date))
  9841. (let* ((date (calendar-cursor-to-date))
  9842. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  9843. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  9844. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  9845. (select-window sw)))
  9846. (defun org-calendar-select ()
  9847. "Return to `org-read-date' with the date currently selected.
  9848. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  9849. (interactive)
  9850. (when (calendar-cursor-to-date)
  9851. (let* ((date (calendar-cursor-to-date))
  9852. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  9853. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  9854. (if (active-minibuffer-window) (exit-minibuffer))))
  9855. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  9856. "Insert a date stamp for the date given by the internal TIME.
  9857. WITH-HM means, use the stamp format that includes the time of the day.
  9858. INACTIVE means use square brackets instead of angular ones, so that the
  9859. stamp will not contribute to the agenda.
  9860. PRE and POST are optional strings to be inserted before and after the
  9861. stamp.
  9862. The command returns the inserted time stamp."
  9863. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  9864. stamp)
  9865. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  9866. (insert-before-markers (or pre ""))
  9867. (insert-before-markers (setq stamp (format-time-string fmt time)))
  9868. (when (listp extra)
  9869. (setq extra (car extra))
  9870. (if (and (stringp extra)
  9871. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  9872. (setq extra (format "-%02d:%02d"
  9873. (string-to-number (match-string 1 extra))
  9874. (string-to-number (match-string 2 extra))))
  9875. (setq extra nil)))
  9876. (when extra
  9877. (backward-char 1)
  9878. (insert-before-markers extra)
  9879. (forward-char 1))
  9880. (insert-before-markers (or post ""))
  9881. (setq org-last-inserted-timestamp stamp)))
  9882. (defun org-toggle-time-stamp-overlays ()
  9883. "Toggle the use of custom time stamp formats."
  9884. (interactive)
  9885. (setq org-display-custom-times (not org-display-custom-times))
  9886. (unless org-display-custom-times
  9887. (let ((p (point-min)) (bmp (buffer-modified-p)))
  9888. (while (setq p (next-single-property-change p 'display))
  9889. (if (and (get-text-property p 'display)
  9890. (eq (get-text-property p 'face) 'org-date))
  9891. (remove-text-properties
  9892. p (setq p (next-single-property-change p 'display))
  9893. '(display t))))
  9894. (set-buffer-modified-p bmp)))
  9895. (if (featurep 'xemacs)
  9896. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  9897. (org-restart-font-lock)
  9898. (setq org-table-may-need-update t)
  9899. (if org-display-custom-times
  9900. (message "Time stamps are overlayed with custom format")
  9901. (message "Time stamp overlays removed")))
  9902. (defun org-display-custom-time (beg end)
  9903. "Overlay modified time stamp format over timestamp between BEG and END."
  9904. (let* ((ts (buffer-substring beg end))
  9905. t1 w1 with-hm tf time str w2 (off 0))
  9906. (save-match-data
  9907. (setq t1 (org-parse-time-string ts t))
  9908. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\)?\\'" ts)
  9909. (setq off (- (match-end 0) (match-beginning 0)))))
  9910. (setq end (- end off))
  9911. (setq w1 (- end beg)
  9912. with-hm (and (nth 1 t1) (nth 2 t1))
  9913. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  9914. time (org-fix-decoded-time t1)
  9915. str (org-add-props
  9916. (format-time-string
  9917. (substring tf 1 -1) (apply 'encode-time time))
  9918. nil 'mouse-face 'highlight)
  9919. w2 (length str))
  9920. (if (not (= w2 w1))
  9921. (add-text-properties (1+ beg) (+ 2 beg)
  9922. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  9923. (if (featurep 'xemacs)
  9924. (progn
  9925. (put-text-property beg end 'invisible t)
  9926. (put-text-property beg end 'end-glyph (make-glyph str)))
  9927. (put-text-property beg end 'display str))))
  9928. (defun org-translate-time (string)
  9929. "Translate all timestamps in STRING to custom format.
  9930. But do this only if the variable `org-display-custom-times' is set."
  9931. (when org-display-custom-times
  9932. (save-match-data
  9933. (let* ((start 0)
  9934. (re org-ts-regexp-both)
  9935. t1 with-hm inactive tf time str beg end)
  9936. (while (setq start (string-match re string start))
  9937. (setq beg (match-beginning 0)
  9938. end (match-end 0)
  9939. t1 (save-match-data
  9940. (org-parse-time-string (substring string beg end) t))
  9941. with-hm (and (nth 1 t1) (nth 2 t1))
  9942. inactive (equal (substring string beg (1+ beg)) "[")
  9943. tf (funcall (if with-hm 'cdr 'car)
  9944. org-time-stamp-custom-formats)
  9945. time (org-fix-decoded-time t1)
  9946. str (format-time-string
  9947. (concat
  9948. (if inactive "[" "<") (substring tf 1 -1)
  9949. (if inactive "]" ">"))
  9950. (apply 'encode-time time))
  9951. string (replace-match str t t string)
  9952. start (+ start (length str)))))))
  9953. string)
  9954. (defun org-fix-decoded-time (time)
  9955. "Set 0 instead of nil for the first 6 elements of time.
  9956. Don't touch the rest."
  9957. (let ((n 0))
  9958. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  9959. (defun org-days-to-time (timestamp-string)
  9960. "Difference between TIMESTAMP-STRING and now in days."
  9961. (- (time-to-days (org-time-string-to-time timestamp-string))
  9962. (time-to-days (current-time))))
  9963. (defun org-deadline-close (timestamp-string &optional ndays)
  9964. "Is the time in TIMESTAMP-STRING close to the current date?"
  9965. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  9966. (and (< (org-days-to-time timestamp-string) ndays)
  9967. (not (org-entry-is-done-p))))
  9968. (defun org-get-wdays (ts)
  9969. "Get the deadline lead time appropriate for timestring TS."
  9970. (cond
  9971. ((<= org-deadline-warning-days 0)
  9972. ;; 0 or negative, enforce this value no matter what
  9973. (- org-deadline-warning-days))
  9974. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\)" ts)
  9975. ;; lead time is specified.
  9976. (floor (* (string-to-number (match-string 1 ts))
  9977. (cdr (assoc (match-string 2 ts)
  9978. '(("d" . 1) ("w" . 7)
  9979. ("m" . 30.4) ("y" . 365.25)))))))
  9980. ;; go for the default.
  9981. (t org-deadline-warning-days)))
  9982. (defun org-calendar-select-mouse (ev)
  9983. "Return to `org-read-date' with the date currently selected.
  9984. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  9985. (interactive "e")
  9986. (mouse-set-point ev)
  9987. (when (calendar-cursor-to-date)
  9988. (let* ((date (calendar-cursor-to-date))
  9989. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  9990. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  9991. (if (active-minibuffer-window) (exit-minibuffer))))
  9992. (defun org-check-deadlines (ndays)
  9993. "Check if there are any deadlines due or past due.
  9994. A deadline is considered due if it happens within `org-deadline-warning-days'
  9995. days from today's date. If the deadline appears in an entry marked DONE,
  9996. it is not shown. The prefix arg NDAYS can be used to test that many
  9997. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  9998. (interactive "P")
  9999. (let* ((org-warn-days
  10000. (cond
  10001. ((equal ndays '(4)) 100000)
  10002. (ndays (prefix-numeric-value ndays))
  10003. (t (abs org-deadline-warning-days))))
  10004. (case-fold-search nil)
  10005. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  10006. (callback
  10007. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  10008. (message "%d deadlines past-due or due within %d days"
  10009. (org-occur regexp nil callback)
  10010. org-warn-days)))
  10011. (defun org-check-before-date (date)
  10012. "Check if there are deadlines or scheduled entries before DATE."
  10013. (interactive (list (org-read-date)))
  10014. (let ((case-fold-search nil)
  10015. (regexp (concat "\\<\\(" org-deadline-string
  10016. "\\|" org-scheduled-string
  10017. "\\) *<\\([^>]+\\)>"))
  10018. (callback
  10019. (lambda () (time-less-p
  10020. (org-time-string-to-time (match-string 2))
  10021. (org-time-string-to-time date)))))
  10022. (message "%d entries before %s"
  10023. (org-occur regexp nil callback) date)))
  10024. (defun org-evaluate-time-range (&optional to-buffer)
  10025. "Evaluate a time range by computing the difference between start and end.
  10026. Normally the result is just printed in the echo area, but with prefix arg
  10027. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  10028. If the time range is actually in a table, the result is inserted into the
  10029. next column.
  10030. For time difference computation, a year is assumed to be exactly 365
  10031. days in order to avoid rounding problems."
  10032. (interactive "P")
  10033. (or
  10034. (org-clock-update-time-maybe)
  10035. (save-excursion
  10036. (unless (org-at-date-range-p t)
  10037. (goto-char (point-at-bol))
  10038. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  10039. (if (not (org-at-date-range-p t))
  10040. (error "Not at a time-stamp range, and none found in current line")))
  10041. (let* ((ts1 (match-string 1))
  10042. (ts2 (match-string 2))
  10043. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  10044. (match-end (match-end 0))
  10045. (time1 (org-time-string-to-time ts1))
  10046. (time2 (org-time-string-to-time ts2))
  10047. (t1 (time-to-seconds time1))
  10048. (t2 (time-to-seconds time2))
  10049. (diff (abs (- t2 t1)))
  10050. (negative (< (- t2 t1) 0))
  10051. ;; (ys (floor (* 365 24 60 60)))
  10052. (ds (* 24 60 60))
  10053. (hs (* 60 60))
  10054. (fy "%dy %dd %02d:%02d")
  10055. (fy1 "%dy %dd")
  10056. (fd "%dd %02d:%02d")
  10057. (fd1 "%dd")
  10058. (fh "%02d:%02d")
  10059. y d h m align)
  10060. (if havetime
  10061. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  10062. y 0
  10063. d (floor (/ diff ds)) diff (mod diff ds)
  10064. h (floor (/ diff hs)) diff (mod diff hs)
  10065. m (floor (/ diff 60)))
  10066. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  10067. y 0
  10068. d (floor (+ (/ diff ds) 0.5))
  10069. h 0 m 0))
  10070. (if (not to-buffer)
  10071. (message "%s" (org-make-tdiff-string y d h m))
  10072. (if (org-at-table-p)
  10073. (progn
  10074. (goto-char match-end)
  10075. (setq align t)
  10076. (and (looking-at " *|") (goto-char (match-end 0))))
  10077. (goto-char match-end))
  10078. (if (looking-at
  10079. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  10080. (replace-match ""))
  10081. (if negative (insert " -"))
  10082. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  10083. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  10084. (insert " " (format fh h m))))
  10085. (if align (org-table-align))
  10086. (message "Time difference inserted")))))
  10087. (defun org-make-tdiff-string (y d h m)
  10088. (let ((fmt "")
  10089. (l nil))
  10090. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  10091. l (push y l)))
  10092. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  10093. l (push d l)))
  10094. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  10095. l (push h l)))
  10096. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  10097. l (push m l)))
  10098. (apply 'format fmt (nreverse l))))
  10099. (defun org-time-string-to-time (s)
  10100. (apply 'encode-time (org-parse-time-string s)))
  10101. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  10102. "Convert a time stamp to an absolute day number.
  10103. If there is a specifyer for a cyclic time stamp, get the closest date to
  10104. DAYNR.
  10105. PREFER and SHOW_ALL are passed through to `org-closest-date'."
  10106. (cond
  10107. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  10108. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  10109. daynr
  10110. (+ daynr 1000)))
  10111. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  10112. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  10113. (time-to-days (current-time))) (match-string 0 s)
  10114. prefer show-all))
  10115. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  10116. (defun org-days-to-iso-week (days)
  10117. "Return the iso week number."
  10118. (require 'cal-iso)
  10119. (car (calendar-iso-from-absolute days)))
  10120. (defun org-small-year-to-year (year)
  10121. "Convert 2-digit years into 4-digit years.
  10122. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  10123. The year 2000 cannot be abbreviated. Any year lager than 99
  10124. is retrned unchanged."
  10125. (if (< year 38)
  10126. (setq year (+ 2000 year))
  10127. (if (< year 100)
  10128. (setq year (+ 1900 year))))
  10129. year)
  10130. (defun org-time-from-absolute (d)
  10131. "Return the time corresponding to date D.
  10132. D may be an absolute day number, or a calendar-type list (month day year)."
  10133. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  10134. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  10135. (defun org-calendar-holiday ()
  10136. "List of holidays, for Diary display in Org-mode."
  10137. (require 'holidays)
  10138. (let ((hl (funcall
  10139. (if (fboundp 'calendar-check-holidays)
  10140. 'calendar-check-holidays 'check-calendar-holidays) date)))
  10141. (if hl (mapconcat 'identity hl "; "))))
  10142. (defun org-diary-sexp-entry (sexp entry date)
  10143. "Process a SEXP diary ENTRY for DATE."
  10144. (require 'diary-lib)
  10145. (let ((result (if calendar-debug-sexp
  10146. (let ((stack-trace-on-error t))
  10147. (eval (car (read-from-string sexp))))
  10148. (condition-case nil
  10149. (eval (car (read-from-string sexp)))
  10150. (error
  10151. (beep)
  10152. (message "Bad sexp at line %d in %s: %s"
  10153. (org-current-line)
  10154. (buffer-file-name) sexp)
  10155. (sleep-for 2))))))
  10156. (cond ((stringp result) result)
  10157. ((and (consp result)
  10158. (stringp (cdr result))) (cdr result))
  10159. (result entry)
  10160. (t nil))))
  10161. (defun org-diary-to-ical-string (frombuf)
  10162. "Get iCalendar entries from diary entries in buffer FROMBUF.
  10163. This uses the icalendar.el library."
  10164. (let* ((tmpdir (if (featurep 'xemacs)
  10165. (temp-directory)
  10166. temporary-file-directory))
  10167. (tmpfile (make-temp-name
  10168. (expand-file-name "orgics" tmpdir)))
  10169. buf rtn b e)
  10170. (save-excursion
  10171. (set-buffer frombuf)
  10172. (icalendar-export-region (point-min) (point-max) tmpfile)
  10173. (setq buf (find-buffer-visiting tmpfile))
  10174. (set-buffer buf)
  10175. (goto-char (point-min))
  10176. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  10177. (setq b (match-beginning 0)))
  10178. (goto-char (point-max))
  10179. (if (re-search-backward "^END:VEVENT" nil t)
  10180. (setq e (match-end 0)))
  10181. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  10182. (kill-buffer buf)
  10183. (delete-file tmpfile)
  10184. rtn))
  10185. (defun org-closest-date (start current change prefer show-all)
  10186. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  10187. When PREFER is `past' return a date that is either CURRENT or past.
  10188. When PREFER is `future', return a date that is either CURRENT or future.
  10189. When SHOW-ALL is nil, only return the current occurence of a time stamp."
  10190. ;; Make the proper lists from the dates
  10191. (catch 'exit
  10192. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  10193. dn dw sday cday n1 n2
  10194. d m y y1 y2 date1 date2 nmonths nm ny m2)
  10195. (setq start (org-date-to-gregorian start)
  10196. current (org-date-to-gregorian
  10197. (if show-all
  10198. current
  10199. (time-to-days (current-time))))
  10200. sday (calendar-absolute-from-gregorian start)
  10201. cday (calendar-absolute-from-gregorian current))
  10202. (if (<= cday sday) (throw 'exit sday))
  10203. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  10204. (setq dn (string-to-number (match-string 1 change))
  10205. dw (cdr (assoc (match-string 2 change) a1)))
  10206. (error "Invalid change specifyer: %s" change))
  10207. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  10208. (cond
  10209. ((eq dw 'day)
  10210. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  10211. n2 (+ n1 dn)))
  10212. ((eq dw 'year)
  10213. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  10214. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  10215. (setq date1 (list m d y1)
  10216. n1 (calendar-absolute-from-gregorian date1)
  10217. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  10218. n2 (calendar-absolute-from-gregorian date2)))
  10219. ((eq dw 'month)
  10220. ;; approx number of month between the two dates
  10221. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  10222. ;; How often does dn fit in there?
  10223. (setq d (nth 1 start) m (car start) y (nth 2 start)
  10224. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  10225. m (+ m nm)
  10226. ny (floor (/ m 12))
  10227. y (+ y ny)
  10228. m (- m (* ny 12)))
  10229. (while (> m 12) (setq m (- m 12) y (1+ y)))
  10230. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  10231. (setq m2 (+ m dn) y2 y)
  10232. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  10233. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  10234. (while (<= n2 cday)
  10235. (setq n1 n2 m m2 y y2)
  10236. (setq m2 (+ m dn) y2 y)
  10237. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  10238. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  10239. (if show-all
  10240. (cond
  10241. ((eq prefer 'past) n1)
  10242. ((eq prefer 'future) (if (= cday n1) n1 n2))
  10243. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  10244. (cond
  10245. ((eq prefer 'past) n1)
  10246. ((eq prefer 'future) (if (= cday n1) n1 n2))
  10247. (t (if (= cday n1) n1 n2)))))))
  10248. (defun org-date-to-gregorian (date)
  10249. "Turn any specification of DATE into a gregorian date for the calendar."
  10250. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  10251. ((and (listp date) (= (length date) 3)) date)
  10252. ((stringp date)
  10253. (setq date (org-parse-time-string date))
  10254. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  10255. ((listp date)
  10256. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  10257. (defun org-parse-time-string (s &optional nodefault)
  10258. "Parse the standard Org-mode time string.
  10259. This should be a lot faster than the normal `parse-time-string'.
  10260. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  10261. hour and minute fields will be nil if not given."
  10262. (if (string-match org-ts-regexp0 s)
  10263. (list 0
  10264. (if (or (match-beginning 8) (not nodefault))
  10265. (string-to-number (or (match-string 8 s) "0")))
  10266. (if (or (match-beginning 7) (not nodefault))
  10267. (string-to-number (or (match-string 7 s) "0")))
  10268. (string-to-number (match-string 4 s))
  10269. (string-to-number (match-string 3 s))
  10270. (string-to-number (match-string 2 s))
  10271. nil nil nil)
  10272. (make-list 9 0)))
  10273. (defun org-timestamp-up (&optional arg)
  10274. "Increase the date item at the cursor by one.
  10275. If the cursor is on the year, change the year. If it is on the month or
  10276. the day, change that.
  10277. With prefix ARG, change by that many units."
  10278. (interactive "p")
  10279. (org-timestamp-change (prefix-numeric-value arg)))
  10280. (defun org-timestamp-down (&optional arg)
  10281. "Decrease the date item at the cursor by one.
  10282. If the cursor is on the year, change the year. If it is on the month or
  10283. the day, change that.
  10284. With prefix ARG, change by that many units."
  10285. (interactive "p")
  10286. (org-timestamp-change (- (prefix-numeric-value arg))))
  10287. (defun org-timestamp-up-day (&optional arg)
  10288. "Increase the date in the time stamp by one day.
  10289. With prefix ARG, change that many days."
  10290. (interactive "p")
  10291. (if (and (not (org-at-timestamp-p t))
  10292. (org-on-heading-p))
  10293. (org-todo 'up)
  10294. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  10295. (defun org-timestamp-down-day (&optional arg)
  10296. "Decrease the date in the time stamp by one day.
  10297. With prefix ARG, change that many days."
  10298. (interactive "p")
  10299. (if (and (not (org-at-timestamp-p t))
  10300. (org-on-heading-p))
  10301. (org-todo 'down)
  10302. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  10303. (defun org-at-timestamp-p (&optional inactive-ok)
  10304. "Determine if the cursor is in or at a timestamp."
  10305. (interactive)
  10306. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  10307. (pos (point))
  10308. (ans (or (looking-at tsr)
  10309. (save-excursion
  10310. (skip-chars-backward "^[<\n\r\t")
  10311. (if (> (point) (point-min)) (backward-char 1))
  10312. (and (looking-at tsr)
  10313. (> (- (match-end 0) pos) -1))))))
  10314. (and ans
  10315. (boundp 'org-ts-what)
  10316. (setq org-ts-what
  10317. (cond
  10318. ((= pos (match-beginning 0)) 'bracket)
  10319. ((= pos (1- (match-end 0))) 'bracket)
  10320. ((org-pos-in-match-range pos 2) 'year)
  10321. ((org-pos-in-match-range pos 3) 'month)
  10322. ((org-pos-in-match-range pos 7) 'hour)
  10323. ((org-pos-in-match-range pos 8) 'minute)
  10324. ((or (org-pos-in-match-range pos 4)
  10325. (org-pos-in-match-range pos 5)) 'day)
  10326. ((and (> pos (or (match-end 8) (match-end 5)))
  10327. (< pos (match-end 0)))
  10328. (- pos (or (match-end 8) (match-end 5))))
  10329. (t 'day))))
  10330. ans))
  10331. (defun org-toggle-timestamp-type ()
  10332. "Toggle the type (<active> or [inactive]) of a time stamp."
  10333. (interactive)
  10334. (when (org-at-timestamp-p t)
  10335. (save-excursion
  10336. (goto-char (match-beginning 0))
  10337. (insert (if (equal (char-after) ?<) "[" "<")) (delete-char 1)
  10338. (goto-char (1- (match-end 0)))
  10339. (insert (if (equal (char-after) ?>) "]" ">")) (delete-char 1))
  10340. (message "Timestamp is now %sactive"
  10341. (if (equal (char-before) ?>) "in" ""))))
  10342. (defun org-timestamp-change (n &optional what)
  10343. "Change the date in the time stamp at point.
  10344. The date will be changed by N times WHAT. WHAT can be `day', `month',
  10345. `year', `minute', `second'. If WHAT is not given, the cursor position
  10346. in the timestamp determines what will be changed."
  10347. (let ((pos (point))
  10348. with-hm inactive
  10349. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  10350. org-ts-what
  10351. extra rem
  10352. ts time time0)
  10353. (if (not (org-at-timestamp-p t))
  10354. (error "Not at a timestamp"))
  10355. (if (and (not what) (eq org-ts-what 'bracket))
  10356. (org-toggle-timestamp-type)
  10357. (if (and (not what) (not (eq org-ts-what 'day))
  10358. org-display-custom-times
  10359. (get-text-property (point) 'display)
  10360. (not (get-text-property (1- (point)) 'display)))
  10361. (setq org-ts-what 'day))
  10362. (setq org-ts-what (or what org-ts-what)
  10363. inactive (= (char-after (match-beginning 0)) ?\[)
  10364. ts (match-string 0))
  10365. (replace-match "")
  10366. (if (string-match
  10367. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\)*\\)[]>]"
  10368. ts)
  10369. (setq extra (match-string 1 ts)))
  10370. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  10371. (setq with-hm t))
  10372. (setq time0 (org-parse-time-string ts))
  10373. (when (and (eq org-ts-what 'minute)
  10374. (eq current-prefix-arg nil))
  10375. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  10376. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  10377. (setcar (cdr time0) (+ (nth 1 time0)
  10378. (if (> n 0) (- rem) (- dm rem))))))
  10379. (setq time
  10380. (encode-time (or (car time0) 0)
  10381. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  10382. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  10383. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  10384. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  10385. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  10386. (nthcdr 6 time0)))
  10387. (when (integerp org-ts-what)
  10388. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  10389. (if (eq what 'calendar)
  10390. (let ((cal-date (org-get-date-from-calendar)))
  10391. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  10392. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  10393. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  10394. (setcar time0 (or (car time0) 0))
  10395. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  10396. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  10397. (setq time (apply 'encode-time time0))))
  10398. (setq org-last-changed-timestamp
  10399. (org-insert-time-stamp time with-hm inactive nil nil extra))
  10400. (org-clock-update-time-maybe)
  10401. (goto-char pos)
  10402. ;; Try to recenter the calendar window, if any
  10403. (if (and org-calendar-follow-timestamp-change
  10404. (get-buffer-window "*Calendar*" t)
  10405. (memq org-ts-what '(day month year)))
  10406. (org-recenter-calendar (time-to-days time))))))
  10407. (defun org-modify-ts-extra (s pos n dm)
  10408. "Change the different parts of the lead-time and repeat fields in timestamp."
  10409. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  10410. ng h m new rem)
  10411. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  10412. (cond
  10413. ((or (org-pos-in-match-range pos 2)
  10414. (org-pos-in-match-range pos 3))
  10415. (setq m (string-to-number (match-string 3 s))
  10416. h (string-to-number (match-string 2 s)))
  10417. (if (org-pos-in-match-range pos 2)
  10418. (setq h (+ h n))
  10419. (setq n (* dm (org-no-warnings (signum n))))
  10420. (when (not (= 0 (setq rem (% m dm))))
  10421. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  10422. (setq m (+ m n)))
  10423. (if (< m 0) (setq m (+ m 60) h (1- h)))
  10424. (if (> m 59) (setq m (- m 60) h (1+ h)))
  10425. (setq h (min 24 (max 0 h)))
  10426. (setq ng 1 new (format "-%02d:%02d" h m)))
  10427. ((org-pos-in-match-range pos 6)
  10428. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  10429. ((org-pos-in-match-range pos 5)
  10430. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  10431. ((org-pos-in-match-range pos 9)
  10432. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  10433. ((org-pos-in-match-range pos 8)
  10434. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  10435. (when ng
  10436. (setq s (concat
  10437. (substring s 0 (match-beginning ng))
  10438. new
  10439. (substring s (match-end ng))))))
  10440. s))
  10441. (defun org-recenter-calendar (date)
  10442. "If the calendar is visible, recenter it to DATE."
  10443. (let* ((win (selected-window))
  10444. (cwin (get-buffer-window "*Calendar*" t))
  10445. (calendar-move-hook nil))
  10446. (when cwin
  10447. (select-window cwin)
  10448. (calendar-goto-date (if (listp date) date
  10449. (calendar-gregorian-from-absolute date)))
  10450. (select-window win))))
  10451. (defun org-goto-calendar (&optional arg)
  10452. "Go to the Emacs calendar at the current date.
  10453. If there is a time stamp in the current line, go to that date.
  10454. A prefix ARG can be used to force the current date."
  10455. (interactive "P")
  10456. (let ((tsr org-ts-regexp) diff
  10457. (calendar-move-hook nil)
  10458. (calendar-view-holidays-initially-flag nil)
  10459. (view-calendar-holidays-initially nil)
  10460. (calendar-view-diary-initially-flag nil)
  10461. (view-diary-entries-initially nil))
  10462. (if (or (org-at-timestamp-p)
  10463. (save-excursion
  10464. (beginning-of-line 1)
  10465. (looking-at (concat ".*" tsr))))
  10466. (let ((d1 (time-to-days (current-time)))
  10467. (d2 (time-to-days
  10468. (org-time-string-to-time (match-string 1)))))
  10469. (setq diff (- d2 d1))))
  10470. (calendar)
  10471. (calendar-goto-today)
  10472. (if (and diff (not arg)) (calendar-forward-day diff))))
  10473. (defun org-get-date-from-calendar ()
  10474. "Return a list (month day year) of date at point in calendar."
  10475. (with-current-buffer "*Calendar*"
  10476. (save-match-data
  10477. (calendar-cursor-to-date))))
  10478. (defun org-date-from-calendar ()
  10479. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  10480. If there is already a time stamp at the cursor position, update it."
  10481. (interactive)
  10482. (if (org-at-timestamp-p t)
  10483. (org-timestamp-change 0 'calendar)
  10484. (let ((cal-date (org-get-date-from-calendar)))
  10485. (org-insert-time-stamp
  10486. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  10487. (defun org-minutes-to-hh:mm-string (m)
  10488. "Compute H:MM from a number of minutes."
  10489. (let ((h (/ m 60)))
  10490. (setq m (- m (* 60 h)))
  10491. (format org-time-clocksum-format h m)))
  10492. (defun org-hh:mm-string-to-minutes (s)
  10493. "Convert a string H:MM to a number of minutes."
  10494. (if (string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  10495. (+ (* (string-to-number (match-string 1 s)) 60)
  10496. (string-to-number (match-string 2 s)))
  10497. 0))
  10498. ;;;; Agenda files
  10499. ;;;###autoload
  10500. (defun org-iswitchb (&optional arg)
  10501. "Use `iswitchb-read-buffer' to prompt for an Org buffer to switch to.
  10502. With a prefix argument, restrict available to files.
  10503. With two prefix arguments, restrict available buffers to agenda files.
  10504. Due to some yet unresolved reason, the global function
  10505. `iswitchb-mode' needs to be active for this function to work."
  10506. (interactive "P")
  10507. (require 'iswitchb)
  10508. (let ((enabled iswitchb-mode) blist)
  10509. (or enabled (iswitchb-mode 1))
  10510. (setq blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  10511. ((equal arg '(16)) (org-buffer-list 'agenda))
  10512. (t (org-buffer-list))))
  10513. (unwind-protect
  10514. (let ((iswitchb-make-buflist-hook
  10515. (lambda ()
  10516. (setq iswitchb-temp-buflist
  10517. (mapcar 'buffer-name blist)))))
  10518. (switch-to-buffer
  10519. (iswitchb-read-buffer
  10520. "Switch-to: " nil t))
  10521. (or enabled (iswitchb-mode -1))))))
  10522. (defun org-buffer-list (&optional predicate exclude-tmp)
  10523. "Return a list of Org buffers.
  10524. PREDICATE can be `export', `files' or `agenda'.
  10525. export restrict the list to Export buffers.
  10526. files restrict the list to buffers visiting Org files.
  10527. agenda restrict the list to buffers visiting agenda files.
  10528. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  10529. (let* ((bfn nil)
  10530. (agenda-files (and (eq predicate 'agenda)
  10531. (mapcar 'file-truename (org-agenda-files t))))
  10532. (filter
  10533. (cond
  10534. ((eq predicate 'files)
  10535. (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
  10536. ((eq predicate 'export)
  10537. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  10538. ((eq predicate 'agenda)
  10539. (lambda (b)
  10540. (with-current-buffer b
  10541. (and (eq major-mode 'org-mode)
  10542. (setq bfn (buffer-file-name b))
  10543. (member (file-truename bfn) agenda-files)))))
  10544. (t (lambda (b) (with-current-buffer b
  10545. (or (eq major-mode 'org-mode)
  10546. (string-match "\*Org .*Export"
  10547. (buffer-name b)))))))))
  10548. (delq nil
  10549. (mapcar
  10550. (lambda(b)
  10551. (if (and (funcall filter b)
  10552. (or (not exclude-tmp)
  10553. (not (string-match "tmp" (buffer-name b)))))
  10554. b
  10555. nil))
  10556. (buffer-list)))))
  10557. (defun org-agenda-files (&optional unrestricted archives)
  10558. "Get the list of agenda files.
  10559. Optional UNRESTRICTED means return the full list even if a restriction
  10560. is currently in place.
  10561. When ARCHIVES is t, include all archive files hat are really being
  10562. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  10563. `org-agenda-archives-mode' is t."
  10564. (let ((files
  10565. (cond
  10566. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  10567. ((stringp org-agenda-files) (org-read-agenda-file-list))
  10568. ((listp org-agenda-files) org-agenda-files)
  10569. (t (error "Invalid value of `org-agenda-files'")))))
  10570. (setq files (apply 'append
  10571. (mapcar (lambda (f)
  10572. (if (file-directory-p f)
  10573. (directory-files
  10574. f t org-agenda-file-regexp)
  10575. (list f)))
  10576. files)))
  10577. (when org-agenda-skip-unavailable-files
  10578. (setq files (delq nil
  10579. (mapcar (function
  10580. (lambda (file)
  10581. (and (file-readable-p file) file)))
  10582. files))))
  10583. (when (or (eq archives t)
  10584. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  10585. (setq files (org-add-archive-files files)))
  10586. files))
  10587. (defun org-edit-agenda-file-list ()
  10588. "Edit the list of agenda files.
  10589. Depending on setup, this either uses customize to edit the variable
  10590. `org-agenda-files', or it visits the file that is holding the list. In the
  10591. latter case, the buffer is set up in a way that saving it automatically kills
  10592. the buffer and restores the previous window configuration."
  10593. (interactive)
  10594. (if (stringp org-agenda-files)
  10595. (let ((cw (current-window-configuration)))
  10596. (find-file org-agenda-files)
  10597. (org-set-local 'org-window-configuration cw)
  10598. (org-add-hook 'after-save-hook
  10599. (lambda ()
  10600. (set-window-configuration
  10601. (prog1 org-window-configuration
  10602. (kill-buffer (current-buffer))))
  10603. (org-install-agenda-files-menu)
  10604. (message "New agenda file list installed"))
  10605. nil 'local)
  10606. (message "%s" (substitute-command-keys
  10607. "Edit list and finish with \\[save-buffer]")))
  10608. (customize-variable 'org-agenda-files)))
  10609. (defun org-store-new-agenda-file-list (list)
  10610. "Set new value for the agenda file list and save it correcly."
  10611. (if (stringp org-agenda-files)
  10612. (let ((f org-agenda-files) b)
  10613. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  10614. (with-temp-file f
  10615. (insert (mapconcat 'identity list "\n") "\n")))
  10616. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  10617. (setq org-agenda-files list)
  10618. (customize-save-variable 'org-agenda-files org-agenda-files))))
  10619. (defun org-read-agenda-file-list ()
  10620. "Read the list of agenda files from a file."
  10621. (when (file-directory-p org-agenda-files)
  10622. (error "`org-agenda-files' cannot be a single directory"))
  10623. (when (stringp org-agenda-files)
  10624. (with-temp-buffer
  10625. (insert-file-contents org-agenda-files)
  10626. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  10627. ;;;###autoload
  10628. (defun org-cycle-agenda-files ()
  10629. "Cycle through the files in `org-agenda-files'.
  10630. If the current buffer visits an agenda file, find the next one in the list.
  10631. If the current buffer does not, find the first agenda file."
  10632. (interactive)
  10633. (let* ((fs (org-agenda-files t))
  10634. (files (append fs (list (car fs))))
  10635. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  10636. file)
  10637. (unless files (error "No agenda files"))
  10638. (catch 'exit
  10639. (while (setq file (pop files))
  10640. (if (equal (file-truename file) tcf)
  10641. (when (car files)
  10642. (find-file (car files))
  10643. (throw 'exit t))))
  10644. (find-file (car fs)))
  10645. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  10646. (defun org-agenda-file-to-front (&optional to-end)
  10647. "Move/add the current file to the top of the agenda file list.
  10648. If the file is not present in the list, it is added to the front. If it is
  10649. present, it is moved there. With optional argument TO-END, add/move to the
  10650. end of the list."
  10651. (interactive "P")
  10652. (let ((org-agenda-skip-unavailable-files nil)
  10653. (file-alist (mapcar (lambda (x)
  10654. (cons (file-truename x) x))
  10655. (org-agenda-files t)))
  10656. (ctf (file-truename buffer-file-name))
  10657. x had)
  10658. (setq x (assoc ctf file-alist) had x)
  10659. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  10660. (if to-end
  10661. (setq file-alist (append (delq x file-alist) (list x)))
  10662. (setq file-alist (cons x (delq x file-alist))))
  10663. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  10664. (org-install-agenda-files-menu)
  10665. (message "File %s to %s of agenda file list"
  10666. (if had "moved" "added") (if to-end "end" "front"))))
  10667. (defun org-remove-file (&optional file)
  10668. "Remove current file from the list of files in variable `org-agenda-files'.
  10669. These are the files which are being checked for agenda entries.
  10670. Optional argument FILE means, use this file instead of the current."
  10671. (interactive)
  10672. (let* ((org-agenda-skip-unavailable-files nil)
  10673. (file (or file buffer-file-name))
  10674. (true-file (file-truename file))
  10675. (afile (abbreviate-file-name file))
  10676. (files (delq nil (mapcar
  10677. (lambda (x)
  10678. (if (equal true-file
  10679. (file-truename x))
  10680. nil x))
  10681. (org-agenda-files t)))))
  10682. (if (not (= (length files) (length (org-agenda-files t))))
  10683. (progn
  10684. (org-store-new-agenda-file-list files)
  10685. (org-install-agenda-files-menu)
  10686. (message "Removed file: %s" afile))
  10687. (message "File was not in list: %s (not removed)" afile))))
  10688. (defun org-file-menu-entry (file)
  10689. (vector file (list 'find-file file) t))
  10690. (defun org-check-agenda-file (file)
  10691. "Make sure FILE exists. If not, ask user what to do."
  10692. (when (not (file-exists-p file))
  10693. (message "non-existent file %s. [R]emove from list or [A]bort?"
  10694. (abbreviate-file-name file))
  10695. (let ((r (downcase (read-char-exclusive))))
  10696. (cond
  10697. ((equal r ?r)
  10698. (org-remove-file file)
  10699. (throw 'nextfile t))
  10700. (t (error "Abort"))))))
  10701. (defun org-get-agenda-file-buffer (file)
  10702. "Get a buffer visiting FILE. If the buffer needs to be created, add
  10703. it to the list of buffers which might be released later."
  10704. (let ((buf (org-find-base-buffer-visiting file)))
  10705. (if buf
  10706. buf ; just return it
  10707. ;; Make a new buffer and remember it
  10708. (setq buf (find-file-noselect file))
  10709. (if buf (push buf org-agenda-new-buffers))
  10710. buf)))
  10711. (defun org-release-buffers (blist)
  10712. "Release all buffers in list, asking the user for confirmation when needed.
  10713. When a buffer is unmodified, it is just killed. When modified, it is saved
  10714. \(if the user agrees) and then killed."
  10715. (let (buf file)
  10716. (while (setq buf (pop blist))
  10717. (setq file (buffer-file-name buf))
  10718. (when (and (buffer-modified-p buf)
  10719. file
  10720. (y-or-n-p (format "Save file %s? " file)))
  10721. (with-current-buffer buf (save-buffer)))
  10722. (kill-buffer buf))))
  10723. (defun org-prepare-agenda-buffers (files)
  10724. "Create buffers for all agenda files, protect archived trees and comments."
  10725. (interactive)
  10726. (let ((pa '(:org-archived t))
  10727. (pc '(:org-comment t))
  10728. (pall '(:org-archived t :org-comment t))
  10729. (inhibit-read-only t)
  10730. (rea (concat ":" org-archive-tag ":"))
  10731. bmp file re)
  10732. (save-excursion
  10733. (save-restriction
  10734. (while (setq file (pop files))
  10735. (if (bufferp file)
  10736. (set-buffer file)
  10737. (org-check-agenda-file file)
  10738. (set-buffer (org-get-agenda-file-buffer file)))
  10739. (widen)
  10740. (setq bmp (buffer-modified-p))
  10741. (org-refresh-category-properties)
  10742. (setq org-todo-keywords-for-agenda
  10743. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  10744. (setq org-done-keywords-for-agenda
  10745. (append org-done-keywords-for-agenda org-done-keywords))
  10746. (setq org-todo-keyword-alist-for-agenda
  10747. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  10748. (setq org-tag-alist-for-agenda
  10749. (append org-tag-alist-for-agenda org-tag-alist))
  10750. (save-excursion
  10751. (remove-text-properties (point-min) (point-max) pall)
  10752. (when org-agenda-skip-archived-trees
  10753. (goto-char (point-min))
  10754. (while (re-search-forward rea nil t)
  10755. (if (org-on-heading-p t)
  10756. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  10757. (goto-char (point-min))
  10758. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  10759. (while (re-search-forward re nil t)
  10760. (add-text-properties
  10761. (match-beginning 0) (org-end-of-subtree t) pc)))
  10762. (set-buffer-modified-p bmp))))
  10763. (setq org-todo-keyword-alist-for-agenda
  10764. (org-uniquify org-todo-keyword-alist-for-agenda)
  10765. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  10766. ;;;; Embedded LaTeX
  10767. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  10768. "Keymap for the minor `org-cdlatex-mode'.")
  10769. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  10770. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  10771. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  10772. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  10773. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  10774. (defvar org-cdlatex-texmathp-advice-is-done nil
  10775. "Flag remembering if we have applied the advice to texmathp already.")
  10776. (define-minor-mode org-cdlatex-mode
  10777. "Toggle the minor `org-cdlatex-mode'.
  10778. This mode supports entering LaTeX environment and math in LaTeX fragments
  10779. in Org-mode.
  10780. \\{org-cdlatex-mode-map}"
  10781. nil " OCDL" nil
  10782. (when org-cdlatex-mode (require 'cdlatex))
  10783. (unless org-cdlatex-texmathp-advice-is-done
  10784. (setq org-cdlatex-texmathp-advice-is-done t)
  10785. (defadvice texmathp (around org-math-always-on activate)
  10786. "Always return t in org-mode buffers.
  10787. This is because we want to insert math symbols without dollars even outside
  10788. the LaTeX math segments. If Orgmode thinks that point is actually inside
  10789. en embedded LaTeX fragement, let texmathp do its job.
  10790. \\[org-cdlatex-mode-map]"
  10791. (interactive)
  10792. (let (p)
  10793. (cond
  10794. ((not (org-mode-p)) ad-do-it)
  10795. ((eq this-command 'cdlatex-math-symbol)
  10796. (setq ad-return-value t
  10797. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  10798. (t
  10799. (let ((p (org-inside-LaTeX-fragment-p)))
  10800. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  10801. (setq ad-return-value t
  10802. texmathp-why '("Org-mode embedded math" . 0))
  10803. (if p ad-do-it)))))))))
  10804. (defun turn-on-org-cdlatex ()
  10805. "Unconditionally turn on `org-cdlatex-mode'."
  10806. (org-cdlatex-mode 1))
  10807. (defun org-inside-LaTeX-fragment-p ()
  10808. "Test if point is inside a LaTeX fragment.
  10809. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  10810. sequence appearing also before point.
  10811. Even though the matchers for math are configurable, this function assumes
  10812. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  10813. delimiters are skipped when they have been removed by customization.
  10814. The return value is nil, or a cons cell with the delimiter and
  10815. and the position of this delimiter.
  10816. This function does a reasonably good job, but can locally be fooled by
  10817. for example currency specifications. For example it will assume being in
  10818. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  10819. fragments that are properly closed, but during editing, we have to live
  10820. with the uncertainty caused by missing closing delimiters. This function
  10821. looks only before point, not after."
  10822. (catch 'exit
  10823. (let ((pos (point))
  10824. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  10825. (lim (progn
  10826. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  10827. (point)))
  10828. dd-on str (start 0) m re)
  10829. (goto-char pos)
  10830. (when dodollar
  10831. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  10832. re (nth 1 (assoc "$" org-latex-regexps)))
  10833. (while (string-match re str start)
  10834. (cond
  10835. ((= (match-end 0) (length str))
  10836. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  10837. ((= (match-end 0) (- (length str) 5))
  10838. (throw 'exit nil))
  10839. (t (setq start (match-end 0))))))
  10840. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  10841. (goto-char pos)
  10842. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  10843. (and (match-beginning 2) (throw 'exit nil))
  10844. ;; count $$
  10845. (while (re-search-backward "\\$\\$" lim t)
  10846. (setq dd-on (not dd-on)))
  10847. (goto-char pos)
  10848. (if dd-on (cons "$$" m))))))
  10849. (defun org-try-cdlatex-tab ()
  10850. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  10851. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  10852. - inside a LaTeX fragment, or
  10853. - after the first word in a line, where an abbreviation expansion could
  10854. insert a LaTeX environment."
  10855. (when org-cdlatex-mode
  10856. (cond
  10857. ((save-excursion
  10858. (skip-chars-backward "a-zA-Z0-9*")
  10859. (skip-chars-backward " \t")
  10860. (bolp))
  10861. (cdlatex-tab) t)
  10862. ((org-inside-LaTeX-fragment-p)
  10863. (cdlatex-tab) t)
  10864. (t nil))))
  10865. (defun org-cdlatex-underscore-caret (&optional arg)
  10866. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  10867. Revert to the normal definition outside of these fragments."
  10868. (interactive "P")
  10869. (if (org-inside-LaTeX-fragment-p)
  10870. (call-interactively 'cdlatex-sub-superscript)
  10871. (let (org-cdlatex-mode)
  10872. (call-interactively (key-binding (vector last-input-event))))))
  10873. (defun org-cdlatex-math-modify (&optional arg)
  10874. "Execute `cdlatex-math-modify' in LaTeX fragments.
  10875. Revert to the normal definition outside of these fragments."
  10876. (interactive "P")
  10877. (if (org-inside-LaTeX-fragment-p)
  10878. (call-interactively 'cdlatex-math-modify)
  10879. (let (org-cdlatex-mode)
  10880. (call-interactively (key-binding (vector last-input-event))))))
  10881. (defvar org-latex-fragment-image-overlays nil
  10882. "List of overlays carrying the images of latex fragments.")
  10883. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  10884. (defun org-remove-latex-fragment-image-overlays ()
  10885. "Remove all overlays with LaTeX fragment images in current buffer."
  10886. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  10887. (setq org-latex-fragment-image-overlays nil))
  10888. (defun org-preview-latex-fragment (&optional subtree)
  10889. "Preview the LaTeX fragment at point, or all locally or globally.
  10890. If the cursor is in a LaTeX fragment, create the image and overlay
  10891. it over the source code. If there is no fragment at point, display
  10892. all fragments in the current text, from one headline to the next. With
  10893. prefix SUBTREE, display all fragments in the current subtree. With a
  10894. double prefix `C-u C-u', or when the cursor is before the first headline,
  10895. display all fragments in the buffer.
  10896. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  10897. (interactive "P")
  10898. (org-remove-latex-fragment-image-overlays)
  10899. (save-excursion
  10900. (save-restriction
  10901. (let (beg end at msg)
  10902. (cond
  10903. ((or (equal subtree '(16))
  10904. (not (save-excursion
  10905. (re-search-backward (concat "^" outline-regexp) nil t))))
  10906. (setq beg (point-min) end (point-max)
  10907. msg "Creating images for buffer...%s"))
  10908. ((equal subtree '(4))
  10909. (org-back-to-heading)
  10910. (setq beg (point) end (org-end-of-subtree t)
  10911. msg "Creating images for subtree...%s"))
  10912. (t
  10913. (if (setq at (org-inside-LaTeX-fragment-p))
  10914. (goto-char (max (point-min) (- (cdr at) 2)))
  10915. (org-back-to-heading))
  10916. (setq beg (point) end (progn (outline-next-heading) (point))
  10917. msg (if at "Creating image...%s"
  10918. "Creating images for entry...%s"))))
  10919. (message msg "")
  10920. (narrow-to-region beg end)
  10921. (goto-char beg)
  10922. (org-format-latex
  10923. (concat "ltxpng/" (file-name-sans-extension
  10924. (file-name-nondirectory
  10925. buffer-file-name)))
  10926. default-directory 'overlays msg at 'forbuffer)
  10927. (message msg "done. Use `C-c C-c' to remove images.")))))
  10928. (defvar org-latex-regexps
  10929. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  10930. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  10931. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  10932. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([ .,?;:'\")\000]\\|$\\)" 2 nil)
  10933. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  10934. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  10935. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  10936. "Regular expressions for matching embedded LaTeX.")
  10937. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  10938. "Replace LaTeX fragments with links to an image, and produce images."
  10939. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  10940. (let* ((prefixnodir (file-name-nondirectory prefix))
  10941. (absprefix (expand-file-name prefix dir))
  10942. (todir (file-name-directory absprefix))
  10943. (opt org-format-latex-options)
  10944. (matchers (plist-get opt :matchers))
  10945. (re-list org-latex-regexps)
  10946. (cnt 0) txt link beg end re e checkdir
  10947. m n block linkfile movefile ov)
  10948. ;; Check if there are old images files with this prefix, and remove them
  10949. (when (file-directory-p todir)
  10950. (mapc 'delete-file
  10951. (directory-files
  10952. todir 'full
  10953. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  10954. ;; Check the different regular expressions
  10955. (while (setq e (pop re-list))
  10956. (setq m (car e) re (nth 1 e) n (nth 2 e)
  10957. block (if (nth 3 e) "\n\n" ""))
  10958. (when (member m matchers)
  10959. (goto-char (point-min))
  10960. (while (re-search-forward re nil t)
  10961. (when (or (not at) (equal (cdr at) (match-beginning n)))
  10962. (setq txt (match-string n)
  10963. beg (match-beginning n) end (match-end n)
  10964. cnt (1+ cnt)
  10965. linkfile (format "%s_%04d.png" prefix cnt)
  10966. movefile (format "%s_%04d.png" absprefix cnt)
  10967. link (concat block "[[file:" linkfile "]]" block))
  10968. (if msg (message msg cnt))
  10969. (goto-char beg)
  10970. (unless checkdir ; make sure the directory exists
  10971. (setq checkdir t)
  10972. (or (file-directory-p todir) (make-directory todir)))
  10973. (org-create-formula-image
  10974. txt movefile opt forbuffer)
  10975. (if overlays
  10976. (progn
  10977. (setq ov (org-make-overlay beg end))
  10978. (if (featurep 'xemacs)
  10979. (progn
  10980. (org-overlay-put ov 'invisible t)
  10981. (org-overlay-put
  10982. ov 'end-glyph
  10983. (make-glyph (vector 'png :file movefile))))
  10984. (org-overlay-put
  10985. ov 'display
  10986. (list 'image :type 'png :file movefile :ascent 'center)))
  10987. (push ov org-latex-fragment-image-overlays)
  10988. (goto-char end))
  10989. (delete-region beg end)
  10990. (insert link))))))))
  10991. ;; This function borrows from Ganesh Swami's latex2png.el
  10992. (defun org-create-formula-image (string tofile options buffer)
  10993. (let* ((tmpdir (if (featurep 'xemacs)
  10994. (temp-directory)
  10995. temporary-file-directory))
  10996. (texfilebase (make-temp-name
  10997. (expand-file-name "orgtex" tmpdir)))
  10998. (texfile (concat texfilebase ".tex"))
  10999. (dvifile (concat texfilebase ".dvi"))
  11000. (pngfile (concat texfilebase ".png"))
  11001. (fnh (if (featurep 'xemacs)
  11002. (font-height (get-face-font 'default))
  11003. (face-attribute 'default :height nil)))
  11004. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  11005. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  11006. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  11007. "Black"))
  11008. (bg (or (plist-get options (if buffer :background :html-background))
  11009. "Transparent")))
  11010. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  11011. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  11012. (with-temp-file texfile
  11013. (insert org-format-latex-header
  11014. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  11015. (let ((dir default-directory))
  11016. (condition-case nil
  11017. (progn
  11018. (cd tmpdir)
  11019. (call-process "latex" nil nil nil texfile))
  11020. (error nil))
  11021. (cd dir))
  11022. (if (not (file-exists-p dvifile))
  11023. (progn (message "Failed to create dvi file from %s" texfile) nil)
  11024. (condition-case nil
  11025. (call-process "dvipng" nil nil nil
  11026. "-E" "-fg" fg "-bg" bg
  11027. "-D" dpi
  11028. ;;"-x" scale "-y" scale
  11029. "-T" "tight"
  11030. "-o" pngfile
  11031. dvifile)
  11032. (error nil))
  11033. (if (not (file-exists-p pngfile))
  11034. (progn (message "Failed to create png file from %s" texfile) nil)
  11035. ;; Use the requested file name and clean up
  11036. (copy-file pngfile tofile 'replace)
  11037. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  11038. (delete-file (concat texfilebase e)))
  11039. pngfile))))
  11040. (defun org-dvipng-color (attr)
  11041. "Return an rgb color specification for dvipng."
  11042. (apply 'format "rgb %s %s %s"
  11043. (mapcar 'org-normalize-color
  11044. (color-values (face-attribute 'default attr nil)))))
  11045. (defun org-normalize-color (value)
  11046. "Return string to be used as color value for an RGB component."
  11047. (format "%g" (/ value 65535.0)))
  11048. ;;;; Key bindings
  11049. ;; Make `C-c C-x' a prefix key
  11050. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  11051. ;; TAB key with modifiers
  11052. (org-defkey org-mode-map "\C-i" 'org-cycle)
  11053. (org-defkey org-mode-map [(tab)] 'org-cycle)
  11054. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  11055. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  11056. (org-defkey org-mode-map "\M-\t" 'org-complete)
  11057. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  11058. ;; The following line is necessary under Suse GNU/Linux
  11059. (unless (featurep 'xemacs)
  11060. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  11061. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  11062. (define-key org-mode-map [backtab] 'org-shifttab)
  11063. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  11064. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  11065. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  11066. ;; Cursor keys with modifiers
  11067. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  11068. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  11069. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  11070. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  11071. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  11072. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  11073. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  11074. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  11075. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  11076. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  11077. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  11078. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  11079. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  11080. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  11081. ;;; Extra keys for tty access.
  11082. ;; We only set them when really needed because otherwise the
  11083. ;; menus don't show the simple keys
  11084. (when (or org-use-extra-keys
  11085. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  11086. (not window-system))
  11087. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  11088. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  11089. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  11090. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  11091. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  11092. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  11093. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  11094. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  11095. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  11096. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  11097. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  11098. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  11099. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  11100. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  11101. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  11102. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  11103. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  11104. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  11105. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  11106. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  11107. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  11108. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
  11109. ;; All the other keys
  11110. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  11111. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  11112. (if (boundp 'narrow-map)
  11113. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  11114. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  11115. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  11116. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  11117. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  11118. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  11119. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  11120. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  11121. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  11122. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  11123. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  11124. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  11125. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  11126. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  11127. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  11128. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  11129. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  11130. (org-defkey org-mode-map "\C-c\\" 'org-tags-sparse-tree) ; Minor-mode res.
  11131. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  11132. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  11133. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  11134. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  11135. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  11136. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  11137. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  11138. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  11139. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  11140. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  11141. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  11142. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  11143. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  11144. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  11145. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  11146. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  11147. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  11148. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  11149. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  11150. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  11151. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  11152. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  11153. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  11154. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  11155. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  11156. (org-defkey org-mode-map "\C-c^" 'org-sort)
  11157. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  11158. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  11159. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  11160. (org-defkey org-mode-map "\C-m" 'org-return)
  11161. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  11162. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  11163. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  11164. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  11165. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  11166. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  11167. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  11168. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  11169. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  11170. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  11171. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  11172. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  11173. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  11174. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  11175. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  11176. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  11177. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  11178. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  11179. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  11180. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  11181. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  11182. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  11183. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  11184. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  11185. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  11186. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  11187. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  11188. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  11189. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  11190. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  11191. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  11192. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  11193. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  11194. (when (featurep 'xemacs)
  11195. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  11196. (defvar org-table-auto-blank-field) ; defined in org-table.el
  11197. (defun org-self-insert-command (N)
  11198. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  11199. If the cursor is in a table looking at whitespace, the whitespace is
  11200. overwritten, and the table is not marked as requiring realignment."
  11201. (interactive "p")
  11202. (if (and (org-table-p)
  11203. (progn
  11204. ;; check if we blank the field, and if that triggers align
  11205. (and (featurep 'org-table) org-table-auto-blank-field
  11206. (member last-command
  11207. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  11208. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  11209. ;; got extra space, this field does not determine column width
  11210. (let (org-table-may-need-update) (org-table-blank-field))
  11211. ;; no extra space, this field may determine column width
  11212. (org-table-blank-field)))
  11213. t)
  11214. (eq N 1)
  11215. (looking-at "[^|\n]* |"))
  11216. (let (org-table-may-need-update)
  11217. (goto-char (1- (match-end 0)))
  11218. (delete-backward-char 1)
  11219. (goto-char (match-beginning 0))
  11220. (self-insert-command N))
  11221. (setq org-table-may-need-update t)
  11222. (self-insert-command N)
  11223. (org-fix-tags-on-the-fly)))
  11224. (defun org-fix-tags-on-the-fly ()
  11225. (when (and (equal (char-after (point-at-bol)) ?*)
  11226. (org-on-heading-p))
  11227. (org-align-tags-here org-tags-column)))
  11228. (defun org-delete-backward-char (N)
  11229. "Like `delete-backward-char', insert whitespace at field end in tables.
  11230. When deleting backwards, in tables this function will insert whitespace in
  11231. front of the next \"|\" separator, to keep the table aligned. The table will
  11232. still be marked for re-alignment if the field did fill the entire column,
  11233. because, in this case the deletion might narrow the column."
  11234. (interactive "p")
  11235. (if (and (org-table-p)
  11236. (eq N 1)
  11237. (string-match "|" (buffer-substring (point-at-bol) (point)))
  11238. (looking-at ".*?|"))
  11239. (let ((pos (point))
  11240. (noalign (looking-at "[^|\n\r]* |"))
  11241. (c org-table-may-need-update))
  11242. (backward-delete-char N)
  11243. (skip-chars-forward "^|")
  11244. (insert " ")
  11245. (goto-char (1- pos))
  11246. ;; noalign: if there were two spaces at the end, this field
  11247. ;; does not determine the width of the column.
  11248. (if noalign (setq org-table-may-need-update c)))
  11249. (backward-delete-char N)
  11250. (org-fix-tags-on-the-fly)))
  11251. (defun org-delete-char (N)
  11252. "Like `delete-char', but insert whitespace at field end in tables.
  11253. When deleting characters, in tables this function will insert whitespace in
  11254. front of the next \"|\" separator, to keep the table aligned. The table will
  11255. still be marked for re-alignment if the field did fill the entire column,
  11256. because, in this case the deletion might narrow the column."
  11257. (interactive "p")
  11258. (if (and (org-table-p)
  11259. (not (bolp))
  11260. (not (= (char-after) ?|))
  11261. (eq N 1))
  11262. (if (looking-at ".*?|")
  11263. (let ((pos (point))
  11264. (noalign (looking-at "[^|\n\r]* |"))
  11265. (c org-table-may-need-update))
  11266. (replace-match (concat
  11267. (substring (match-string 0) 1 -1)
  11268. " |"))
  11269. (goto-char pos)
  11270. ;; noalign: if there were two spaces at the end, this field
  11271. ;; does not determine the width of the column.
  11272. (if noalign (setq org-table-may-need-update c)))
  11273. (delete-char N))
  11274. (delete-char N)
  11275. (org-fix-tags-on-the-fly)))
  11276. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  11277. (put 'org-self-insert-command 'delete-selection t)
  11278. (put 'orgtbl-self-insert-command 'delete-selection t)
  11279. (put 'org-delete-char 'delete-selection 'supersede)
  11280. (put 'org-delete-backward-char 'delete-selection 'supersede)
  11281. ;; Make `flyspell-mode' delay after some commands
  11282. (put 'org-self-insert-command 'flyspell-delayed t)
  11283. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  11284. (put 'org-delete-char 'flyspell-delayed t)
  11285. (put 'org-delete-backward-char 'flyspell-delayed t)
  11286. ;; Make pabbrev-mode expand after org-mode commands
  11287. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  11288. (put 'orgybl-self-insert-command 'pabbrev-expand-after-command t)
  11289. ;; How to do this: Measure non-white length of current string
  11290. ;; If equal to column width, we should realign.
  11291. (defun org-remap (map &rest commands)
  11292. "In MAP, remap the functions given in COMMANDS.
  11293. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  11294. (let (new old)
  11295. (while commands
  11296. (setq old (pop commands) new (pop commands))
  11297. (if (fboundp 'command-remapping)
  11298. (org-defkey map (vector 'remap old) new)
  11299. (substitute-key-definition old new map global-map)))))
  11300. (when (eq org-enable-table-editor 'optimized)
  11301. ;; If the user wants maximum table support, we need to hijack
  11302. ;; some standard editing functions
  11303. (org-remap org-mode-map
  11304. 'self-insert-command 'org-self-insert-command
  11305. 'delete-char 'org-delete-char
  11306. 'delete-backward-char 'org-delete-backward-char)
  11307. (org-defkey org-mode-map "|" 'org-force-self-insert))
  11308. (defun org-shiftcursor-error ()
  11309. "Throw an error because Shift-Cursor command was applied in wrong context."
  11310. (error "This command is active in special context like tables, headlines or timestamps"))
  11311. (defun org-shifttab (&optional arg)
  11312. "Global visibility cycling or move to previous table field.
  11313. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  11314. on context.
  11315. See the individual commands for more information."
  11316. (interactive "P")
  11317. (cond
  11318. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  11319. ((integerp arg)
  11320. (message "Content view to level: %d" arg)
  11321. (org-content (prefix-numeric-value arg))
  11322. (setq org-cycle-global-status 'overview))
  11323. (t (call-interactively 'org-global-cycle))))
  11324. (defun org-shiftmetaleft ()
  11325. "Promote subtree or delete table column.
  11326. Calls `org-promote-subtree', `org-outdent-item',
  11327. or `org-table-delete-column', depending on context.
  11328. See the individual commands for more information."
  11329. (interactive)
  11330. (cond
  11331. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  11332. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  11333. ((org-at-item-p) (call-interactively 'org-outdent-item))
  11334. (t (org-shiftcursor-error))))
  11335. (defun org-shiftmetaright ()
  11336. "Demote subtree or insert table column.
  11337. Calls `org-demote-subtree', `org-indent-item',
  11338. or `org-table-insert-column', depending on context.
  11339. See the individual commands for more information."
  11340. (interactive)
  11341. (cond
  11342. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  11343. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  11344. ((org-at-item-p) (call-interactively 'org-indent-item))
  11345. (t (org-shiftcursor-error))))
  11346. (defun org-shiftmetaup (&optional arg)
  11347. "Move subtree up or kill table row.
  11348. Calls `org-move-subtree-up' or `org-table-kill-row' or
  11349. `org-move-item-up' depending on context. See the individual commands
  11350. for more information."
  11351. (interactive "P")
  11352. (cond
  11353. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  11354. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  11355. ((org-at-item-p) (call-interactively 'org-move-item-up))
  11356. (t (org-shiftcursor-error))))
  11357. (defun org-shiftmetadown (&optional arg)
  11358. "Move subtree down or insert table row.
  11359. Calls `org-move-subtree-down' or `org-table-insert-row' or
  11360. `org-move-item-down', depending on context. See the individual
  11361. commands for more information."
  11362. (interactive "P")
  11363. (cond
  11364. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  11365. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  11366. ((org-at-item-p) (call-interactively 'org-move-item-down))
  11367. (t (org-shiftcursor-error))))
  11368. (defun org-metaleft (&optional arg)
  11369. "Promote heading or move table column to left.
  11370. Calls `org-do-promote' or `org-table-move-column', depending on context.
  11371. With no specific context, calls the Emacs default `backward-word'.
  11372. See the individual commands for more information."
  11373. (interactive "P")
  11374. (cond
  11375. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  11376. ((or (org-on-heading-p) (org-region-active-p))
  11377. (call-interactively 'org-do-promote))
  11378. ((org-at-item-p) (call-interactively 'org-outdent-item))
  11379. (t (call-interactively 'backward-word))))
  11380. (defun org-metaright (&optional arg)
  11381. "Demote subtree or move table column to right.
  11382. Calls `org-do-demote' or `org-table-move-column', depending on context.
  11383. With no specific context, calls the Emacs default `forward-word'.
  11384. See the individual commands for more information."
  11385. (interactive "P")
  11386. (cond
  11387. ((org-at-table-p) (call-interactively 'org-table-move-column))
  11388. ((or (org-on-heading-p) (org-region-active-p))
  11389. (call-interactively 'org-do-demote))
  11390. ((org-at-item-p) (call-interactively 'org-indent-item))
  11391. (t (call-interactively 'forward-word))))
  11392. (defun org-metaup (&optional arg)
  11393. "Move subtree up or move table row up.
  11394. Calls `org-move-subtree-up' or `org-table-move-row' or
  11395. `org-move-item-up', depending on context. See the individual commands
  11396. for more information."
  11397. (interactive "P")
  11398. (cond
  11399. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  11400. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  11401. ((org-at-item-p) (call-interactively 'org-move-item-up))
  11402. (t (transpose-lines 1) (beginning-of-line -1))))
  11403. (defun org-metadown (&optional arg)
  11404. "Move subtree down or move table row down.
  11405. Calls `org-move-subtree-down' or `org-table-move-row' or
  11406. `org-move-item-down', depending on context. See the individual
  11407. commands for more information."
  11408. (interactive "P")
  11409. (cond
  11410. ((org-at-table-p) (call-interactively 'org-table-move-row))
  11411. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  11412. ((org-at-item-p) (call-interactively 'org-move-item-down))
  11413. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  11414. (defun org-shiftup (&optional arg)
  11415. "Increase item in timestamp or increase priority of current headline.
  11416. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  11417. depending on context. See the individual commands for more information."
  11418. (interactive "P")
  11419. (cond
  11420. ((org-at-timestamp-p t)
  11421. (call-interactively (if org-edit-timestamp-down-means-later
  11422. 'org-timestamp-down 'org-timestamp-up)))
  11423. ((org-on-heading-p) (call-interactively 'org-priority-up))
  11424. ((org-at-item-p) (call-interactively 'org-previous-item))
  11425. ((org-clocktable-try-shift 'up arg))
  11426. (t (call-interactively 'org-beginning-of-item) (beginning-of-line 1))))
  11427. (defun org-shiftdown (&optional arg)
  11428. "Decrease item in timestamp or decrease priority of current headline.
  11429. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  11430. depending on context. See the individual commands for more information."
  11431. (interactive "P")
  11432. (cond
  11433. ((org-at-timestamp-p t)
  11434. (call-interactively (if org-edit-timestamp-down-means-later
  11435. 'org-timestamp-up 'org-timestamp-down)))
  11436. ((org-on-heading-p) (call-interactively 'org-priority-down))
  11437. ((org-clocktable-try-shift 'down arg))
  11438. (t (call-interactively 'org-next-item))))
  11439. (defun org-shiftright (&optional arg)
  11440. "Next TODO keyword or timestamp one day later, depending on context."
  11441. (interactive "P")
  11442. (cond
  11443. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  11444. ((org-on-heading-p) (org-call-with-arg 'org-todo 'right))
  11445. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet nil))
  11446. ((org-at-property-p) (call-interactively 'org-property-next-allowed-value))
  11447. ((org-clocktable-try-shift 'right arg))
  11448. (t (org-shiftcursor-error))))
  11449. (defun org-shiftleft (&optional arg)
  11450. "Previous TODO keyword or timestamp one day earlier, depending on context."
  11451. (interactive "P")
  11452. (cond
  11453. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  11454. ((org-on-heading-p) (org-call-with-arg 'org-todo 'left))
  11455. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet 'previous))
  11456. ((org-at-property-p)
  11457. (call-interactively 'org-property-previous-allowed-value))
  11458. ((org-clocktable-try-shift 'left arg))
  11459. (t (org-shiftcursor-error))))
  11460. (defun org-shiftcontrolright ()
  11461. "Switch to next TODO set."
  11462. (interactive)
  11463. (cond
  11464. ((org-on-heading-p) (org-call-with-arg 'org-todo 'nextset))
  11465. (t (org-shiftcursor-error))))
  11466. (defun org-shiftcontrolleft ()
  11467. "Switch to previous TODO set."
  11468. (interactive)
  11469. (cond
  11470. ((org-on-heading-p) (org-call-with-arg 'org-todo 'previousset))
  11471. (t (org-shiftcursor-error))))
  11472. (defun org-ctrl-c-ret ()
  11473. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  11474. (interactive)
  11475. (cond
  11476. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  11477. (t (call-interactively 'org-insert-heading))))
  11478. (defun org-copy-special ()
  11479. "Copy region in table or copy current subtree.
  11480. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  11481. See the individual commands for more information."
  11482. (interactive)
  11483. (call-interactively
  11484. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  11485. (defun org-cut-special ()
  11486. "Cut region in table or cut current subtree.
  11487. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  11488. See the individual commands for more information."
  11489. (interactive)
  11490. (call-interactively
  11491. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  11492. (defun org-paste-special (arg)
  11493. "Paste rectangular region into table, or past subtree relative to level.
  11494. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  11495. See the individual commands for more information."
  11496. (interactive "P")
  11497. (if (org-at-table-p)
  11498. (org-table-paste-rectangle)
  11499. (org-paste-subtree arg)))
  11500. (defun org-edit-special ()
  11501. "Call a special editor for the stuff at point.
  11502. When at a table, call the formula editor with `org-table-edit-formulas'.
  11503. When at the first line of an src example, call `org-edit-src-code'.
  11504. When in an #+include line, visit the include file. Otherwise call
  11505. `ffap' to visit the file at point."
  11506. (interactive)
  11507. (cond
  11508. ((org-at-table-p)
  11509. (call-interactively 'org-table-edit-formulas))
  11510. ((save-excursion
  11511. (beginning-of-line 1)
  11512. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  11513. (find-file (org-trim (match-string 1))))
  11514. ((org-edit-src-code))
  11515. ((org-edit-fixed-width-region))
  11516. (t (call-interactively 'ffap))))
  11517. (defun org-ctrl-c-ctrl-c (&optional arg)
  11518. "Set tags in headline, or update according to changed information at point.
  11519. This command does many different things, depending on context:
  11520. - If the cursor is in a headline, prompt for tags and insert them
  11521. into the current line, aligned to `org-tags-column'. When called
  11522. with prefix arg, realign all tags in the current buffer.
  11523. - If the cursor is in one of the special #+KEYWORD lines, this
  11524. triggers scanning the buffer for these lines and updating the
  11525. information.
  11526. - If the cursor is inside a table, realign the table. This command
  11527. works even if the automatic table editor has been turned off.
  11528. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  11529. the entire table.
  11530. - If the cursor is a the beginning of a dynamic block, update it.
  11531. - If the cursor is inside a table created by the table.el package,
  11532. activate that table.
  11533. - If the current buffer is a remember buffer, close note and file
  11534. it. A prefix argument of 1 files to the default location
  11535. without further interaction. A prefix argument of 2 files to
  11536. the currently clocking task.
  11537. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  11538. links in this buffer.
  11539. - If the cursor is on a numbered item in a plain list, renumber the
  11540. ordered list.
  11541. - If the cursor is on a checkbox, toggle it."
  11542. (interactive "P")
  11543. (let ((org-enable-table-editor t))
  11544. (cond
  11545. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  11546. org-occur-highlights
  11547. org-latex-fragment-image-overlays)
  11548. (and (boundp 'org-clock-overlays) (org-remove-clock-overlays))
  11549. (org-remove-occur-highlights)
  11550. (org-remove-latex-fragment-image-overlays)
  11551. (message "Temporary highlights/overlays removed from current buffer"))
  11552. ((and (local-variable-p 'org-finish-function (current-buffer))
  11553. (fboundp org-finish-function))
  11554. (funcall org-finish-function))
  11555. ((org-at-property-p)
  11556. (call-interactively 'org-property-action))
  11557. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  11558. ((org-on-heading-p) (call-interactively 'org-set-tags))
  11559. ((org-at-table.el-p)
  11560. (require 'table)
  11561. (beginning-of-line 1)
  11562. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  11563. (call-interactively 'table-recognize-table))
  11564. ((org-at-table-p)
  11565. (org-table-maybe-eval-formula)
  11566. (if arg
  11567. (call-interactively 'org-table-recalculate)
  11568. (org-table-maybe-recalculate-line))
  11569. (call-interactively 'org-table-align))
  11570. ((org-at-item-checkbox-p)
  11571. (call-interactively 'org-toggle-checkbox))
  11572. ((org-at-item-p)
  11573. (call-interactively 'org-maybe-renumber-ordered-list))
  11574. ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
  11575. ;; Dynamic block
  11576. (beginning-of-line 1)
  11577. (save-excursion (org-update-dblock)))
  11578. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  11579. (cond
  11580. ((equal (match-string 1) "TBLFM")
  11581. ;; Recalculate the table before this line
  11582. (save-excursion
  11583. (beginning-of-line 1)
  11584. (skip-chars-backward " \r\n\t")
  11585. (if (org-at-table-p)
  11586. (org-call-with-arg 'org-table-recalculate t))))
  11587. (t
  11588. ; (org-set-regexps-and-options)
  11589. ; (org-restart-font-lock)
  11590. (let ((org-inhibit-startup t)) (org-mode-restart))
  11591. (message "Local setup has been refreshed"))))
  11592. (t (error "C-c C-c can do nothing useful at this location.")))))
  11593. (defun org-mode-restart ()
  11594. "Restart Org-mode, to scan again for special lines.
  11595. Also updates the keyword regular expressions."
  11596. (interactive)
  11597. (org-mode)
  11598. (message "Org-mode restarted"))
  11599. (defun org-kill-note-or-show-branches ()
  11600. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  11601. (interactive)
  11602. (if (not org-finish-function)
  11603. (call-interactively 'show-branches)
  11604. (let ((org-note-abort t))
  11605. (funcall org-finish-function))))
  11606. (defun org-return (&optional indent)
  11607. "Goto next table row or insert a newline.
  11608. Calls `org-table-next-row' or `newline', depending on context.
  11609. See the individual commands for more information."
  11610. (interactive)
  11611. (cond
  11612. ((bobp) (if indent (newline-and-indent) (newline)))
  11613. ((and (org-at-heading-p)
  11614. (looking-at
  11615. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  11616. (org-show-entry)
  11617. (end-of-line 1)
  11618. (newline))
  11619. ((org-at-table-p)
  11620. (org-table-justify-field-maybe)
  11621. (call-interactively 'org-table-next-row))
  11622. (t (if indent (newline-and-indent) (newline)))))
  11623. (defun org-return-indent ()
  11624. "Goto next table row or insert a newline and indent.
  11625. Calls `org-table-next-row' or `newline-and-indent', depending on
  11626. context. See the individual commands for more information."
  11627. (interactive)
  11628. (org-return t))
  11629. (defun org-ctrl-c-star ()
  11630. "Compute table, or change heading status of lines.
  11631. Calls `org-table-recalculate' or `org-toggle-region-headings',
  11632. depending on context. This will also turn a plain list item or a normal
  11633. line into a subheading."
  11634. (interactive)
  11635. (cond
  11636. ((org-at-table-p)
  11637. (call-interactively 'org-table-recalculate))
  11638. ((org-region-active-p)
  11639. ;; Convert all lines in region to list items
  11640. (call-interactively 'org-toggle-region-headings))
  11641. ((org-on-heading-p)
  11642. (org-toggle-region-headings (point-at-bol)
  11643. (min (1+ (point-at-eol)) (point-max))))
  11644. ((org-at-item-p)
  11645. ;; Convert to heading
  11646. (let ((level (save-match-data
  11647. (save-excursion
  11648. (condition-case nil
  11649. (progn
  11650. (org-back-to-heading t)
  11651. (funcall outline-level))
  11652. (error 0))))))
  11653. (replace-match
  11654. (concat (make-string (org-get-valid-level level 1) ?*) " ") t t)))
  11655. (t (org-toggle-region-headings (point-at-bol)
  11656. (min (1+ (point-at-eol)) (point-max))))))
  11657. (defun org-ctrl-c-minus ()
  11658. "Insert separator line in table or modify bullet status of line.
  11659. Also turns a plain line or a region of lines into list items.
  11660. Calls `org-table-insert-hline', `org-toggle-region-items', or
  11661. `org-cycle-list-bullet', depending on context."
  11662. (interactive)
  11663. (cond
  11664. ((org-at-table-p)
  11665. (call-interactively 'org-table-insert-hline))
  11666. ((org-on-heading-p)
  11667. ;; Convert to item
  11668. (save-excursion
  11669. (beginning-of-line 1)
  11670. (if (looking-at "\\*+ ")
  11671. (replace-match (concat (make-string (- (match-end 0) (point) 1) ?\ ) "- ")))))
  11672. ((org-region-active-p)
  11673. ;; Convert all lines in region to list items
  11674. (call-interactively 'org-toggle-region-items))
  11675. ((org-in-item-p)
  11676. (call-interactively 'org-cycle-list-bullet))
  11677. (t (org-toggle-region-items (point-at-bol)
  11678. (min (1+ (point-at-eol)) (point-max))))))
  11679. (defun org-toggle-region-items (beg end)
  11680. "Convert all lines in region to list items.
  11681. If the first line is already an item, convert all list items in the region
  11682. to normal lines."
  11683. (interactive "r")
  11684. (let (l2 l)
  11685. (save-excursion
  11686. (goto-char end)
  11687. (setq l2 (org-current-line))
  11688. (goto-char beg)
  11689. (beginning-of-line 1)
  11690. (setq l (1- (org-current-line)))
  11691. (if (org-at-item-p)
  11692. ;; We already have items, de-itemize
  11693. (while (< (setq l (1+ l)) l2)
  11694. (when (org-at-item-p)
  11695. (goto-char (match-beginning 2))
  11696. (delete-region (match-beginning 2) (match-end 2))
  11697. (and (looking-at "[ \t]+") (replace-match "")))
  11698. (beginning-of-line 2))
  11699. (while (< (setq l (1+ l)) l2)
  11700. (unless (org-at-item-p)
  11701. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  11702. (replace-match "\\1- \\2")))
  11703. (beginning-of-line 2))))))
  11704. (defun org-toggle-region-headings (beg end)
  11705. "Convert all lines in region to list items.
  11706. If the first line is already an item, convert all list items in the region
  11707. to normal lines."
  11708. (interactive "r")
  11709. (let (l2 l)
  11710. (save-excursion
  11711. (goto-char end)
  11712. (setq l2 (org-current-line))
  11713. (goto-char beg)
  11714. (beginning-of-line 1)
  11715. (setq l (1- (org-current-line)))
  11716. (if (org-on-heading-p)
  11717. ;; We already have headlines, de-star them
  11718. (while (< (setq l (1+ l)) l2)
  11719. (when (org-on-heading-p t)
  11720. (and (looking-at outline-regexp) (replace-match "")))
  11721. (beginning-of-line 2))
  11722. (let* ((stars (save-excursion
  11723. (re-search-backward org-complex-heading-regexp nil t)
  11724. (or (match-string 1) "*")))
  11725. (add-stars (if org-odd-levels-only "**" "*"))
  11726. (rpl (concat stars add-stars " \\2")))
  11727. (while (< (setq l (1+ l)) l2)
  11728. (unless (org-on-heading-p)
  11729. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  11730. (replace-match rpl)))
  11731. (beginning-of-line 2)))))))
  11732. (defun org-meta-return (&optional arg)
  11733. "Insert a new heading or wrap a region in a table.
  11734. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  11735. See the individual commands for more information."
  11736. (interactive "P")
  11737. (cond
  11738. ((org-at-table-p)
  11739. (call-interactively 'org-table-wrap-region))
  11740. (t (call-interactively 'org-insert-heading))))
  11741. ;;; Menu entries
  11742. ;; Define the Org-mode menus
  11743. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  11744. '("Tbl"
  11745. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  11746. ["Next Field" org-cycle (org-at-table-p)]
  11747. ["Previous Field" org-shifttab (org-at-table-p)]
  11748. ["Next Row" org-return (org-at-table-p)]
  11749. "--"
  11750. ["Blank Field" org-table-blank-field (org-at-table-p)]
  11751. ["Edit Field" org-table-edit-field (org-at-table-p)]
  11752. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  11753. "--"
  11754. ("Column"
  11755. ["Move Column Left" org-metaleft (org-at-table-p)]
  11756. ["Move Column Right" org-metaright (org-at-table-p)]
  11757. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  11758. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  11759. ("Row"
  11760. ["Move Row Up" org-metaup (org-at-table-p)]
  11761. ["Move Row Down" org-metadown (org-at-table-p)]
  11762. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  11763. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  11764. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  11765. "--"
  11766. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  11767. ("Rectangle"
  11768. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  11769. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  11770. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  11771. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  11772. "--"
  11773. ("Calculate"
  11774. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  11775. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  11776. ["Edit Formulas" org-edit-special (org-at-table-p)]
  11777. "--"
  11778. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  11779. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  11780. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  11781. "--"
  11782. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  11783. "--"
  11784. ["Sum Column/Rectangle" org-table-sum
  11785. (or (org-at-table-p) (org-region-active-p))]
  11786. ["Which Column?" org-table-current-column (org-at-table-p)])
  11787. ["Debug Formulas"
  11788. org-table-toggle-formula-debugger
  11789. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  11790. ["Show Col/Row Numbers"
  11791. org-table-toggle-coordinate-overlays
  11792. :style toggle
  11793. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  11794. "--"
  11795. ["Create" org-table-create (and (not (org-at-table-p))
  11796. org-enable-table-editor)]
  11797. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  11798. ["Import from File" org-table-import (not (org-at-table-p))]
  11799. ["Export to File" org-table-export (org-at-table-p)]
  11800. "--"
  11801. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  11802. (easy-menu-define org-org-menu org-mode-map "Org menu"
  11803. '("Org"
  11804. ("Show/Hide"
  11805. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  11806. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  11807. ["Sparse Tree..." org-sparse-tree t]
  11808. ["Reveal Context" org-reveal t]
  11809. ["Show All" show-all t]
  11810. "--"
  11811. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  11812. "--"
  11813. ["New Heading" org-insert-heading t]
  11814. ("Navigate Headings"
  11815. ["Up" outline-up-heading t]
  11816. ["Next" outline-next-visible-heading t]
  11817. ["Previous" outline-previous-visible-heading t]
  11818. ["Next Same Level" outline-forward-same-level t]
  11819. ["Previous Same Level" outline-backward-same-level t]
  11820. "--"
  11821. ["Jump" org-goto t])
  11822. ("Edit Structure"
  11823. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  11824. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  11825. "--"
  11826. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  11827. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  11828. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  11829. "--"
  11830. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  11831. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  11832. ["Demote Heading" org-metaright (not (org-at-table-p))]
  11833. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  11834. "--"
  11835. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  11836. "--"
  11837. ["Convert to odd levels" org-convert-to-odd-levels t]
  11838. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  11839. ("Editing"
  11840. ["Emphasis..." org-emphasize t]
  11841. ["Edit Source Example" org-edit-special t])
  11842. ("Archive"
  11843. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  11844. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  11845. ; :active t :keys "C-u C-c C-x C-a"]
  11846. ["Sparse trees open ARCHIVE trees"
  11847. (setq org-sparse-tree-open-archived-trees
  11848. (not org-sparse-tree-open-archived-trees))
  11849. :style toggle :selected org-sparse-tree-open-archived-trees]
  11850. ["Cycling opens ARCHIVE trees"
  11851. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  11852. :style toggle :selected org-cycle-open-archived-trees]
  11853. "--"
  11854. ["Move subtree to archive sibling" org-archive-to-archive-sibling t]
  11855. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  11856. ; ["Check and Move Children" (org-archive-subtree '(4))
  11857. ; :active t :keys "C-u C-c C-x C-s"]
  11858. )
  11859. "--"
  11860. ("TODO Lists"
  11861. ["TODO/DONE/-" org-todo t]
  11862. ("Select keyword"
  11863. ["Next keyword" org-shiftright (org-on-heading-p)]
  11864. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  11865. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  11866. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  11867. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  11868. ["Show TODO Tree" org-show-todo-tree t]
  11869. ["Global TODO list" org-todo-list t]
  11870. "--"
  11871. ["Set Priority" org-priority t]
  11872. ["Priority Up" org-shiftup t]
  11873. ["Priority Down" org-shiftdown t])
  11874. ("TAGS and Properties"
  11875. ["Set Tags" 'org-set-tags-command t]
  11876. ["Change tag in region" 'org-change-tag-in-region (org-region-active-p)]
  11877. "--"
  11878. ["Set property" 'org-set-property t]
  11879. ["Column view of properties" org-columns t]
  11880. ["Insert Column View DBlock" org-insert-columns-dblock t])
  11881. ("Dates and Scheduling"
  11882. ["Timestamp" org-time-stamp t]
  11883. ["Timestamp (inactive)" org-time-stamp-inactive t]
  11884. ("Change Date"
  11885. ["1 Day Later" org-shiftright t]
  11886. ["1 Day Earlier" org-shiftleft t]
  11887. ["1 ... Later" org-shiftup t]
  11888. ["1 ... Earlier" org-shiftdown t])
  11889. ["Compute Time Range" org-evaluate-time-range t]
  11890. ["Schedule Item" org-schedule t]
  11891. ["Deadline" org-deadline t]
  11892. "--"
  11893. ["Custom time format" org-toggle-time-stamp-overlays
  11894. :style radio :selected org-display-custom-times]
  11895. "--"
  11896. ["Goto Calendar" org-goto-calendar t]
  11897. ["Date from Calendar" org-date-from-calendar t])
  11898. ("Logging work"
  11899. ["Clock in" org-clock-in t]
  11900. ["Clock out" org-clock-out t]
  11901. ["Clock cancel" org-clock-cancel t]
  11902. ["Goto running clock" org-clock-goto t]
  11903. ["Display times" org-clock-display t]
  11904. ["Create clock table" org-clock-report t]
  11905. "--"
  11906. ["Record DONE time"
  11907. (progn (setq org-log-done (not org-log-done))
  11908. (message "Switching to %s will %s record a timestamp"
  11909. (car org-done-keywords)
  11910. (if org-log-done "automatically" "not")))
  11911. :style toggle :selected org-log-done])
  11912. "--"
  11913. ["Agenda Command..." org-agenda t]
  11914. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  11915. ("File List for Agenda")
  11916. ("Special views current file"
  11917. ["TODO Tree" org-show-todo-tree t]
  11918. ["Check Deadlines" org-check-deadlines t]
  11919. ["Timeline" org-timeline t]
  11920. ["Tags Tree" org-tags-sparse-tree t])
  11921. "--"
  11922. ("Hyperlinks"
  11923. ["Store Link (Global)" org-store-link t]
  11924. ["Insert Link" org-insert-link t]
  11925. ["Follow Link" org-open-at-point t]
  11926. "--"
  11927. ["Next link" org-next-link t]
  11928. ["Previous link" org-previous-link t]
  11929. "--"
  11930. ["Descriptive Links"
  11931. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  11932. :style radio
  11933. :selected (member '(org-link) buffer-invisibility-spec)]
  11934. ["Literal Links"
  11935. (progn
  11936. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  11937. :style radio
  11938. :selected (not (member '(org-link) buffer-invisibility-spec))])
  11939. "--"
  11940. ["Export/Publish..." org-export t]
  11941. ("LaTeX"
  11942. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  11943. :selected org-cdlatex-mode]
  11944. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  11945. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  11946. ["Modify math symbol" org-cdlatex-math-modify
  11947. (org-inside-LaTeX-fragment-p)]
  11948. ["Export LaTeX fragments as images"
  11949. (if (featurep 'org-exp)
  11950. (setq org-export-with-LaTeX-fragments
  11951. (not org-export-with-LaTeX-fragments))
  11952. (require 'org-exp))
  11953. :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
  11954. org-export-with-LaTeX-fragments)])
  11955. "--"
  11956. ("Documentation"
  11957. ["Show Version" org-version t]
  11958. ["Info Documentation" org-info t])
  11959. ("Customize"
  11960. ["Browse Org Group" org-customize t]
  11961. "--"
  11962. ["Expand This Menu" org-create-customize-menu
  11963. (fboundp 'customize-menu-create)])
  11964. "--"
  11965. ["Refresh setup" org-mode-restart t]
  11966. ))
  11967. (defun org-info (&optional node)
  11968. "Read documentation for Org-mode in the info system.
  11969. With optional NODE, go directly to that node."
  11970. (interactive)
  11971. (info (format "(org)%s" (or node ""))))
  11972. (defun org-install-agenda-files-menu ()
  11973. (let ((bl (buffer-list)))
  11974. (save-excursion
  11975. (while bl
  11976. (set-buffer (pop bl))
  11977. (if (org-mode-p) (setq bl nil)))
  11978. (when (org-mode-p)
  11979. (easy-menu-change
  11980. '("Org") "File List for Agenda"
  11981. (append
  11982. (list
  11983. ["Edit File List" (org-edit-agenda-file-list) t]
  11984. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  11985. ["Remove Current File from List" org-remove-file t]
  11986. ["Cycle through agenda files" org-cycle-agenda-files t]
  11987. ["Occur in all agenda files" org-occur-in-agenda-files t]
  11988. "--")
  11989. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  11990. ;;;; Documentation
  11991. ;;;###autoload
  11992. (defun org-require-autoloaded-modules ()
  11993. (interactive)
  11994. (mapc 'require
  11995. '(org-agenda org-archive org-clock org-colview
  11996. org-exp org-id org-export-latex org-publish
  11997. org-remember org-table)))
  11998. ;;;###autoload
  11999. (defun org-customize ()
  12000. "Call the customize function with org as argument."
  12001. (interactive)
  12002. (org-load-modules-maybe)
  12003. (org-require-autoloaded-modules)
  12004. (customize-browse 'org))
  12005. (defun org-create-customize-menu ()
  12006. "Create a full customization menu for Org-mode, insert it into the menu."
  12007. (interactive)
  12008. (org-load-modules-maybe)
  12009. (org-require-autoloaded-modules)
  12010. (if (fboundp 'customize-menu-create)
  12011. (progn
  12012. (easy-menu-change
  12013. '("Org") "Customize"
  12014. `(["Browse Org group" org-customize t]
  12015. "--"
  12016. ,(customize-menu-create 'org)
  12017. ["Set" Custom-set t]
  12018. ["Save" Custom-save t]
  12019. ["Reset to Current" Custom-reset-current t]
  12020. ["Reset to Saved" Custom-reset-saved t]
  12021. ["Reset to Standard Settings" Custom-reset-standard t]))
  12022. (message "\"Org\"-menu now contains full customization menu"))
  12023. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  12024. ;;;; Miscellaneous stuff
  12025. ;;; Generally useful functions
  12026. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  12027. "Display the given MESSAGE as a warning."
  12028. (if (fboundp 'display-warning)
  12029. (display-warning 'org message
  12030. (if (featurep 'xemacs)
  12031. 'warning
  12032. :warning))
  12033. (let ((buf (get-buffer-create "*Org warnings*")))
  12034. (with-current-buffer buf
  12035. (goto-char (point-max))
  12036. (insert "Warning (Org): " message)
  12037. (unless (bolp)
  12038. (newline)))
  12039. (display-buffer buf)
  12040. (sit-for 0))))
  12041. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  12042. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  12043. (if (and marker (marker-buffer marker)
  12044. (buffer-live-p (marker-buffer marker)))
  12045. (progn
  12046. (switch-to-buffer (marker-buffer marker))
  12047. (if (or (> marker (point-max)) (< marker (point-min)))
  12048. (widen))
  12049. (goto-char marker))
  12050. (if bookmark
  12051. (bookmark-jump bookmark)
  12052. (error "Cannot find location"))))
  12053. (defun org-quote-csv-field (s)
  12054. "Quote field for inclusion in CSV material."
  12055. (if (string-match "[\",]" s)
  12056. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  12057. s))
  12058. (defun org-plist-delete (plist property)
  12059. "Delete PROPERTY from PLIST.
  12060. This is in contrast to merely setting it to 0."
  12061. (let (p)
  12062. (while plist
  12063. (if (not (eq property (car plist)))
  12064. (setq p (plist-put p (car plist) (nth 1 plist))))
  12065. (setq plist (cddr plist)))
  12066. p))
  12067. (defun org-force-self-insert (N)
  12068. "Needed to enforce self-insert under remapping."
  12069. (interactive "p")
  12070. (self-insert-command N))
  12071. (defun org-string-width (s)
  12072. "Compute width of string, ignoring invisible characters.
  12073. This ignores character with invisibility property `org-link', and also
  12074. characters with property `org-cwidth', because these will become invisible
  12075. upon the next fontification round."
  12076. (let (b l)
  12077. (when (or (eq t buffer-invisibility-spec)
  12078. (assq 'org-link buffer-invisibility-spec))
  12079. (while (setq b (text-property-any 0 (length s)
  12080. 'invisible 'org-link s))
  12081. (setq s (concat (substring s 0 b)
  12082. (substring s (or (next-single-property-change
  12083. b 'invisible s) (length s)))))))
  12084. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  12085. (setq s (concat (substring s 0 b)
  12086. (substring s (or (next-single-property-change
  12087. b 'org-cwidth s) (length s))))))
  12088. (setq l (string-width s) b -1)
  12089. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  12090. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  12091. l))
  12092. (defun org-get-indentation (&optional line)
  12093. "Get the indentation of the current line, interpreting tabs.
  12094. When LINE is given, assume it represents a line and compute its indentation."
  12095. (if line
  12096. (if (string-match "^ *" (org-remove-tabs line))
  12097. (match-end 0))
  12098. (save-excursion
  12099. (beginning-of-line 1)
  12100. (skip-chars-forward " \t")
  12101. (current-column))))
  12102. (defun org-remove-tabs (s &optional width)
  12103. "Replace tabulators in S with spaces.
  12104. Assumes that s is a single line, starting in column 0."
  12105. (setq width (or width tab-width))
  12106. (while (string-match "\t" s)
  12107. (setq s (replace-match
  12108. (make-string
  12109. (- (* width (/ (+ (match-beginning 0) width) width))
  12110. (match-beginning 0)) ?\ )
  12111. t t s)))
  12112. s)
  12113. (defun org-fix-indentation (line ind)
  12114. "Fix indentation in LINE.
  12115. IND is a cons cell with target and minimum indentation.
  12116. If the current indenation in LINE is smaller than the minimum,
  12117. leave it alone. If it is larger than ind, set it to the target."
  12118. (let* ((l (org-remove-tabs line))
  12119. (i (org-get-indentation l))
  12120. (i1 (car ind)) (i2 (cdr ind)))
  12121. (if (>= i i2) (setq l (substring line i2)))
  12122. (if (> i1 0)
  12123. (concat (make-string i1 ?\ ) l)
  12124. l)))
  12125. (defun org-base-buffer (buffer)
  12126. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  12127. (if (not buffer)
  12128. buffer
  12129. (or (buffer-base-buffer buffer)
  12130. buffer)))
  12131. (defun org-trim (s)
  12132. "Remove whitespace at beginning and end of string."
  12133. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  12134. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  12135. s)
  12136. (defun org-wrap (string &optional width lines)
  12137. "Wrap string to either a number of lines, or a width in characters.
  12138. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  12139. that costs. If there is a word longer than WIDTH, the text is actually
  12140. wrapped to the length of that word.
  12141. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  12142. many lines, whatever width that takes.
  12143. The return value is a list of lines, without newlines at the end."
  12144. (let* ((words (org-split-string string "[ \t\n]+"))
  12145. (maxword (apply 'max (mapcar 'org-string-width words)))
  12146. w ll)
  12147. (cond (width
  12148. (org-do-wrap words (max maxword width)))
  12149. (lines
  12150. (setq w maxword)
  12151. (setq ll (org-do-wrap words maxword))
  12152. (if (<= (length ll) lines)
  12153. ll
  12154. (setq ll words)
  12155. (while (> (length ll) lines)
  12156. (setq w (1+ w))
  12157. (setq ll (org-do-wrap words w)))
  12158. ll))
  12159. (t (error "Cannot wrap this")))))
  12160. (defun org-do-wrap (words width)
  12161. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  12162. (let (lines line)
  12163. (while words
  12164. (setq line (pop words))
  12165. (while (and words (< (+ (length line) (length (car words))) width))
  12166. (setq line (concat line " " (pop words))))
  12167. (setq lines (push line lines)))
  12168. (nreverse lines)))
  12169. (defun org-split-string (string &optional separators)
  12170. "Splits STRING into substrings at SEPARATORS.
  12171. No empty strings are returned if there are matches at the beginning
  12172. and end of string."
  12173. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  12174. (start 0)
  12175. notfirst
  12176. (list nil))
  12177. (while (and (string-match rexp string
  12178. (if (and notfirst
  12179. (= start (match-beginning 0))
  12180. (< start (length string)))
  12181. (1+ start) start))
  12182. (< (match-beginning 0) (length string)))
  12183. (setq notfirst t)
  12184. (or (eq (match-beginning 0) 0)
  12185. (and (eq (match-beginning 0) (match-end 0))
  12186. (eq (match-beginning 0) start))
  12187. (setq list
  12188. (cons (substring string start (match-beginning 0))
  12189. list)))
  12190. (setq start (match-end 0)))
  12191. (or (eq start (length string))
  12192. (setq list
  12193. (cons (substring string start)
  12194. list)))
  12195. (nreverse list)))
  12196. (defun org-context ()
  12197. "Return a list of contexts of the current cursor position.
  12198. If several contexts apply, all are returned.
  12199. Each context entry is a list with a symbol naming the context, and
  12200. two positions indicating start and end of the context. Possible
  12201. contexts are:
  12202. :headline anywhere in a headline
  12203. :headline-stars on the leading stars in a headline
  12204. :todo-keyword on a TODO keyword (including DONE) in a headline
  12205. :tags on the TAGS in a headline
  12206. :priority on the priority cookie in a headline
  12207. :item on the first line of a plain list item
  12208. :item-bullet on the bullet/number of a plain list item
  12209. :checkbox on the checkbox in a plain list item
  12210. :table in an org-mode table
  12211. :table-special on a special filed in a table
  12212. :table-table in a table.el table
  12213. :link on a hyperlink
  12214. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  12215. :target on a <<target>>
  12216. :radio-target on a <<<radio-target>>>
  12217. :latex-fragment on a LaTeX fragment
  12218. :latex-preview on a LaTeX fragment with overlayed preview image
  12219. This function expects the position to be visible because it uses font-lock
  12220. faces as a help to recognize the following contexts: :table-special, :link,
  12221. and :keyword."
  12222. (let* ((f (get-text-property (point) 'face))
  12223. (faces (if (listp f) f (list f)))
  12224. (p (point)) clist o)
  12225. ;; First the large context
  12226. (cond
  12227. ((org-on-heading-p t)
  12228. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  12229. (when (progn
  12230. (beginning-of-line 1)
  12231. (looking-at org-todo-line-tags-regexp))
  12232. (push (org-point-in-group p 1 :headline-stars) clist)
  12233. (push (org-point-in-group p 2 :todo-keyword) clist)
  12234. (push (org-point-in-group p 4 :tags) clist))
  12235. (goto-char p)
  12236. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  12237. (if (looking-at "\\[#[A-Z0-9]\\]")
  12238. (push (org-point-in-group p 0 :priority) clist)))
  12239. ((org-at-item-p)
  12240. (push (org-point-in-group p 2 :item-bullet) clist)
  12241. (push (list :item (point-at-bol)
  12242. (save-excursion (org-end-of-item) (point)))
  12243. clist)
  12244. (and (org-at-item-checkbox-p)
  12245. (push (org-point-in-group p 0 :checkbox) clist)))
  12246. ((org-at-table-p)
  12247. (push (list :table (org-table-begin) (org-table-end)) clist)
  12248. (if (memq 'org-formula faces)
  12249. (push (list :table-special
  12250. (previous-single-property-change p 'face)
  12251. (next-single-property-change p 'face)) clist)))
  12252. ((org-at-table-p 'any)
  12253. (push (list :table-table) clist)))
  12254. (goto-char p)
  12255. ;; Now the small context
  12256. (cond
  12257. ((org-at-timestamp-p)
  12258. (push (org-point-in-group p 0 :timestamp) clist))
  12259. ((memq 'org-link faces)
  12260. (push (list :link
  12261. (previous-single-property-change p 'face)
  12262. (next-single-property-change p 'face)) clist))
  12263. ((memq 'org-special-keyword faces)
  12264. (push (list :keyword
  12265. (previous-single-property-change p 'face)
  12266. (next-single-property-change p 'face)) clist))
  12267. ((org-on-target-p)
  12268. (push (org-point-in-group p 0 :target) clist)
  12269. (goto-char (1- (match-beginning 0)))
  12270. (if (looking-at org-radio-target-regexp)
  12271. (push (org-point-in-group p 0 :radio-target) clist))
  12272. (goto-char p))
  12273. ((setq o (car (delq nil
  12274. (mapcar
  12275. (lambda (x)
  12276. (if (memq x org-latex-fragment-image-overlays) x))
  12277. (org-overlays-at (point))))))
  12278. (push (list :latex-fragment
  12279. (org-overlay-start o) (org-overlay-end o)) clist)
  12280. (push (list :latex-preview
  12281. (org-overlay-start o) (org-overlay-end o)) clist))
  12282. ((org-inside-LaTeX-fragment-p)
  12283. ;; FIXME: positions wrong.
  12284. (push (list :latex-fragment (point) (point)) clist)))
  12285. (setq clist (nreverse (delq nil clist)))
  12286. clist))
  12287. ;; FIXME: Compare with at-regexp-p Do we need both?
  12288. (defun org-in-regexp (re &optional nlines visually)
  12289. "Check if point is inside a match of regexp.
  12290. Normally only the current line is checked, but you can include NLINES extra
  12291. lines both before and after point into the search.
  12292. If VISUALLY is set, require that the cursor is not after the match but
  12293. really on, so that the block visually is on the match."
  12294. (catch 'exit
  12295. (let ((pos (point))
  12296. (eol (point-at-eol (+ 1 (or nlines 0))))
  12297. (inc (if visually 1 0)))
  12298. (save-excursion
  12299. (beginning-of-line (- 1 (or nlines 0)))
  12300. (while (re-search-forward re eol t)
  12301. (if (and (<= (match-beginning 0) pos)
  12302. (>= (+ inc (match-end 0)) pos))
  12303. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  12304. (defun org-at-regexp-p (regexp)
  12305. "Is point inside a match of REGEXP in the current line?"
  12306. (catch 'exit
  12307. (save-excursion
  12308. (let ((pos (point)) (end (point-at-eol)))
  12309. (beginning-of-line 1)
  12310. (while (re-search-forward regexp end t)
  12311. (if (and (<= (match-beginning 0) pos)
  12312. (>= (match-end 0) pos))
  12313. (throw 'exit t)))
  12314. nil))))
  12315. (defun org-occur-in-agenda-files (regexp &optional nlines)
  12316. "Call `multi-occur' with buffers for all agenda files."
  12317. (interactive "sOrg-files matching: \np")
  12318. (let* ((files (org-agenda-files))
  12319. (tnames (mapcar 'file-truename files))
  12320. (extra org-agenda-text-search-extra-files)
  12321. f)
  12322. (when (eq (car extra) 'agenda-archives)
  12323. (setq extra (cdr extra))
  12324. (setq files (org-add-archive-files files)))
  12325. (while (setq f (pop extra))
  12326. (unless (member (file-truename f) tnames)
  12327. (add-to-list 'files f 'append)
  12328. (add-to-list 'tnames (file-truename f) 'append)))
  12329. (multi-occur
  12330. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  12331. regexp)))
  12332. (if (boundp 'occur-mode-find-occurrence-hook)
  12333. ;; Emacs 23
  12334. (add-hook 'occur-mode-find-occurrence-hook
  12335. (lambda ()
  12336. (when (org-mode-p)
  12337. (org-reveal))))
  12338. ;; Emacs 22
  12339. (defadvice occur-mode-goto-occurrence
  12340. (after org-occur-reveal activate)
  12341. (and (org-mode-p) (org-reveal)))
  12342. (defadvice occur-mode-goto-occurrence-other-window
  12343. (after org-occur-reveal activate)
  12344. (and (org-mode-p) (org-reveal)))
  12345. (defadvice occur-mode-display-occurrence
  12346. (after org-occur-reveal activate)
  12347. (when (org-mode-p)
  12348. (let ((pos (occur-mode-find-occurrence)))
  12349. (with-current-buffer (marker-buffer pos)
  12350. (save-excursion
  12351. (goto-char pos)
  12352. (org-reveal)))))))
  12353. (defun org-uniquify (list)
  12354. "Remove duplicate elements from LIST."
  12355. (let (res)
  12356. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  12357. res))
  12358. (defun org-delete-all (elts list)
  12359. "Remove all elements in ELTS from LIST."
  12360. (while elts
  12361. (setq list (delete (pop elts) list)))
  12362. list)
  12363. (defun org-back-over-empty-lines ()
  12364. "Move backwards over witespace, to the beginning of the first empty line.
  12365. Returns the number of empty lines passed."
  12366. (let ((pos (point)))
  12367. (skip-chars-backward " \t\n\r")
  12368. (beginning-of-line 2)
  12369. (goto-char (min (point) pos))
  12370. (count-lines (point) pos)))
  12371. (defun org-skip-whitespace ()
  12372. (skip-chars-forward " \t\n\r"))
  12373. (defun org-point-in-group (point group &optional context)
  12374. "Check if POINT is in match-group GROUP.
  12375. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  12376. match. If the match group does ot exist or point is not inside it,
  12377. return nil."
  12378. (and (match-beginning group)
  12379. (>= point (match-beginning group))
  12380. (<= point (match-end group))
  12381. (if context
  12382. (list context (match-beginning group) (match-end group))
  12383. t)))
  12384. (defun org-switch-to-buffer-other-window (&rest args)
  12385. "Switch to buffer in a second window on the current frame.
  12386. In particular, do not allow pop-up frames."
  12387. (let (pop-up-frames special-display-buffer-names special-display-regexps
  12388. special-display-function)
  12389. (apply 'switch-to-buffer-other-window args)))
  12390. (defun org-combine-plists (&rest plists)
  12391. "Create a single property list from all plists in PLISTS.
  12392. The process starts by copying the first list, and then setting properties
  12393. from the other lists. Settings in the last list are the most significant
  12394. ones and overrule settings in the other lists."
  12395. (let ((rtn (copy-sequence (pop plists)))
  12396. p v ls)
  12397. (while plists
  12398. (setq ls (pop plists))
  12399. (while ls
  12400. (setq p (pop ls) v (pop ls))
  12401. (setq rtn (plist-put rtn p v))))
  12402. rtn))
  12403. (defun org-move-line-down (arg)
  12404. "Move the current line down. With prefix argument, move it past ARG lines."
  12405. (interactive "p")
  12406. (let ((col (current-column))
  12407. beg end pos)
  12408. (beginning-of-line 1) (setq beg (point))
  12409. (beginning-of-line 2) (setq end (point))
  12410. (beginning-of-line (+ 1 arg))
  12411. (setq pos (move-marker (make-marker) (point)))
  12412. (insert (delete-and-extract-region beg end))
  12413. (goto-char pos)
  12414. (org-move-to-column col)))
  12415. (defun org-move-line-up (arg)
  12416. "Move the current line up. With prefix argument, move it past ARG lines."
  12417. (interactive "p")
  12418. (let ((col (current-column))
  12419. beg end pos)
  12420. (beginning-of-line 1) (setq beg (point))
  12421. (beginning-of-line 2) (setq end (point))
  12422. (beginning-of-line (- arg))
  12423. (setq pos (move-marker (make-marker) (point)))
  12424. (insert (delete-and-extract-region beg end))
  12425. (goto-char pos)
  12426. (org-move-to-column col)))
  12427. (defun org-replace-escapes (string table)
  12428. "Replace %-escapes in STRING with values in TABLE.
  12429. TABLE is an association list with keys like \"%a\" and string values.
  12430. The sequences in STRING may contain normal field width and padding information,
  12431. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  12432. so values can contain further %-escapes if they are define later in TABLE."
  12433. (let ((case-fold-search nil)
  12434. e re rpl)
  12435. (while (setq e (pop table))
  12436. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  12437. (while (string-match re string)
  12438. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  12439. (cdr e)))
  12440. (setq string (replace-match rpl t t string))))
  12441. string))
  12442. (defun org-sublist (list start end)
  12443. "Return a section of LIST, from START to END.
  12444. Counting starts at 1."
  12445. (let (rtn (c start))
  12446. (setq list (nthcdr (1- start) list))
  12447. (while (and list (<= c end))
  12448. (push (pop list) rtn)
  12449. (setq c (1+ c)))
  12450. (nreverse rtn)))
  12451. (defun org-find-base-buffer-visiting (file)
  12452. "Like `find-buffer-visiting' but alway return the base buffer and
  12453. not an indirect buffer."
  12454. (let ((buf (find-buffer-visiting file)))
  12455. (if buf
  12456. (or (buffer-base-buffer buf) buf)
  12457. nil)))
  12458. (defun org-image-file-name-regexp ()
  12459. "Return regexp matching the file names of images."
  12460. (if (fboundp 'image-file-name-regexp)
  12461. (image-file-name-regexp)
  12462. (let ((image-file-name-extensions
  12463. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  12464. "xbm" "xpm" "pbm" "pgm" "ppm")))
  12465. (concat "\\."
  12466. (regexp-opt (nconc (mapcar 'upcase
  12467. image-file-name-extensions)
  12468. image-file-name-extensions)
  12469. t)
  12470. "\\'"))))
  12471. (defun org-file-image-p (file)
  12472. "Return non-nil if FILE is an image."
  12473. (save-match-data
  12474. (string-match (org-image-file-name-regexp) file)))
  12475. (defun org-get-cursor-date ()
  12476. "Return the date at cursor in as a time.
  12477. This works in the calendar and in the agenda, anywhere else it just
  12478. returns the current time."
  12479. (let (date day defd)
  12480. (cond
  12481. ((eq major-mode 'calendar-mode)
  12482. (setq date (calendar-cursor-to-date)
  12483. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  12484. ((eq major-mode 'org-agenda-mode)
  12485. (setq day (get-text-property (point) 'day))
  12486. (if day
  12487. (setq date (calendar-gregorian-from-absolute day)
  12488. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  12489. (nth 2 date))))))
  12490. (or defd (current-time))))
  12491. (defvar org-agenda-action-marker (make-marker)
  12492. "Marker pointing to the entry for the next agenda action.")
  12493. (defun org-mark-entry-for-agenda-action ()
  12494. "Mark the current entry as target of an agenda action.
  12495. Agenda actions are actions executed from the agenda with the key `k',
  12496. which make use of the date at the cursor."
  12497. (interactive)
  12498. (move-marker org-agenda-action-marker
  12499. (save-excursion (org-back-to-heading t) (point))
  12500. (current-buffer))
  12501. (message
  12502. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  12503. ;;; Paragraph filling stuff.
  12504. ;; We want this to be just right, so use the full arsenal.
  12505. (defun org-indent-line-function ()
  12506. "Indent line like previous, but further if previous was headline or item."
  12507. (interactive)
  12508. (let* ((pos (point))
  12509. (itemp (org-at-item-p))
  12510. column bpos bcol tpos tcol bullet btype bullet-type)
  12511. ;; Find the previous relevant line
  12512. (beginning-of-line 1)
  12513. (cond
  12514. ((looking-at "#") (setq column 0))
  12515. ((looking-at "\\*+ ") (setq column 0))
  12516. (t
  12517. (beginning-of-line 0)
  12518. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]"))
  12519. (beginning-of-line 0))
  12520. (cond
  12521. ((looking-at "\\*+[ \t]+")
  12522. (if (not org-adapt-indentation)
  12523. (setq column 0)
  12524. (goto-char (match-end 0))
  12525. (setq column (current-column))))
  12526. ((org-in-item-p)
  12527. (org-beginning-of-item)
  12528. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
  12529. (setq bpos (match-beginning 1) tpos (match-end 0)
  12530. bcol (progn (goto-char bpos) (current-column))
  12531. tcol (progn (goto-char tpos) (current-column))
  12532. bullet (match-string 1)
  12533. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  12534. (if (> tcol (+ bcol org-description-max-indent))
  12535. (setq tcol (+ bcol 5)))
  12536. (if (not itemp)
  12537. (setq column tcol)
  12538. (goto-char pos)
  12539. (beginning-of-line 1)
  12540. (if (looking-at "\\S-")
  12541. (progn
  12542. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  12543. (setq bullet (match-string 1)
  12544. btype (if (string-match "[0-9]" bullet) "n" bullet))
  12545. (setq column (if (equal btype bullet-type) bcol tcol)))
  12546. (setq column (org-get-indentation)))))
  12547. (t (setq column (org-get-indentation))))))
  12548. (goto-char pos)
  12549. (if (<= (current-column) (current-indentation))
  12550. (org-indent-line-to column)
  12551. (save-excursion (org-indent-line-to column)))
  12552. (setq column (current-column))
  12553. (beginning-of-line 1)
  12554. (if (looking-at
  12555. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  12556. (replace-match (concat "\\1" (format org-property-format
  12557. (match-string 2) (match-string 3)))
  12558. t nil))
  12559. (org-move-to-column column)))
  12560. (defun org-set-autofill-regexps ()
  12561. (interactive)
  12562. ;; In the paragraph separator we include headlines, because filling
  12563. ;; text in a line directly attached to a headline would otherwise
  12564. ;; fill the headline as well.
  12565. (org-set-local 'comment-start-skip "^#+[ \t]*")
  12566. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  12567. ;; The paragraph starter includes hand-formatted lists.
  12568. (org-set-local 'paragraph-start
  12569. "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  12570. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  12571. ;; But only if the user has not turned off tables or fixed-width regions
  12572. (org-set-local
  12573. 'auto-fill-inhibit-regexp
  12574. (concat "\\*+ \\|#\\+"
  12575. "\\|[ \t]*" org-keyword-time-regexp
  12576. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  12577. (concat
  12578. "\\|[ \t]*["
  12579. (if org-enable-table-editor "|" "")
  12580. (if org-enable-fixed-width-editor ":" "")
  12581. "]"))))
  12582. ;; We use our own fill-paragraph function, to make sure that tables
  12583. ;; and fixed-width regions are not wrapped. That function will pass
  12584. ;; through to `fill-paragraph' when appropriate.
  12585. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  12586. ; Adaptive filling: To get full control, first make sure that
  12587. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  12588. (org-set-local 'adaptive-fill-regexp "\000")
  12589. (org-set-local 'adaptive-fill-function
  12590. 'org-adaptive-fill-function)
  12591. (org-set-local
  12592. 'align-mode-rules-list
  12593. '((org-in-buffer-settings
  12594. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  12595. (modes . '(org-mode))))))
  12596. (defun org-fill-paragraph (&optional justify)
  12597. "Re-align a table, pass through to fill-paragraph if no table."
  12598. (let ((table-p (org-at-table-p))
  12599. (table.el-p (org-at-table.el-p)))
  12600. (cond ((and (equal (char-after (point-at-bol)) ?*)
  12601. (save-excursion (goto-char (point-at-bol))
  12602. (looking-at outline-regexp)))
  12603. t) ; skip headlines
  12604. (table.el-p t) ; skip table.el tables
  12605. (table-p (org-table-align) t) ; align org-mode tables
  12606. (t nil)))) ; call paragraph-fill
  12607. ;; For reference, this is the default value of adaptive-fill-regexp
  12608. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  12609. (defun org-adaptive-fill-function ()
  12610. "Return a fill prefix for org-mode files.
  12611. In particular, this makes sure hanging paragraphs for hand-formatted lists
  12612. work correctly."
  12613. (cond ((looking-at "#[ \t]+")
  12614. (match-string 0))
  12615. ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
  12616. (save-excursion
  12617. (if (> (match-end 1) (+ (match-beginning 1)
  12618. org-description-max-indent))
  12619. (goto-char (+ (match-beginning 1) 5))
  12620. (goto-char (match-end 0)))
  12621. (make-string (current-column) ?\ )))
  12622. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] \\)?")
  12623. (save-excursion
  12624. (goto-char (match-end 0))
  12625. (make-string (current-column) ?\ )))
  12626. (t nil)))
  12627. ;;; Other stuff.
  12628. (defun org-toggle-fixed-width-section (arg)
  12629. "Toggle the fixed-width export.
  12630. If there is no active region, the QUOTE keyword at the current headline is
  12631. inserted or removed. When present, it causes the text between this headline
  12632. and the next to be exported as fixed-width text, and unmodified.
  12633. If there is an active region, this command adds or removes a colon as the
  12634. first character of this line. If the first character of a line is a colon,
  12635. this line is also exported in fixed-width font."
  12636. (interactive "P")
  12637. (let* ((cc 0)
  12638. (regionp (org-region-active-p))
  12639. (beg (if regionp (region-beginning) (point)))
  12640. (end (if regionp (region-end)))
  12641. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  12642. (case-fold-search nil)
  12643. (re "[ \t]*\\(:\\)")
  12644. off)
  12645. (if regionp
  12646. (save-excursion
  12647. (goto-char beg)
  12648. (setq cc (current-column))
  12649. (beginning-of-line 1)
  12650. (setq off (looking-at re))
  12651. (while (> nlines 0)
  12652. (setq nlines (1- nlines))
  12653. (beginning-of-line 1)
  12654. (cond
  12655. (arg
  12656. (org-move-to-column cc t)
  12657. (insert ":\n")
  12658. (forward-line -1))
  12659. ((and off (looking-at re))
  12660. (replace-match "" t t nil 1))
  12661. ((not off) (org-move-to-column cc t) (insert ":")))
  12662. (forward-line 1)))
  12663. (save-excursion
  12664. (org-back-to-heading)
  12665. (if (looking-at (concat outline-regexp
  12666. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  12667. (replace-match "" t t nil 1)
  12668. (if (looking-at outline-regexp)
  12669. (progn
  12670. (goto-char (match-end 0))
  12671. (insert org-quote-string " "))))))))
  12672. ;;;; Functions extending outline functionality
  12673. (defun org-beginning-of-line (&optional arg)
  12674. "Go to the beginning of the current line. If that is invisible, continue
  12675. to a visible line beginning. This makes the function of C-a more intuitive.
  12676. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  12677. first attempt, and only move to after the tags when the cursor is already
  12678. beyond the end of the headline."
  12679. (interactive "P")
  12680. (let ((pos (point)) refpos)
  12681. (beginning-of-line 1)
  12682. (if (bobp)
  12683. nil
  12684. (backward-char 1)
  12685. (if (org-invisible-p)
  12686. (while (and (not (bobp)) (org-invisible-p))
  12687. (backward-char 1)
  12688. (beginning-of-line 1))
  12689. (forward-char 1)))
  12690. (when org-special-ctrl-a/e
  12691. (cond
  12692. ((and (looking-at org-complex-heading-regexp)
  12693. (= (char-after (match-end 1)) ?\ ))
  12694. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  12695. (point-at-eol)))
  12696. (goto-char
  12697. (if (eq org-special-ctrl-a/e t)
  12698. (cond ((> pos refpos) refpos)
  12699. ((= pos (point)) refpos)
  12700. (t (point)))
  12701. (cond ((> pos (point)) (point))
  12702. ((not (eq last-command this-command)) (point))
  12703. (t refpos)))))
  12704. ((org-at-item-p)
  12705. (goto-char
  12706. (if (eq org-special-ctrl-a/e t)
  12707. (cond ((> pos (match-end 4)) (match-end 4))
  12708. ((= pos (point)) (match-end 4))
  12709. (t (point)))
  12710. (cond ((> pos (point)) (point))
  12711. ((not (eq last-command this-command)) (point))
  12712. (t (match-end 4))))))))
  12713. (org-no-warnings
  12714. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  12715. (defun org-end-of-line (&optional arg)
  12716. "Go to the end of the line.
  12717. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  12718. first attempt, and only move to after the tags when the cursor is already
  12719. beyond the end of the headline."
  12720. (interactive "P")
  12721. (if (or (not org-special-ctrl-a/e)
  12722. (not (org-on-heading-p)))
  12723. (end-of-line arg)
  12724. (let ((pos (point)))
  12725. (beginning-of-line 1)
  12726. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  12727. (if (eq org-special-ctrl-a/e t)
  12728. (if (or (< pos (match-beginning 1))
  12729. (= pos (match-end 0)))
  12730. (goto-char (match-beginning 1))
  12731. (goto-char (match-end 0)))
  12732. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  12733. (goto-char (match-end 0))
  12734. (goto-char (match-beginning 1))))
  12735. (end-of-line arg))))
  12736. (org-no-warnings
  12737. (and (featurep 'xemacs) (setq zmacs-region-stays t))))
  12738. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  12739. (define-key org-mode-map "\C-e" 'org-end-of-line)
  12740. (defun org-kill-line (&optional arg)
  12741. "Kill line, to tags or end of line."
  12742. (interactive "P")
  12743. (cond
  12744. ((or (not org-special-ctrl-k)
  12745. (bolp)
  12746. (not (org-on-heading-p)))
  12747. (call-interactively 'kill-line))
  12748. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  12749. (kill-region (point) (match-beginning 1))
  12750. (org-set-tags nil t))
  12751. (t (kill-region (point) (point-at-eol)))))
  12752. (define-key org-mode-map "\C-k" 'org-kill-line)
  12753. (defun org-yank ()
  12754. "Yank. If the kill is a subtree, treat it specially.
  12755. This command will look at the current kill and check it is a single
  12756. subtree, or a series of subtrees[1]. If it passes the test, it is
  12757. treated specially, depending on the value of the following variables, both
  12758. set by default.
  12759. org-yank-folded-subtrees
  12760. When set, the subree(s) wiil be folded after insertion.
  12761. org-yank-adjusted-subtrees
  12762. When set, the subtree will be promoted or demoted in order to
  12763. fit into the local outline tree structure.
  12764. \[1] Basically, the test checks if the first non-white line is a heading
  12765. and if there are no other headings with fewer stars."
  12766. (interactive)
  12767. (let ((subtreep (org-kill-is-subtree-p)))
  12768. (if org-yank-folded-subtrees
  12769. (let ((beg (point))
  12770. end)
  12771. (if (and subtreep org-yank-adjusted-subtrees)
  12772. (org-paste-subtree nil nil 'for-yank)
  12773. (call-interactively 'yank))
  12774. (setq end (point))
  12775. (goto-char beg)
  12776. (when (and (bolp) subtreep)
  12777. (or (looking-at outline-regexp)
  12778. (re-search-forward (concat "^" outline-regexp) end t))
  12779. (while (and (< (point) end) (looking-at outline-regexp))
  12780. (hide-subtree)
  12781. (org-cycle-show-empty-lines 'folded)
  12782. (condition-case nil
  12783. (outline-forward-same-level 1)
  12784. (error (goto-char end)))))
  12785. (goto-char end)
  12786. (skip-chars-forward " \t\n\r"))
  12787. (if (and subtreep org-yank-adjusted-subtrees)
  12788. (org-paste-subtree nil nil 'for-yank)
  12789. (call-interactively 'yank)))))
  12790. (define-key org-mode-map "\C-y" 'org-yank)
  12791. (defun org-invisible-p ()
  12792. "Check if point is at a character currently not visible."
  12793. ;; Early versions of noutline don't have `outline-invisible-p'.
  12794. (if (fboundp 'outline-invisible-p)
  12795. (outline-invisible-p)
  12796. (get-char-property (point) 'invisible)))
  12797. (defun org-invisible-p2 ()
  12798. "Check if point is at a character currently not visible."
  12799. (save-excursion
  12800. (if (and (eolp) (not (bobp))) (backward-char 1))
  12801. ;; Early versions of noutline don't have `outline-invisible-p'.
  12802. (if (fboundp 'outline-invisible-p)
  12803. (outline-invisible-p)
  12804. (get-char-property (point) 'invisible))))
  12805. (defalias 'org-back-to-heading 'outline-back-to-heading)
  12806. (defalias 'org-on-heading-p 'outline-on-heading-p)
  12807. (defalias 'org-at-heading-p 'outline-on-heading-p)
  12808. (defun org-at-heading-or-item-p ()
  12809. (or (org-on-heading-p) (org-at-item-p)))
  12810. (defun org-on-target-p ()
  12811. (or (org-in-regexp org-radio-target-regexp)
  12812. (org-in-regexp org-target-regexp)))
  12813. (defun org-up-heading-all (arg)
  12814. "Move to the heading line of which the present line is a subheading.
  12815. This function considers both visible and invisible heading lines.
  12816. With argument, move up ARG levels."
  12817. (if (fboundp 'outline-up-heading-all)
  12818. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  12819. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  12820. (defun org-up-heading-safe ()
  12821. "Move to the heading line of which the present line is a subheading.
  12822. This version will not throw an error. It will return the level of the
  12823. headline found, or nil if no higher level is found."
  12824. (let ((pos (point)) start-level level
  12825. (re (concat "^" outline-regexp)))
  12826. (catch 'exit
  12827. (outline-back-to-heading t)
  12828. (setq start-level (funcall outline-level))
  12829. (if (equal start-level 1) (throw 'exit nil))
  12830. (while (re-search-backward re nil t)
  12831. (setq level (funcall outline-level))
  12832. (if (< level start-level) (throw 'exit level)))
  12833. nil)))
  12834. (defun org-first-sibling-p ()
  12835. "Is this heading the first child of its parents?"
  12836. (interactive)
  12837. (let ((re (concat "^" outline-regexp))
  12838. level l)
  12839. (unless (org-at-heading-p t)
  12840. (error "Not at a heading"))
  12841. (setq level (funcall outline-level))
  12842. (save-excursion
  12843. (if (not (re-search-backward re nil t))
  12844. t
  12845. (setq l (funcall outline-level))
  12846. (< l level)))))
  12847. (defun org-goto-sibling (&optional previous)
  12848. "Goto the next sibling, even if it is invisible.
  12849. When PREVIOUS is set, go to the previous sibling instead. Returns t
  12850. when a sibling was found. When none is found, return nil and don't
  12851. move point."
  12852. (let ((fun (if previous 're-search-backward 're-search-forward))
  12853. (pos (point))
  12854. (re (concat "^" outline-regexp))
  12855. level l)
  12856. (when (condition-case nil (org-back-to-heading t) (error nil))
  12857. (setq level (funcall outline-level))
  12858. (catch 'exit
  12859. (or previous (forward-char 1))
  12860. (while (funcall fun re nil t)
  12861. (setq l (funcall outline-level))
  12862. (when (< l level) (goto-char pos) (throw 'exit nil))
  12863. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  12864. (goto-char pos)
  12865. nil))))
  12866. (defun org-show-siblings ()
  12867. "Show all siblings of the current headline."
  12868. (save-excursion
  12869. (while (org-goto-sibling) (org-flag-heading nil)))
  12870. (save-excursion
  12871. (while (org-goto-sibling 'previous)
  12872. (org-flag-heading nil))))
  12873. (defun org-show-hidden-entry ()
  12874. "Show an entry where even the heading is hidden."
  12875. (save-excursion
  12876. (org-show-entry)))
  12877. (defun org-flag-heading (flag &optional entry)
  12878. "Flag the current heading. FLAG non-nil means make invisible.
  12879. When ENTRY is non-nil, show the entire entry."
  12880. (save-excursion
  12881. (org-back-to-heading t)
  12882. ;; Check if we should show the entire entry
  12883. (if entry
  12884. (progn
  12885. (org-show-entry)
  12886. (save-excursion
  12887. (and (outline-next-heading)
  12888. (org-flag-heading nil))))
  12889. (outline-flag-region (max (point-min) (1- (point)))
  12890. (save-excursion (outline-end-of-heading) (point))
  12891. flag))))
  12892. (defun org-forward-same-level (arg)
  12893. "Move forward to the ARG'th subheading at same level as this one.
  12894. Stop at the first and last subheadings of a superior heading.
  12895. This is like outline-forward-same-level, but invisible headings are ok."
  12896. (interactive "p")
  12897. (outline-back-to-heading t)
  12898. (while (> arg 0)
  12899. (let ((point-to-move-to (save-excursion
  12900. (org-get-next-sibling))))
  12901. (if point-to-move-to
  12902. (progn
  12903. (goto-char point-to-move-to)
  12904. (setq arg (1- arg)))
  12905. (progn
  12906. (setq arg 0)
  12907. (error "No following same-level heading"))))))
  12908. (defun org-get-next-sibling ()
  12909. "Move to next heading of the same level, and return point.
  12910. If there is no such heading, return nil.
  12911. This is like outline-next-sibling, but invisible headings are ok."
  12912. (let ((level (funcall outline-level)))
  12913. (outline-next-heading)
  12914. (while (and (not (eobp)) (> (funcall outline-level) level))
  12915. (outline-next-heading))
  12916. (if (or (eobp) (< (funcall outline-level) level))
  12917. nil
  12918. (point))))
  12919. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  12920. ;; This is an exact copy of the original function, but it uses
  12921. ;; `org-back-to-heading', to make it work also in invisible
  12922. ;; trees. And is uses an invisible-OK argument.
  12923. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  12924. (org-back-to-heading invisible-OK)
  12925. (let ((first t)
  12926. (level (funcall outline-level)))
  12927. (while (and (not (eobp))
  12928. (or first (> (funcall outline-level) level)))
  12929. (setq first nil)
  12930. (outline-next-heading))
  12931. (unless to-heading
  12932. (if (memq (preceding-char) '(?\n ?\^M))
  12933. (progn
  12934. ;; Go to end of line before heading
  12935. (forward-char -1)
  12936. (if (memq (preceding-char) '(?\n ?\^M))
  12937. ;; leave blank line before heading
  12938. (forward-char -1))))))
  12939. (point))
  12940. (defun org-show-subtree ()
  12941. "Show everything after this heading at deeper levels."
  12942. (outline-flag-region
  12943. (point)
  12944. (save-excursion
  12945. (outline-end-of-subtree) (outline-next-heading) (point))
  12946. nil))
  12947. (defun org-show-entry ()
  12948. "Show the body directly following this heading.
  12949. Show the heading too, if it is currently invisible."
  12950. (interactive)
  12951. (save-excursion
  12952. (condition-case nil
  12953. (progn
  12954. (org-back-to-heading t)
  12955. (outline-flag-region
  12956. (max (point-min) (1- (point)))
  12957. (save-excursion
  12958. (re-search-forward
  12959. (concat "[\r\n]\\(" outline-regexp "\\)") nil 'move)
  12960. (or (match-beginning 1) (point-max)))
  12961. nil))
  12962. (error nil))))
  12963. (defun org-make-options-regexp (kwds)
  12964. "Make a regular expression for keyword lines."
  12965. (concat
  12966. "^"
  12967. "#?[ \t]*\\+\\("
  12968. (mapconcat 'regexp-quote kwds "\\|")
  12969. "\\):[ \t]*"
  12970. "\\(.+\\)"))
  12971. ;; Make isearch reveal the necessary context
  12972. (defun org-isearch-end ()
  12973. "Reveal context after isearch exits."
  12974. (when isearch-success ; only if search was successful
  12975. (if (featurep 'xemacs)
  12976. ;; Under XEmacs, the hook is run in the correct place,
  12977. ;; we directly show the context.
  12978. (org-show-context 'isearch)
  12979. ;; In Emacs the hook runs *before* restoring the overlays.
  12980. ;; So we have to use a one-time post-command-hook to do this.
  12981. ;; (Emacs 22 has a special variable, see function `org-mode')
  12982. (unless (and (boundp 'isearch-mode-end-hook-quit)
  12983. isearch-mode-end-hook-quit)
  12984. ;; Only when the isearch was not quitted.
  12985. (org-add-hook 'post-command-hook 'org-isearch-post-command
  12986. 'append 'local)))))
  12987. (defun org-isearch-post-command ()
  12988. "Remove self from hook, and show context."
  12989. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  12990. (org-show-context 'isearch))
  12991. ;;;; Integration with and fixes for other packages
  12992. ;;; Imenu support
  12993. (defvar org-imenu-markers nil
  12994. "All markers currently used by Imenu.")
  12995. (make-variable-buffer-local 'org-imenu-markers)
  12996. (defun org-imenu-new-marker (&optional pos)
  12997. "Return a new marker for use by Imenu, and remember the marker."
  12998. (let ((m (make-marker)))
  12999. (move-marker m (or pos (point)))
  13000. (push m org-imenu-markers)
  13001. m))
  13002. (defun org-imenu-get-tree ()
  13003. "Produce the index for Imenu."
  13004. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  13005. (setq org-imenu-markers nil)
  13006. (let* ((n org-imenu-depth)
  13007. (re (concat "^" outline-regexp))
  13008. (subs (make-vector (1+ n) nil))
  13009. (last-level 0)
  13010. m tree level head)
  13011. (save-excursion
  13012. (save-restriction
  13013. (widen)
  13014. (goto-char (point-max))
  13015. (while (re-search-backward re nil t)
  13016. (setq level (org-reduced-level (funcall outline-level)))
  13017. (when (<= level n)
  13018. (looking-at org-complex-heading-regexp)
  13019. (setq head (org-link-display-format
  13020. (org-match-string-no-properties 4))
  13021. m (org-imenu-new-marker))
  13022. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  13023. (if (>= level last-level)
  13024. (push (cons head m) (aref subs level))
  13025. (push (cons head (aref subs (1+ level))) (aref subs level))
  13026. (loop for i from (1+ level) to n do (aset subs i nil)))
  13027. (setq last-level level)))))
  13028. (aref subs 1)))
  13029. (eval-after-load "imenu"
  13030. '(progn
  13031. (add-hook 'imenu-after-jump-hook
  13032. (lambda ()
  13033. (if (eq major-mode 'org-mode)
  13034. (org-show-context 'org-goto))))))
  13035. (defun org-link-display-format (link)
  13036. "Replace a link with either the description, or the link target
  13037. if no description is present"
  13038. (save-match-data
  13039. (if (string-match org-bracket-link-analytic-regexp link)
  13040. (replace-match (or (match-string 5 link)
  13041. (concat (match-string 1 link)
  13042. (match-string 3 link)))
  13043. nil nil link)
  13044. link)))
  13045. ;; Speedbar support
  13046. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  13047. "Overlay marking the agenda restriction line in speedbar.")
  13048. (org-overlay-put org-speedbar-restriction-lock-overlay
  13049. 'face 'org-agenda-restriction-lock)
  13050. (org-overlay-put org-speedbar-restriction-lock-overlay
  13051. 'help-echo "Agendas are currently limited to this item.")
  13052. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  13053. (defun org-speedbar-set-agenda-restriction ()
  13054. "Restrict future agenda commands to the location at point in speedbar.
  13055. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  13056. (interactive)
  13057. (require 'org-agenda)
  13058. (let (p m tp np dir txt w)
  13059. (cond
  13060. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  13061. 'org-imenu t))
  13062. (setq m (get-text-property p 'org-imenu-marker))
  13063. (save-excursion
  13064. (save-restriction
  13065. (set-buffer (marker-buffer m))
  13066. (goto-char m)
  13067. (org-agenda-set-restriction-lock 'subtree))))
  13068. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  13069. 'speedbar-function 'speedbar-find-file))
  13070. (setq tp (previous-single-property-change
  13071. (1+ p) 'speedbar-function)
  13072. np (next-single-property-change
  13073. tp 'speedbar-function)
  13074. dir (speedbar-line-directory)
  13075. txt (buffer-substring-no-properties (or tp (point-min))
  13076. (or np (point-max))))
  13077. (save-excursion
  13078. (save-restriction
  13079. (set-buffer (find-file-noselect
  13080. (let ((default-directory dir))
  13081. (expand-file-name txt))))
  13082. (unless (org-mode-p)
  13083. (error "Cannot restrict to non-Org-mode file"))
  13084. (org-agenda-set-restriction-lock 'file))))
  13085. (t (error "Don't know how to restrict Org-mode's agenda")))
  13086. (org-move-overlay org-speedbar-restriction-lock-overlay
  13087. (point-at-bol) (point-at-eol))
  13088. (setq current-prefix-arg nil)
  13089. (org-agenda-maybe-redo)))
  13090. (eval-after-load "speedbar"
  13091. '(progn
  13092. (speedbar-add-supported-extension ".org")
  13093. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  13094. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  13095. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  13096. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  13097. (add-hook 'speedbar-visiting-tag-hook
  13098. (lambda () (org-show-context 'org-goto)))))
  13099. ;;; Fixes and Hacks for problems with other packages
  13100. ;; Make flyspell not check words in links, to not mess up our keymap
  13101. (defun org-mode-flyspell-verify ()
  13102. "Don't let flyspell put overlays at active buttons."
  13103. (not (get-text-property (point) 'keymap)))
  13104. ;; Make `bookmark-jump' show the jump location if it was hidden.
  13105. (eval-after-load "bookmark"
  13106. '(if (boundp 'bookmark-after-jump-hook)
  13107. ;; We can use the hook
  13108. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  13109. ;; Hook not available, use advice
  13110. (defadvice bookmark-jump (after org-make-visible activate)
  13111. "Make the position visible."
  13112. (org-bookmark-jump-unhide))))
  13113. ;; Make sure saveplace show the location if it was hidden
  13114. (eval-after-load "saveplace"
  13115. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  13116. "Make the position visible."
  13117. (org-bookmark-jump-unhide)))
  13118. (defun org-bookmark-jump-unhide ()
  13119. "Unhide the current position, to show the bookmark location."
  13120. (and (org-mode-p)
  13121. (or (org-invisible-p)
  13122. (save-excursion (goto-char (max (point-min) (1- (point))))
  13123. (org-invisible-p)))
  13124. (org-show-context 'bookmark-jump)))
  13125. ;; Make session.el ignore our circular variable
  13126. (eval-after-load "session"
  13127. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  13128. ;;;; Experimental code
  13129. (defun org-closed-in-range ()
  13130. "Sparse tree of items closed in a certain time range.
  13131. Still experimental, may disappear in the future."
  13132. (interactive)
  13133. ;; Get the time interval from the user.
  13134. (let* ((time1 (time-to-seconds
  13135. (org-read-date nil 'to-time nil "Starting date: ")))
  13136. (time2 (time-to-seconds
  13137. (org-read-date nil 'to-time nil "End date:")))
  13138. ;; callback function
  13139. (callback (lambda ()
  13140. (let ((time
  13141. (time-to-seconds
  13142. (apply 'encode-time
  13143. (org-parse-time-string
  13144. (match-string 1))))))
  13145. ;; check if time in interval
  13146. (and (>= time time1) (<= time time2))))))
  13147. ;; make tree, check each match with the callback
  13148. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  13149. ;;;; Finish up
  13150. (provide 'org)
  13151. (run-hooks 'org-load-hook)
  13152. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  13153. ;;; org.el ends here