org.el 505 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754
  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.02b
  9. ;;
  10. ;; This file is part of GNU Emacs.
  11. ;;
  12. ;; GNU Emacs is free software; you can redistribute it and/or modify
  13. ;; it under the terms of the GNU General Public License as published by
  14. ;; the Free Software Foundation; either version 3, or (at your option)
  15. ;; any later version.
  16. ;; GNU Emacs is distributed in the hope that it will be useful,
  17. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. ;; GNU General Public License for more details.
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs; see the file COPYING. If not, write to the
  22. ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  23. ;; Boston, MA 02110-1301, USA.
  24. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  25. ;;
  26. ;;; Commentary:
  27. ;;
  28. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  29. ;; project planning with a fast and effective plain-text system.
  30. ;;
  31. ;; Org-mode develops organizational tasks around NOTES files that contain
  32. ;; information about projects as plain text. Org-mode is implemented on
  33. ;; top of outline-mode, which makes it possible to keep the content of
  34. ;; large files well structured. Visibility cycling and structure editing
  35. ;; help to work with the tree. Tables are easily created with a built-in
  36. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  37. ;; and scheduling. It dynamically compiles entries into an agenda that
  38. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  39. ;; Plain text URL-like links connect to websites, emails, Usenet
  40. ;; messages, BBDB entries, and any files related to the projects. For
  41. ;; printing and sharing of notes, an Org-mode file can be exported as a
  42. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  43. ;; iCalendar file. It can also serve as a publishing tool for a set of
  44. ;; linked webpages.
  45. ;;
  46. ;; Installation and Activation
  47. ;; ---------------------------
  48. ;; See the corresponding sections in the manual at
  49. ;;
  50. ;; http://orgmode.org/org.html#Installation
  51. ;;
  52. ;; Documentation
  53. ;; -------------
  54. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  55. ;; distribution also contains a PDF version of it. At the homepage of
  56. ;; Org-mode, you can read the same text online as HTML. There is also an
  57. ;; excellent reference card made by Philip Rooke. This card can be found
  58. ;; in the etc/ directory of Emacs 22.
  59. ;;
  60. ;; A list of recent changes can be found at
  61. ;; http://orgmode.org/Changes.html
  62. ;;
  63. ;;; Code:
  64. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  65. (defvar org-table-formula-constants-local nil
  66. "Local version of `org-table-formula-constants'.")
  67. (make-variable-buffer-local 'org-table-formula-constants-local)
  68. ;;;; Require other packages
  69. (eval-when-compile
  70. (require 'cl)
  71. (require 'gnus-sum)
  72. (require 'calendar))
  73. ;; For XEmacs, noutline is not yet provided by outline.el, so arrange for
  74. ;; the file noutline.el being loaded.
  75. (if (featurep 'xemacs) (condition-case nil (require 'noutline)))
  76. ;; We require noutline, which might be provided in outline.el
  77. (require 'outline) (require 'noutline)
  78. ;; Other stuff we need.
  79. (require 'time-date)
  80. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  81. (require 'easymenu)
  82. (require 'org-macs)
  83. (require 'org-compat)
  84. (require 'org-faces)
  85. ;;;; Customization variables
  86. ;;; Version
  87. (defconst org-version "6.02b"
  88. "The version number of the file org.el.")
  89. (defun org-version (&optional here)
  90. "Show the org-mode version in the echo area.
  91. With prefix arg HERE, insert it at point."
  92. (interactive "P")
  93. (let ((version (format "Org-mode version %s" org-version)))
  94. (message version)
  95. (if here
  96. (insert version))))
  97. ;;; Compatibility constants
  98. ;;; The custom variables
  99. (defgroup org nil
  100. "Outline-based notes management and organizer."
  101. :tag "Org"
  102. :group 'outlines
  103. :group 'hypermedia
  104. :group 'calendar)
  105. (defcustom org-load-hook nil
  106. "Hook that is run after org.el has been loaded."
  107. :group 'org
  108. :type 'hook)
  109. (defvar org-modules) ; defined below
  110. (defvar org-modules-loaded nil
  111. "Have the modules been loaded already?")
  112. (defun org-load-modules-maybe (&optional force)
  113. "Load all extensions listed in `org-default-extensions'."
  114. (when (or force (not org-modules-loaded))
  115. (mapc (lambda (ext)
  116. (condition-case nil (require ext)
  117. (error (message "Problems while trying to load feature `%s'" ext))))
  118. org-modules)
  119. (setq org-modules-loaded t)))
  120. (defun org-set-modules (var value)
  121. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  122. (set var value)
  123. (when (featurep 'org)
  124. (org-load-modules-maybe 'force)))
  125. (defcustom org-modules '(org-bbdb org-bibtex org-gnus org-info org-infojs org-irc org-mew org-mhe org-rmail org-vm org-wl)
  126. "Modules that should always be loaded together with org.el.
  127. If a description starts with <C>, the file is not part of emacs
  128. and loading it will require that you have downloaded and properly installed
  129. the org-mode distribution.
  130. You can also use this system to load external packages (i.e. neither Org
  131. core modules, not modules from the CONTRIB directory). Just add symbols
  132. to the end of the list. If the package is called org-xyz.e, then you need
  133. to add the symbol `xyz', and the package must have a call to
  134. (provide 'org-xyz)"
  135. :group 'org
  136. :set 'org-set-modules
  137. :type
  138. '(set :greedy t
  139. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  140. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  141. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  142. (const :tag " info: Links to Info nodes" org-info)
  143. (const :tag " infojs: Set up Sebastian Rose's JavaScript org-info.js" org-infojs)
  144. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  145. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  146. (const :tag " mew Links to Mew folders/messages" org-mew)
  147. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  148. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  149. (const :tag " vm: Links to VM folders/messages" org-vm)
  150. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  151. (const :tag " mouse: Additional mouse support" org-mouse)
  152. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  153. (const :tag "C bookmark: Org links to bookmarks" org-bookmark)
  154. (const :tag "C depend: TODO dependencies for Org-mode" org-depend)
  155. (const :tag "C elisp-symbol: Org links to emacs-lisp symbols" org-elisp-symbol)
  156. (const :tag "C eval: Include command output as text" org-eval)
  157. (const :tag "C expiry: Expiry mechanism for Org entries" org-expiry)
  158. (const :tag "C id: Global id's for identifying entries" org-id)
  159. (const :tag "C interactive-query: Interactive modification of tags query" org-interactive-query)
  160. (const :tag "C mairix: Hook mairix search into Org for different MUAs" org-mairix)
  161. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  162. (const :tag "C mew: Support for links to messages in Mew" org-mew)
  163. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  164. (const :tag "C registry: A registry for Org links" org-registry)
  165. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  166. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  167. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  168. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  169. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  170. (defgroup org-startup nil
  171. "Options concerning startup of Org-mode."
  172. :tag "Org Startup"
  173. :group 'org)
  174. (defcustom org-startup-folded t
  175. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  176. This can also be configured on a per-file basis by adding one of
  177. the following lines anywhere in the buffer:
  178. #+STARTUP: fold
  179. #+STARTUP: nofold
  180. #+STARTUP: content"
  181. :group 'org-startup
  182. :type '(choice
  183. (const :tag "nofold: show all" nil)
  184. (const :tag "fold: overview" t)
  185. (const :tag "content: all headlines" content)))
  186. (defcustom org-startup-truncated t
  187. "Non-nil means, entering Org-mode will set `truncate-lines'.
  188. This is useful since some lines containing links can be very long and
  189. uninteresting. Also tables look terrible when wrapped."
  190. :group 'org-startup
  191. :type 'boolean)
  192. (defcustom org-startup-align-all-tables nil
  193. "Non-nil means, align all tables when visiting a file.
  194. This is useful when the column width in tables is forced with <N> cookies
  195. in table fields. Such tables will look correct only after the first re-align.
  196. This can also be configured on a per-file basis by adding one of
  197. the following lines anywhere in the buffer:
  198. #+STARTUP: align
  199. #+STARTUP: noalign"
  200. :group 'org-startup
  201. :type 'boolean)
  202. (defcustom org-insert-mode-line-in-empty-file nil
  203. "Non-nil means insert the first line setting Org-mode in empty files.
  204. When the function `org-mode' is called interactively in an empty file, this
  205. normally means that the file name does not automatically trigger Org-mode.
  206. To ensure that the file will always be in Org-mode in the future, a
  207. line enforcing Org-mode will be inserted into the buffer, if this option
  208. has been set."
  209. :group 'org-startup
  210. :type 'boolean)
  211. (defcustom org-replace-disputed-keys nil
  212. "Non-nil means use alternative key bindings for some keys.
  213. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  214. These keys are also used by other packages like `CUA-mode' or `windmove.el'.
  215. If you want to use Org-mode together with one of these other modes,
  216. or more generally if you would like to move some Org-mode commands to
  217. other keys, set this variable and configure the keys with the variable
  218. `org-disputed-keys'.
  219. This option is only relevant at load-time of Org-mode, and must be set
  220. *before* org.el is loaded. Changing it requires a restart of Emacs to
  221. become effective."
  222. :group 'org-startup
  223. :type 'boolean)
  224. (if (fboundp 'defvaralias)
  225. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  226. (defcustom org-disputed-keys
  227. '(([(shift up)] . [(meta p)])
  228. ([(shift down)] . [(meta n)])
  229. ([(shift left)] . [(meta -)])
  230. ([(shift right)] . [(meta +)])
  231. ([(control shift right)] . [(meta shift +)])
  232. ([(control shift left)] . [(meta shift -)]))
  233. "Keys for which Org-mode and other modes compete.
  234. This is an alist, cars are the default keys, second element specifies
  235. the alternative to use when `org-replace-disputed-keys' is t.
  236. Keys can be specified in any syntax supported by `define-key'.
  237. The value of this option takes effect only at Org-mode's startup,
  238. therefore you'll have to restart Emacs to apply it after changing."
  239. :group 'org-startup
  240. :type 'alist)
  241. (defun org-key (key)
  242. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  243. Or return the original if not disputed."
  244. (if org-replace-disputed-keys
  245. (let* ((nkey (key-description key))
  246. (x (org-find-if (lambda (x)
  247. (equal (key-description (car x)) nkey))
  248. org-disputed-keys)))
  249. (if x (cdr x) key))
  250. key))
  251. (defun org-find-if (predicate seq)
  252. (catch 'exit
  253. (while seq
  254. (if (funcall predicate (car seq))
  255. (throw 'exit (car seq))
  256. (pop seq)))))
  257. (defun org-defkey (keymap key def)
  258. "Define a key, possibly translated, as returned by `org-key'."
  259. (define-key keymap (org-key key) def))
  260. (defcustom org-ellipsis nil
  261. "The ellipsis to use in the Org-mode outline.
  262. When nil, just use the standard three dots. When a string, use that instead,
  263. When a face, use the standart 3 dots, but with the specified face.
  264. The change affects only Org-mode (which will then use its own display table).
  265. Changing this requires executing `M-x org-mode' in a buffer to become
  266. effective."
  267. :group 'org-startup
  268. :type '(choice (const :tag "Default" nil)
  269. (face :tag "Face" :value org-warning)
  270. (string :tag "String" :value "...#")))
  271. (defvar org-display-table nil
  272. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  273. (defgroup org-keywords nil
  274. "Keywords in Org-mode."
  275. :tag "Org Keywords"
  276. :group 'org)
  277. (defcustom org-deadline-string "DEADLINE:"
  278. "String to mark deadline entries.
  279. A deadline is this string, followed by a time stamp. Should be a word,
  280. terminated by a colon. You can insert a schedule keyword and
  281. a timestamp with \\[org-deadline].
  282. Changes become only effective after restarting Emacs."
  283. :group 'org-keywords
  284. :type 'string)
  285. (defcustom org-scheduled-string "SCHEDULED:"
  286. "String to mark scheduled TODO entries.
  287. A schedule is this string, followed by a time stamp. Should be a word,
  288. terminated by a colon. You can insert a schedule keyword and
  289. a timestamp with \\[org-schedule].
  290. Changes become only effective after restarting Emacs."
  291. :group 'org-keywords
  292. :type 'string)
  293. (defcustom org-closed-string "CLOSED:"
  294. "String used as the prefix for timestamps logging closing a TODO entry."
  295. :group 'org-keywords
  296. :type 'string)
  297. (defcustom org-clock-string "CLOCK:"
  298. "String used as prefix for timestamps clocking work hours on an item."
  299. :group 'org-keywords
  300. :type 'string)
  301. (defcustom org-comment-string "COMMENT"
  302. "Entries starting with this keyword will never be exported.
  303. An entry can be toggled between COMMENT and normal with
  304. \\[org-toggle-comment].
  305. Changes become only effective after restarting Emacs."
  306. :group 'org-keywords
  307. :type 'string)
  308. (defcustom org-quote-string "QUOTE"
  309. "Entries starting with this keyword will be exported in fixed-width font.
  310. Quoting applies only to the text in the entry following the headline, and does
  311. not extend beyond the next headline, even if that is lower level.
  312. An entry can be toggled between QUOTE and normal with
  313. \\[org-toggle-fixed-width-section]."
  314. :group 'org-keywords
  315. :type 'string)
  316. (defconst org-repeat-re
  317. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\([.+]?\\+[0-9]+[dwmy]\\)"
  318. "Regular expression for specifying repeated events.
  319. After a match, group 1 contains the repeat expression.")
  320. (defgroup org-structure nil
  321. "Options concerning the general structure of Org-mode files."
  322. :tag "Org Structure"
  323. :group 'org)
  324. (defgroup org-reveal-location nil
  325. "Options about how to make context of a location visible."
  326. :tag "Org Reveal Location"
  327. :group 'org-structure)
  328. (defconst org-context-choice
  329. '(choice
  330. (const :tag "Always" t)
  331. (const :tag "Never" nil)
  332. (repeat :greedy t :tag "Individual contexts"
  333. (cons
  334. (choice :tag "Context"
  335. (const agenda)
  336. (const org-goto)
  337. (const occur-tree)
  338. (const tags-tree)
  339. (const link-search)
  340. (const mark-goto)
  341. (const bookmark-jump)
  342. (const isearch)
  343. (const default))
  344. (boolean))))
  345. "Contexts for the reveal options.")
  346. (defcustom org-show-hierarchy-above '((default . t))
  347. "Non-nil means, show full hierarchy when revealing a location.
  348. Org-mode often shows locations in an org-mode file which might have
  349. been invisible before. When this is set, the hierarchy of headings
  350. above the exposed location is shown.
  351. Turning this off for example for sparse trees makes them very compact.
  352. Instead of t, this can also be an alist specifying this option for different
  353. contexts. Valid contexts are
  354. agenda when exposing an entry from the agenda
  355. org-goto when using the command `org-goto' on key C-c C-j
  356. occur-tree when using the command `org-occur' on key C-c /
  357. tags-tree when constructing a sparse tree based on tags matches
  358. link-search when exposing search matches associated with a link
  359. mark-goto when exposing the jump goal of a mark
  360. bookmark-jump when exposing a bookmark location
  361. isearch when exiting from an incremental search
  362. default default for all contexts not set explicitly"
  363. :group 'org-reveal-location
  364. :type org-context-choice)
  365. (defcustom org-show-following-heading '((default . nil))
  366. "Non-nil means, show following heading when revealing a location.
  367. Org-mode often shows locations in an org-mode file which might have
  368. been invisible before. When this is set, the heading following the
  369. match is shown.
  370. Turning this off for example for sparse trees makes them very compact,
  371. but makes it harder to edit the location of the match. In such a case,
  372. use the command \\[org-reveal] to show more context.
  373. Instead of t, this can also be an alist specifying this option for different
  374. contexts. See `org-show-hierarchy-above' for valid contexts."
  375. :group 'org-reveal-location
  376. :type org-context-choice)
  377. (defcustom org-show-siblings '((default . nil) (isearch t))
  378. "Non-nil means, show all sibling heading when revealing a location.
  379. Org-mode often shows locations in an org-mode file which might have
  380. been invisible before. When this is set, the sibling of the current entry
  381. heading are all made visible. If `org-show-hierarchy-above' is t,
  382. the same happens on each level of the hierarchy above the current entry.
  383. By default this is on for the isearch context, off for all other contexts.
  384. Turning this off for example for sparse trees makes them very compact,
  385. but makes it harder to edit the location of the match. In such a case,
  386. use the command \\[org-reveal] to show more context.
  387. Instead of t, this can also be an alist specifying this option for different
  388. contexts. See `org-show-hierarchy-above' for valid contexts."
  389. :group 'org-reveal-location
  390. :type org-context-choice)
  391. (defcustom org-show-entry-below '((default . nil))
  392. "Non-nil means, show the entry below a headline when revealing a location.
  393. Org-mode often shows locations in an org-mode file which might have
  394. been invisible before. When this is set, the text below the headline that is
  395. exposed is also shown.
  396. By default this is off for all contexts.
  397. Instead of t, this can also be an alist specifying this option for different
  398. contexts. See `org-show-hierarchy-above' for valid contexts."
  399. :group 'org-reveal-location
  400. :type org-context-choice)
  401. (defcustom org-indirect-buffer-display 'other-window
  402. "How should indirect tree buffers be displayed?
  403. This applies to indirect buffers created with the commands
  404. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  405. Valid values are:
  406. current-window Display in the current window
  407. other-window Just display in another window.
  408. dedicated-frame Create one new frame, and re-use it each time.
  409. new-frame Make a new frame each time. Note that in this case
  410. previously-made indirect buffers are kept, and you need to
  411. kill these buffers yourself."
  412. :group 'org-structure
  413. :group 'org-agenda-windows
  414. :type '(choice
  415. (const :tag "In current window" current-window)
  416. (const :tag "In current frame, other window" other-window)
  417. (const :tag "Each time a new frame" new-frame)
  418. (const :tag "One dedicated frame" dedicated-frame)))
  419. (defgroup org-cycle nil
  420. "Options concerning visibility cycling in Org-mode."
  421. :tag "Org Cycle"
  422. :group 'org-structure)
  423. (defcustom org-drawers '("PROPERTIES" "CLOCK")
  424. "Names of drawers. Drawers are not opened by cycling on the headline above.
  425. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  426. this:
  427. :DRAWERNAME:
  428. .....
  429. :END:
  430. The drawer \"PROPERTIES\" is special for capturing properties through
  431. the property API.
  432. Drawers can be defined on the per-file basis with a line like:
  433. #+DRAWERS: HIDDEN STATE PROPERTIES"
  434. :group 'org-structure
  435. :type '(repeat (string :tag "Drawer Name")))
  436. (defcustom org-cycle-global-at-bob nil
  437. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  438. This makes it possible to do global cycling without having to use S-TAB or
  439. C-u TAB. For this special case to work, the first line of the buffer
  440. must not be a headline - it may be empty ot some other text. When used in
  441. this way, `org-cycle-hook' is disables temporarily, to make sure the
  442. cursor stays at the beginning of the buffer.
  443. When this option is nil, don't do anything special at the beginning
  444. of the buffer."
  445. :group 'org-cycle
  446. :type 'boolean)
  447. (defcustom org-cycle-emulate-tab t
  448. "Where should `org-cycle' emulate TAB.
  449. nil Never
  450. white Only in completely white lines
  451. whitestart Only at the beginning of lines, before the first non-white char
  452. t Everywhere except in headlines
  453. exc-hl-bol Everywhere except at the start of a headline
  454. If TAB is used in a place where it does not emulate TAB, the current subtree
  455. visibility is cycled."
  456. :group 'org-cycle
  457. :type '(choice (const :tag "Never" nil)
  458. (const :tag "Only in completely white lines" white)
  459. (const :tag "Before first char in a line" whitestart)
  460. (const :tag "Everywhere except in headlines" t)
  461. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  462. ))
  463. (defcustom org-cycle-separator-lines 2
  464. "Number of empty lines needed to keep an empty line between collapsed trees.
  465. If you leave an empty line between the end of a subtree and the following
  466. headline, this empty line is hidden when the subtree is folded.
  467. Org-mode will leave (exactly) one empty line visible if the number of
  468. empty lines is equal or larger to the number given in this variable.
  469. So the default 2 means, at least 2 empty lines after the end of a subtree
  470. are needed to produce free space between a collapsed subtree and the
  471. following headline.
  472. Special case: when 0, never leave empty lines in collapsed view."
  473. :group 'org-cycle
  474. :type 'integer)
  475. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  476. org-cycle-hide-drawers
  477. org-cycle-show-empty-lines
  478. org-optimize-window-after-visibility-change)
  479. "Hook that is run after `org-cycle' has changed the buffer visibility.
  480. The function(s) in this hook must accept a single argument which indicates
  481. the new state that was set by the most recent `org-cycle' command. The
  482. argument is a symbol. After a global state change, it can have the values
  483. `overview', `content', or `all'. After a local state change, it can have
  484. the values `folded', `children', or `subtree'."
  485. :group 'org-cycle
  486. :type 'hook)
  487. (defgroup org-edit-structure nil
  488. "Options concerning structure editing in Org-mode."
  489. :tag "Org Edit Structure"
  490. :group 'org-structure)
  491. (defcustom org-odd-levels-only nil
  492. "Non-nil means, skip even levels and only use odd levels for the outline.
  493. This has the effect that two stars are being added/taken away in
  494. promotion/demotion commands. It also influences how levels are
  495. handled by the exporters.
  496. Changing it requires restart of `font-lock-mode' to become effective
  497. for fontification also in regions already fontified.
  498. You may also set this on a per-file basis by adding one of the following
  499. lines to the buffer:
  500. #+STARTUP: odd
  501. #+STARTUP: oddeven"
  502. :group 'org-edit-structure
  503. :group 'org-font-lock
  504. :type 'boolean)
  505. (defcustom org-adapt-indentation t
  506. "Non-nil means, adapt indentation when promoting and demoting.
  507. When this is set and the *entire* text in an entry is indented, the
  508. indentation is increased by one space in a demotion command, and
  509. decreased by one in a promotion command. If any line in the entry
  510. body starts at column 0, indentation is not changed at all."
  511. :group 'org-edit-structure
  512. :type 'boolean)
  513. (defcustom org-special-ctrl-a/e nil
  514. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  515. When t, `C-a' will bring back the cursor to the beginning of the
  516. headline text, i.e. after the stars and after a possible TODO keyword.
  517. In an item, this will be the position after the bullet.
  518. When the cursor is already at that position, another `C-a' will bring
  519. it to the beginning of the line.
  520. `C-e' will jump to the end of the headline, ignoring the presence of tags
  521. in the headline. A second `C-e' will then jump to the true end of the
  522. line, after any tags.
  523. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  524. and only a directly following, identical keypress will bring the cursor
  525. to the special positions."
  526. :group 'org-edit-structure
  527. :type '(choice
  528. (const :tag "off" nil)
  529. (const :tag "after bullet first" t)
  530. (const :tag "border first" reversed)))
  531. (if (fboundp 'defvaralias)
  532. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  533. (defcustom org-special-ctrl-k nil
  534. "Non-nil means `C-k' will behave specially in headlines.
  535. When nil, `C-k' will call the default `kill-line' command.
  536. When t, the following will happen while the cursor is in the headline:
  537. - When the cursor is at the beginning of a headline, kill the entire
  538. line and possible the folded subtree below the line.
  539. - When in the middle of the headline text, kill the headline up to the tags.
  540. - When after the headline text, kill the tags."
  541. :group 'org-edit-structure
  542. :type 'boolean)
  543. (defcustom org-M-RET-may-split-line '((default . t))
  544. "Non-nil means, M-RET will split the line at the cursor position.
  545. When nil, it will go to the end of the line before making a
  546. new line.
  547. You may also set this option in a different way for different
  548. contexts. Valid contexts are:
  549. headline when creating a new headline
  550. item when creating a new item
  551. table in a table field
  552. default the value to be used for all contexts not explicitly
  553. customized"
  554. :group 'org-structure
  555. :group 'org-table
  556. :type '(choice
  557. (const :tag "Always" t)
  558. (const :tag "Never" nil)
  559. (repeat :greedy t :tag "Individual contexts"
  560. (cons
  561. (choice :tag "Context"
  562. (const headline)
  563. (const item)
  564. (const table)
  565. (const default))
  566. (boolean)))))
  567. (defcustom org-blank-before-new-entry '((heading . nil)
  568. (plain-list-item . nil))
  569. "Should `org-insert-heading' leave a blank line before new heading/item?
  570. The value is an alist, with `heading' and `plain-list-item' as car,
  571. and a boolean flag as cdr."
  572. :group 'org-edit-structure
  573. :type '(list
  574. (cons (const heading) (boolean))
  575. (cons (const plain-list-item) (boolean))))
  576. (defcustom org-insert-heading-hook nil
  577. "Hook being run after inserting a new heading."
  578. :group 'org-edit-structure
  579. :type 'hook)
  580. (defcustom org-enable-fixed-width-editor t
  581. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  582. This currently only means, they are never auto-wrapped.
  583. When nil, such lines will be treated like ordinary lines.
  584. See also the QUOTE keyword."
  585. :group 'org-edit-structure
  586. :type 'boolean)
  587. (defcustom org-goto-auto-isearch t
  588. "Non-nil means, typing characters in org-goto starts incremental search."
  589. :group 'org-edit-structure
  590. :type 'boolean)
  591. (defgroup org-sparse-trees nil
  592. "Options concerning sparse trees in Org-mode."
  593. :tag "Org Sparse Trees"
  594. :group 'org-structure)
  595. (defcustom org-highlight-sparse-tree-matches t
  596. "Non-nil means, highlight all matches that define a sparse tree.
  597. The highlights will automatically disappear the next time the buffer is
  598. changed by an edit command."
  599. :group 'org-sparse-trees
  600. :type 'boolean)
  601. (defcustom org-remove-highlights-with-change t
  602. "Non-nil means, any change to the buffer will remove temporary highlights.
  603. Such highlights are created by `org-occur' and `org-clock-display'.
  604. When nil, `C-c C-c needs to be used to get rid of the highlights.
  605. The highlights created by `org-preview-latex-fragment' always need
  606. `C-c C-c' to be removed."
  607. :group 'org-sparse-trees
  608. :group 'org-time
  609. :type 'boolean)
  610. (defcustom org-occur-hook '(org-first-headline-recenter)
  611. "Hook that is run after `org-occur' has constructed a sparse tree.
  612. This can be used to recenter the window to show as much of the structure
  613. as possible."
  614. :group 'org-sparse-trees
  615. :type 'hook)
  616. (defgroup org-plain-lists nil
  617. "Options concerning plain lists in Org-mode."
  618. :tag "Org Plain lists"
  619. :group 'org-structure)
  620. (defcustom org-cycle-include-plain-lists nil
  621. "Non-nil means, include plain lists into visibility cycling.
  622. This means that during cycling, plain list items will *temporarily* be
  623. interpreted as outline headlines with a level given by 1000+i where i is the
  624. indentation of the bullet. In all other operations, plain list items are
  625. not seen as headlines. For example, you cannot assign a TODO keyword to
  626. such an item."
  627. :group 'org-plain-lists
  628. :type 'boolean)
  629. (defcustom org-plain-list-ordered-item-terminator t
  630. "The character that makes a line with leading number an ordered list item.
  631. Valid values are ?. and ?\). To get both terminators, use t. While
  632. ?. may look nicer, it creates the danger that a line with leading
  633. number may be incorrectly interpreted as an item. ?\) therefore is
  634. the safe choice."
  635. :group 'org-plain-lists
  636. :type '(choice (const :tag "dot like in \"2.\"" ?.)
  637. (const :tag "paren like in \"2)\"" ?\))
  638. (const :tab "both" t)))
  639. (defcustom org-empty-line-terminates-plain-lists nil
  640. "Non-nil means, an empty line ends all plain list levels.
  641. When nil, empty lines are part of the preceeding item."
  642. :group 'org-plain-lists
  643. :type 'boolean)
  644. (defcustom org-auto-renumber-ordered-lists t
  645. "Non-nil means, automatically renumber ordered plain lists.
  646. Renumbering happens when the sequence have been changed with
  647. \\[org-shiftmetaup] or \\[org-shiftmetadown]. After other editing commands,
  648. use \\[org-ctrl-c-ctrl-c] to trigger renumbering."
  649. :group 'org-plain-lists
  650. :type 'boolean)
  651. (defcustom org-provide-checkbox-statistics t
  652. "Non-nil means, update checkbox statistics after insert and toggle.
  653. When this is set, checkbox statistics is updated each time you either insert
  654. a new checkbox with \\[org-insert-todo-heading] or toggle a checkbox
  655. with \\[org-ctrl-c-ctrl-c\\]."
  656. :group 'org-plain-lists
  657. :type 'boolean)
  658. (defgroup org-imenu-and-speedbar nil
  659. "Options concerning imenu and speedbar in Org-mode."
  660. :tag "Org Imenu and Speedbar"
  661. :group 'org-structure)
  662. (defcustom org-imenu-depth 2
  663. "The maximum level for Imenu access to Org-mode headlines.
  664. This also applied for speedbar access."
  665. :group 'org-imenu-and-speedbar
  666. :type 'number)
  667. (defgroup org-table nil
  668. "Options concerning tables in Org-mode."
  669. :tag "Org Table"
  670. :group 'org)
  671. (defcustom org-enable-table-editor 'optimized
  672. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  673. When nil, such lines will be treated like ordinary lines.
  674. When equal to the symbol `optimized', the table editor will be optimized to
  675. do the following:
  676. - Automatic overwrite mode in front of whitespace in table fields.
  677. This makes the structure of the table stay in tact as long as the edited
  678. field does not exceed the column width.
  679. - Minimize the number of realigns. Normally, the table is aligned each time
  680. TAB or RET are pressed to move to another field. With optimization this
  681. happens only if changes to a field might have changed the column width.
  682. Optimization requires replacing the functions `self-insert-command',
  683. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  684. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  685. very good at guessing when a re-align will be necessary, but you can always
  686. force one with \\[org-ctrl-c-ctrl-c].
  687. If you would like to use the optimized version in Org-mode, but the
  688. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  689. This variable can be used to turn on and off the table editor during a session,
  690. but in order to toggle optimization, a restart is required.
  691. See also the variable `org-table-auto-blank-field'."
  692. :group 'org-table
  693. :type '(choice
  694. (const :tag "off" nil)
  695. (const :tag "on" t)
  696. (const :tag "on, optimized" optimized)))
  697. (defcustom org-table-tab-recognizes-table.el t
  698. "Non-nil means, TAB will automatically notice a table.el table.
  699. When it sees such a table, it moves point into it and - if necessary -
  700. calls `table-recognize-table'."
  701. :group 'org-table-editing
  702. :type 'boolean)
  703. (defgroup org-link nil
  704. "Options concerning links in Org-mode."
  705. :tag "Org Link"
  706. :group 'org)
  707. (defvar org-link-abbrev-alist-local nil
  708. "Buffer-local version of `org-link-abbrev-alist', which see.
  709. The value of this is taken from the #+LINK lines.")
  710. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  711. (defcustom org-link-abbrev-alist nil
  712. "Alist of link abbreviations.
  713. The car of each element is a string, to be replaced at the start of a link.
  714. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  715. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  716. [[linkkey:tag][description]]
  717. If REPLACE is a string, the tag will simply be appended to create the link.
  718. If the string contains \"%s\", the tag will be inserted there.
  719. REPLACE may also be a function that will be called with the tag as the
  720. only argument to create the link, which should be returned as a string.
  721. See the manual for examples."
  722. :group 'org-link
  723. :type 'alist)
  724. (defcustom org-descriptive-links t
  725. "Non-nil means, hide link part and only show description of bracket links.
  726. Bracket links are like [[link][descritpion]]. This variable sets the initial
  727. state in new org-mode buffers. The setting can then be toggled on a
  728. per-buffer basis from the Org->Hyperlinks menu."
  729. :group 'org-link
  730. :type 'boolean)
  731. (defcustom org-link-file-path-type 'adaptive
  732. "How the path name in file links should be stored.
  733. Valid values are:
  734. relative Relative to the current directory, i.e. the directory of the file
  735. into which the link is being inserted.
  736. absolute Absolute path, if possible with ~ for home directory.
  737. noabbrev Absolute path, no abbreviation of home directory.
  738. adaptive Use relative path for files in the current directory and sub-
  739. directories of it. For other files, use an absolute path."
  740. :group 'org-link
  741. :type '(choice
  742. (const relative)
  743. (const absolute)
  744. (const noabbrev)
  745. (const adaptive)))
  746. (defcustom org-activate-links '(bracket angle plain radio tag date)
  747. "Types of links that should be activated in Org-mode files.
  748. This is a list of symbols, each leading to the activation of a certain link
  749. type. In principle, it does not hurt to turn on most link types - there may
  750. be a small gain when turning off unused link types. The types are:
  751. bracket The recommended [[link][description]] or [[link]] links with hiding.
  752. angular Links in angular brackes that may contain whitespace like
  753. <bbdb:Carsten Dominik>.
  754. plain Plain links in normal text, no whitespace, like http://google.com.
  755. radio Text that is matched by a radio target, see manual for details.
  756. tag Tag settings in a headline (link to tag search).
  757. date Time stamps (link to calendar).
  758. Changing this variable requires a restart of Emacs to become effective."
  759. :group 'org-link
  760. :type '(set (const :tag "Double bracket links (new style)" bracket)
  761. (const :tag "Angular bracket links (old style)" angular)
  762. (const :tag "Plain text links" plain)
  763. (const :tag "Radio target matches" radio)
  764. (const :tag "Tags" tag)
  765. (const :tag "Timestamps" date)))
  766. (defcustom org-make-link-description-function nil
  767. "Function to use to generate link descriptions from links. If
  768. nil the link location will be used. This function must take two
  769. parameters; the first is the link and the second the description
  770. org-insert-link has generated, and should return the description
  771. to use."
  772. :group 'org-link
  773. :type 'function)
  774. (defgroup org-link-store nil
  775. "Options concerning storing links in Org-mode."
  776. :tag "Org Store Link"
  777. :group 'org-link)
  778. (defcustom org-email-link-description-format "Email %c: %.30s"
  779. "Format of the description part of a link to an email or usenet message.
  780. The following %-excapes will be replaced by corresponding information:
  781. %F full \"From\" field
  782. %f name, taken from \"From\" field, address if no name
  783. %T full \"To\" field
  784. %t first name in \"To\" field, address if no name
  785. %c correspondent. Unually \"from NAME\", but if you sent it yourself, it
  786. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  787. %s subject
  788. %m message-id.
  789. You may use normal field width specification between the % and the letter.
  790. This is for example useful to limit the length of the subject.
  791. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  792. :group 'org-link-store
  793. :type 'string)
  794. (defcustom org-from-is-user-regexp
  795. (let (r1 r2)
  796. (when (and user-mail-address (not (string= user-mail-address "")))
  797. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  798. (when (and user-full-name (not (string= user-full-name "")))
  799. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  800. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  801. "Regexp mached against the \"From:\" header of an email or usenet message.
  802. It should match if the message is from the user him/herself."
  803. :group 'org-link-store
  804. :type 'regexp)
  805. (defcustom org-context-in-file-links t
  806. "Non-nil means, file links from `org-store-link' contain context.
  807. A search string will be added to the file name with :: as separator and
  808. used to find the context when the link is activated by the command
  809. `org-open-at-point'.
  810. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  811. negates this setting for the duration of the command."
  812. :group 'org-link-store
  813. :type 'boolean)
  814. (defcustom org-keep-stored-link-after-insertion nil
  815. "Non-nil means, keep link in list for entire session.
  816. The command `org-store-link' adds a link pointing to the current
  817. location to an internal list. These links accumulate during a session.
  818. The command `org-insert-link' can be used to insert links into any
  819. Org-mode file (offering completion for all stored links). When this
  820. option is nil, every link which has been inserted once using \\[org-insert-link]
  821. will be removed from the list, to make completing the unused links
  822. more efficient."
  823. :group 'org-link-store
  824. :type 'boolean)
  825. (defgroup org-link-follow nil
  826. "Options concerning following links in Org-mode."
  827. :tag "Org Follow Link"
  828. :group 'org-link)
  829. (defcustom org-follow-link-hook nil
  830. "Hook that is run after a link has been followed."
  831. :group 'org-link-follow
  832. :type 'hook)
  833. (defcustom org-tab-follows-link nil
  834. "Non-nil means, on links TAB will follow the link.
  835. Needs to be set before org.el is loaded."
  836. :group 'org-link-follow
  837. :type 'boolean)
  838. (defcustom org-return-follows-link nil
  839. "Non-nil means, on links RET will follow the link.
  840. Needs to be set before org.el is loaded."
  841. :group 'org-link-follow
  842. :type 'boolean)
  843. (defcustom org-mouse-1-follows-link
  844. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  845. "Non-nil means, mouse-1 on a link will follow the link.
  846. A longer mouse click will still set point. Does not work on XEmacs.
  847. Needs to be set before org.el is loaded."
  848. :group 'org-link-follow
  849. :type 'boolean)
  850. (defcustom org-mark-ring-length 4
  851. "Number of different positions to be recorded in the ring
  852. Changing this requires a restart of Emacs to work correctly."
  853. :group 'org-link-follow
  854. :type 'interger)
  855. (defcustom org-link-frame-setup
  856. '((vm . vm-visit-folder-other-frame)
  857. (gnus . gnus-other-frame)
  858. (file . find-file-other-window))
  859. "Setup the frame configuration for following links.
  860. When following a link with Emacs, it may often be useful to display
  861. this link in another window or frame. This variable can be used to
  862. set this up for the different types of links.
  863. For VM, use any of
  864. `vm-visit-folder'
  865. `vm-visit-folder-other-frame'
  866. For Gnus, use any of
  867. `gnus'
  868. `gnus-other-frame'
  869. For FILE, use any of
  870. `find-file'
  871. `find-file-other-window'
  872. `find-file-other-frame'
  873. For the calendar, use the variable `calendar-setup'.
  874. For BBDB, it is currently only possible to display the matches in
  875. another window."
  876. :group 'org-link-follow
  877. :type '(list
  878. (cons (const vm)
  879. (choice
  880. (const vm-visit-folder)
  881. (const vm-visit-folder-other-window)
  882. (const vm-visit-folder-other-frame)))
  883. (cons (const gnus)
  884. (choice
  885. (const gnus)
  886. (const gnus-other-frame)))
  887. (cons (const file)
  888. (choice
  889. (const find-file)
  890. (const find-file-other-window)
  891. (const find-file-other-frame)))))
  892. (defcustom org-display-internal-link-with-indirect-buffer nil
  893. "Non-nil means, use indirect buffer to display infile links.
  894. Activating internal links (from one location in a file to another location
  895. in the same file) normally just jumps to the location. When the link is
  896. activated with a C-u prefix (or with mouse-3), the link is displayed in
  897. another window. When this option is set, the other window actually displays
  898. an indirect buffer clone of the current buffer, to avoid any visibility
  899. changes to the current buffer."
  900. :group 'org-link-follow
  901. :type 'boolean)
  902. (defcustom org-open-non-existing-files nil
  903. "Non-nil means, `org-open-file' will open non-existing files.
  904. When nil, an error will be generated."
  905. :group 'org-link-follow
  906. :type 'boolean)
  907. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  908. "Function and arguments to call for following mailto links.
  909. This is a list with the first element being a lisp function, and the
  910. remaining elements being arguments to the function. In string arguments,
  911. %a will be replaced by the address, and %s will be replaced by the subject
  912. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  913. :group 'org-link-follow
  914. :type '(choice
  915. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  916. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  917. (const :tag "message-mail" (message-mail "%a" "%s"))
  918. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  919. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  920. "Non-nil means, ask for confirmation before executing shell links.
  921. Shell links can be dangerous: just think about a link
  922. [[shell:rm -rf ~/*][Google Search]]
  923. This link would show up in your Org-mode document as \"Google Search\",
  924. but really it would remove your entire home directory.
  925. Therefore we advise against setting this variable to nil.
  926. Just change it to `y-or-n-p' of you want to confirm with a
  927. single keystroke rather than having to type \"yes\"."
  928. :group 'org-link-follow
  929. :type '(choice
  930. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  931. (const :tag "with y-or-n (faster)" y-or-n-p)
  932. (const :tag "no confirmation (dangerous)" nil)))
  933. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  934. "Non-nil means, ask for confirmation before executing Emacs Lisp links.
  935. Elisp links can be dangerous: just think about a link
  936. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  937. This link would show up in your Org-mode document as \"Google Search\",
  938. but really it would remove your entire home directory.
  939. Therefore we advise against setting this variable to nil.
  940. Just change it to `y-or-n-p' of you want to confirm with a
  941. single keystroke rather than having to type \"yes\"."
  942. :group 'org-link-follow
  943. :type '(choice
  944. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  945. (const :tag "with y-or-n (faster)" y-or-n-p)
  946. (const :tag "no confirmation (dangerous)" nil)))
  947. (defconst org-file-apps-defaults-gnu
  948. '((remote . emacs)
  949. (t . mailcap))
  950. "Default file applications on a UNIX or GNU/Linux system.
  951. See `org-file-apps'.")
  952. (defconst org-file-apps-defaults-macosx
  953. '((remote . emacs)
  954. (t . "open %s")
  955. ("ps" . "gv %s")
  956. ("ps.gz" . "gv %s")
  957. ("eps" . "gv %s")
  958. ("eps.gz" . "gv %s")
  959. ("dvi" . "xdvi %s")
  960. ("fig" . "xfig %s"))
  961. "Default file applications on a MacOS X system.
  962. The system \"open\" is known as a default, but we use X11 applications
  963. for some files for which the OS does not have a good default.
  964. See `org-file-apps'.")
  965. (defconst org-file-apps-defaults-windowsnt
  966. (list
  967. '(remote . emacs)
  968. (cons t
  969. (list (if (featurep 'xemacs)
  970. 'mswindows-shell-execute
  971. 'w32-shell-execute)
  972. "open" 'file)))
  973. "Default file applications on a Windows NT system.
  974. The system \"open\" is used for most files.
  975. See `org-file-apps'.")
  976. (defcustom org-file-apps
  977. '(
  978. ("txt" . emacs)
  979. ("tex" . emacs)
  980. ("ltx" . emacs)
  981. ("org" . emacs)
  982. ("el" . emacs)
  983. ("bib" . emacs)
  984. )
  985. "External applications for opening `file:path' items in a document.
  986. Org-mode uses system defaults for different file types, but
  987. you can use this variable to set the application for a given file
  988. extension. The entries in this list are cons cells where the car identifies
  989. files and the cdr the corresponding command. Possible values for the
  990. file identifier are
  991. \"ext\" A string identifying an extension
  992. `directory' Matches a directory
  993. `remote' Matches a remote file, accessible through tramp or efs.
  994. Remote files most likely should be visited through Emacs
  995. because external applications cannot handle such paths.
  996. t Default for all remaining files
  997. Possible values for the command are:
  998. `emacs' The file will be visited by the current Emacs process.
  999. `default' Use the default application for this file type.
  1000. string A command to be executed by a shell; %s will be replaced
  1001. by the path to the file.
  1002. sexp A Lisp form which will be evaluated. The file path will
  1003. be available in the Lisp variable `file'.
  1004. For more examples, see the system specific constants
  1005. `org-file-apps-defaults-macosx'
  1006. `org-file-apps-defaults-windowsnt'
  1007. `org-file-apps-defaults-gnu'."
  1008. :group 'org-link-follow
  1009. :type '(repeat
  1010. (cons (choice :value ""
  1011. (string :tag "Extension")
  1012. (const :tag "Default for unrecognized files" t)
  1013. (const :tag "Remote file" remote)
  1014. (const :tag "Links to a directory" directory))
  1015. (choice :value ""
  1016. (const :tag "Visit with Emacs" emacs)
  1017. (const :tag "Use system default" default)
  1018. (string :tag "Command")
  1019. (sexp :tag "Lisp form")))))
  1020. (defgroup org-refile nil
  1021. "Options concerning refiling entries in Org-mode."
  1022. :tag "Org Remember"
  1023. :group 'org)
  1024. (defcustom org-directory "~/org"
  1025. "Directory with org files.
  1026. This directory will be used as default to prompt for org files.
  1027. Used by the hooks for remember.el."
  1028. :group 'org-refile
  1029. :group 'org-remember
  1030. :type 'directory)
  1031. (defcustom org-default-notes-file "~/.notes"
  1032. "Default target for storing notes.
  1033. Used by the hooks for remember.el. This can be a string, or nil to mean
  1034. the value of `remember-data-file'.
  1035. You can set this on a per-template basis with the variable
  1036. `org-remember-templates'."
  1037. :group 'org-refile
  1038. :group 'org-remember
  1039. :type '(choice
  1040. (const :tag "Default from remember-data-file" nil)
  1041. file))
  1042. (defcustom org-goto-interface 'outline
  1043. "The default interface to be used for `org-goto'.
  1044. Allowed vaues are:
  1045. outline The interface shows an outline of the relevant file
  1046. and the correct heading is found by moving through
  1047. the outline or by searching with incremental search.
  1048. outline-path-completion Headlines in the current buffer are offered via
  1049. completion."
  1050. :group 'org-refile
  1051. :type '(choice
  1052. (const :tag "Outline" outline)
  1053. (const :tag "Outline-path-completion" outline-path-completion)))
  1054. (defcustom org-reverse-note-order nil
  1055. "Non-nil means, store new notes at the beginning of a file or entry.
  1056. When nil, new notes will be filed to the end of a file or entry.
  1057. This can also be a list with cons cells of regular expressions that
  1058. are matched against file names, and values."
  1059. :group 'org-remember
  1060. :type '(choice
  1061. (const :tag "Reverse always" t)
  1062. (const :tag "Reverse never" nil)
  1063. (repeat :tag "By file name regexp"
  1064. (cons regexp boolean))))
  1065. (defcustom org-refile-targets nil
  1066. "Targets for refiling entries with \\[org-refile].
  1067. This is list of cons cells. Each cell contains:
  1068. - a specification of the files to be considered, either a list of files,
  1069. or a symbol whose function or variable value will be used to retrieve
  1070. a file name or a list of file names. Nil means, refile to a different
  1071. heading in the current buffer.
  1072. - A specification of how to find candidate refile targets. This may be
  1073. any of
  1074. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1075. This tag has to be present in all target headlines, inheritance will
  1076. not be considered.
  1077. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1078. todo keyword.
  1079. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1080. headlines that are refiling targets.
  1081. - a cons cell (:level . N). Any headline of level N is considered a target.
  1082. - a cons cell (:maxlevel . N). Any headline with level <= N is a target."
  1083. :group 'org-remember
  1084. :type '(repeat
  1085. (cons
  1086. (choice :value org-agenda-files
  1087. (const :tag "All agenda files" org-agenda-files)
  1088. (const :tag "Current buffer" nil)
  1089. (function) (variable) (file))
  1090. (choice :tag "Identify target headline by"
  1091. (cons :tag "Specific tag" (const :tag) (string))
  1092. (cons :tag "TODO keyword" (const :todo) (string))
  1093. (cons :tag "Regular expression" (const :regexp) (regexp))
  1094. (cons :tag "Level number" (const :level) (integer))
  1095. (cons :tag "Max Level number" (const :maxlevel) (integer))))))
  1096. (defcustom org-refile-use-outline-path nil
  1097. "Non-nil means, provide refile targets as paths.
  1098. So a level 3 headline will be available as level1/level2/level3.
  1099. When the value is `file', also include the file name (without directory)
  1100. into the path. When `full-file-path', include the full file path."
  1101. :group 'org-remember
  1102. :type '(choice
  1103. (const :tag "Not" nil)
  1104. (const :tag "Yes" t)
  1105. (const :tag "Start with file name" file)
  1106. (const :tag "Start with full file path" full-file-path)))
  1107. (defgroup org-todo nil
  1108. "Options concerning TODO items in Org-mode."
  1109. :tag "Org TODO"
  1110. :group 'org)
  1111. (defgroup org-progress nil
  1112. "Options concerning Progress logging in Org-mode."
  1113. :tag "Org Progress"
  1114. :group 'org-time)
  1115. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1116. "List of TODO entry keyword sequences and their interpretation.
  1117. \\<org-mode-map>This is a list of sequences.
  1118. Each sequence starts with a symbol, either `sequence' or `type',
  1119. indicating if the keywords should be interpreted as a sequence of
  1120. action steps, or as different types of TODO items. The first
  1121. keywords are states requiring action - these states will select a headline
  1122. for inclusion into the global TODO list Org-mode produces. If one of
  1123. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1124. signify that no further action is necessary. If \"|\" is not found,
  1125. the last keyword is treated as the only DONE state of the sequence.
  1126. The command \\[org-todo] cycles an entry through these states, and one
  1127. additional state where no keyword is present. For details about this
  1128. cycling, see the manual.
  1129. TODO keywords and interpretation can also be set on a per-file basis with
  1130. the special #+SEQ_TODO and #+TYP_TODO lines.
  1131. Each keyword can optionally specify a character for fast state selection
  1132. \(in combination with the variable `org-use-fast-todo-selection')
  1133. and specifiers for state change logging, using the same syntax
  1134. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1135. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1136. indicates to record a time stamp each time this state is selected.
  1137. Each keyword may also specify if a timestamp or a note should be
  1138. recorded when entering or leaving the state, by adding additional
  1139. characters in the parenthesis after the keyword. This looks like this:
  1140. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1141. record only the time of the state change. With X and Y being either
  1142. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1143. Y when leaving the state if and only if the *target* state does not
  1144. define X. You may omit any of the fast-selection key or X or /Y,
  1145. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1146. For backward compatibility, this variable may also be just a list
  1147. of keywords - in this case the interptetation (sequence or type) will be
  1148. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1149. :group 'org-todo
  1150. :group 'org-keywords
  1151. :type '(choice
  1152. (repeat :tag "Old syntax, just keywords"
  1153. (string :tag "Keyword"))
  1154. (repeat :tag "New syntax"
  1155. (cons
  1156. (choice
  1157. :tag "Interpretation"
  1158. (const :tag "Sequence (cycling hits every state)" sequence)
  1159. (const :tag "Type (cycling directly to DONE)" type))
  1160. (repeat
  1161. (string :tag "Keyword"))))))
  1162. (defvar org-todo-keywords-1 nil
  1163. "All TODO and DONE keywords active in a buffer.")
  1164. (make-variable-buffer-local 'org-todo-keywords-1)
  1165. (defvar org-todo-keywords-for-agenda nil)
  1166. (defvar org-done-keywords-for-agenda nil)
  1167. (defvar org-agenda-contributing-files nil)
  1168. (defvar org-not-done-keywords nil)
  1169. (make-variable-buffer-local 'org-not-done-keywords)
  1170. (defvar org-done-keywords nil)
  1171. (make-variable-buffer-local 'org-done-keywords)
  1172. (defvar org-todo-heads nil)
  1173. (make-variable-buffer-local 'org-todo-heads)
  1174. (defvar org-todo-sets nil)
  1175. (make-variable-buffer-local 'org-todo-sets)
  1176. (defvar org-todo-log-states nil)
  1177. (make-variable-buffer-local 'org-todo-log-states)
  1178. (defvar org-todo-kwd-alist nil)
  1179. (make-variable-buffer-local 'org-todo-kwd-alist)
  1180. (defvar org-todo-key-alist nil)
  1181. (make-variable-buffer-local 'org-todo-key-alist)
  1182. (defvar org-todo-key-trigger nil)
  1183. (make-variable-buffer-local 'org-todo-key-trigger)
  1184. (defcustom org-todo-interpretation 'sequence
  1185. "Controls how TODO keywords are interpreted.
  1186. This variable is in principle obsolete and is only used for
  1187. backward compatibility, if the interpretation of todo keywords is
  1188. not given already in `org-todo-keywords'. See that variable for
  1189. more information."
  1190. :group 'org-todo
  1191. :group 'org-keywords
  1192. :type '(choice (const sequence)
  1193. (const type)))
  1194. (defcustom org-use-fast-todo-selection 'prefix
  1195. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1196. This variable describes if and under what circumstances the cycling
  1197. mechanism for TODO keywords will be replaced by a single-key, direct
  1198. selection scheme.
  1199. When nil, fast selection is never used.
  1200. When the symbol `prefix', it will be used when `org-todo' is called with
  1201. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1202. in an agenda buffer.
  1203. When t, fast selection is used by default. In this case, the prefix
  1204. argument forces cycling instead.
  1205. In all cases, the special interface is only used if access keys have actually
  1206. been assigned by the user, i.e. if keywords in the configuration are followed
  1207. by a letter in parenthesis, like TODO(t)."
  1208. :group 'org-todo
  1209. :type '(choice
  1210. (const :tag "Never" nil)
  1211. (const :tag "By default" t)
  1212. (const :tag "Only with C-u C-c C-t" prefix)))
  1213. (defcustom org-after-todo-state-change-hook nil
  1214. "Hook which is run after the state of a TODO item was changed.
  1215. The new state (a string with a TODO keyword, or nil) is available in the
  1216. Lisp variable `state'."
  1217. :group 'org-todo
  1218. :type 'hook)
  1219. (defcustom org-log-done nil
  1220. "Non-nil means, record a CLOSED timestamp when moving an entry to DONE.
  1221. When equal to the list (done), also prompt for a closing note.
  1222. This can also be configured on a per-file basis by adding one of
  1223. the following lines anywhere in the buffer:
  1224. #+STARTUP: logdone
  1225. #+STARTUP: lognotedone
  1226. #+STARTUP: nologdone"
  1227. :group 'org-todo
  1228. :group 'org-progress
  1229. :type '(choice
  1230. (const :tag "No logging" nil)
  1231. (const :tag "Record CLOSED timestamp" time)
  1232. (const :tag "Record CLOSED timestamp with closing note." note)))
  1233. ;; Normalize old uses of org-log-done.
  1234. (cond
  1235. ((eq org-log-done t) (setq org-log-done 'time))
  1236. ((and (listp org-log-done) (memq 'done org-log-done))
  1237. (setq org-log-done 'note)))
  1238. (defcustom org-log-note-clock-out nil
  1239. "Non-nil means, recored a note when clocking out of an item.
  1240. This can also be configured on a per-file basis by adding one of
  1241. the following lines anywhere in the buffer:
  1242. #+STARTUP: lognoteclock-out
  1243. #+STARTUP: nolognoteclock-out"
  1244. :group 'org-todo
  1245. :group 'org-progress
  1246. :type 'boolean)
  1247. (defcustom org-log-done-with-time t
  1248. "Non-nil means, the CLOSED time stamp will contain date and time.
  1249. When nil, only the date will be recorded."
  1250. :group 'org-progress
  1251. :type 'boolean)
  1252. (defcustom org-log-note-headings
  1253. '((done . "CLOSING NOTE %t")
  1254. (state . "State %-12s %t")
  1255. (note . "Note taken on %t")
  1256. (clock-out . ""))
  1257. "Headings for notes added to entries.
  1258. The value is an alist, with the car being a symbol indicating the note
  1259. context, and the cdr is the heading to be used. The heading may also be the
  1260. empty string.
  1261. %t in the heading will be replaced by a time stamp.
  1262. %s will be replaced by the new TODO state, in double quotes.
  1263. %u will be replaced by the user name.
  1264. %U will be replaced by the full user name."
  1265. :group 'org-todo
  1266. :group 'org-progress
  1267. :type '(list :greedy t
  1268. (cons (const :tag "Heading when closing an item" done) string)
  1269. (cons (const :tag
  1270. "Heading when changing todo state (todo sequence only)"
  1271. state) string)
  1272. (cons (const :tag "Heading when just taking a note" note) string)
  1273. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1274. (unless (assq 'note org-log-note-headings)
  1275. (push '(note . "%t") org-log-note-headings))
  1276. (defcustom org-log-states-order-reversed t
  1277. "Non-nil means, the latest state change note will be directly after heading.
  1278. When nil, the notes will be orderer according to time."
  1279. :group 'org-todo
  1280. :group 'org-progress
  1281. :type 'boolean)
  1282. (defcustom org-log-repeat 'time
  1283. "Non-nil means, record moving through the DONE state when triggering repeat.
  1284. An auto-repeating tasks is immediately switched back to TODO when marked
  1285. done. If you are not logging state changes (by adding \"@\" or \"!\" to
  1286. the TODO keyword definition, or recording a cloing note by setting
  1287. `org-log-done', there will be no record of the task moving trhough DONE.
  1288. This variable forces taking a note anyway. Possible values are:
  1289. nil Don't force a record
  1290. time Record a time stamp
  1291. note Record a note
  1292. This option can also be set with on a per-file-basis with
  1293. #+STARTUP: logrepeat
  1294. #+STARTUP: lognoterepeat
  1295. #+STARTUP: nologrepeat
  1296. You can have local logging settings for a subtree by setting the LOGGING
  1297. property to one or more of these keywords."
  1298. :group 'org-todo
  1299. :group 'org-progress
  1300. :type '(choice
  1301. (const :tag "Don't force a record" nil)
  1302. (const :tag "Force recording the DONE state" time)
  1303. (const :tag "Force recording a note with the DONE state" note)))
  1304. (defgroup org-priorities nil
  1305. "Priorities in Org-mode."
  1306. :tag "Org Priorities"
  1307. :group 'org-todo)
  1308. (defcustom org-highest-priority ?A
  1309. "The highest priority of TODO items. A character like ?A, ?B etc.
  1310. Must have a smaller ASCII number than `org-lowest-priority'."
  1311. :group 'org-priorities
  1312. :type 'character)
  1313. (defcustom org-lowest-priority ?C
  1314. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1315. Must have a larger ASCII number than `org-highest-priority'."
  1316. :group 'org-priorities
  1317. :type 'character)
  1318. (defcustom org-default-priority ?B
  1319. "The default priority of TODO items.
  1320. This is the priority an item get if no explicit priority is given."
  1321. :group 'org-priorities
  1322. :type 'character)
  1323. (defcustom org-priority-start-cycle-with-default t
  1324. "Non-nil means, start with default priority when starting to cycle.
  1325. When this is nil, the first step in the cycle will be (depending on the
  1326. command used) one higher or lower that the default priority."
  1327. :group 'org-priorities
  1328. :type 'boolean)
  1329. (defgroup org-time nil
  1330. "Options concerning time stamps and deadlines in Org-mode."
  1331. :tag "Org Time"
  1332. :group 'org)
  1333. (defcustom org-insert-labeled-timestamps-at-point nil
  1334. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1335. When nil, these labeled time stamps are forces into the second line of an
  1336. entry, just after the headline. When scheduling from the global TODO list,
  1337. the time stamp will always be forced into the second line."
  1338. :group 'org-time
  1339. :type 'boolean)
  1340. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1341. "Formats for `format-time-string' which are used for time stamps.
  1342. It is not recommended to change this constant.")
  1343. (defcustom org-time-stamp-rounding-minutes '(0 5)
  1344. "Number of minutes to round time stamps to.
  1345. These are two values, the first applies when first creating a time stamp.
  1346. The second applies when changing it with the commands `S-up' and `S-down'.
  1347. When changing the time stamp, this means that it will change in steps
  1348. of N minutes, as given by the second value.
  1349. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  1350. numbers should be factors of 60, so for example 5, 10, 15.
  1351. When this is larger than 1, you can still force an exact time-stamp by using
  1352. a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
  1353. and by using a prefix arg to `S-up/down' to specify the exact number
  1354. of minutes to shift."
  1355. :group 'org-time
  1356. :get '(lambda (var) ; Make sure all entries have 5 elements
  1357. (if (integerp (default-value var))
  1358. (list (default-value var) 5)
  1359. (default-value var)))
  1360. :type '(list
  1361. (integer :tag "when inserting times")
  1362. (integer :tag "when modifying times")))
  1363. ;; Normalize old customizations of this variable.
  1364. (when (integerp org-time-stamp-rounding-minutes)
  1365. (setq org-time-stamp-rounding-minutes
  1366. (list org-time-stamp-rounding-minutes
  1367. org-time-stamp-rounding-minutes)))
  1368. (defcustom org-display-custom-times nil
  1369. "Non-nil means, overlay custom formats over all time stamps.
  1370. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1371. To turn this on on a per-file basis, insert anywhere in the file:
  1372. #+STARTUP: customtime"
  1373. :group 'org-time
  1374. :set 'set-default
  1375. :type 'sexp)
  1376. (make-variable-buffer-local 'org-display-custom-times)
  1377. (defcustom org-time-stamp-custom-formats
  1378. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1379. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1380. These are overlayed over the default ISO format if the variable
  1381. `org-display-custom-times' is set. Time like %H:%M should be at the
  1382. end of the second format."
  1383. :group 'org-time
  1384. :type 'sexp)
  1385. (defun org-time-stamp-format (&optional long inactive)
  1386. "Get the right format for a time string."
  1387. (let ((f (if long (cdr org-time-stamp-formats)
  1388. (car org-time-stamp-formats))))
  1389. (if inactive
  1390. (concat "[" (substring f 1 -1) "]")
  1391. f)))
  1392. (defcustom org-deadline-warning-days 14
  1393. "No. of days before expiration during which a deadline becomes active.
  1394. This variable governs the display in sparse trees and in the agenda.
  1395. When 0 or negative, it means use this number (the absolute value of it)
  1396. even if a deadline has a different individual lead time specified."
  1397. :group 'org-time
  1398. :group 'org-agenda-daily/weekly
  1399. :type 'number)
  1400. (defcustom org-read-date-prefer-future t
  1401. "Non-nil means, assume future for incomplete date input from user.
  1402. This affects the following situations:
  1403. 1. The user gives a day, but no month.
  1404. For example, if today is the 15th, and you enter \"3\", Org-mode will
  1405. read this as the third of *next* month. However, if you enter \"17\",
  1406. it will be considered as *this* month.
  1407. 2. The user gives a month but not a year.
  1408. For example, if it is april and you enter \"feb 2\", this will be read
  1409. as feb 2, *next* year. \"May 5\", however, will be this year.
  1410. Currently this does not work for ISO week specifications.
  1411. When this option is nil, the current month and year will always be used
  1412. as defaults."
  1413. :group 'org-time
  1414. :type 'boolean)
  1415. (defcustom org-read-date-display-live t
  1416. "Non-nil means, display current interpretation of date prompt live.
  1417. This display will be in an overlay, in the minibuffer."
  1418. :group 'org-time
  1419. :type 'boolean)
  1420. (defcustom org-read-date-popup-calendar t
  1421. "Non-nil means, pop up a calendar when prompting for a date.
  1422. In the calendar, the date can be selected with mouse-1. However, the
  1423. minibuffer will also be active, and you can simply enter the date as well.
  1424. When nil, only the minibuffer will be available."
  1425. :group 'org-time
  1426. :type 'boolean)
  1427. (if (fboundp 'defvaralias)
  1428. (defvaralias 'org-popup-calendar-for-date-prompt
  1429. 'org-read-date-popup-calendar))
  1430. (defcustom org-extend-today-until 0
  1431. "The hour when your day really ends.
  1432. This has influence for the following applications:
  1433. - When switching the agenda to \"today\". It it is still earlier than
  1434. the time given here, the day recognized as TODAY is actually yesterday.
  1435. - When a date is read from the user and it is still before the time given
  1436. here, the current date and time will be assumed to be yesterday, 23:59.
  1437. FIXME:
  1438. IMPORTANT: This is still a very experimental feature, it may disappear
  1439. again or it may be extended to mean more things."
  1440. :group 'org-time
  1441. :type 'number)
  1442. (defcustom org-edit-timestamp-down-means-later nil
  1443. "Non-nil means, S-down will increase the time in a time stamp.
  1444. When nil, S-up will increase."
  1445. :group 'org-time
  1446. :type 'boolean)
  1447. (defcustom org-calendar-follow-timestamp-change t
  1448. "Non-nil means, make the calendar window follow timestamp changes.
  1449. When a timestamp is modified and the calendar window is visible, it will be
  1450. moved to the new date."
  1451. :group 'org-time
  1452. :type 'boolean)
  1453. (defgroup org-tags nil
  1454. "Options concerning tags in Org-mode."
  1455. :tag "Org Tags"
  1456. :group 'org)
  1457. (defcustom org-tag-alist nil
  1458. "List of tags allowed in Org-mode files.
  1459. When this list is nil, Org-mode will base TAG input on what is already in the
  1460. buffer.
  1461. The value of this variable is an alist, the car of each entry must be a
  1462. keyword as a string, the cdr may be a character that is used to select
  1463. that tag through the fast-tag-selection interface.
  1464. See the manual for details."
  1465. :group 'org-tags
  1466. :type '(repeat
  1467. (choice
  1468. (cons (string :tag "Tag name")
  1469. (character :tag "Access char"))
  1470. (const :tag "Start radio group" (:startgroup))
  1471. (const :tag "End radio group" (:endgroup)))))
  1472. (defcustom org-use-fast-tag-selection 'auto
  1473. "Non-nil means, use fast tag selection scheme.
  1474. This is a special interface to select and deselect tags with single keys.
  1475. When nil, fast selection is never used.
  1476. When the symbol `auto', fast selection is used if and only if selection
  1477. characters for tags have been configured, either through the variable
  1478. `org-tag-alist' or through a #+TAGS line in the buffer.
  1479. When t, fast selection is always used and selection keys are assigned
  1480. automatically if necessary."
  1481. :group 'org-tags
  1482. :type '(choice
  1483. (const :tag "Always" t)
  1484. (const :tag "Never" nil)
  1485. (const :tag "When selection characters are configured" 'auto)))
  1486. (defcustom org-fast-tag-selection-single-key nil
  1487. "Non-nil means, fast tag selection exits after first change.
  1488. When nil, you have to press RET to exit it.
  1489. During fast tag selection, you can toggle this flag with `C-c'.
  1490. This variable can also have the value `expert'. In this case, the window
  1491. displaying the tags menu is not even shown, until you press C-c again."
  1492. :group 'org-tags
  1493. :type '(choice
  1494. (const :tag "No" nil)
  1495. (const :tag "Yes" t)
  1496. (const :tag "Expert" expert)))
  1497. (defvar org-fast-tag-selection-include-todo nil
  1498. "Non-nil means, fast tags selection interface will also offer TODO states.
  1499. This is an undocumented feature, you should not rely on it.")
  1500. (defcustom org-tags-column (if (featurep 'xemacs) -79 -80)
  1501. "The column to which tags should be indented in a headline.
  1502. If this number is positive, it specifies the column. If it is negative,
  1503. it means that the tags should be flushright to that column. For example,
  1504. -80 works well for a normal 80 character screen."
  1505. :group 'org-tags
  1506. :type 'integer)
  1507. (defcustom org-auto-align-tags t
  1508. "Non-nil means, realign tags after pro/demotion of TODO state change.
  1509. These operations change the length of a headline and therefore shift
  1510. the tags around. With this options turned on, after each such operation
  1511. the tags are again aligned to `org-tags-column'."
  1512. :group 'org-tags
  1513. :type 'boolean)
  1514. (defcustom org-use-tag-inheritance t
  1515. "Non-nil means, tags in levels apply also for sublevels.
  1516. When nil, only the tags directly given in a specific line apply there.
  1517. If you turn off this option, you very likely want to turn on the
  1518. companion option `org-tags-match-list-sublevels'.
  1519. This may also be a list of tags that should be inherited, or a regexp that
  1520. matches tags that should be inherited."
  1521. :group 'org-tags
  1522. :type '(choice
  1523. (const :tag "Not" nil)
  1524. (const :tag "Always" t)
  1525. (repeat :tag "Specific tags" (string :tag "Tag"))
  1526. (regexp :tag "Tags matched by regexp")))
  1527. (defun org-tag-inherit-p (tag)
  1528. "Check if TAG is one that should be inherited."
  1529. (cond
  1530. ((eq org-use-tag-inheritance t) t)
  1531. ((not org-use-tag-inheritance) nil)
  1532. ((stringp org-use-tag-inheritance)
  1533. (string-match org-use-tag-inheritance tag))
  1534. ((listp org-use-tag-inheritance)
  1535. (member tag org-use-tag-inheritance))
  1536. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  1537. (defcustom org-tags-match-list-sublevels nil
  1538. "Non-nil means list also sublevels of headlines matching tag search.
  1539. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  1540. the sublevels of a headline matching a tag search often also match
  1541. the same search. Listing all of them can create very long lists.
  1542. Setting this variable to nil causes subtrees of a match to be skipped.
  1543. This option is off by default, because inheritance in on. If you turn
  1544. inheritance off, you very likely want to turn this option on.
  1545. As a special case, if the tag search is restricted to TODO items, the
  1546. value of this variable is ignored and sublevels are always checked, to
  1547. make sure all corresponding TODO items find their way into the list."
  1548. :group 'org-tags
  1549. :type 'boolean)
  1550. (defvar org-tags-history nil
  1551. "History of minibuffer reads for tags.")
  1552. (defvar org-last-tags-completion-table nil
  1553. "The last used completion table for tags.")
  1554. (defvar org-after-tags-change-hook nil
  1555. "Hook that is run after the tags in a line have changed.")
  1556. (defgroup org-properties nil
  1557. "Options concerning properties in Org-mode."
  1558. :tag "Org Properties"
  1559. :group 'org)
  1560. (defcustom org-property-format "%-10s %s"
  1561. "How property key/value pairs should be formatted by `indent-line'.
  1562. When `indent-line' hits a property definition, it will format the line
  1563. according to this format, mainly to make sure that the values are
  1564. lined-up with respect to each other."
  1565. :group 'org-properties
  1566. :type 'string)
  1567. (defcustom org-use-property-inheritance nil
  1568. "Non-nil means, properties apply also for sublevels.
  1569. This setting is chiefly used during property searches. Turning it on can
  1570. cause significant overhead when doing a search, which is why it is not
  1571. on by default.
  1572. When nil, only the properties directly given in the current entry count.
  1573. When t, every property is inherited. The value may also be a list of
  1574. properties that should have inheritance, or a regular expression matching
  1575. properties that should be inherited.
  1576. However, note that some special properties use inheritance under special
  1577. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  1578. and the properties ending in \"_ALL\" when they are used as descriptor
  1579. for valid values of a property.
  1580. Note for programmers:
  1581. When querying an entry with `org-entry-get', you can control if inheritance
  1582. should be used. By default, `org-entry-get' looks only at the local
  1583. properties. You can request inheritance by setting the inherit argument
  1584. to t (to force inheritance) or to `selective' (to respect the setting
  1585. in this variable)."
  1586. :group 'org-properties
  1587. :type '(choice
  1588. (const :tag "Not" nil)
  1589. (const :tag "Always" t)
  1590. (repeat :tag "Specific properties" (string :tag "Property"))
  1591. (regexp :tag "Properties matched by regexp")))
  1592. (defun org-property-inherit-p (property)
  1593. "Check if PROPERTY is one that should be inherited."
  1594. (cond
  1595. ((eq org-use-property-inheritance t) t)
  1596. ((not org-use-property-inheritance) nil)
  1597. ((stringp org-use-property-inheritance)
  1598. (string-match org-use-property-inheritance property))
  1599. ((listp org-use-property-inheritance)
  1600. (member property org-use-property-inheritance))
  1601. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  1602. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  1603. "The default column format, if no other format has been defined.
  1604. This variable can be set on the per-file basis by inserting a line
  1605. #+COLUMNS: %25ITEM ....."
  1606. :group 'org-properties
  1607. :type 'string)
  1608. (defcustom org-effort-property "Effort"
  1609. "The property that is being used to keep track of effort estimates.
  1610. Effort estimates given in this property need to have the format H:MM."
  1611. :group 'org-properties
  1612. :group 'org-progress
  1613. :type '(string :tag "Property"))
  1614. (defcustom org-global-properties nil
  1615. "List of property/value pairs that can be inherited by any entry.
  1616. You can set buffer-local values for this by adding lines like
  1617. #+PROPERTY: NAME VALUE"
  1618. :group 'org-properties
  1619. :type '(repeat
  1620. (cons (string :tag "Property")
  1621. (string :tag "Value"))))
  1622. (defvar org-local-properties nil
  1623. "List of property/value pairs that can be inherited by any entry.
  1624. Valid for the current buffer.
  1625. This variable is populated from #+PROPERTY lines.")
  1626. (defgroup org-agenda nil
  1627. "Options concerning agenda views in Org-mode."
  1628. :tag "Org Agenda"
  1629. :group 'org)
  1630. (defvar org-category nil
  1631. "Variable used by org files to set a category for agenda display.
  1632. Such files should use a file variable to set it, for example
  1633. # -*- mode: org; org-category: \"ELisp\"
  1634. or contain a special line
  1635. #+CATEGORY: ELisp
  1636. If the file does not specify a category, then file's base name
  1637. is used instead.")
  1638. (make-variable-buffer-local 'org-category)
  1639. (defcustom org-agenda-files nil
  1640. "The files to be used for agenda display.
  1641. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  1642. \\[org-remove-file]. You can also use customize to edit the list.
  1643. If an entry is a directory, all files in that directory that are matched by
  1644. `org-agenda-file-regexp' will be part of the file list.
  1645. If the value of the variable is not a list but a single file name, then
  1646. the list of agenda files is actually stored and maintained in that file, one
  1647. agenda file per line."
  1648. :group 'org-agenda
  1649. :type '(choice
  1650. (repeat :tag "List of files and directories" file)
  1651. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  1652. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  1653. "Regular expression to match files for `org-agenda-files'.
  1654. If any element in the list in that variable contains a directory instead
  1655. of a normal file, all files in that directory that are matched by this
  1656. regular expression will be included."
  1657. :group 'org-agenda
  1658. :type 'regexp)
  1659. (defcustom org-agenda-text-search-extra-files nil
  1660. "List of extra files to be searched by text search commands.
  1661. These files will be search in addition to the agenda files by the
  1662. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  1663. Note that these files will only be searched for text search commands,
  1664. not for the other agenda views like todo lists, tag searches or the weekly
  1665. agenda. This variable is intended to list notes and possibly archive files
  1666. that should also be searched by these two commands.
  1667. In fact, if the first element in the list is the symbol `agenda-archives',
  1668. than all archive files of all agenda files will be added to the search
  1669. scope."
  1670. :group 'org-agenda
  1671. :type '(set :greedy t
  1672. (const :tag "Agenda Archives" agenda-archives)
  1673. (repeat :inline t (file))))
  1674. (if (fboundp 'defvaralias)
  1675. (defvaralias 'org-agenda-multi-occur-extra-files
  1676. 'org-agenda-text-search-extra-files))
  1677. (defcustom org-agenda-skip-unavailable-files nil
  1678. "t means to just skip non-reachable files in `org-agenda-files'.
  1679. Nil means to remove them, after a query, from the list."
  1680. :group 'org-agenda
  1681. :type 'boolean)
  1682. (defcustom org-calendar-to-agenda-key [?c]
  1683. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  1684. The command `org-calendar-goto-agenda' will be bound to this key. The
  1685. default is the character `c' because then `c' can be used to switch back and
  1686. forth between agenda and calendar."
  1687. :group 'org-agenda
  1688. :type 'sexp)
  1689. (eval-after-load "calendar"
  1690. '(org-defkey calendar-mode-map org-calendar-to-agenda-key
  1691. 'org-calendar-goto-agenda))
  1692. (defgroup org-latex nil
  1693. "Options for embedding LaTeX code into Org-mode."
  1694. :tag "Org LaTeX"
  1695. :group 'org)
  1696. (defcustom org-format-latex-options
  1697. '(:foreground default :background default :scale 1.0
  1698. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  1699. :matchers ("begin" "$" "$$" "\\(" "\\["))
  1700. "Options for creating images from LaTeX fragments.
  1701. This is a property list with the following properties:
  1702. :foreground the foreground color for images embedded in emacs, e.g. \"Black\".
  1703. `default' means use the forground of the default face.
  1704. :background the background color, or \"Transparent\".
  1705. `default' means use the background of the default face.
  1706. :scale a scaling factor for the size of the images
  1707. :html-foreground, :html-background, :html-scale
  1708. The same numbers for HTML export.
  1709. :matchers a list indicating which matchers should be used to
  1710. find LaTeX fragments. Valid members of this list are:
  1711. \"begin\" find environments
  1712. \"$\" find math expressions surrounded by $...$
  1713. \"$$\" find math expressions surrounded by $$....$$
  1714. \"\\(\" find math expressions surrounded by \\(...\\)
  1715. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  1716. :group 'org-latex
  1717. :type 'plist)
  1718. (defcustom org-format-latex-header "\\documentclass{article}
  1719. \\usepackage{fullpage} % do not remove
  1720. \\usepackage{amssymb}
  1721. \\usepackage[usenames]{color}
  1722. \\usepackage{amsmath}
  1723. \\usepackage{latexsym}
  1724. \\usepackage[mathscr]{eucal}
  1725. \\pagestyle{empty} % do not remove"
  1726. "The document header used for processing LaTeX fragments."
  1727. :group 'org-latex
  1728. :type 'string)
  1729. (defgroup org-font-lock nil
  1730. "Font-lock settings for highlighting in Org-mode."
  1731. :tag "Org Font Lock"
  1732. :group 'org)
  1733. (defcustom org-level-color-stars-only nil
  1734. "Non-nil means fontify only the stars in each headline.
  1735. When nil, the entire headline is fontified.
  1736. Changing it requires restart of `font-lock-mode' to become effective
  1737. also in regions already fontified."
  1738. :group 'org-font-lock
  1739. :type 'boolean)
  1740. (defcustom org-hide-leading-stars nil
  1741. "Non-nil means, hide the first N-1 stars in a headline.
  1742. This works by using the face `org-hide' for these stars. This
  1743. face is white for a light background, and black for a dark
  1744. background. You may have to customize the face `org-hide' to
  1745. make this work.
  1746. Changing it requires restart of `font-lock-mode' to become effective
  1747. also in regions already fontified.
  1748. You may also set this on a per-file basis by adding one of the following
  1749. lines to the buffer:
  1750. #+STARTUP: hidestars
  1751. #+STARTUP: showstars"
  1752. :group 'org-font-lock
  1753. :type 'boolean)
  1754. (defcustom org-fontify-done-headline nil
  1755. "Non-nil means, change the face of a headline if it is marked DONE.
  1756. Normally, only the TODO/DONE keyword indicates the state of a headline.
  1757. When this is non-nil, the headline after the keyword is set to the
  1758. `org-headline-done' as an additional indication."
  1759. :group 'org-font-lock
  1760. :type 'boolean)
  1761. (defcustom org-fontify-emphasized-text t
  1762. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  1763. Changing this variable requires a restart of Emacs to take effect."
  1764. :group 'org-font-lock
  1765. :type 'boolean)
  1766. (defcustom org-highlight-latex-fragments-and-specials nil
  1767. "Non-nil means, fontify what is treated specially by the exporters."
  1768. :group 'org-font-lock
  1769. :type 'boolean)
  1770. (defcustom org-hide-emphasis-markers nil
  1771. "Non-nil mean font-lock should hide the emphasis marker characters."
  1772. :group 'org-font-lock
  1773. :type 'boolean)
  1774. (defvar org-emph-re nil
  1775. "Regular expression for matching emphasis.")
  1776. (defvar org-verbatim-re nil
  1777. "Regular expression for matching verbatim text.")
  1778. (defvar org-emphasis-regexp-components) ; defined just below
  1779. (defvar org-emphasis-alist) ; defined just below
  1780. (defun org-set-emph-re (var val)
  1781. "Set variable and compute the emphasis regular expression."
  1782. (set var val)
  1783. (when (and (boundp 'org-emphasis-alist)
  1784. (boundp 'org-emphasis-regexp-components)
  1785. org-emphasis-alist org-emphasis-regexp-components)
  1786. (let* ((e org-emphasis-regexp-components)
  1787. (pre (car e))
  1788. (post (nth 1 e))
  1789. (border (nth 2 e))
  1790. (body (nth 3 e))
  1791. (nl (nth 4 e))
  1792. (stacked (and nil (nth 5 e))) ; stacked is no longer allowed, forced to nil
  1793. (body1 (concat body "*?"))
  1794. (markers (mapconcat 'car org-emphasis-alist ""))
  1795. (vmarkers (mapconcat
  1796. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  1797. org-emphasis-alist "")))
  1798. ;; make sure special characters appear at the right position in the class
  1799. (if (string-match "\\^" markers)
  1800. (setq markers (concat (replace-match "" t t markers) "^")))
  1801. (if (string-match "-" markers)
  1802. (setq markers (concat (replace-match "" t t markers) "-")))
  1803. (if (string-match "\\^" vmarkers)
  1804. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  1805. (if (string-match "-" vmarkers)
  1806. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  1807. (if (> nl 0)
  1808. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  1809. (int-to-string nl) "\\}")))
  1810. ;; Make the regexp
  1811. (setq org-emph-re
  1812. (concat "\\([" pre (if (and nil stacked) markers) "]\\|^\\)"
  1813. "\\("
  1814. "\\([" markers "]\\)"
  1815. "\\("
  1816. "[^" border "]\\|"
  1817. "[^" border (if (and nil stacked) markers) "]"
  1818. body1
  1819. "[^" border (if (and nil stacked) markers) "]"
  1820. "\\)"
  1821. "\\3\\)"
  1822. "\\([" post (if (and nil stacked) markers) "]\\|$\\)"))
  1823. (setq org-verbatim-re
  1824. (concat "\\([" pre "]\\|^\\)"
  1825. "\\("
  1826. "\\([" vmarkers "]\\)"
  1827. "\\("
  1828. "[^" border "]\\|"
  1829. "[^" border "]"
  1830. body1
  1831. "[^" border "]"
  1832. "\\)"
  1833. "\\3\\)"
  1834. "\\([" post "]\\|$\\)")))))
  1835. (defcustom org-emphasis-regexp-components
  1836. '(" \t('\"" "- \t.,:?;'\")" " \t\r\n,\"'" "." 1)
  1837. "Components used to build the regular expression for emphasis.
  1838. This is a list with 6 entries. Terminology: In an emphasis string
  1839. like \" *strong word* \", we call the initial space PREMATCH, the final
  1840. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  1841. and \"trong wor\" is the body. The different components in this variable
  1842. specify what is allowed/forbidden in each part:
  1843. pre Chars allowed as prematch. Beginning of line will be allowed too.
  1844. post Chars allowed as postmatch. End of line will be allowed too.
  1845. border The chars *forbidden* as border characters.
  1846. body-regexp A regexp like \".\" to match a body character. Don't use
  1847. non-shy groups here, and don't allow newline here.
  1848. newline The maximum number of newlines allowed in an emphasis exp.
  1849. Use customize to modify this, or restart Emacs after changing it."
  1850. :group 'org-font-lock
  1851. :set 'org-set-emph-re
  1852. :type '(list
  1853. (sexp :tag "Allowed chars in pre ")
  1854. (sexp :tag "Allowed chars in post ")
  1855. (sexp :tag "Forbidden chars in border ")
  1856. (sexp :tag "Regexp for body ")
  1857. (integer :tag "number of newlines allowed")
  1858. (option (boolean :tag "Stacking (DISABLED) "))))
  1859. (defcustom org-emphasis-alist
  1860. `(("*" bold "<b>" "</b>")
  1861. ("/" italic "<i>" "</i>")
  1862. ("_" underline "<u>" "</u>")
  1863. ("=" org-code "<code>" "</code>" verbatim)
  1864. ("~" org-verbatim "" "" verbatim)
  1865. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  1866. "<del>" "</del>")
  1867. )
  1868. "Special syntax for emphasized text.
  1869. Text starting and ending with a special character will be emphasized, for
  1870. example *bold*, _underlined_ and /italic/. This variable sets the marker
  1871. characters, the face to be used by font-lock for highlighting in Org-mode
  1872. Emacs buffers, and the HTML tags to be used for this.
  1873. Use customize to modify this, or restart Emacs after changing it."
  1874. :group 'org-font-lock
  1875. :set 'org-set-emph-re
  1876. :type '(repeat
  1877. (list
  1878. (string :tag "Marker character")
  1879. (choice
  1880. (face :tag "Font-lock-face")
  1881. (plist :tag "Face property list"))
  1882. (string :tag "HTML start tag")
  1883. (string :tag "HTML end tag")
  1884. (option (const verbatim)))))
  1885. ;;; Miscellaneous options
  1886. (defgroup org-completion nil
  1887. "Completion in Org-mode."
  1888. :tag "Org Completion"
  1889. :group 'org)
  1890. (defcustom org-completion-fallback-command 'hippie-expand
  1891. "The expansion command called by \\[org-complete] in normal context.
  1892. Normal means, no org-mode-specific context."
  1893. :group 'org-completion
  1894. :type 'function)
  1895. ;;; Functions and variables from ther packages
  1896. ;; Declared here to avoid compiler warnings
  1897. ;; XEmacs only
  1898. (defvar outline-mode-menu-heading)
  1899. (defvar outline-mode-menu-show)
  1900. (defvar outline-mode-menu-hide)
  1901. (defvar zmacs-regions) ; XEmacs regions
  1902. ;; Emacs only
  1903. (defvar mark-active)
  1904. ;; Various packages
  1905. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  1906. (declare-function calendar-forward-day "cal-move" (arg))
  1907. (declare-function calendar-goto-date "cal-move" (date))
  1908. (declare-function calendar-goto-today "cal-move" ())
  1909. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  1910. (defvar calc-embedded-close-formula)
  1911. (defvar calc-embedded-open-formula)
  1912. (declare-function cdlatex-tab "ext:cdlatex" ())
  1913. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  1914. (defvar font-lock-unfontify-region-function)
  1915. (declare-function iswitchb-mode "iswitchb" (&optional arg))
  1916. (declare-function iswitchb-read-buffer (prompt &optional default require-match start matches-set))
  1917. (defvar iswitchb-temp-buflist)
  1918. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  1919. (declare-function org-agenda-skip "org-agenda" ())
  1920. (declare-function org-format-agenda-item "org-agenda"
  1921. (extra txt &optional category tags dotime noprefix remove-re))
  1922. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  1923. (declare-function org-agenda-change-all-lines "org-agenda"
  1924. (newhead hdmarker &optional fixface))
  1925. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  1926. (declare-function org-agenda-maybe-redo "org-agenda" ())
  1927. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda" nil)
  1928. (declare-function parse-time-string "parse-time" (string))
  1929. (declare-function remember "remember" (&optional initial))
  1930. (declare-function remember-buffer-desc "remember" ())
  1931. (declare-function remember-finalize "remember" ())
  1932. (defvar remember-save-after-remembering)
  1933. (defvar remember-data-file)
  1934. (defvar remember-register)
  1935. (defvar remember-buffer)
  1936. (defvar remember-handler-functions)
  1937. (defvar remember-annotation-functions)
  1938. (defvar texmathp-why)
  1939. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  1940. (declare-function table--at-cell-p "table" (position &optional object at-column))
  1941. (defvar w3m-current-url)
  1942. (defvar w3m-current-title)
  1943. (defvar org-latex-regexps)
  1944. ;;; Autoload and prepare some org modules
  1945. ;; Some table stuff that needs to be defined here, because it is used
  1946. ;; by the functions setting up org-mode or checking for table context.
  1947. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  1948. "Detects an org-type or table-type table.")
  1949. (defconst org-table-line-regexp "^[ \t]*|"
  1950. "Detects an org-type table line.")
  1951. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  1952. "Detects an org-type table line.")
  1953. (defconst org-table-hline-regexp "^[ \t]*|-"
  1954. "Detects an org-type table hline.")
  1955. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  1956. "Detects a table-type table hline.")
  1957. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  1958. "Searching from within a table (any type) this finds the first line
  1959. outside the table.")
  1960. ;; Autoload the functions in org-table.el that are needed by functions here.
  1961. (eval-and-compile
  1962. (org-autoload "org-table"
  1963. '(org-table-align org-table-begin org-table-blank-field
  1964. org-table-convert org-table-convert-region org-table-copy-down
  1965. org-table-copy-region org-table-create
  1966. org-table-create-or-convert-from-region
  1967. org-table-create-with-table.el org-table-current-dline
  1968. org-table-cut-region org-table-delete-column org-table-edit-field
  1969. org-table-edit-formulas org-table-end org-table-eval-formula
  1970. org-table-export org-table-field-info
  1971. org-table-get-stored-formulas org-table-goto-column
  1972. org-table-hline-and-move org-table-import org-table-insert-column
  1973. org-table-insert-hline org-table-insert-row org-table-iterate
  1974. org-table-justify-field-maybe org-table-kill-row
  1975. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  1976. org-table-move-column org-table-move-column-left
  1977. org-table-move-column-right org-table-move-row
  1978. org-table-move-row-down org-table-move-row-up
  1979. org-table-next-field org-table-next-row org-table-paste-rectangle
  1980. org-table-previous-field org-table-recalculate
  1981. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  1982. org-table-toggle-coordinate-overlays
  1983. org-table-toggle-formula-debugger org-table-wrap-region
  1984. orgtbl-mode turn-on-orgtbl)))
  1985. (defun org-at-table-p (&optional table-type)
  1986. "Return t if the cursor is inside an org-type table.
  1987. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  1988. (if org-enable-table-editor
  1989. (save-excursion
  1990. (beginning-of-line 1)
  1991. (looking-at (if table-type org-table-any-line-regexp
  1992. org-table-line-regexp)))
  1993. nil))
  1994. (defsubst org-table-p () (org-at-table-p))
  1995. (defun org-at-table.el-p ()
  1996. "Return t if and only if we are at a table.el table."
  1997. (and (org-at-table-p 'any)
  1998. (save-excursion
  1999. (goto-char (org-table-begin 'any))
  2000. (looking-at org-table1-hline-regexp))))
  2001. (defun org-table-recognize-table.el ()
  2002. "If there is a table.el table nearby, recognize it and move into it."
  2003. (if org-table-tab-recognizes-table.el
  2004. (if (org-at-table.el-p)
  2005. (progn
  2006. (beginning-of-line 1)
  2007. (if (looking-at org-table-dataline-regexp)
  2008. nil
  2009. (if (looking-at org-table1-hline-regexp)
  2010. (progn
  2011. (beginning-of-line 2)
  2012. (if (looking-at org-table-any-border-regexp)
  2013. (beginning-of-line -1)))))
  2014. (if (re-search-forward "|" (org-table-end t) t)
  2015. (progn
  2016. (require 'table)
  2017. (if (table--at-cell-p (point))
  2018. t
  2019. (message "recognizing table.el table...")
  2020. (table-recognize-table)
  2021. (message "recognizing table.el table...done")))
  2022. (error "This should not happen..."))
  2023. t)
  2024. nil)
  2025. nil))
  2026. (defun org-at-table-hline-p ()
  2027. "Return t if the cursor is inside a hline in a table."
  2028. (if org-enable-table-editor
  2029. (save-excursion
  2030. (beginning-of-line 1)
  2031. (looking-at org-table-hline-regexp))
  2032. nil))
  2033. (defvar org-table-clean-did-remove-column nil)
  2034. (defun org-table-map-tables (function)
  2035. "Apply FUNCTION to the start of all tables in the buffer."
  2036. (save-excursion
  2037. (save-restriction
  2038. (widen)
  2039. (goto-char (point-min))
  2040. (while (re-search-forward org-table-any-line-regexp nil t)
  2041. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  2042. (beginning-of-line 1)
  2043. (if (looking-at org-table-line-regexp)
  2044. (save-excursion (funcall function)))
  2045. (re-search-forward org-table-any-border-regexp nil 1))))
  2046. (message "Mapping tables: done"))
  2047. ;; Declare and autoload functions from org-exp.el
  2048. (declare-function org-default-export-plist "org-exp")
  2049. (declare-function org-infile-export-plist "org-exp")
  2050. (declare-function org-get-current-options "org-exp")
  2051. (eval-and-compile
  2052. (org-autoload "org-exp"
  2053. '(org-export org-export-as-ascii org-export-visible
  2054. org-insert-export-options-template org-export-as-html-and-open
  2055. org-export-as-html-batch org-export-as-html-to-buffer
  2056. org-replace-region-by-html org-export-region-as-html
  2057. org-export-as-html org-export-icalendar-this-file
  2058. org-export-icalendar-all-agenda-files
  2059. org-export-icalendar-combine-agenda-files org-export-as-xoxo)))
  2060. ;; Declare and autoload functions from org-exp.el
  2061. (eval-and-compile
  2062. (org-autoload "org-exp"
  2063. '(org-agenda org-agenda-list org-search-view
  2064. org-todo-list org-tags-view org-agenda-list-stuck-projects
  2065. org-diary org-agenda-to-appt)))
  2066. ;; Autoload org-remember
  2067. (eval-and-compile
  2068. (org-autoload "org-remember"
  2069. '(org-remember-insinuate org-remember-annotation
  2070. org-remember-apply-template org-remember org-remember-handler)))
  2071. ;; Autoload org-clock.el
  2072. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock")
  2073. (defvar org-clock-marker (make-marker)
  2074. "Marker recording the last clock-in.")
  2075. (eval-and-compile
  2076. (org-autoload
  2077. "org-clock"
  2078. '(org-clock-in org-clock-out org-clock-cancel
  2079. org-clock-goto org-clock-sum org-clock-display
  2080. org-remove-clock-overlays org-clock-report
  2081. org-clocktable-shift org-dblock-write:clocktable
  2082. org-get-clocktable)))
  2083. (defun org-clock-update-time-maybe ()
  2084. "If this is a CLOCK line, update it and return t.
  2085. Otherwise, return nil."
  2086. (interactive)
  2087. (save-excursion
  2088. (beginning-of-line 1)
  2089. (skip-chars-forward " \t")
  2090. (when (looking-at org-clock-string)
  2091. (let ((re (concat "[ \t]*" org-clock-string
  2092. " *[[<]\\([^]>]+\\)[]>]-+[[<]\\([^]>]+\\)[]>]"
  2093. "\\([ \t]*=>.*\\)?"))
  2094. ts te h m s)
  2095. (if (not (looking-at re))
  2096. nil
  2097. (and (match-end 3) (delete-region (match-beginning 3) (match-end 3)))
  2098. (end-of-line 1)
  2099. (setq ts (match-string 1)
  2100. te (match-string 2))
  2101. (setq s (- (time-to-seconds
  2102. (apply 'encode-time (org-parse-time-string te)))
  2103. (time-to-seconds
  2104. (apply 'encode-time (org-parse-time-string ts))))
  2105. h (floor (/ s 3600))
  2106. s (- s (* 3600 h))
  2107. m (floor (/ s 60))
  2108. s (- s (* 60 s)))
  2109. (insert " => " (format "%2d:%02d" h m))
  2110. t)))))
  2111. (defun org-check-running-clock ()
  2112. "Check if the current buffer contains the running clock.
  2113. If yes, offer to stop it and to save the buffer with the changes."
  2114. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2115. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  2116. (buffer-name))))
  2117. (org-clock-out)
  2118. (when (y-or-n-p "Save changed buffer?")
  2119. (save-buffer))))
  2120. (defun org-clocktable-try-shift (dir n)
  2121. "Check if this line starts a clock table, if yes, shift the time block."
  2122. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  2123. (org-clocktable-shift dir n)))
  2124. ;; Autoload archiving code
  2125. ;; The stuff that is needed for cycling and tags has to be defined here.
  2126. (defgroup org-archive nil
  2127. "Options concerning archiving in Org-mode."
  2128. :tag "Org Archive"
  2129. :group 'org-structure)
  2130. (defcustom org-archive-location "%s_archive::"
  2131. "The location where subtrees should be archived.
  2132. Otherwise, the value of this variable is a string, consisting of two
  2133. parts, separated by a double-colon.
  2134. The first part is a file name - when omitted, archiving happens in the same
  2135. file. %s will be replaced by the current file name (without directory part).
  2136. Archiving to a different file is useful to keep archived entries from
  2137. contributing to the Org-mode Agenda.
  2138. The part after the double colon is a headline. The archived entries will be
  2139. filed under that headline. When omitted, the subtrees are simply filed away
  2140. at the end of the file, as top-level entries.
  2141. Here are a few examples:
  2142. \"%s_archive::\"
  2143. If the current file is Projects.org, archive in file
  2144. Projects.org_archive, as top-level trees. This is the default.
  2145. \"::* Archived Tasks\"
  2146. Archive in the current file, under the top-level headline
  2147. \"* Archived Tasks\".
  2148. \"~/org/archive.org::\"
  2149. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  2150. \"basement::** Finished Tasks\"
  2151. Archive in file ./basement (relative path), as level 3 trees
  2152. below the level 2 heading \"** Finished Tasks\".
  2153. You may set this option on a per-file basis by adding to the buffer a
  2154. line like
  2155. #+ARCHIVE: basement::** Finished Tasks
  2156. You may also define it locally for a subtree by setting an ARCHIVE property
  2157. in the entry. If such a property is found in an entry, or anywhere up
  2158. the hierarchy, it will be used."
  2159. :group 'org-archive
  2160. :type 'string)
  2161. (defcustom org-archive-tag "ARCHIVE"
  2162. "The tag that marks a subtree as archived.
  2163. An archived subtree does not open during visibility cycling, and does
  2164. not contribute to the agenda listings.
  2165. After changing this, font-lock must be restarted in the relevant buffers to
  2166. get the proper fontification."
  2167. :group 'org-archive
  2168. :group 'org-keywords
  2169. :type 'string)
  2170. (defcustom org-agenda-skip-archived-trees t
  2171. "Non-nil means, the agenda will skip any items located in archived trees.
  2172. An archived tree is a tree marked with the tag ARCHIVE."
  2173. :group 'org-archive
  2174. :group 'org-agenda-skip
  2175. :type 'boolean)
  2176. (defcustom org-cycle-open-archived-trees nil
  2177. "Non-nil means, `org-cycle' will open archived trees.
  2178. An archived tree is a tree marked with the tag ARCHIVE.
  2179. When nil, archived trees will stay folded. You can still open them with
  2180. normal outline commands like `show-all', but not with the cycling commands."
  2181. :group 'org-archive
  2182. :group 'org-cycle
  2183. :type 'boolean)
  2184. (defcustom org-sparse-tree-open-archived-trees nil
  2185. "Non-nil means sparse tree construction shows matches in archived trees.
  2186. When nil, matches in these trees are highlighted, but the trees are kept in
  2187. collapsed state."
  2188. :group 'org-archive
  2189. :group 'org-sparse-trees
  2190. :type 'boolean)
  2191. (defun org-cycle-hide-archived-subtrees (state)
  2192. "Re-hide all archived subtrees after a visibility state change."
  2193. (when (and (not org-cycle-open-archived-trees)
  2194. (not (memq state '(overview folded))))
  2195. (save-excursion
  2196. (let* ((globalp (memq state '(contents all)))
  2197. (beg (if globalp (point-min) (point)))
  2198. (end (if globalp (point-max) (org-end-of-subtree t))))
  2199. (org-hide-archived-subtrees beg end)
  2200. (goto-char beg)
  2201. (if (looking-at (concat ".*:" org-archive-tag ":"))
  2202. (message "%s" (substitute-command-keys
  2203. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  2204. (defun org-force-cycle-archived ()
  2205. "Cycle subtree even if it is archived."
  2206. (interactive)
  2207. (setq this-command 'org-cycle)
  2208. (let ((org-cycle-open-archived-trees t))
  2209. (call-interactively 'org-cycle)))
  2210. (defun org-hide-archived-subtrees (beg end)
  2211. "Re-hide all archived subtrees after a visibility state change."
  2212. (save-excursion
  2213. (let* ((re (concat ":" org-archive-tag ":")))
  2214. (goto-char beg)
  2215. (while (re-search-forward re end t)
  2216. (and (org-on-heading-p) (hide-subtree))
  2217. (org-end-of-subtree t)))))
  2218. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  2219. (eval-and-compile
  2220. (org-autoload "org-archive"
  2221. '(org-add-archive-files org-archive-subtree
  2222. org-archive-to-archive-sibling org-toggle-archive-tag)))
  2223. ;; Autoload Column View Code
  2224. (declare-function org-columns-number-to-string "org-colview")
  2225. (declare-function org-columns-get-format-and-top-level "org-colview")
  2226. (declare-function org-columns-compute "org-colview")
  2227. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  2228. '(org-columns-number-to-string org-columns-get-format-and-top-level
  2229. org-columns-compute org-agenda-columns org-columns-remove-overlays
  2230. org-columns org-insert-columns-dblock))
  2231. ;;; Variables for pre-computed regular expressions, all buffer local
  2232. (defvar org-drawer-regexp nil
  2233. "Matches first line of a hidden block.")
  2234. (make-variable-buffer-local 'org-drawer-regexp)
  2235. (defvar org-todo-regexp nil
  2236. "Matches any of the TODO state keywords.")
  2237. (make-variable-buffer-local 'org-todo-regexp)
  2238. (defvar org-not-done-regexp nil
  2239. "Matches any of the TODO state keywords except the last one.")
  2240. (make-variable-buffer-local 'org-not-done-regexp)
  2241. (defvar org-todo-line-regexp nil
  2242. "Matches a headline and puts TODO state into group 2 if present.")
  2243. (make-variable-buffer-local 'org-todo-line-regexp)
  2244. (defvar org-complex-heading-regexp nil
  2245. "Matches a headline and puts everything into groups:
  2246. group 1: the stars
  2247. group 2: The todo keyword, maybe
  2248. group 3: Priority cookie
  2249. group 4: True headline
  2250. group 5: Tags")
  2251. (make-variable-buffer-local 'org-complex-heading-regexp)
  2252. (defvar org-todo-line-tags-regexp nil
  2253. "Matches a headline and puts TODO state into group 2 if present.
  2254. Also put tags into group 4 if tags are present.")
  2255. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  2256. (defvar org-nl-done-regexp nil
  2257. "Matches newline followed by a headline with the DONE keyword.")
  2258. (make-variable-buffer-local 'org-nl-done-regexp)
  2259. (defvar org-looking-at-done-regexp nil
  2260. "Matches the DONE keyword a point.")
  2261. (make-variable-buffer-local 'org-looking-at-done-regexp)
  2262. (defvar org-ds-keyword-length 12
  2263. "Maximum length of the Deadline and SCHEDULED keywords.")
  2264. (make-variable-buffer-local 'org-ds-keyword-length)
  2265. (defvar org-deadline-regexp nil
  2266. "Matches the DEADLINE keyword.")
  2267. (make-variable-buffer-local 'org-deadline-regexp)
  2268. (defvar org-deadline-time-regexp nil
  2269. "Matches the DEADLINE keyword together with a time stamp.")
  2270. (make-variable-buffer-local 'org-deadline-time-regexp)
  2271. (defvar org-deadline-line-regexp nil
  2272. "Matches the DEADLINE keyword and the rest of the line.")
  2273. (make-variable-buffer-local 'org-deadline-line-regexp)
  2274. (defvar org-scheduled-regexp nil
  2275. "Matches the SCHEDULED keyword.")
  2276. (make-variable-buffer-local 'org-scheduled-regexp)
  2277. (defvar org-scheduled-time-regexp nil
  2278. "Matches the SCHEDULED keyword together with a time stamp.")
  2279. (make-variable-buffer-local 'org-scheduled-time-regexp)
  2280. (defvar org-closed-time-regexp nil
  2281. "Matches the CLOSED keyword together with a time stamp.")
  2282. (make-variable-buffer-local 'org-closed-time-regexp)
  2283. (defvar org-keyword-time-regexp nil
  2284. "Matches any of the 4 keywords, together with the time stamp.")
  2285. (make-variable-buffer-local 'org-keyword-time-regexp)
  2286. (defvar org-keyword-time-not-clock-regexp nil
  2287. "Matches any of the 3 keywords, together with the time stamp.")
  2288. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  2289. (defvar org-maybe-keyword-time-regexp nil
  2290. "Matches a timestamp, possibly preceeded by a keyword.")
  2291. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  2292. (defvar org-planning-or-clock-line-re nil
  2293. "Matches a line with planning or clock info.")
  2294. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  2295. (defconst org-plain-time-of-day-regexp
  2296. (concat
  2297. "\\(\\<[012]?[0-9]"
  2298. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2299. "\\(--?"
  2300. "\\(\\<[012]?[0-9]"
  2301. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2302. "\\)?")
  2303. "Regular expression to match a plain time or time range.
  2304. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2305. groups carry important information:
  2306. 0 the full match
  2307. 1 the first time, range or not
  2308. 8 the second time, if it is a range.")
  2309. (defconst org-plain-time-extension-regexp
  2310. (concat
  2311. "\\(\\<[012]?[0-9]"
  2312. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2313. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  2314. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  2315. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2316. groups carry important information:
  2317. 0 the full match
  2318. 7 hours of duration
  2319. 9 minutes of duration")
  2320. (defconst org-stamp-time-of-day-regexp
  2321. (concat
  2322. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  2323. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  2324. "\\(--?"
  2325. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  2326. "Regular expression to match a timestamp time or time range.
  2327. After a match, the following groups carry important information:
  2328. 0 the full match
  2329. 1 date plus weekday, for backreferencing to make sure both times on same day
  2330. 2 the first time, range or not
  2331. 4 the second time, if it is a range.")
  2332. (defconst org-startup-options
  2333. '(("fold" org-startup-folded t)
  2334. ("overview" org-startup-folded t)
  2335. ("nofold" org-startup-folded nil)
  2336. ("showall" org-startup-folded nil)
  2337. ("content" org-startup-folded content)
  2338. ("hidestars" org-hide-leading-stars t)
  2339. ("showstars" org-hide-leading-stars nil)
  2340. ("odd" org-odd-levels-only t)
  2341. ("oddeven" org-odd-levels-only nil)
  2342. ("align" org-startup-align-all-tables t)
  2343. ("noalign" org-startup-align-all-tables nil)
  2344. ("customtime" org-display-custom-times t)
  2345. ("logdone" org-log-done time)
  2346. ("lognotedone" org-log-done note)
  2347. ("nologdone" org-log-done nil)
  2348. ("lognoteclock-out" org-log-note-clock-out t)
  2349. ("nolognoteclock-out" org-log-note-clock-out nil)
  2350. ("logrepeat" org-log-repeat state)
  2351. ("lognoterepeat" org-log-repeat note)
  2352. ("nologrepeat" org-log-repeat nil)
  2353. ("constcgs" constants-unit-system cgs)
  2354. ("constSI" constants-unit-system SI))
  2355. "Variable associated with STARTUP options for org-mode.
  2356. Each element is a list of three items: The startup options as written
  2357. in the #+STARTUP line, the corresponding variable, and the value to
  2358. set this variable to if the option is found. An optional forth element PUSH
  2359. means to push this value onto the list in the variable.")
  2360. (defun org-set-regexps-and-options ()
  2361. "Precompute regular expressions for current buffer."
  2362. (when (org-mode-p)
  2363. (org-set-local 'org-todo-kwd-alist nil)
  2364. (org-set-local 'org-todo-key-alist nil)
  2365. (org-set-local 'org-todo-key-trigger nil)
  2366. (org-set-local 'org-todo-keywords-1 nil)
  2367. (org-set-local 'org-done-keywords nil)
  2368. (org-set-local 'org-todo-heads nil)
  2369. (org-set-local 'org-todo-sets nil)
  2370. (org-set-local 'org-todo-log-states nil)
  2371. (let ((re (org-make-options-regexp
  2372. '("CATEGORY" "SEQ_TODO" "TYP_TODO" "TODO" "COLUMNS"
  2373. "STARTUP" "ARCHIVE" "TAGS" "LINK" "PRIORITIES"
  2374. "CONSTANTS" "PROPERTY" "DRAWERS")))
  2375. (splitre "[ \t]+")
  2376. kwds kws0 kwsa key log value cat arch tags const links hw dws
  2377. tail sep kws1 prio props drawers)
  2378. (save-excursion
  2379. (save-restriction
  2380. (widen)
  2381. (goto-char (point-min))
  2382. (while (re-search-forward re nil t)
  2383. (setq key (match-string 1) value (org-match-string-no-properties 2))
  2384. (cond
  2385. ((equal key "CATEGORY")
  2386. (if (string-match "[ \t]+$" value)
  2387. (setq value (replace-match "" t t value)))
  2388. (setq cat value))
  2389. ((member key '("SEQ_TODO" "TODO"))
  2390. (push (cons 'sequence (org-split-string value splitre)) kwds))
  2391. ((equal key "TYP_TODO")
  2392. (push (cons 'type (org-split-string value splitre)) kwds))
  2393. ((equal key "TAGS")
  2394. (setq tags (append tags (org-split-string value splitre))))
  2395. ((equal key "COLUMNS")
  2396. (org-set-local 'org-columns-default-format value))
  2397. ((equal key "LINK")
  2398. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  2399. (push (cons (match-string 1 value)
  2400. (org-trim (match-string 2 value)))
  2401. links)))
  2402. ((equal key "PRIORITIES")
  2403. (setq prio (org-split-string value " +")))
  2404. ((equal key "PROPERTY")
  2405. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  2406. (push (cons (match-string 1 value) (match-string 2 value))
  2407. props)))
  2408. ((equal key "DRAWERS")
  2409. (setq drawers (org-split-string value splitre)))
  2410. ((equal key "CONSTANTS")
  2411. (setq const (append const (org-split-string value splitre))))
  2412. ((equal key "STARTUP")
  2413. (let ((opts (org-split-string value splitre))
  2414. l var val)
  2415. (while (setq l (pop opts))
  2416. (when (setq l (assoc l org-startup-options))
  2417. (setq var (nth 1 l) val (nth 2 l))
  2418. (if (not (nth 3 l))
  2419. (set (make-local-variable var) val)
  2420. (if (not (listp (symbol-value var)))
  2421. (set (make-local-variable var) nil))
  2422. (set (make-local-variable var) (symbol-value var))
  2423. (add-to-list var val))))))
  2424. ((equal key "ARCHIVE")
  2425. (string-match " *$" value)
  2426. (setq arch (replace-match "" t t value))
  2427. (remove-text-properties 0 (length arch)
  2428. '(face t fontified t) arch)))
  2429. )))
  2430. (when cat
  2431. (org-set-local 'org-category (intern cat))
  2432. (push (cons "CATEGORY" cat) props))
  2433. (when prio
  2434. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  2435. (setq prio (mapcar 'string-to-char prio))
  2436. (org-set-local 'org-highest-priority (nth 0 prio))
  2437. (org-set-local 'org-lowest-priority (nth 1 prio))
  2438. (org-set-local 'org-default-priority (nth 2 prio)))
  2439. (and props (org-set-local 'org-local-properties (nreverse props)))
  2440. (and drawers (org-set-local 'org-drawers drawers))
  2441. (and arch (org-set-local 'org-archive-location arch))
  2442. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  2443. ;; Process the TODO keywords
  2444. (unless kwds
  2445. ;; Use the global values as if they had been given locally.
  2446. (setq kwds (default-value 'org-todo-keywords))
  2447. (if (stringp (car kwds))
  2448. (setq kwds (list (cons org-todo-interpretation
  2449. (default-value 'org-todo-keywords)))))
  2450. (setq kwds (reverse kwds)))
  2451. (setq kwds (nreverse kwds))
  2452. (let (inter kws kw)
  2453. (while (setq kws (pop kwds))
  2454. (setq inter (pop kws) sep (member "|" kws)
  2455. kws0 (delete "|" (copy-sequence kws))
  2456. kwsa nil
  2457. kws1 (mapcar
  2458. (lambda (x)
  2459. ;; 1 2
  2460. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  2461. (progn
  2462. (setq kw (match-string 1 x)
  2463. key (and (match-end 2) (match-string 2 x))
  2464. log (org-extract-log-state-settings x))
  2465. (push (cons kw (and key (string-to-char key))) kwsa)
  2466. (and log (push log org-todo-log-states))
  2467. kw)
  2468. (error "Invalid TODO keyword %s" x)))
  2469. kws0)
  2470. kwsa (if kwsa (append '((:startgroup))
  2471. (nreverse kwsa)
  2472. '((:endgroup))))
  2473. hw (car kws1)
  2474. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  2475. tail (list inter hw (car dws) (org-last dws)))
  2476. (add-to-list 'org-todo-heads hw 'append)
  2477. (push kws1 org-todo-sets)
  2478. (setq org-done-keywords (append org-done-keywords dws nil))
  2479. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  2480. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  2481. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  2482. (setq org-todo-sets (nreverse org-todo-sets)
  2483. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  2484. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  2485. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  2486. ;; Process the constants
  2487. (when const
  2488. (let (e cst)
  2489. (while (setq e (pop const))
  2490. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  2491. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  2492. (setq org-table-formula-constants-local cst)))
  2493. ;; Process the tags.
  2494. (when tags
  2495. (let (e tgs)
  2496. (while (setq e (pop tags))
  2497. (cond
  2498. ((equal e "{") (push '(:startgroup) tgs))
  2499. ((equal e "}") (push '(:endgroup) tgs))
  2500. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  2501. (push (cons (match-string 1 e)
  2502. (string-to-char (match-string 2 e)))
  2503. tgs))
  2504. (t (push (list e) tgs))))
  2505. (org-set-local 'org-tag-alist nil)
  2506. (while (setq e (pop tgs))
  2507. (or (and (stringp (car e))
  2508. (assoc (car e) org-tag-alist))
  2509. (push e org-tag-alist))))))
  2510. ;; Compute the regular expressions and other local variables
  2511. (if (not org-done-keywords)
  2512. (setq org-done-keywords (list (org-last org-todo-keywords-1))))
  2513. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  2514. (length org-scheduled-string)
  2515. (length org-clock-string)
  2516. (length org-closed-string)))
  2517. org-drawer-regexp
  2518. (concat "^[ \t]*:\\("
  2519. (mapconcat 'regexp-quote org-drawers "\\|")
  2520. "\\):[ \t]*$")
  2521. org-not-done-keywords
  2522. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  2523. org-todo-regexp
  2524. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  2525. "\\|") "\\)\\>")
  2526. org-not-done-regexp
  2527. (concat "\\<\\("
  2528. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  2529. "\\)\\>")
  2530. org-todo-line-regexp
  2531. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  2532. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2533. "\\)\\>\\)?[ \t]*\\(.*\\)")
  2534. org-complex-heading-regexp
  2535. (concat "^\\(\\*+\\)\\(?:[ \t]+\\("
  2536. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2537. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  2538. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  2539. org-nl-done-regexp
  2540. (concat "\n\\*+[ \t]+"
  2541. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  2542. "\\)" "\\>")
  2543. org-todo-line-tags-regexp
  2544. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  2545. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2546. (org-re
  2547. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  2548. org-looking-at-done-regexp
  2549. (concat "^" "\\(?:"
  2550. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  2551. "\\>")
  2552. org-deadline-regexp (concat "\\<" org-deadline-string)
  2553. org-deadline-time-regexp
  2554. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  2555. org-deadline-line-regexp
  2556. (concat "\\<\\(" org-deadline-string "\\).*")
  2557. org-scheduled-regexp
  2558. (concat "\\<" org-scheduled-string)
  2559. org-scheduled-time-regexp
  2560. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  2561. org-closed-time-regexp
  2562. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  2563. org-keyword-time-regexp
  2564. (concat "\\<\\(" org-scheduled-string
  2565. "\\|" org-deadline-string
  2566. "\\|" org-closed-string
  2567. "\\|" org-clock-string "\\)"
  2568. " *[[<]\\([^]>]+\\)[]>]")
  2569. org-keyword-time-not-clock-regexp
  2570. (concat "\\<\\(" org-scheduled-string
  2571. "\\|" org-deadline-string
  2572. "\\|" org-closed-string
  2573. "\\)"
  2574. " *[[<]\\([^]>]+\\)[]>]")
  2575. org-maybe-keyword-time-regexp
  2576. (concat "\\(\\<\\(" org-scheduled-string
  2577. "\\|" org-deadline-string
  2578. "\\|" org-closed-string
  2579. "\\|" org-clock-string "\\)\\)?"
  2580. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  2581. org-planning-or-clock-line-re
  2582. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  2583. "\\|" org-deadline-string
  2584. "\\|" org-closed-string "\\|" org-clock-string
  2585. "\\)\\>\\)")
  2586. )
  2587. (org-compute-latex-and-specials-regexp)
  2588. (org-set-font-lock-defaults)))
  2589. (defun org-extract-log-state-settings (x)
  2590. "Extract the log state setting from a TODO keyword string.
  2591. This will extract info from a string like \"WAIT(w@/!)\"."
  2592. (let (kw key log1 log2)
  2593. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  2594. (setq kw (match-string 1 x)
  2595. key (and (match-end 2) (match-string 2 x))
  2596. log1 (and (match-end 3) (match-string 3 x))
  2597. log2 (and (match-end 4) (match-string 4 x)))
  2598. (and (or log1 log2)
  2599. (list kw
  2600. (and log1 (if (equal log1 "!") 'time 'note))
  2601. (and log2 (if (equal log2 "!") 'time 'note)))))))
  2602. (defun org-remove-keyword-keys (list)
  2603. "Remove a pair of parenthesis at the end of each string in LIST."
  2604. (mapcar (lambda (x)
  2605. (if (string-match "(.*)$" x)
  2606. (substring x 0 (match-beginning 0))
  2607. x))
  2608. list))
  2609. ;; FIXME: this could be done much better, using second characters etc.
  2610. (defun org-assign-fast-keys (alist)
  2611. "Assign fast keys to a keyword-key alist.
  2612. Respect keys that are already there."
  2613. (let (new e k c c1 c2 (char ?a))
  2614. (while (setq e (pop alist))
  2615. (cond
  2616. ((equal e '(:startgroup)) (push e new))
  2617. ((equal e '(:endgroup)) (push e new))
  2618. (t
  2619. (setq k (car e) c2 nil)
  2620. (if (cdr e)
  2621. (setq c (cdr e))
  2622. ;; automatically assign a character.
  2623. (setq c1 (string-to-char
  2624. (downcase (substring
  2625. k (if (= (string-to-char k) ?@) 1 0)))))
  2626. (if (or (rassoc c1 new) (rassoc c1 alist))
  2627. (while (or (rassoc char new) (rassoc char alist))
  2628. (setq char (1+ char)))
  2629. (setq c2 c1))
  2630. (setq c (or c2 char)))
  2631. (push (cons k c) new))))
  2632. (nreverse new)))
  2633. ;;; Some variables used in various places
  2634. (defvar org-window-configuration nil
  2635. "Used in various places to store a window configuration.")
  2636. (defvar org-finish-function nil
  2637. "Function to be called when `C-c C-c' is used.
  2638. This is for getting out of special buffers like remember.")
  2639. ;; FIXME: Occasionally check by commenting these, to make sure
  2640. ;; no other functions uses these, forgetting to let-bind them.
  2641. (defvar entry)
  2642. (defvar state)
  2643. (defvar last-state)
  2644. (defvar date)
  2645. (defvar description)
  2646. ;; Defined somewhere in this file, but used before definition.
  2647. (defvar org-html-entities)
  2648. (defvar org-struct-menu)
  2649. (defvar org-org-menu)
  2650. (defvar org-tbl-menu)
  2651. (defvar org-agenda-keymap)
  2652. ;;;; Define the Org-mode
  2653. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  2654. (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."))
  2655. ;; We use a before-change function to check if a table might need
  2656. ;; an update.
  2657. (defvar org-table-may-need-update t
  2658. "Indicates that a table might need an update.
  2659. This variable is set by `org-before-change-function'.
  2660. `org-table-align' sets it back to nil.")
  2661. (defun org-before-change-function (beg end)
  2662. "Every change indicates that a table might need an update."
  2663. (setq org-table-may-need-update t))
  2664. (defvar org-mode-map)
  2665. (defvar org-mode-hook nil)
  2666. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  2667. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  2668. (defvar org-table-buffer-is-an nil)
  2669. (defconst org-outline-regexp "\\*+ ")
  2670. ;;;###autoload
  2671. (define-derived-mode org-mode outline-mode "Org"
  2672. "Outline-based notes management and organizer, alias
  2673. \"Carsten's outline-mode for keeping track of everything.\"
  2674. Org-mode develops organizational tasks around a NOTES file which
  2675. contains information about projects as plain text. Org-mode is
  2676. implemented on top of outline-mode, which is ideal to keep the content
  2677. of large files well structured. It supports ToDo items, deadlines and
  2678. time stamps, which magically appear in the diary listing of the Emacs
  2679. calendar. Tables are easily created with a built-in table editor.
  2680. Plain text URL-like links connect to websites, emails (VM), Usenet
  2681. messages (Gnus), BBDB entries, and any files related to the project.
  2682. For printing and sharing of notes, an Org-mode file (or a part of it)
  2683. can be exported as a structured ASCII or HTML file.
  2684. The following commands are available:
  2685. \\{org-mode-map}"
  2686. ;; Get rid of Outline menus, they are not needed
  2687. ;; Need to do this here because define-derived-mode sets up
  2688. ;; the keymap so late. Still, it is a waste to call this each time
  2689. ;; we switch another buffer into org-mode.
  2690. (if (featurep 'xemacs)
  2691. (when (boundp 'outline-mode-menu-heading)
  2692. ;; Assume this is Greg's port, it used easymenu
  2693. (easy-menu-remove outline-mode-menu-heading)
  2694. (easy-menu-remove outline-mode-menu-show)
  2695. (easy-menu-remove outline-mode-menu-hide))
  2696. (define-key org-mode-map [menu-bar headings] 'undefined)
  2697. (define-key org-mode-map [menu-bar hide] 'undefined)
  2698. (define-key org-mode-map [menu-bar show] 'undefined))
  2699. (org-load-modules-maybe)
  2700. (easy-menu-add org-org-menu)
  2701. (easy-menu-add org-tbl-menu)
  2702. (org-install-agenda-files-menu)
  2703. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  2704. (org-add-to-invisibility-spec '(org-cwidth))
  2705. (when (featurep 'xemacs)
  2706. (org-set-local 'line-move-ignore-invisible t))
  2707. (org-set-local 'outline-regexp org-outline-regexp)
  2708. (org-set-local 'outline-level 'org-outline-level)
  2709. (when (and org-ellipsis
  2710. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  2711. (fboundp 'make-glyph-code))
  2712. (unless org-display-table
  2713. (setq org-display-table (make-display-table)))
  2714. (set-display-table-slot
  2715. org-display-table 4
  2716. (vconcat (mapcar
  2717. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  2718. org-ellipsis)))
  2719. (if (stringp org-ellipsis) org-ellipsis "..."))))
  2720. (setq buffer-display-table org-display-table))
  2721. (org-set-regexps-and-options)
  2722. ;; Calc embedded
  2723. (org-set-local 'calc-embedded-open-mode "# ")
  2724. (modify-syntax-entry ?# "<")
  2725. (modify-syntax-entry ?@ "w")
  2726. (if org-startup-truncated (setq truncate-lines t))
  2727. (org-set-local 'font-lock-unfontify-region-function
  2728. 'org-unfontify-region)
  2729. ;; Activate before-change-function
  2730. (org-set-local 'org-table-may-need-update t)
  2731. (org-add-hook 'before-change-functions 'org-before-change-function nil
  2732. 'local)
  2733. ;; Check for running clock before killing a buffer
  2734. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  2735. ;; Paragraphs and auto-filling
  2736. (org-set-autofill-regexps)
  2737. (setq indent-line-function 'org-indent-line-function)
  2738. (org-update-radio-target-regexp)
  2739. ;; Comment characters
  2740. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  2741. (org-set-local 'comment-padding " ")
  2742. ;; Align options lines
  2743. (org-set-local
  2744. 'align-mode-rules-list
  2745. '((org-in-buffer-settings
  2746. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  2747. (modes . '(org-mode)))))
  2748. ;; Imenu
  2749. (org-set-local 'imenu-create-index-function
  2750. 'org-imenu-get-tree)
  2751. ;; Make isearch reveal context
  2752. (if (or (featurep 'xemacs)
  2753. (not (boundp 'outline-isearch-open-invisible-function)))
  2754. ;; Emacs 21 and XEmacs make use of the hook
  2755. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  2756. ;; Emacs 22 deals with this through a special variable
  2757. (org-set-local 'outline-isearch-open-invisible-function
  2758. (lambda (&rest ignore) (org-show-context 'isearch))))
  2759. ;; If empty file that did not turn on org-mode automatically, make it to.
  2760. (if (and org-insert-mode-line-in-empty-file
  2761. (interactive-p)
  2762. (= (point-min) (point-max)))
  2763. (insert "# -*- mode: org -*-\n\n"))
  2764. (unless org-inhibit-startup
  2765. (when org-startup-align-all-tables
  2766. (let ((bmp (buffer-modified-p)))
  2767. (org-table-map-tables 'org-table-align)
  2768. (set-buffer-modified-p bmp)))
  2769. (org-cycle-hide-drawers 'all)
  2770. (cond
  2771. ((eq org-startup-folded t)
  2772. (org-cycle '(4)))
  2773. ((eq org-startup-folded 'content)
  2774. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  2775. (org-cycle '(4)) (org-cycle '(4)))))))
  2776. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  2777. (defun org-current-time ()
  2778. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  2779. (if (> (car org-time-stamp-rounding-minutes) 1)
  2780. (let ((r (car org-time-stamp-rounding-minutes))
  2781. (time (decode-time)))
  2782. (apply 'encode-time
  2783. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  2784. (nthcdr 2 time))))
  2785. (current-time)))
  2786. ;;;; Font-Lock stuff, including the activators
  2787. (defvar org-mouse-map (make-sparse-keymap))
  2788. (org-defkey org-mouse-map
  2789. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  2790. (org-defkey org-mouse-map
  2791. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  2792. (when org-mouse-1-follows-link
  2793. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  2794. (when org-tab-follows-link
  2795. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  2796. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  2797. (when org-return-follows-link
  2798. (org-defkey org-mouse-map [(return)] 'org-open-at-point)
  2799. (org-defkey org-mouse-map "\C-m" 'org-open-at-point))
  2800. (require 'font-lock)
  2801. (defconst org-non-link-chars "]\t\n\r<>")
  2802. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  2803. "shell" "elisp"))
  2804. (defvar org-link-types-re nil
  2805. "Matches a link that has a url-like prefix like \"http:\"")
  2806. (defvar org-link-re-with-space nil
  2807. "Matches a link with spaces, optional angular brackets around it.")
  2808. (defvar org-link-re-with-space2 nil
  2809. "Matches a link with spaces, optional angular brackets around it.")
  2810. (defvar org-angle-link-re nil
  2811. "Matches link with angular brackets, spaces are allowed.")
  2812. (defvar org-plain-link-re nil
  2813. "Matches plain link, without spaces.")
  2814. (defvar org-bracket-link-regexp nil
  2815. "Matches a link in double brackets.")
  2816. (defvar org-bracket-link-analytic-regexp nil
  2817. "Regular expression used to analyze links.
  2818. Here is what the match groups contain after a match:
  2819. 1: http:
  2820. 2: http
  2821. 3: path
  2822. 4: [desc]
  2823. 5: desc")
  2824. (defvar org-any-link-re nil
  2825. "Regular expression matching any link.")
  2826. (defun org-make-link-regexps ()
  2827. "Update the link regular expressions.
  2828. This should be called after the variable `org-link-types' has changed."
  2829. (setq org-link-types-re
  2830. (concat
  2831. "\\`\\(" (mapconcat 'identity org-link-types "\\|") "\\):")
  2832. org-link-re-with-space
  2833. (concat
  2834. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2835. "\\([^" org-non-link-chars " ]"
  2836. "[^" org-non-link-chars "]*"
  2837. "[^" org-non-link-chars " ]\\)>?")
  2838. org-link-re-with-space2
  2839. (concat
  2840. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2841. "\\([^" org-non-link-chars " ]"
  2842. "[^]\t\n\r]*"
  2843. "[^" org-non-link-chars " ]\\)>?")
  2844. org-angle-link-re
  2845. (concat
  2846. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2847. "\\([^" org-non-link-chars " ]"
  2848. "[^" org-non-link-chars "]*"
  2849. "\\)>")
  2850. org-plain-link-re
  2851. (concat
  2852. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2853. "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  2854. org-bracket-link-regexp
  2855. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  2856. org-bracket-link-analytic-regexp
  2857. (concat
  2858. "\\[\\["
  2859. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  2860. "\\([^]]+\\)"
  2861. "\\]"
  2862. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  2863. "\\]")
  2864. org-any-link-re
  2865. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  2866. org-angle-link-re "\\)\\|\\("
  2867. org-plain-link-re "\\)")))
  2868. (org-make-link-regexps)
  2869. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  2870. "Regular expression for fast time stamp matching.")
  2871. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  2872. "Regular expression for fast time stamp matching.")
  2873. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  2874. "Regular expression matching time strings for analysis.
  2875. This one does not require the space after the date, so it can be used
  2876. on a string that terminates immediately after the date.")
  2877. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  2878. "Regular expression matching time strings for analysis.")
  2879. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  2880. "Regular expression matching time stamps, with groups.")
  2881. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  2882. "Regular expression matching time stamps (also [..]), with groups.")
  2883. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  2884. "Regular expression matching a time stamp range.")
  2885. (defconst org-tr-regexp-both
  2886. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  2887. "Regular expression matching a time stamp range.")
  2888. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  2889. org-ts-regexp "\\)?")
  2890. "Regular expression matching a time stamp or time stamp range.")
  2891. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  2892. org-ts-regexp-both "\\)?")
  2893. "Regular expression matching a time stamp or time stamp range.
  2894. The time stamps may be either active or inactive.")
  2895. (defvar org-emph-face nil)
  2896. (defun org-do-emphasis-faces (limit)
  2897. "Run through the buffer and add overlays to links."
  2898. (let (rtn)
  2899. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  2900. (if (not (= (char-after (match-beginning 3))
  2901. (char-after (match-beginning 4))))
  2902. (progn
  2903. (setq rtn t)
  2904. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  2905. 'face
  2906. (nth 1 (assoc (match-string 3)
  2907. org-emphasis-alist)))
  2908. (add-text-properties (match-beginning 2) (match-end 2)
  2909. '(font-lock-multiline t))
  2910. (when org-hide-emphasis-markers
  2911. (add-text-properties (match-end 4) (match-beginning 5)
  2912. '(invisible org-link))
  2913. (add-text-properties (match-beginning 3) (match-end 3)
  2914. '(invisible org-link)))))
  2915. (backward-char 1))
  2916. rtn))
  2917. (defun org-emphasize (&optional char)
  2918. "Insert or change an emphasis, i.e. a font like bold or italic.
  2919. If there is an active region, change that region to a new emphasis.
  2920. If there is no region, just insert the marker characters and position
  2921. the cursor between them.
  2922. CHAR should be either the marker character, or the first character of the
  2923. HTML tag associated with that emphasis. If CHAR is a space, the means
  2924. to remove the emphasis of the selected region.
  2925. If char is not given (for example in an interactive call) it
  2926. will be prompted for."
  2927. (interactive)
  2928. (let ((eal org-emphasis-alist) e det
  2929. (erc org-emphasis-regexp-components)
  2930. (prompt "")
  2931. (string "") beg end move tag c s)
  2932. (if (org-region-active-p)
  2933. (setq beg (region-beginning) end (region-end)
  2934. string (buffer-substring beg end))
  2935. (setq move t))
  2936. (while (setq e (pop eal))
  2937. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  2938. c (aref tag 0))
  2939. (push (cons c (string-to-char (car e))) det)
  2940. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  2941. (substring tag 1)))))
  2942. (unless char
  2943. (message "%s" (concat "Emphasis marker or tag:" prompt))
  2944. (setq char (read-char-exclusive)))
  2945. (setq char (or (cdr (assoc char det)) char))
  2946. (if (equal char ?\ )
  2947. (setq s "" move nil)
  2948. (unless (assoc (char-to-string char) org-emphasis-alist)
  2949. (error "No such emphasis marker: \"%c\"" char))
  2950. (setq s (char-to-string char)))
  2951. (while (and (> (length string) 1)
  2952. (equal (substring string 0 1) (substring string -1))
  2953. (assoc (substring string 0 1) org-emphasis-alist))
  2954. (setq string (substring string 1 -1)))
  2955. (setq string (concat s string s))
  2956. (if beg (delete-region beg end))
  2957. (unless (or (bolp)
  2958. (string-match (concat "[" (nth 0 erc) "\n]")
  2959. (char-to-string (char-before (point)))))
  2960. (insert " "))
  2961. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  2962. (char-to-string (char-after (point))))
  2963. (insert " ") (backward-char 1))
  2964. (insert string)
  2965. (and move (backward-char 1))))
  2966. (defconst org-nonsticky-props
  2967. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  2968. (defun org-activate-plain-links (limit)
  2969. "Run through the buffer and add overlays to links."
  2970. (catch 'exit
  2971. (let (f)
  2972. (while (re-search-forward org-plain-link-re limit t)
  2973. (setq f (get-text-property (match-beginning 0) 'face))
  2974. (if (or (eq f 'org-tag)
  2975. (and (listp f) (memq 'org-tag f)))
  2976. nil
  2977. (add-text-properties (match-beginning 0) (match-end 0)
  2978. (list 'mouse-face 'highlight
  2979. 'rear-nonsticky org-nonsticky-props
  2980. 'keymap org-mouse-map
  2981. ))
  2982. (throw 'exit t))))))
  2983. (defun org-activate-code (limit)
  2984. (if (re-search-forward "^[ \t]*\\(:.*\\)" limit t)
  2985. (unless (get-text-property (match-beginning 1) 'face)
  2986. (remove-text-properties (match-beginning 0) (match-end 0)
  2987. '(display t invisible t intangible t))
  2988. t)))
  2989. (defun org-activate-angle-links (limit)
  2990. "Run through the buffer and add overlays to links."
  2991. (if (re-search-forward org-angle-link-re limit t)
  2992. (progn
  2993. (add-text-properties (match-beginning 0) (match-end 0)
  2994. (list 'mouse-face 'highlight
  2995. 'rear-nonsticky org-nonsticky-props
  2996. 'keymap org-mouse-map
  2997. ))
  2998. t)))
  2999. (defun org-activate-bracket-links (limit)
  3000. "Run through the buffer and add overlays to bracketed links."
  3001. (if (re-search-forward org-bracket-link-regexp limit t)
  3002. (let* ((help (concat "LINK: "
  3003. (org-match-string-no-properties 1)))
  3004. ;; FIXME: above we should remove the escapes.
  3005. ;; but that requires another match, protecting match data,
  3006. ;; a lot of overhead for font-lock.
  3007. (ip (org-maybe-intangible
  3008. (list 'invisible 'org-link 'rear-nonsticky org-nonsticky-props
  3009. 'keymap org-mouse-map 'mouse-face 'highlight
  3010. 'font-lock-multiline t 'help-echo help)))
  3011. (vp (list 'rear-nonsticky org-nonsticky-props
  3012. 'keymap org-mouse-map 'mouse-face 'highlight
  3013. ' font-lock-multiline t 'help-echo help)))
  3014. ;; We need to remove the invisible property here. Table narrowing
  3015. ;; may have made some of this invisible.
  3016. (remove-text-properties (match-beginning 0) (match-end 0)
  3017. '(invisible nil))
  3018. (if (match-end 3)
  3019. (progn
  3020. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  3021. (add-text-properties (match-beginning 3) (match-end 3) vp)
  3022. (add-text-properties (match-end 3) (match-end 0) ip))
  3023. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  3024. (add-text-properties (match-beginning 1) (match-end 1) vp)
  3025. (add-text-properties (match-end 1) (match-end 0) ip))
  3026. t)))
  3027. (defun org-activate-dates (limit)
  3028. "Run through the buffer and add overlays to dates."
  3029. (if (re-search-forward org-tsr-regexp-both limit t)
  3030. (progn
  3031. (add-text-properties (match-beginning 0) (match-end 0)
  3032. (list 'mouse-face 'highlight
  3033. 'rear-nonsticky org-nonsticky-props
  3034. 'keymap org-mouse-map))
  3035. (when org-display-custom-times
  3036. (if (match-end 3)
  3037. (org-display-custom-time (match-beginning 3) (match-end 3)))
  3038. (org-display-custom-time (match-beginning 1) (match-end 1)))
  3039. t)))
  3040. (defvar org-target-link-regexp nil
  3041. "Regular expression matching radio targets in plain text.")
  3042. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  3043. "Regular expression matching a link target.")
  3044. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  3045. "Regular expression matching a radio target.")
  3046. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  3047. "Regular expression matching any target.")
  3048. (defun org-activate-target-links (limit)
  3049. "Run through the buffer and add overlays to target matches."
  3050. (when org-target-link-regexp
  3051. (let ((case-fold-search t))
  3052. (if (re-search-forward org-target-link-regexp limit t)
  3053. (progn
  3054. (add-text-properties (match-beginning 0) (match-end 0)
  3055. (list 'mouse-face 'highlight
  3056. 'rear-nonsticky org-nonsticky-props
  3057. 'keymap org-mouse-map
  3058. 'help-echo "Radio target link"
  3059. 'org-linked-text t))
  3060. t)))))
  3061. (defun org-update-radio-target-regexp ()
  3062. "Find all radio targets in this file and update the regular expression."
  3063. (interactive)
  3064. (when (memq 'radio org-activate-links)
  3065. (setq org-target-link-regexp
  3066. (org-make-target-link-regexp (org-all-targets 'radio)))
  3067. (org-restart-font-lock)))
  3068. (defun org-hide-wide-columns (limit)
  3069. (let (s e)
  3070. (setq s (text-property-any (point) (or limit (point-max))
  3071. 'org-cwidth t))
  3072. (when s
  3073. (setq e (next-single-property-change s 'org-cwidth))
  3074. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  3075. (goto-char e)
  3076. t)))
  3077. (defvar org-latex-and-specials-regexp nil
  3078. "Regular expression for highlighting export special stuff.")
  3079. (defvar org-match-substring-regexp)
  3080. (defvar org-match-substring-with-braces-regexp)
  3081. (defvar org-export-html-special-string-regexps)
  3082. (defun org-compute-latex-and-specials-regexp ()
  3083. "Compute regular expression for stuff treated specially by exporters."
  3084. (if (not org-highlight-latex-fragments-and-specials)
  3085. (org-set-local 'org-latex-and-specials-regexp nil)
  3086. (require 'org-exp)
  3087. (let*
  3088. ((matchers (plist-get org-format-latex-options :matchers))
  3089. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  3090. org-latex-regexps)))
  3091. (options (org-combine-plists (org-default-export-plist)
  3092. (org-infile-export-plist)))
  3093. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  3094. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  3095. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  3096. (org-export-html-expand (plist-get options :expand-quoted-html))
  3097. (org-export-with-special-strings (plist-get options :special-strings))
  3098. (re-sub
  3099. (cond
  3100. ((equal org-export-with-sub-superscripts '{})
  3101. (list org-match-substring-with-braces-regexp))
  3102. (org-export-with-sub-superscripts
  3103. (list org-match-substring-regexp))
  3104. (t nil)))
  3105. (re-latex
  3106. (if org-export-with-LaTeX-fragments
  3107. (mapcar (lambda (x) (nth 1 x)) latexs)))
  3108. (re-macros
  3109. (if org-export-with-TeX-macros
  3110. (list (concat "\\\\"
  3111. (regexp-opt
  3112. (append (mapcar 'car org-html-entities)
  3113. (if (boundp 'org-latex-entities)
  3114. org-latex-entities nil))
  3115. 'words))) ; FIXME
  3116. ))
  3117. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  3118. (re-special (if org-export-with-special-strings
  3119. (mapcar (lambda (x) (car x))
  3120. org-export-html-special-string-regexps)))
  3121. (re-rest
  3122. (delq nil
  3123. (list
  3124. (if org-export-html-expand "@<[^>\n]+>")
  3125. ))))
  3126. (org-set-local
  3127. 'org-latex-and-specials-regexp
  3128. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  3129. re-rest) "\\|")))))
  3130. (defun org-do-latex-and-special-faces (limit)
  3131. "Run through the buffer and add overlays to links."
  3132. (when org-latex-and-specials-regexp
  3133. (let (rtn d)
  3134. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  3135. limit t))
  3136. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  3137. 'face))
  3138. '(org-code org-verbatim underline)))
  3139. (progn
  3140. (setq rtn t
  3141. d (cond ((member (char-after (1+ (match-beginning 0)))
  3142. '(?_ ?^)) 1)
  3143. (t 0)))
  3144. (font-lock-prepend-text-property
  3145. (+ d (match-beginning 0)) (match-end 0)
  3146. 'face 'org-latex-and-export-specials)
  3147. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  3148. '(font-lock-multiline t)))))
  3149. rtn)))
  3150. (defun org-restart-font-lock ()
  3151. "Restart font-lock-mode, to force refontification."
  3152. (when (and (boundp 'font-lock-mode) font-lock-mode)
  3153. (font-lock-mode -1)
  3154. (font-lock-mode 1)))
  3155. (defun org-all-targets (&optional radio)
  3156. "Return a list of all targets in this file.
  3157. With optional argument RADIO, only find radio targets."
  3158. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  3159. rtn)
  3160. (save-excursion
  3161. (goto-char (point-min))
  3162. (while (re-search-forward re nil t)
  3163. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  3164. rtn)))
  3165. (defun org-make-target-link-regexp (targets)
  3166. "Make regular expression matching all strings in TARGETS.
  3167. The regular expression finds the targets also if there is a line break
  3168. between words."
  3169. (and targets
  3170. (concat
  3171. "\\<\\("
  3172. (mapconcat
  3173. (lambda (x)
  3174. (while (string-match " +" x)
  3175. (setq x (replace-match "\\s-+" t t x)))
  3176. x)
  3177. targets
  3178. "\\|")
  3179. "\\)\\>")))
  3180. (defun org-activate-tags (limit)
  3181. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  3182. (progn
  3183. (add-text-properties (match-beginning 1) (match-end 1)
  3184. (list 'mouse-face 'highlight
  3185. 'rear-nonsticky org-nonsticky-props
  3186. 'keymap org-mouse-map))
  3187. t)))
  3188. (defun org-outline-level ()
  3189. (save-excursion
  3190. (looking-at outline-regexp)
  3191. (if (match-beginning 1)
  3192. (+ (org-get-string-indentation (match-string 1)) 1000)
  3193. (1- (- (match-end 0) (match-beginning 0))))))
  3194. (defvar org-font-lock-keywords nil)
  3195. (defconst org-property-re (org-re "^[ \t]*\\(:\\([[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  3196. "Regular expression matching a property line.")
  3197. (defvar org-font-lock-hook nil
  3198. "Functions to be called for special font loch stuff.")
  3199. (defun org-font-lock-hook (limit)
  3200. (run-hook-with-args 'org-font-lock-hook limit))
  3201. (defun org-set-font-lock-defaults ()
  3202. (let* ((em org-fontify-emphasized-text)
  3203. (lk org-activate-links)
  3204. (org-font-lock-extra-keywords
  3205. (list
  3206. ;; Call the hook
  3207. '(org-font-lock-hook)
  3208. ;; Headlines
  3209. '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
  3210. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  3211. ;; Table lines
  3212. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  3213. (1 'org-table t))
  3214. ;; Table internals
  3215. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  3216. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  3217. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  3218. ;; Drawers
  3219. (list org-drawer-regexp '(0 'org-special-keyword t))
  3220. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  3221. ;; Properties
  3222. (list org-property-re
  3223. '(1 'org-special-keyword t)
  3224. '(3 'org-property-value t))
  3225. (if org-format-transports-properties-p
  3226. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  3227. ;; Links
  3228. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  3229. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  3230. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  3231. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  3232. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  3233. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  3234. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  3235. '(org-hide-wide-columns (0 nil append))
  3236. ;; TODO lines
  3237. (list (concat "^\\*+[ \t]+" org-todo-regexp)
  3238. '(1 (org-get-todo-face 1) t))
  3239. ;; DONE
  3240. (if org-fontify-done-headline
  3241. (list (concat "^[*]+ +\\<\\("
  3242. (mapconcat 'regexp-quote org-done-keywords "\\|")
  3243. "\\)\\(.*\\)")
  3244. '(2 'org-headline-done t))
  3245. nil)
  3246. ;; Priorities
  3247. (list (concat "\\[#[A-Z0-9]\\]") '(0 'org-special-keyword t))
  3248. ;; Special keywords
  3249. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  3250. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  3251. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  3252. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  3253. ;; Emphasis
  3254. (if em
  3255. (if (featurep 'xemacs)
  3256. '(org-do-emphasis-faces (0 nil append))
  3257. '(org-do-emphasis-faces)))
  3258. ;; Checkboxes
  3259. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  3260. 2 'bold prepend)
  3261. (if org-provide-checkbox-statistics
  3262. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  3263. (0 (org-get-checkbox-statistics-face) t)))
  3264. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  3265. '(1 'org-archived prepend))
  3266. ;; Specials
  3267. '(org-do-latex-and-special-faces)
  3268. ;; Code
  3269. '(org-activate-code (1 'org-code t))
  3270. ;; COMMENT
  3271. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  3272. "\\|" org-quote-string "\\)\\>")
  3273. '(1 'org-special-keyword t))
  3274. '("^#.*" (0 'font-lock-comment-face t))
  3275. )))
  3276. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  3277. ;; Now set the full font-lock-keywords
  3278. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  3279. (org-set-local 'font-lock-defaults
  3280. '(org-font-lock-keywords t nil nil backward-paragraph))
  3281. (kill-local-variable 'font-lock-keywords) nil))
  3282. (defvar org-m nil)
  3283. (defvar org-l nil)
  3284. (defvar org-f nil)
  3285. (defun org-get-level-face (n)
  3286. "Get the right face for match N in font-lock matching of healdines."
  3287. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  3288. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  3289. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  3290. (cond
  3291. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  3292. ((eq n 2) org-f)
  3293. (t (if org-level-color-stars-only nil org-f))))
  3294. (defun org-get-todo-face (kwd)
  3295. "Get the right face for a TODO keyword KWD.
  3296. If KWD is a number, get the corresponding match group."
  3297. (if (numberp kwd) (setq kwd (match-string kwd)))
  3298. (or (cdr (assoc kwd org-todo-keyword-faces))
  3299. (and (member kwd org-done-keywords) 'org-done)
  3300. 'org-todo))
  3301. (defun org-unfontify-region (beg end &optional maybe_loudly)
  3302. "Remove fontification and activation overlays from links."
  3303. (font-lock-default-unfontify-region beg end)
  3304. (let* ((buffer-undo-list t)
  3305. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  3306. (inhibit-modification-hooks t)
  3307. deactivate-mark buffer-file-name buffer-file-truename)
  3308. (remove-text-properties beg end
  3309. '(mouse-face t keymap t org-linked-text t
  3310. invisible t intangible t))))
  3311. ;;;; Visibility cycling, including org-goto and indirect buffer
  3312. ;;; Cycling
  3313. (defvar org-cycle-global-status nil)
  3314. (make-variable-buffer-local 'org-cycle-global-status)
  3315. (defvar org-cycle-subtree-status nil)
  3316. (make-variable-buffer-local 'org-cycle-subtree-status)
  3317. ;;;###autoload
  3318. (defun org-cycle (&optional arg)
  3319. "Visibility cycling for Org-mode.
  3320. - When this function is called with a prefix argument, rotate the entire
  3321. buffer through 3 states (global cycling)
  3322. 1. OVERVIEW: Show only top-level headlines.
  3323. 2. CONTENTS: Show all headlines of all levels, but no body text.
  3324. 3. SHOW ALL: Show everything.
  3325. - When point is at the beginning of a headline, rotate the subtree started
  3326. by this line through 3 different states (local cycling)
  3327. 1. FOLDED: Only the main headline is shown.
  3328. 2. CHILDREN: The main headline and the direct children are shown.
  3329. From this state, you can move to one of the children
  3330. and zoom in further.
  3331. 3. SUBTREE: Show the entire subtree, including body text.
  3332. - When there is a numeric prefix, go up to a heading with level ARG, do
  3333. a `show-subtree' and return to the previous cursor position. If ARG
  3334. is negative, go up that many levels.
  3335. - When point is not at the beginning of a headline, execute
  3336. `indent-relative', like TAB normally does. See the option
  3337. `org-cycle-emulate-tab' for details.
  3338. - Special case: if point is at the beginning of the buffer and there is
  3339. no headline in line 1, this function will act as if called with prefix arg.
  3340. But only if also the variable `org-cycle-global-at-bob' is t."
  3341. (interactive "P")
  3342. (org-load-modules-maybe)
  3343. (let* ((outline-regexp
  3344. (if (and (org-mode-p) org-cycle-include-plain-lists)
  3345. "\\(?:\\*+ \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"
  3346. outline-regexp))
  3347. (bob-special (and org-cycle-global-at-bob (bobp)
  3348. (not (looking-at outline-regexp))))
  3349. (org-cycle-hook
  3350. (if bob-special
  3351. (delq 'org-optimize-window-after-visibility-change
  3352. (copy-sequence org-cycle-hook))
  3353. org-cycle-hook))
  3354. (pos (point)))
  3355. (if (or bob-special (equal arg '(4)))
  3356. ;; special case: use global cycling
  3357. (setq arg t))
  3358. (cond
  3359. ((org-at-table-p 'any)
  3360. ;; Enter the table or move to the next field in the table
  3361. (or (org-table-recognize-table.el)
  3362. (progn
  3363. (if arg (org-table-edit-field t)
  3364. (org-table-justify-field-maybe)
  3365. (call-interactively 'org-table-next-field)))))
  3366. ((eq arg t) ;; Global cycling
  3367. (cond
  3368. ((and (eq last-command this-command)
  3369. (eq org-cycle-global-status 'overview))
  3370. ;; We just created the overview - now do table of contents
  3371. ;; This can be slow in very large buffers, so indicate action
  3372. (message "CONTENTS...")
  3373. (org-content)
  3374. (message "CONTENTS...done")
  3375. (setq org-cycle-global-status 'contents)
  3376. (run-hook-with-args 'org-cycle-hook 'contents))
  3377. ((and (eq last-command this-command)
  3378. (eq org-cycle-global-status 'contents))
  3379. ;; We just showed the table of contents - now show everything
  3380. (show-all)
  3381. (message "SHOW ALL")
  3382. (setq org-cycle-global-status 'all)
  3383. (run-hook-with-args 'org-cycle-hook 'all))
  3384. (t
  3385. ;; Default action: go to overview
  3386. (org-overview)
  3387. (message "OVERVIEW")
  3388. (setq org-cycle-global-status 'overview)
  3389. (run-hook-with-args 'org-cycle-hook 'overview))))
  3390. ((and org-drawers org-drawer-regexp
  3391. (save-excursion
  3392. (beginning-of-line 1)
  3393. (looking-at org-drawer-regexp)))
  3394. ;; Toggle block visibility
  3395. (org-flag-drawer
  3396. (not (get-char-property (match-end 0) 'invisible))))
  3397. ((integerp arg)
  3398. ;; Show-subtree, ARG levels up from here.
  3399. (save-excursion
  3400. (org-back-to-heading)
  3401. (outline-up-heading (if (< arg 0) (- arg)
  3402. (- (funcall outline-level) arg)))
  3403. (org-show-subtree)))
  3404. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  3405. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  3406. ;; At a heading: rotate between three different views
  3407. (org-back-to-heading)
  3408. (let ((goal-column 0) eoh eol eos)
  3409. ;; First, some boundaries
  3410. (save-excursion
  3411. (org-back-to-heading)
  3412. (save-excursion
  3413. (beginning-of-line 2)
  3414. (while (and (not (eobp)) ;; this is like `next-line'
  3415. (get-char-property (1- (point)) 'invisible))
  3416. (beginning-of-line 2)) (setq eol (point)))
  3417. (outline-end-of-heading) (setq eoh (point))
  3418. (org-end-of-subtree t)
  3419. (unless (eobp)
  3420. (skip-chars-forward " \t\n")
  3421. (beginning-of-line 1) ; in case this is an item
  3422. )
  3423. (setq eos (1- (point))))
  3424. ;; Find out what to do next and set `this-command'
  3425. (cond
  3426. ((= eos eoh)
  3427. ;; Nothing is hidden behind this heading
  3428. (message "EMPTY ENTRY")
  3429. (setq org-cycle-subtree-status nil)
  3430. (save-excursion
  3431. (goto-char eos)
  3432. (outline-next-heading)
  3433. (if (org-invisible-p) (org-flag-heading nil))))
  3434. ((or (>= eol eos)
  3435. (not (string-match "\\S-" (buffer-substring eol eos))))
  3436. ;; Entire subtree is hidden in one line: open it
  3437. (org-show-entry)
  3438. (show-children)
  3439. (message "CHILDREN")
  3440. (save-excursion
  3441. (goto-char eos)
  3442. (outline-next-heading)
  3443. (if (org-invisible-p) (org-flag-heading nil)))
  3444. (setq org-cycle-subtree-status 'children)
  3445. (run-hook-with-args 'org-cycle-hook 'children))
  3446. ((and (eq last-command this-command)
  3447. (eq org-cycle-subtree-status 'children))
  3448. ;; We just showed the children, now show everything.
  3449. (org-show-subtree)
  3450. (message "SUBTREE")
  3451. (setq org-cycle-subtree-status 'subtree)
  3452. (run-hook-with-args 'org-cycle-hook 'subtree))
  3453. (t
  3454. ;; Default action: hide the subtree.
  3455. (hide-subtree)
  3456. (message "FOLDED")
  3457. (setq org-cycle-subtree-status 'folded)
  3458. (run-hook-with-args 'org-cycle-hook 'folded)))))
  3459. ;; TAB emulation
  3460. (buffer-read-only (org-back-to-heading))
  3461. ((org-try-cdlatex-tab))
  3462. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  3463. (or (not (bolp))
  3464. (not (looking-at outline-regexp))))
  3465. (call-interactively (global-key-binding "\t")))
  3466. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  3467. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  3468. (or (and (eq org-cycle-emulate-tab 'white)
  3469. (= (match-end 0) (point-at-eol)))
  3470. (and (eq org-cycle-emulate-tab 'whitestart)
  3471. (>= (match-end 0) pos))))
  3472. t
  3473. (eq org-cycle-emulate-tab t))
  3474. (call-interactively (global-key-binding "\t")))
  3475. (t (save-excursion
  3476. (org-back-to-heading)
  3477. (org-cycle))))))
  3478. ;;;###autoload
  3479. (defun org-global-cycle (&optional arg)
  3480. "Cycle the global visibility. For details see `org-cycle'."
  3481. (interactive "P")
  3482. (let ((org-cycle-include-plain-lists
  3483. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  3484. (if (integerp arg)
  3485. (progn
  3486. (show-all)
  3487. (hide-sublevels arg)
  3488. (setq org-cycle-global-status 'contents))
  3489. (org-cycle '(4)))))
  3490. (defun org-overview ()
  3491. "Switch to overview mode, shoing only top-level headlines.
  3492. Really, this shows all headlines with level equal or greater than the level
  3493. of the first headline in the buffer. This is important, because if the
  3494. first headline is not level one, then (hide-sublevels 1) gives confusing
  3495. results."
  3496. (interactive)
  3497. (let ((level (save-excursion
  3498. (goto-char (point-min))
  3499. (if (re-search-forward (concat "^" outline-regexp) nil t)
  3500. (progn
  3501. (goto-char (match-beginning 0))
  3502. (funcall outline-level))))))
  3503. (and level (hide-sublevels level))))
  3504. (defun org-content (&optional arg)
  3505. "Show all headlines in the buffer, like a table of contents.
  3506. With numerical argument N, show content up to level N."
  3507. (interactive "P")
  3508. (save-excursion
  3509. ;; Visit all headings and show their offspring
  3510. (and (integerp arg) (org-overview))
  3511. (goto-char (point-max))
  3512. (catch 'exit
  3513. (while (and (progn (condition-case nil
  3514. (outline-previous-visible-heading 1)
  3515. (error (goto-char (point-min))))
  3516. t)
  3517. (looking-at outline-regexp))
  3518. (if (integerp arg)
  3519. (show-children (1- arg))
  3520. (show-branches))
  3521. (if (bobp) (throw 'exit nil))))))
  3522. (defun org-optimize-window-after-visibility-change (state)
  3523. "Adjust the window after a change in outline visibility.
  3524. This function is the default value of the hook `org-cycle-hook'."
  3525. (when (get-buffer-window (current-buffer))
  3526. (cond
  3527. ; ((eq state 'overview) (org-first-headline-recenter 1))
  3528. ; ((eq state 'overview) (org-beginning-of-line))
  3529. ((eq state 'content) nil)
  3530. ((eq state 'all) nil)
  3531. ((eq state 'folded) nil)
  3532. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  3533. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  3534. (defun org-compact-display-after-subtree-move ()
  3535. (let (beg end)
  3536. (save-excursion
  3537. (if (org-up-heading-safe)
  3538. (progn
  3539. (hide-subtree)
  3540. (show-entry)
  3541. (show-children)
  3542. (org-cycle-show-empty-lines 'children)
  3543. (org-cycle-hide-drawers 'children))
  3544. (org-overview)))))
  3545. (defun org-cycle-show-empty-lines (state)
  3546. "Show empty lines above all visible headlines.
  3547. The region to be covered depends on STATE when called through
  3548. `org-cycle-hook'. Lisp program can use t for STATE to get the
  3549. entire buffer covered. Note that an empty line is only shown if there
  3550. are at least `org-cycle-separator-lines' empty lines before the headeline."
  3551. (when (> org-cycle-separator-lines 0)
  3552. (save-excursion
  3553. (let* ((n org-cycle-separator-lines)
  3554. (re (cond
  3555. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  3556. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  3557. (t (let ((ns (number-to-string (- n 2))))
  3558. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  3559. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  3560. beg end)
  3561. (cond
  3562. ((memq state '(overview contents t))
  3563. (setq beg (point-min) end (point-max)))
  3564. ((memq state '(children folded))
  3565. (setq beg (point) end (progn (org-end-of-subtree t t)
  3566. (beginning-of-line 2)
  3567. (point)))))
  3568. (when beg
  3569. (goto-char beg)
  3570. (while (re-search-forward re end t)
  3571. (if (not (get-char-property (match-end 1) 'invisible))
  3572. (outline-flag-region
  3573. (match-beginning 1) (match-end 1) nil)))))))
  3574. ;; Never hide empty lines at the end of the file.
  3575. (save-excursion
  3576. (goto-char (point-max))
  3577. (outline-previous-heading)
  3578. (outline-end-of-heading)
  3579. (if (and (looking-at "[ \t\n]+")
  3580. (= (match-end 0) (point-max)))
  3581. (outline-flag-region (point) (match-end 0) nil))))
  3582. (defun org-cycle-hide-drawers (state)
  3583. "Re-hide all drawers after a visibility state change."
  3584. (when (and (org-mode-p)
  3585. (not (memq state '(overview folded))))
  3586. (save-excursion
  3587. (let* ((globalp (memq state '(contents all)))
  3588. (beg (if globalp (point-min) (point)))
  3589. (end (if globalp (point-max) (org-end-of-subtree t))))
  3590. (goto-char beg)
  3591. (while (re-search-forward org-drawer-regexp end t)
  3592. (org-flag-drawer t))))))
  3593. (defun org-flag-drawer (flag)
  3594. (save-excursion
  3595. (beginning-of-line 1)
  3596. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  3597. (let ((b (match-end 0))
  3598. (outline-regexp org-outline-regexp))
  3599. (if (re-search-forward
  3600. "^[ \t]*:END:"
  3601. (save-excursion (outline-next-heading) (point)) t)
  3602. (outline-flag-region b (point-at-eol) flag)
  3603. (error ":END: line missing"))))))
  3604. (defun org-subtree-end-visible-p ()
  3605. "Is the end of the current subtree visible?"
  3606. (pos-visible-in-window-p
  3607. (save-excursion (org-end-of-subtree t) (point))))
  3608. (defun org-first-headline-recenter (&optional N)
  3609. "Move cursor to the first headline and recenter the headline.
  3610. Optional argument N means, put the headline into the Nth line of the window."
  3611. (goto-char (point-min))
  3612. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  3613. (beginning-of-line)
  3614. (recenter (prefix-numeric-value N))))
  3615. ;;; Org-goto
  3616. (defvar org-goto-window-configuration nil)
  3617. (defvar org-goto-marker nil)
  3618. (defvar org-goto-map
  3619. (let ((map (make-sparse-keymap)))
  3620. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  3621. (while (setq cmd (pop cmds))
  3622. (substitute-key-definition cmd cmd map global-map)))
  3623. (suppress-keymap map)
  3624. (org-defkey map "\C-m" 'org-goto-ret)
  3625. (org-defkey map [(return)] 'org-goto-ret)
  3626. (org-defkey map [(left)] 'org-goto-left)
  3627. (org-defkey map [(right)] 'org-goto-right)
  3628. (org-defkey map [(control ?g)] 'org-goto-quit)
  3629. (org-defkey map "\C-i" 'org-cycle)
  3630. (org-defkey map [(tab)] 'org-cycle)
  3631. (org-defkey map [(down)] 'outline-next-visible-heading)
  3632. (org-defkey map [(up)] 'outline-previous-visible-heading)
  3633. (if org-goto-auto-isearch
  3634. (if (fboundp 'define-key-after)
  3635. (define-key-after map [t] 'org-goto-local-auto-isearch)
  3636. nil)
  3637. (org-defkey map "q" 'org-goto-quit)
  3638. (org-defkey map "n" 'outline-next-visible-heading)
  3639. (org-defkey map "p" 'outline-previous-visible-heading)
  3640. (org-defkey map "f" 'outline-forward-same-level)
  3641. (org-defkey map "b" 'outline-backward-same-level)
  3642. (org-defkey map "u" 'outline-up-heading))
  3643. (org-defkey map "/" 'org-occur)
  3644. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  3645. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  3646. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  3647. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  3648. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  3649. map))
  3650. (defconst org-goto-help
  3651. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  3652. RET=jump to location [Q]uit and return to previous location
  3653. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  3654. (defvar org-goto-start-pos) ; dynamically scoped parameter
  3655. (defun org-goto (&optional alternative-interface)
  3656. "Look up a different location in the current file, keeping current visibility.
  3657. When you want look-up or go to a different location in a document, the
  3658. fastest way is often to fold the entire buffer and then dive into the tree.
  3659. This method has the disadvantage, that the previous location will be folded,
  3660. which may not be what you want.
  3661. This command works around this by showing a copy of the current buffer
  3662. in an indirect buffer, in overview mode. You can dive into the tree in
  3663. that copy, use org-occur and incremental search to find a location.
  3664. When pressing RET or `Q', the command returns to the original buffer in
  3665. which the visibility is still unchanged. After RET is will also jump to
  3666. the location selected in the indirect buffer and expose the
  3667. the headline hierarchy above."
  3668. (interactive "P")
  3669. (let* ((org-refile-targets '((nil . (:maxlevel . 10))))
  3670. (org-refile-use-outline-path t)
  3671. (interface
  3672. (if (not alternative-interface)
  3673. org-goto-interface
  3674. (if (eq org-goto-interface 'outline)
  3675. 'outline-path-completion
  3676. 'outline)))
  3677. (org-goto-start-pos (point))
  3678. (selected-point
  3679. (if (eq interface 'outline)
  3680. (car (org-get-location (current-buffer) org-goto-help))
  3681. (nth 3 (org-refile-get-location "Goto: ")))))
  3682. (if selected-point
  3683. (progn
  3684. (org-mark-ring-push org-goto-start-pos)
  3685. (goto-char selected-point)
  3686. (if (or (org-invisible-p) (org-invisible-p2))
  3687. (org-show-context 'org-goto)))
  3688. (message "Quit"))))
  3689. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  3690. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  3691. (defvar org-goto-local-auto-isearch-map) ; defined below
  3692. (defun org-get-location (buf help)
  3693. "Let the user select a location in the Org-mode buffer BUF.
  3694. This function uses a recursive edit. It returns the selected position
  3695. or nil."
  3696. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  3697. (isearch-hide-immediately nil)
  3698. (isearch-search-fun-function
  3699. (lambda () 'org-goto-local-search-forward-headings))
  3700. (org-goto-selected-point org-goto-exit-command))
  3701. (save-excursion
  3702. (save-window-excursion
  3703. (delete-other-windows)
  3704. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  3705. (switch-to-buffer
  3706. (condition-case nil
  3707. (make-indirect-buffer (current-buffer) "*org-goto*")
  3708. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  3709. (with-output-to-temp-buffer "*Help*"
  3710. (princ help))
  3711. (shrink-window-if-larger-than-buffer (get-buffer-window "*Help*"))
  3712. (setq buffer-read-only nil)
  3713. (let ((org-startup-truncated t)
  3714. (org-startup-folded nil)
  3715. (org-startup-align-all-tables nil))
  3716. (org-mode)
  3717. (org-overview))
  3718. (setq buffer-read-only t)
  3719. (if (and (boundp 'org-goto-start-pos)
  3720. (integer-or-marker-p org-goto-start-pos))
  3721. (let ((org-show-hierarchy-above t)
  3722. (org-show-siblings t)
  3723. (org-show-following-heading t))
  3724. (goto-char org-goto-start-pos)
  3725. (and (org-invisible-p) (org-show-context)))
  3726. (goto-char (point-min)))
  3727. (org-beginning-of-line)
  3728. (message "Select location and press RET")
  3729. (use-local-map org-goto-map)
  3730. (recursive-edit)
  3731. ))
  3732. (kill-buffer "*org-goto*")
  3733. (cons org-goto-selected-point org-goto-exit-command)))
  3734. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  3735. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  3736. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  3737. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  3738. (defun org-goto-local-search-forward-headings (string bound noerror)
  3739. "Search and make sure that anu matches are in headlines."
  3740. (catch 'return
  3741. (while (search-forward string bound noerror)
  3742. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  3743. (and (member :headline context)
  3744. (not (member :tags context))))
  3745. (throw 'return (point))))))
  3746. (defun org-goto-local-auto-isearch ()
  3747. "Start isearch."
  3748. (interactive)
  3749. (goto-char (point-min))
  3750. (let ((keys (this-command-keys)))
  3751. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  3752. (isearch-mode t)
  3753. (isearch-process-search-char (string-to-char keys)))))
  3754. (defun org-goto-ret (&optional arg)
  3755. "Finish `org-goto' by going to the new location."
  3756. (interactive "P")
  3757. (setq org-goto-selected-point (point)
  3758. org-goto-exit-command 'return)
  3759. (throw 'exit nil))
  3760. (defun org-goto-left ()
  3761. "Finish `org-goto' by going to the new location."
  3762. (interactive)
  3763. (if (org-on-heading-p)
  3764. (progn
  3765. (beginning-of-line 1)
  3766. (setq org-goto-selected-point (point)
  3767. org-goto-exit-command 'left)
  3768. (throw 'exit nil))
  3769. (error "Not on a heading")))
  3770. (defun org-goto-right ()
  3771. "Finish `org-goto' by going to the new location."
  3772. (interactive)
  3773. (if (org-on-heading-p)
  3774. (progn
  3775. (setq org-goto-selected-point (point)
  3776. org-goto-exit-command 'right)
  3777. (throw 'exit nil))
  3778. (error "Not on a heading")))
  3779. (defun org-goto-quit ()
  3780. "Finish `org-goto' without cursor motion."
  3781. (interactive)
  3782. (setq org-goto-selected-point nil)
  3783. (setq org-goto-exit-command 'quit)
  3784. (throw 'exit nil))
  3785. ;;; Indirect buffer display of subtrees
  3786. (defvar org-indirect-dedicated-frame nil
  3787. "This is the frame being used for indirect tree display.")
  3788. (defvar org-last-indirect-buffer nil)
  3789. (defun org-tree-to-indirect-buffer (&optional arg)
  3790. "Create indirect buffer and narrow it to current subtree.
  3791. With numerical prefix ARG, go up to this level and then take that tree.
  3792. If ARG is negative, go up that many levels.
  3793. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  3794. indirect buffer previously made with this command, to avoid proliferation of
  3795. indirect buffers. However, when you call the command with a `C-u' prefix, or
  3796. when `org-indirect-buffer-display' is `new-frame', the last buffer
  3797. is kept so that you can work with several indirect buffers at the same time.
  3798. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  3799. requests that a new frame be made for the new buffer, so that the dedicated
  3800. frame is not changed."
  3801. (interactive "P")
  3802. (let ((cbuf (current-buffer))
  3803. (cwin (selected-window))
  3804. (pos (point))
  3805. beg end level heading ibuf)
  3806. (save-excursion
  3807. (org-back-to-heading t)
  3808. (when (numberp arg)
  3809. (setq level (org-outline-level))
  3810. (if (< arg 0) (setq arg (+ level arg)))
  3811. (while (> (setq level (org-outline-level)) arg)
  3812. (outline-up-heading 1 t)))
  3813. (setq beg (point)
  3814. heading (org-get-heading))
  3815. (org-end-of-subtree t) (setq end (point)))
  3816. (if (and (buffer-live-p org-last-indirect-buffer)
  3817. (not (eq org-indirect-buffer-display 'new-frame))
  3818. (not arg))
  3819. (kill-buffer org-last-indirect-buffer))
  3820. (setq ibuf (org-get-indirect-buffer cbuf)
  3821. org-last-indirect-buffer ibuf)
  3822. (cond
  3823. ((or (eq org-indirect-buffer-display 'new-frame)
  3824. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  3825. (select-frame (make-frame))
  3826. (delete-other-windows)
  3827. (switch-to-buffer ibuf)
  3828. (org-set-frame-title heading))
  3829. ((eq org-indirect-buffer-display 'dedicated-frame)
  3830. (raise-frame
  3831. (select-frame (or (and org-indirect-dedicated-frame
  3832. (frame-live-p org-indirect-dedicated-frame)
  3833. org-indirect-dedicated-frame)
  3834. (setq org-indirect-dedicated-frame (make-frame)))))
  3835. (delete-other-windows)
  3836. (switch-to-buffer ibuf)
  3837. (org-set-frame-title (concat "Indirect: " heading)))
  3838. ((eq org-indirect-buffer-display 'current-window)
  3839. (switch-to-buffer ibuf))
  3840. ((eq org-indirect-buffer-display 'other-window)
  3841. (pop-to-buffer ibuf))
  3842. (t (error "Invalid value.")))
  3843. (if (featurep 'xemacs)
  3844. (save-excursion (org-mode) (turn-on-font-lock)))
  3845. (narrow-to-region beg end)
  3846. (show-all)
  3847. (goto-char pos)
  3848. (and (window-live-p cwin) (select-window cwin))))
  3849. (defun org-get-indirect-buffer (&optional buffer)
  3850. (setq buffer (or buffer (current-buffer)))
  3851. (let ((n 1) (base (buffer-name buffer)) bname)
  3852. (while (buffer-live-p
  3853. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  3854. (setq n (1+ n)))
  3855. (condition-case nil
  3856. (make-indirect-buffer buffer bname 'clone)
  3857. (error (make-indirect-buffer buffer bname)))))
  3858. (defun org-set-frame-title (title)
  3859. "Set the title of the current frame to the string TITLE."
  3860. ;; FIXME: how to name a single frame in XEmacs???
  3861. (unless (featurep 'xemacs)
  3862. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  3863. ;;;; Structure editing
  3864. ;;; Inserting headlines
  3865. (defun org-insert-heading (&optional force-heading)
  3866. "Insert a new heading or item with same depth at point.
  3867. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  3868. If point is at the beginning of a headline, insert a sibling before the
  3869. current headline. If point is not at the beginning, do not split the line,
  3870. but create the new hedline after the current line."
  3871. (interactive "P")
  3872. (if (= (buffer-size) 0)
  3873. (insert "\n* ")
  3874. (when (or force-heading (not (org-insert-item)))
  3875. (let* ((head (save-excursion
  3876. (condition-case nil
  3877. (progn
  3878. (org-back-to-heading)
  3879. (match-string 0))
  3880. (error "*"))))
  3881. (blank (cdr (assq 'heading org-blank-before-new-entry)))
  3882. pos)
  3883. (cond
  3884. ((and (org-on-heading-p) (bolp)
  3885. (or (bobp)
  3886. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  3887. ;; insert before the current line
  3888. (open-line (if blank 2 1)))
  3889. ((and (bolp)
  3890. (or (bobp)
  3891. (save-excursion
  3892. (backward-char 1) (not (org-invisible-p)))))
  3893. ;; insert right here
  3894. nil)
  3895. (t
  3896. ;; in the middle of the line
  3897. (org-show-entry)
  3898. (let ((split
  3899. (org-get-alist-option org-M-RET-may-split-line 'headline))
  3900. tags pos)
  3901. (if (org-on-heading-p)
  3902. (progn
  3903. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  3904. (setq tags (and (match-end 2) (match-string 2)))
  3905. (and (match-end 1)
  3906. (delete-region (match-beginning 1) (match-end 1)))
  3907. (setq pos (point-at-bol))
  3908. (or split (end-of-line 1))
  3909. (delete-horizontal-space)
  3910. (newline (if blank 2 1))
  3911. (when tags
  3912. (save-excursion
  3913. (goto-char pos)
  3914. (end-of-line 1)
  3915. (insert " " tags)
  3916. (org-set-tags nil 'align))))
  3917. (or split (end-of-line 1))
  3918. (newline (if blank 2 1))))))
  3919. (insert head) (just-one-space)
  3920. (setq pos (point))
  3921. (end-of-line 1)
  3922. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  3923. (run-hooks 'org-insert-heading-hook)))))
  3924. (defun org-get-heading (&optional no-tags)
  3925. "Return the heading of the current entry, without the stars."
  3926. (save-excursion
  3927. (org-back-to-heading t)
  3928. (if (looking-at
  3929. (if no-tags
  3930. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  3931. "\\*+[ \t]+\\([^\r\n]*\\)"))
  3932. (match-string 1) "")))
  3933. (defun org-insert-heading-after-current ()
  3934. "Insert a new heading with same level as current, after current subtree."
  3935. (interactive)
  3936. (org-back-to-heading)
  3937. (org-insert-heading)
  3938. (org-move-subtree-down)
  3939. (end-of-line 1))
  3940. (defun org-insert-todo-heading (arg)
  3941. "Insert a new heading with the same level and TODO state as current heading.
  3942. If the heading has no TODO state, or if the state is DONE, use the first
  3943. state (TODO by default). Also with prefix arg, force first state."
  3944. (interactive "P")
  3945. (when (not (org-insert-item 'checkbox))
  3946. (org-insert-heading)
  3947. (save-excursion
  3948. (org-back-to-heading)
  3949. (outline-previous-heading)
  3950. (looking-at org-todo-line-regexp))
  3951. (if (or arg
  3952. (not (match-beginning 2))
  3953. (member (match-string 2) org-done-keywords))
  3954. (insert (car org-todo-keywords-1) " ")
  3955. (insert (match-string 2) " "))))
  3956. (defun org-insert-subheading (arg)
  3957. "Insert a new subheading and demote it.
  3958. Works for outline headings and for plain lists alike."
  3959. (interactive "P")
  3960. (org-insert-heading arg)
  3961. (cond
  3962. ((org-on-heading-p) (org-do-demote))
  3963. ((org-at-item-p) (org-indent-item 1))))
  3964. (defun org-insert-todo-subheading (arg)
  3965. "Insert a new subheading with TODO keyword or checkbox and demote it.
  3966. Works for outline headings and for plain lists alike."
  3967. (interactive "P")
  3968. (org-insert-todo-heading arg)
  3969. (cond
  3970. ((org-on-heading-p) (org-do-demote))
  3971. ((org-at-item-p) (org-indent-item 1))))
  3972. ;;; Promotion and Demotion
  3973. (defun org-promote-subtree ()
  3974. "Promote the entire subtree.
  3975. See also `org-promote'."
  3976. (interactive)
  3977. (save-excursion
  3978. (org-map-tree 'org-promote))
  3979. (org-fix-position-after-promote))
  3980. (defun org-demote-subtree ()
  3981. "Demote the entire subtree. See `org-demote'.
  3982. See also `org-promote'."
  3983. (interactive)
  3984. (save-excursion
  3985. (org-map-tree 'org-demote))
  3986. (org-fix-position-after-promote))
  3987. (defun org-do-promote ()
  3988. "Promote the current heading higher up the tree.
  3989. If the region is active in `transient-mark-mode', promote all headings
  3990. in the region."
  3991. (interactive)
  3992. (save-excursion
  3993. (if (org-region-active-p)
  3994. (org-map-region 'org-promote (region-beginning) (region-end))
  3995. (org-promote)))
  3996. (org-fix-position-after-promote))
  3997. (defun org-do-demote ()
  3998. "Demote the current heading lower down the tree.
  3999. If the region is active in `transient-mark-mode', demote all headings
  4000. in the region."
  4001. (interactive)
  4002. (save-excursion
  4003. (if (org-region-active-p)
  4004. (org-map-region 'org-demote (region-beginning) (region-end))
  4005. (org-demote)))
  4006. (org-fix-position-after-promote))
  4007. (defun org-fix-position-after-promote ()
  4008. "Make sure that after pro/demotion cursor position is right."
  4009. (let ((pos (point)))
  4010. (when (save-excursion
  4011. (beginning-of-line 1)
  4012. (looking-at org-todo-line-regexp)
  4013. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  4014. (cond ((eobp) (insert " "))
  4015. ((eolp) (insert " "))
  4016. ((equal (char-after) ?\ ) (forward-char 1))))))
  4017. (defun org-reduced-level (l)
  4018. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  4019. (defun org-get-valid-level (level &optional change)
  4020. "Rectify a level change under the influence of `org-odd-levels-only'
  4021. LEVEL is a current level, CHANGE is by how much the level should be
  4022. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  4023. even level numbers will become the next higher odd number."
  4024. (if org-odd-levels-only
  4025. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  4026. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  4027. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  4028. (max 1 (+ level change))))
  4029. (if (boundp 'define-obsolete-function-alias)
  4030. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  4031. (define-obsolete-function-alias 'org-get-legal-level
  4032. 'org-get-valid-level)
  4033. (define-obsolete-function-alias 'org-get-legal-level
  4034. 'org-get-valid-level "23.1")))
  4035. (defun org-promote ()
  4036. "Promote the current heading higher up the tree.
  4037. If the region is active in `transient-mark-mode', promote all headings
  4038. in the region."
  4039. (org-back-to-heading t)
  4040. (let* ((level (save-match-data (funcall outline-level)))
  4041. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  4042. (diff (abs (- level (length up-head) -1))))
  4043. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  4044. (replace-match up-head nil t)
  4045. ;; Fixup tag positioning
  4046. (and org-auto-align-tags (org-set-tags nil t))
  4047. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  4048. (defun org-demote ()
  4049. "Demote the current heading lower down the tree.
  4050. If the region is active in `transient-mark-mode', demote all headings
  4051. in the region."
  4052. (org-back-to-heading t)
  4053. (let* ((level (save-match-data (funcall outline-level)))
  4054. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  4055. (diff (abs (- level (length down-head) -1))))
  4056. (replace-match down-head nil t)
  4057. ;; Fixup tag positioning
  4058. (and org-auto-align-tags (org-set-tags nil t))
  4059. (if org-adapt-indentation (org-fixup-indentation diff))))
  4060. (defun org-map-tree (fun)
  4061. "Call FUN for every heading underneath the current one."
  4062. (org-back-to-heading)
  4063. (let ((level (funcall outline-level)))
  4064. (save-excursion
  4065. (funcall fun)
  4066. (while (and (progn
  4067. (outline-next-heading)
  4068. (> (funcall outline-level) level))
  4069. (not (eobp)))
  4070. (funcall fun)))))
  4071. (defun org-map-region (fun beg end)
  4072. "Call FUN for every heading between BEG and END."
  4073. (let ((org-ignore-region t))
  4074. (save-excursion
  4075. (setq end (copy-marker end))
  4076. (goto-char beg)
  4077. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  4078. (< (point) end))
  4079. (funcall fun))
  4080. (while (and (progn
  4081. (outline-next-heading)
  4082. (< (point) end))
  4083. (not (eobp)))
  4084. (funcall fun)))))
  4085. (defun org-fixup-indentation (diff)
  4086. "Change the indentation in the current entry by DIFF
  4087. However, if any line in the current entry has no indentation, or if it
  4088. would end up with no indentation after the change, nothing at all is done."
  4089. (save-excursion
  4090. (let ((end (save-excursion (outline-next-heading)
  4091. (point-marker)))
  4092. (prohibit (if (> diff 0)
  4093. "^\\S-"
  4094. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  4095. col)
  4096. (unless (save-excursion (end-of-line 1)
  4097. (re-search-forward prohibit end t))
  4098. (while (and (< (point) end)
  4099. (re-search-forward "^[ \t]+" end t))
  4100. (goto-char (match-end 0))
  4101. (setq col (current-column))
  4102. (if (< diff 0) (replace-match ""))
  4103. (indent-to (+ diff col))))
  4104. (move-marker end nil))))
  4105. (defun org-convert-to-odd-levels ()
  4106. "Convert an org-mode file with all levels allowed to one with odd levels.
  4107. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  4108. level 5 etc."
  4109. (interactive)
  4110. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  4111. (let ((org-odd-levels-only nil) n)
  4112. (save-excursion
  4113. (goto-char (point-min))
  4114. (while (re-search-forward "^\\*\\*+ " nil t)
  4115. (setq n (- (length (match-string 0)) 2))
  4116. (while (>= (setq n (1- n)) 0)
  4117. (org-demote))
  4118. (end-of-line 1))))))
  4119. (defun org-convert-to-oddeven-levels ()
  4120. "Convert an org-mode file with only odd levels to one with odd and even levels.
  4121. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  4122. section with an even level, conversion would destroy the structure of the file. An error
  4123. is signaled in this case."
  4124. (interactive)
  4125. (goto-char (point-min))
  4126. ;; First check if there are no even levels
  4127. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  4128. (org-show-context t)
  4129. (error "Not all levels are odd in this file. Conversion not possible."))
  4130. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  4131. (let ((org-odd-levels-only nil) n)
  4132. (save-excursion
  4133. (goto-char (point-min))
  4134. (while (re-search-forward "^\\*\\*+ " nil t)
  4135. (setq n (/ (1- (length (match-string 0))) 2))
  4136. (while (>= (setq n (1- n)) 0)
  4137. (org-promote))
  4138. (end-of-line 1))))))
  4139. (defun org-tr-level (n)
  4140. "Make N odd if required."
  4141. (if org-odd-levels-only (1+ (/ n 2)) n))
  4142. ;;; Vertical tree motion, cutting and pasting of subtrees
  4143. (defun org-move-subtree-up (&optional arg)
  4144. "Move the current subtree up past ARG headlines of the same level."
  4145. (interactive "p")
  4146. (org-move-subtree-down (- (prefix-numeric-value arg))))
  4147. (defun org-move-subtree-down (&optional arg)
  4148. "Move the current subtree down past ARG headlines of the same level."
  4149. (interactive "p")
  4150. (setq arg (prefix-numeric-value arg))
  4151. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  4152. 'outline-get-last-sibling))
  4153. (ins-point (make-marker))
  4154. (cnt (abs arg))
  4155. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  4156. ;; Select the tree
  4157. (org-back-to-heading)
  4158. (setq beg0 (point))
  4159. (save-excursion
  4160. (setq ne-beg (org-back-over-empty-lines))
  4161. (setq beg (point)))
  4162. (save-match-data
  4163. (save-excursion (outline-end-of-heading)
  4164. (setq folded (org-invisible-p)))
  4165. (outline-end-of-subtree))
  4166. (outline-next-heading)
  4167. (setq ne-end (org-back-over-empty-lines))
  4168. (setq end (point))
  4169. (goto-char beg0)
  4170. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  4171. ;; include less whitespace
  4172. (save-excursion
  4173. (goto-char beg)
  4174. (forward-line (- ne-beg ne-end))
  4175. (setq beg (point))))
  4176. ;; Find insertion point, with error handling
  4177. (while (> cnt 0)
  4178. (or (and (funcall movfunc) (looking-at outline-regexp))
  4179. (progn (goto-char beg0)
  4180. (error "Cannot move past superior level or buffer limit")))
  4181. (setq cnt (1- cnt)))
  4182. (if (> arg 0)
  4183. ;; Moving forward - still need to move over subtree
  4184. (progn (org-end-of-subtree t t)
  4185. (save-excursion
  4186. (org-back-over-empty-lines)
  4187. (or (bolp) (newline)))))
  4188. (setq ne-ins (org-back-over-empty-lines))
  4189. (move-marker ins-point (point))
  4190. (setq txt (buffer-substring beg end))
  4191. (org-save-markers-in-region beg end)
  4192. (delete-region beg end)
  4193. (outline-flag-region (1- beg) beg nil)
  4194. (outline-flag-region (1- (point)) (point) nil)
  4195. (let ((bbb (point)))
  4196. (insert-before-markers txt)
  4197. (org-reinstall-markers-in-region bbb)
  4198. (move-marker ins-point bbb))
  4199. (or (bolp) (insert "\n"))
  4200. (setq ins-end (point))
  4201. (goto-char ins-point)
  4202. (org-skip-whitespace)
  4203. (when (and (< arg 0)
  4204. (org-first-sibling-p)
  4205. (> ne-ins ne-beg))
  4206. ;; Move whitespace back to beginning
  4207. (save-excursion
  4208. (goto-char ins-end)
  4209. (let ((kill-whole-line t))
  4210. (kill-line (- ne-ins ne-beg)) (point)))
  4211. (insert (make-string (- ne-ins ne-beg) ?\n)))
  4212. (move-marker ins-point nil)
  4213. (org-compact-display-after-subtree-move)
  4214. (unless folded
  4215. (org-show-entry)
  4216. (show-children)
  4217. (org-cycle-hide-drawers 'children))))
  4218. (defvar org-subtree-clip ""
  4219. "Clipboard for cut and paste of subtrees.
  4220. This is actually only a copy of the kill, because we use the normal kill
  4221. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  4222. (defvar org-subtree-clip-folded nil
  4223. "Was the last copied subtree folded?
  4224. This is used to fold the tree back after pasting.")
  4225. (defun org-cut-subtree (&optional n)
  4226. "Cut the current subtree into the clipboard.
  4227. With prefix arg N, cut this many sequential subtrees.
  4228. This is a short-hand for marking the subtree and then cutting it."
  4229. (interactive "p")
  4230. (org-copy-subtree n 'cut))
  4231. (defun org-copy-subtree (&optional n cut force-store-markers)
  4232. "Cut the current subtree into the clipboard.
  4233. With prefix arg N, cut this many sequential subtrees.
  4234. This is a short-hand for marking the subtree and then copying it.
  4235. If CUT is non-nil, actually cut the subtree.
  4236. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  4237. of some markers in the region, even if CUT is non-nil. This is
  4238. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  4239. (interactive "p")
  4240. (let (beg end folded (beg0 (point)))
  4241. (if (interactive-p)
  4242. (org-back-to-heading nil) ; take what looks like a subtree
  4243. (org-back-to-heading t)) ; take what is really there
  4244. (org-back-over-empty-lines)
  4245. (setq beg (point))
  4246. (skip-chars-forward " \t\r\n")
  4247. (save-match-data
  4248. (save-excursion (outline-end-of-heading)
  4249. (setq folded (org-invisible-p)))
  4250. (condition-case nil
  4251. (outline-forward-same-level (1- n))
  4252. (error nil))
  4253. (org-end-of-subtree t t))
  4254. (org-back-over-empty-lines)
  4255. (setq end (point))
  4256. (goto-char beg0)
  4257. (when (> end beg)
  4258. (setq org-subtree-clip-folded folded)
  4259. (when (or cut force-store-markers)
  4260. (org-save-markers-in-region beg end))
  4261. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  4262. (setq org-subtree-clip (current-kill 0))
  4263. (message "%s: Subtree(s) with %d characters"
  4264. (if cut "Cut" "Copied")
  4265. (length org-subtree-clip)))))
  4266. (defun org-paste-subtree (&optional level tree)
  4267. "Paste the clipboard as a subtree, with modification of headline level.
  4268. The entire subtree is promoted or demoted in order to match a new headline
  4269. level. By default, the new level is derived from the visible headings
  4270. before and after the insertion point, and taken to be the inferior headline
  4271. level of the two. So if the previous visible heading is level 3 and the
  4272. next is level 4 (or vice versa), level 4 will be used for insertion.
  4273. This makes sure that the subtree remains an independent subtree and does
  4274. not swallow low level entries.
  4275. You can also force a different level, either by using a numeric prefix
  4276. argument, or by inserting the heading marker by hand. For example, if the
  4277. cursor is after \"*****\", then the tree will be shifted to level 5.
  4278. If you want to insert the tree as is, just use \\[yank].
  4279. If optional TREE is given, use this text instead of the kill ring."
  4280. (interactive "P")
  4281. (unless (org-kill-is-subtree-p tree)
  4282. (error "%s"
  4283. (substitute-command-keys
  4284. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  4285. (let* ((txt (or tree (and kill-ring (current-kill 0))))
  4286. (^re (concat "^\\(" outline-regexp "\\)"))
  4287. (re (concat "\\(" outline-regexp "\\)"))
  4288. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  4289. (old-level (if (string-match ^re txt)
  4290. (- (match-end 0) (match-beginning 0) 1)
  4291. -1))
  4292. (force-level (cond (level (prefix-numeric-value level))
  4293. ((string-match
  4294. ^re_ (buffer-substring (point-at-bol) (point)))
  4295. (- (match-end 1) (match-beginning 1)))
  4296. (t nil)))
  4297. (previous-level (save-excursion
  4298. (condition-case nil
  4299. (progn
  4300. (outline-previous-visible-heading 1)
  4301. (if (looking-at re)
  4302. (- (match-end 0) (match-beginning 0) 1)
  4303. 1))
  4304. (error 1))))
  4305. (next-level (save-excursion
  4306. (condition-case nil
  4307. (progn
  4308. (or (looking-at outline-regexp)
  4309. (outline-next-visible-heading 1))
  4310. (if (looking-at re)
  4311. (- (match-end 0) (match-beginning 0) 1)
  4312. 1))
  4313. (error 1))))
  4314. (new-level (or force-level (max previous-level next-level)))
  4315. (shift (if (or (= old-level -1)
  4316. (= new-level -1)
  4317. (= old-level new-level))
  4318. 0
  4319. (- new-level old-level)))
  4320. (delta (if (> shift 0) -1 1))
  4321. (func (if (> shift 0) 'org-demote 'org-promote))
  4322. (org-odd-levels-only nil)
  4323. beg end)
  4324. ;; Remove the forced level indicator
  4325. (if force-level
  4326. (delete-region (point-at-bol) (point)))
  4327. ;; Paste
  4328. (beginning-of-line 1)
  4329. (org-back-over-empty-lines)
  4330. (setq beg (point))
  4331. (insert-before-markers txt)
  4332. (unless (string-match "\n\\'" txt) (insert "\n"))
  4333. (org-reinstall-markers-in-region beg)
  4334. (setq end (point))
  4335. (goto-char beg)
  4336. (skip-chars-forward " \t\n\r")
  4337. (setq beg (point))
  4338. ;; Shift if necessary
  4339. (unless (= shift 0)
  4340. (save-restriction
  4341. (narrow-to-region beg end)
  4342. (while (not (= shift 0))
  4343. (org-map-region func (point-min) (point-max))
  4344. (setq shift (+ delta shift)))
  4345. (goto-char (point-min))))
  4346. (when (interactive-p)
  4347. (message "Clipboard pasted as level %d subtree" new-level))
  4348. (if (and kill-ring
  4349. (eq org-subtree-clip (current-kill 0))
  4350. org-subtree-clip-folded)
  4351. ;; The tree was folded before it was killed/copied
  4352. (hide-subtree))))
  4353. (defun org-kill-is-subtree-p (&optional txt)
  4354. "Check if the current kill is an outline subtree, or a set of trees.
  4355. Returns nil if kill does not start with a headline, or if the first
  4356. headline level is not the largest headline level in the tree.
  4357. So this will actually accept several entries of equal levels as well,
  4358. which is OK for `org-paste-subtree'.
  4359. If optional TXT is given, check this string instead of the current kill."
  4360. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  4361. (start-level (and kill
  4362. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  4363. org-outline-regexp "\\)")
  4364. kill)
  4365. (- (match-end 2) (match-beginning 2) 1)))
  4366. (re (concat "^" org-outline-regexp))
  4367. (start (1+ (match-beginning 2))))
  4368. (if (not start-level)
  4369. (progn
  4370. nil) ;; does not even start with a heading
  4371. (catch 'exit
  4372. (while (setq start (string-match re kill (1+ start)))
  4373. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  4374. (throw 'exit nil)))
  4375. t))))
  4376. (defvar org-markers-to-move nil
  4377. "Markers that should be moved with a cut-and-paste operation.
  4378. Those markers are stored together with their positions relative to
  4379. the start of the region.")
  4380. (defun org-save-markers-in-region (beg end)
  4381. "Check markers in region.
  4382. If these markers are between BEG and END, record their position relative
  4383. to BEG, so that after moving the block of text, we can put the markers back
  4384. into place.
  4385. This function gets called just before an entry or tree gets cut from the
  4386. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  4387. called immediately, to move the markers with the entries."
  4388. (setq org-markers-to-move nil)
  4389. (when (featurep 'org-clock)
  4390. (org-clock-save-markers-for-cut-and-paste))
  4391. (when (featurep 'org-agenda)
  4392. (org-agenda-save-markers-for-cut-and-paste)))
  4393. (defun org-check-and-save-marker (marker beg end)
  4394. "Check if MARKER is between BEG and END.
  4395. If yes, remember the marker and the distance to BEG."
  4396. (when (and (marker-buffer marker)
  4397. (equal (marker-buffer marker) (current-buffer)))
  4398. (if (and (>= marker beg) (< marker end))
  4399. (push (cons marker (- marker beg)) org-markers-to-move))))
  4400. (defun org-reinstall-markers-in-region (beg)
  4401. "Move all remembered markers to their position relative to BEG."
  4402. (mapc (lambda (x)
  4403. (move-marker (car x) (+ beg (cdr x))))
  4404. org-markers-to-move)
  4405. (setq org-markers-to-move nil))
  4406. (defun org-narrow-to-subtree ()
  4407. "Narrow buffer to the current subtree."
  4408. (interactive)
  4409. (save-excursion
  4410. (save-match-data
  4411. (narrow-to-region
  4412. (progn (org-back-to-heading) (point))
  4413. (progn (org-end-of-subtree t t) (point))))))
  4414. ;;; Outline Sorting
  4415. (defun org-sort (with-case)
  4416. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  4417. Optional argument WITH-CASE means sort case-sensitively."
  4418. (interactive "P")
  4419. (if (org-at-table-p)
  4420. (org-call-with-arg 'org-table-sort-lines with-case)
  4421. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  4422. (defun org-sort-remove-invisible (s)
  4423. (remove-text-properties 0 (length s) org-rm-props s)
  4424. (while (string-match org-bracket-link-regexp s)
  4425. (setq s (replace-match (if (match-end 2)
  4426. (match-string 3 s)
  4427. (match-string 1 s)) t t s)))
  4428. s)
  4429. (defvar org-priority-regexp) ; defined later in the file
  4430. (defun org-sort-entries-or-items (&optional with-case sorting-type getkey-func property)
  4431. "Sort entries on a certain level of an outline tree.
  4432. If there is an active region, the entries in the region are sorted.
  4433. Else, if the cursor is before the first entry, sort the top-level items.
  4434. Else, the children of the entry at point are sorted.
  4435. Sorting can be alphabetically, numerically, and by date/time as given by
  4436. the first time stamp in the entry. The command prompts for the sorting
  4437. type unless it has been given to the function through the SORTING-TYPE
  4438. argument, which needs to a character, any of (?n ?N ?a ?A ?t ?T ?p ?P ?f ?F).
  4439. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  4440. called with point at the beginning of the record. It must return either
  4441. a string or a number that should serve as the sorting key for that record.
  4442. Comparing entries ignores case by default. However, with an optional argument
  4443. WITH-CASE, the sorting considers case as well."
  4444. (interactive "P")
  4445. (let ((case-func (if with-case 'identity 'downcase))
  4446. start beg end stars re re2
  4447. txt what tmp plain-list-p)
  4448. ;; Find beginning and end of region to sort
  4449. (cond
  4450. ((org-region-active-p)
  4451. ;; we will sort the region
  4452. (setq end (region-end)
  4453. what "region")
  4454. (goto-char (region-beginning))
  4455. (if (not (org-on-heading-p)) (outline-next-heading))
  4456. (setq start (point)))
  4457. ((org-at-item-p)
  4458. ;; we will sort this plain list
  4459. (org-beginning-of-item-list) (setq start (point))
  4460. (org-end-of-item-list) (setq end (point))
  4461. (goto-char start)
  4462. (setq plain-list-p t
  4463. what "plain list"))
  4464. ((or (org-on-heading-p)
  4465. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  4466. ;; we will sort the children of the current headline
  4467. (org-back-to-heading)
  4468. (setq start (point)
  4469. end (progn (org-end-of-subtree t t)
  4470. (org-back-over-empty-lines)
  4471. (point))
  4472. what "children")
  4473. (goto-char start)
  4474. (show-subtree)
  4475. (outline-next-heading))
  4476. (t
  4477. ;; we will sort the top-level entries in this file
  4478. (goto-char (point-min))
  4479. (or (org-on-heading-p) (outline-next-heading))
  4480. (setq start (point) end (point-max) what "top-level")
  4481. (goto-char start)
  4482. (show-all)))
  4483. (setq beg (point))
  4484. (if (>= beg end) (error "Nothing to sort"))
  4485. (unless plain-list-p
  4486. (looking-at "\\(\\*+\\)")
  4487. (setq stars (match-string 1)
  4488. re (concat "^" (regexp-quote stars) " +")
  4489. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  4490. txt (buffer-substring beg end))
  4491. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  4492. (if (and (not (equal stars "*")) (string-match re2 txt))
  4493. (error "Region to sort contains a level above the first entry")))
  4494. (unless sorting-type
  4495. (message
  4496. (if plain-list-p
  4497. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  4498. "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:")
  4499. what)
  4500. (setq sorting-type (read-char-exclusive))
  4501. (and (= (downcase sorting-type) ?f)
  4502. (setq getkey-func
  4503. (completing-read "Sort using function: "
  4504. obarray 'fboundp t nil nil))
  4505. (setq getkey-func (intern getkey-func)))
  4506. (and (= (downcase sorting-type) ?r)
  4507. (setq property
  4508. (completing-read "Property: "
  4509. (mapcar 'list (org-buffer-property-keys t))
  4510. nil t))))
  4511. (message "Sorting entries...")
  4512. (save-restriction
  4513. (narrow-to-region start end)
  4514. (let ((dcst (downcase sorting-type))
  4515. (now (current-time)))
  4516. (sort-subr
  4517. (/= dcst sorting-type)
  4518. ;; This function moves to the beginning character of the "record" to
  4519. ;; be sorted.
  4520. (if plain-list-p
  4521. (lambda nil
  4522. (if (org-at-item-p) t (goto-char (point-max))))
  4523. (lambda nil
  4524. (if (re-search-forward re nil t)
  4525. (goto-char (match-beginning 0))
  4526. (goto-char (point-max)))))
  4527. ;; This function moves to the last character of the "record" being
  4528. ;; sorted.
  4529. (if plain-list-p
  4530. 'org-end-of-item
  4531. (lambda nil
  4532. (save-match-data
  4533. (condition-case nil
  4534. (outline-forward-same-level 1)
  4535. (error
  4536. (goto-char (point-max)))))))
  4537. ;; This function returns the value that gets sorted against.
  4538. (if plain-list-p
  4539. (lambda nil
  4540. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  4541. (cond
  4542. ((= dcst ?n)
  4543. (string-to-number (buffer-substring (match-end 0)
  4544. (point-at-eol))))
  4545. ((= dcst ?a)
  4546. (buffer-substring (match-end 0) (point-at-eol)))
  4547. ((= dcst ?t)
  4548. (if (re-search-forward org-ts-regexp
  4549. (point-at-eol) t)
  4550. (org-time-string-to-time (match-string 0))
  4551. now))
  4552. ((= dcst ?f)
  4553. (if getkey-func
  4554. (progn
  4555. (setq tmp (funcall getkey-func))
  4556. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  4557. tmp)
  4558. (error "Invalid key function `%s'" getkey-func)))
  4559. (t (error "Invalid sorting type `%c'" sorting-type)))))
  4560. (lambda nil
  4561. (cond
  4562. ((= dcst ?n)
  4563. (if (looking-at outline-regexp)
  4564. (string-to-number (buffer-substring (match-end 0)
  4565. (point-at-eol)))
  4566. nil))
  4567. ((= dcst ?a)
  4568. (funcall case-func (buffer-substring (point-at-bol)
  4569. (point-at-eol))))
  4570. ((= dcst ?t)
  4571. (if (re-search-forward org-ts-regexp
  4572. (save-excursion
  4573. (forward-line 2)
  4574. (point)) t)
  4575. (org-time-string-to-time (match-string 0))
  4576. now))
  4577. ((= dcst ?p)
  4578. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  4579. (string-to-char (match-string 2))
  4580. org-default-priority))
  4581. ((= dcst ?r)
  4582. (or (org-entry-get nil property) ""))
  4583. ((= dcst ?o)
  4584. (if (looking-at org-complex-heading-regexp)
  4585. (- 9999 (length (member (match-string 2)
  4586. org-todo-keywords-1)))))
  4587. ((= dcst ?f)
  4588. (if getkey-func
  4589. (progn
  4590. (setq tmp (funcall getkey-func))
  4591. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  4592. tmp)
  4593. (error "Invalid key function `%s'" getkey-func)))
  4594. (t (error "Invalid sorting type `%c'" sorting-type)))))
  4595. nil
  4596. (cond
  4597. ((= dcst ?a) 'string<)
  4598. ((= dcst ?t) 'time-less-p)
  4599. (t nil)))))
  4600. (message "Sorting entries...done")))
  4601. (defun org-do-sort (table what &optional with-case sorting-type)
  4602. "Sort TABLE of WHAT according to SORTING-TYPE.
  4603. The user will be prompted for the SORTING-TYPE if the call to this
  4604. function does not specify it. WHAT is only for the prompt, to indicate
  4605. what is being sorted. The sorting key will be extracted from
  4606. the car of the elements of the table.
  4607. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  4608. (unless sorting-type
  4609. (message
  4610. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  4611. what)
  4612. (setq sorting-type (read-char-exclusive)))
  4613. (let ((dcst (downcase sorting-type))
  4614. extractfun comparefun)
  4615. ;; Define the appropriate functions
  4616. (cond
  4617. ((= dcst ?n)
  4618. (setq extractfun 'string-to-number
  4619. comparefun (if (= dcst sorting-type) '< '>)))
  4620. ((= dcst ?a)
  4621. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  4622. (lambda(x) (downcase (org-sort-remove-invisible x))))
  4623. comparefun (if (= dcst sorting-type)
  4624. 'string<
  4625. (lambda (a b) (and (not (string< a b))
  4626. (not (string= a b)))))))
  4627. ((= dcst ?t)
  4628. (setq extractfun
  4629. (lambda (x)
  4630. (if (string-match org-ts-regexp x)
  4631. (time-to-seconds
  4632. (org-time-string-to-time (match-string 0 x)))
  4633. 0))
  4634. comparefun (if (= dcst sorting-type) '< '>)))
  4635. (t (error "Invalid sorting type `%c'" sorting-type)))
  4636. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  4637. table)
  4638. (lambda (a b) (funcall comparefun (car a) (car b))))))
  4639. ;;;; Plain list items, including checkboxes
  4640. ;;; Plain list items
  4641. (defun org-at-item-p ()
  4642. "Is point in a line starting a hand-formatted item?"
  4643. (let ((llt org-plain-list-ordered-item-terminator))
  4644. (save-excursion
  4645. (goto-char (point-at-bol))
  4646. (looking-at
  4647. (cond
  4648. ((eq llt t) "\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  4649. ((= llt ?.) "\\([ \t]*\\([-+]\\|\\([0-9]+\\.\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  4650. ((= llt ?\)) "\\([ \t]*\\([-+]\\|\\([0-9]+))\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  4651. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))))))
  4652. (defun org-in-item-p ()
  4653. "It the cursor inside a plain list item.
  4654. Does not have to be the first line."
  4655. (save-excursion
  4656. (condition-case nil
  4657. (progn
  4658. (org-beginning-of-item)
  4659. (org-at-item-p)
  4660. t)
  4661. (error nil))))
  4662. (defun org-insert-item (&optional checkbox)
  4663. "Insert a new item at the current level.
  4664. Return t when things worked, nil when we are not in an item."
  4665. (when (save-excursion
  4666. (condition-case nil
  4667. (progn
  4668. (org-beginning-of-item)
  4669. (org-at-item-p)
  4670. (if (org-invisible-p) (error "Invisible item"))
  4671. t)
  4672. (error nil)))
  4673. (let* ((bul (match-string 0))
  4674. (eow (save-excursion (beginning-of-line 1) (looking-at "[ \t]*")
  4675. (match-end 0)))
  4676. (blank (cdr (assq 'plain-list-item org-blank-before-new-entry)))
  4677. pos)
  4678. (cond
  4679. ((and (org-at-item-p) (<= (point) eow))
  4680. ;; before the bullet
  4681. (beginning-of-line 1)
  4682. (open-line (if blank 2 1)))
  4683. ((<= (point) eow)
  4684. (beginning-of-line 1))
  4685. (t
  4686. (unless (org-get-alist-option org-M-RET-may-split-line 'item)
  4687. (end-of-line 1)
  4688. (delete-horizontal-space))
  4689. (newline (if blank 2 1))))
  4690. (insert bul (if checkbox "[ ]" ""))
  4691. (just-one-space)
  4692. (setq pos (point))
  4693. (end-of-line 1)
  4694. (unless (= (point) pos) (just-one-space) (backward-delete-char 1)))
  4695. (org-maybe-renumber-ordered-list)
  4696. (and checkbox (org-update-checkbox-count-maybe))
  4697. t))
  4698. ;;; Checkboxes
  4699. (defun org-at-item-checkbox-p ()
  4700. "Is point at a line starting a plain-list item with a checklet?"
  4701. (and (org-at-item-p)
  4702. (save-excursion
  4703. (goto-char (match-end 0))
  4704. (skip-chars-forward " \t")
  4705. (looking-at "\\[[- X]\\]"))))
  4706. (defun org-toggle-checkbox (&optional arg)
  4707. "Toggle the checkbox in the current line."
  4708. (interactive "P")
  4709. (catch 'exit
  4710. (let (beg end status (firstnew 'unknown))
  4711. (cond
  4712. ((org-region-active-p)
  4713. (setq beg (region-beginning) end (region-end)))
  4714. ((org-on-heading-p)
  4715. (setq beg (point) end (save-excursion (outline-next-heading) (point))))
  4716. ((org-at-item-checkbox-p)
  4717. (let ((pos (point)))
  4718. (replace-match
  4719. (cond (arg "[-]")
  4720. ((member (match-string 0) '("[ ]" "[-]")) "[X]")
  4721. (t "[ ]"))
  4722. t t)
  4723. (goto-char pos))
  4724. (throw 'exit t))
  4725. (t (error "Not at a checkbox or heading, and no active region")))
  4726. (save-excursion
  4727. (goto-char beg)
  4728. (while (< (point) end)
  4729. (when (org-at-item-checkbox-p)
  4730. (setq status (equal (match-string 0) "[X]"))
  4731. (when (eq firstnew 'unknown)
  4732. (setq firstnew (not status)))
  4733. (replace-match
  4734. (if (if arg (not status) firstnew) "[X]" "[ ]") t t))
  4735. (beginning-of-line 2)))))
  4736. (org-update-checkbox-count-maybe))
  4737. (defun org-update-checkbox-count-maybe ()
  4738. "Update checkbox statistics unless turned off by user."
  4739. (when org-provide-checkbox-statistics
  4740. (org-update-checkbox-count)))
  4741. (defun org-update-checkbox-count (&optional all)
  4742. "Update the checkbox statistics in the current section.
  4743. This will find all statistic cookies like [57%] and [6/12] and update them
  4744. with the current numbers. With optional prefix argument ALL, do this for
  4745. the whole buffer."
  4746. (interactive "P")
  4747. (save-excursion
  4748. (let* ((buffer-invisibility-spec (org-inhibit-invisibility)) ; Emacs 21
  4749. (beg (condition-case nil
  4750. (progn (outline-back-to-heading) (point))
  4751. (error (point-min))))
  4752. (end (move-marker (make-marker)
  4753. (progn (outline-next-heading) (point))))
  4754. (re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  4755. (re-box "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)")
  4756. (re-find (concat re "\\|" re-box))
  4757. beg-cookie end-cookie is-percent c-on c-off lim
  4758. eline curr-ind next-ind continue-from startsearch
  4759. (cstat 0)
  4760. )
  4761. (when all
  4762. (goto-char (point-min))
  4763. (outline-next-heading)
  4764. (setq beg (point) end (point-max)))
  4765. (goto-char end)
  4766. ;; find each statistic cookie
  4767. (while (re-search-backward re-find beg t)
  4768. (setq beg-cookie (match-beginning 1)
  4769. end-cookie (match-end 1)
  4770. cstat (+ cstat (if end-cookie 1 0))
  4771. startsearch (point-at-eol)
  4772. continue-from (point-at-bol)
  4773. is-percent (match-beginning 2)
  4774. lim (cond
  4775. ((org-on-heading-p) (outline-next-heading) (point))
  4776. ((org-at-item-p) (org-end-of-item) (point))
  4777. (t nil))
  4778. c-on 0
  4779. c-off 0)
  4780. (when lim
  4781. ;; find first checkbox for this cookie and gather
  4782. ;; statistics from all that are at this indentation level
  4783. (goto-char startsearch)
  4784. (if (re-search-forward re-box lim t)
  4785. (progn
  4786. (org-beginning-of-item)
  4787. (setq curr-ind (org-get-indentation))
  4788. (setq next-ind curr-ind)
  4789. (while (and (bolp) (org-at-item-p) (= curr-ind next-ind))
  4790. (save-excursion (end-of-line) (setq eline (point)))
  4791. (if (re-search-forward re-box eline t)
  4792. (if (member (match-string 2) '("[ ]" "[-]"))
  4793. (setq c-off (1+ c-off))
  4794. (setq c-on (1+ c-on))
  4795. )
  4796. )
  4797. (org-end-of-item)
  4798. (setq next-ind (org-get-indentation))
  4799. )))
  4800. (goto-char continue-from)
  4801. ;; update cookie
  4802. (when end-cookie
  4803. (delete-region beg-cookie end-cookie)
  4804. (goto-char beg-cookie)
  4805. (insert
  4806. (if is-percent
  4807. (format "[%d%%]" (/ (* 100 c-on) (max 1 (+ c-on c-off))))
  4808. (format "[%d/%d]" c-on (+ c-on c-off)))))
  4809. ;; update items checkbox if it has one
  4810. (when (org-at-item-p)
  4811. (org-beginning-of-item)
  4812. (when (and (> (+ c-on c-off) 0)
  4813. (re-search-forward re-box (point-at-eol) t))
  4814. (setq beg-cookie (match-beginning 2)
  4815. end-cookie (match-end 2))
  4816. (delete-region beg-cookie end-cookie)
  4817. (goto-char beg-cookie)
  4818. (cond ((= c-off 0) (insert "[X]"))
  4819. ((= c-on 0) (insert "[ ]"))
  4820. (t (insert "[-]")))
  4821. )))
  4822. (goto-char continue-from))
  4823. (when (interactive-p)
  4824. (message "Checkbox satistics updated %s (%d places)"
  4825. (if all "in entire file" "in current outline entry") cstat)))))
  4826. (defun org-get-checkbox-statistics-face ()
  4827. "Select the face for checkbox statistics.
  4828. The face will be `org-done' when all relevant boxes are checked. Otherwise
  4829. it will be `org-todo'."
  4830. (if (match-end 1)
  4831. (if (equal (match-string 1) "100%") 'org-done 'org-todo)
  4832. (if (and (> (match-end 2) (match-beginning 2))
  4833. (equal (match-string 2) (match-string 3)))
  4834. 'org-done
  4835. 'org-todo)))
  4836. (defun org-get-indentation (&optional line)
  4837. "Get the indentation of the current line, interpreting tabs.
  4838. When LINE is given, assume it represents a line and compute its indentation."
  4839. (if line
  4840. (if (string-match "^ *" (org-remove-tabs line))
  4841. (match-end 0))
  4842. (save-excursion
  4843. (beginning-of-line 1)
  4844. (skip-chars-forward " \t")
  4845. (current-column))))
  4846. (defun org-remove-tabs (s &optional width)
  4847. "Replace tabulators in S with spaces.
  4848. Assumes that s is a single line, starting in column 0."
  4849. (setq width (or width tab-width))
  4850. (while (string-match "\t" s)
  4851. (setq s (replace-match
  4852. (make-string
  4853. (- (* width (/ (+ (match-beginning 0) width) width))
  4854. (match-beginning 0)) ?\ )
  4855. t t s)))
  4856. s)
  4857. (defun org-fix-indentation (line ind)
  4858. "Fix indentation in LINE.
  4859. IND is a cons cell with target and minimum indentation.
  4860. If the current indenation in LINE is smaller than the minimum,
  4861. leave it alone. If it is larger than ind, set it to the target."
  4862. (let* ((l (org-remove-tabs line))
  4863. (i (org-get-indentation l))
  4864. (i1 (car ind)) (i2 (cdr ind)))
  4865. (if (>= i i2) (setq l (substring line i2)))
  4866. (if (> i1 0)
  4867. (concat (make-string i1 ?\ ) l)
  4868. l)))
  4869. (defun org-beginning-of-item ()
  4870. "Go to the beginning of the current hand-formatted item.
  4871. If the cursor is not in an item, throw an error."
  4872. (interactive)
  4873. (let ((pos (point))
  4874. (limit (save-excursion
  4875. (condition-case nil
  4876. (progn
  4877. (org-back-to-heading)
  4878. (beginning-of-line 2) (point))
  4879. (error (point-min)))))
  4880. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  4881. ind ind1)
  4882. (if (org-at-item-p)
  4883. (beginning-of-line 1)
  4884. (beginning-of-line 1)
  4885. (skip-chars-forward " \t")
  4886. (setq ind (current-column))
  4887. (if (catch 'exit
  4888. (while t
  4889. (beginning-of-line 0)
  4890. (if (or (bobp) (< (point) limit)) (throw 'exit nil))
  4891. (if (looking-at "[ \t]*$")
  4892. (setq ind1 ind-empty)
  4893. (skip-chars-forward " \t")
  4894. (setq ind1 (current-column)))
  4895. (if (< ind1 ind)
  4896. (progn (beginning-of-line 1) (throw 'exit (org-at-item-p))))))
  4897. nil
  4898. (goto-char pos)
  4899. (error "Not in an item")))))
  4900. (defun org-end-of-item ()
  4901. "Go to the end of the current hand-formatted item.
  4902. If the cursor is not in an item, throw an error."
  4903. (interactive)
  4904. (let* ((pos (point))
  4905. ind1
  4906. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  4907. (limit (save-excursion (outline-next-heading) (point)))
  4908. (ind (save-excursion
  4909. (org-beginning-of-item)
  4910. (skip-chars-forward " \t")
  4911. (current-column)))
  4912. (end (catch 'exit
  4913. (while t
  4914. (beginning-of-line 2)
  4915. (if (eobp) (throw 'exit (point)))
  4916. (if (>= (point) limit) (throw 'exit (point-at-bol)))
  4917. (if (looking-at "[ \t]*$")
  4918. (setq ind1 ind-empty)
  4919. (skip-chars-forward " \t")
  4920. (setq ind1 (current-column)))
  4921. (if (<= ind1 ind)
  4922. (throw 'exit (point-at-bol)))))))
  4923. (if end
  4924. (goto-char end)
  4925. (goto-char pos)
  4926. (error "Not in an item"))))
  4927. (defun org-next-item ()
  4928. "Move to the beginning of the next item in the current plain list.
  4929. Error if not at a plain list, or if this is the last item in the list."
  4930. (interactive)
  4931. (let (ind ind1 (pos (point)))
  4932. (org-beginning-of-item)
  4933. (setq ind (org-get-indentation))
  4934. (org-end-of-item)
  4935. (setq ind1 (org-get-indentation))
  4936. (unless (and (org-at-item-p) (= ind ind1))
  4937. (goto-char pos)
  4938. (error "On last item"))))
  4939. (defun org-previous-item ()
  4940. "Move to the beginning of the previous item in the current plain list.
  4941. Error if not at a plain list, or if this is the first item in the list."
  4942. (interactive)
  4943. (let (beg ind ind1 (pos (point)))
  4944. (org-beginning-of-item)
  4945. (setq beg (point))
  4946. (setq ind (org-get-indentation))
  4947. (goto-char beg)
  4948. (catch 'exit
  4949. (while t
  4950. (beginning-of-line 0)
  4951. (if (looking-at "[ \t]*$")
  4952. nil
  4953. (if (<= (setq ind1 (org-get-indentation)) ind)
  4954. (throw 'exit t)))))
  4955. (condition-case nil
  4956. (if (or (not (org-at-item-p))
  4957. (< ind1 (1- ind)))
  4958. (error "")
  4959. (org-beginning-of-item))
  4960. (error (goto-char pos)
  4961. (error "On first item")))))
  4962. (defun org-first-list-item-p ()
  4963. "Is this heading the item in a plain list?"
  4964. (unless (org-at-item-p)
  4965. (error "Not at a plain list item"))
  4966. (org-beginning-of-item)
  4967. (= (point) (save-excursion (org-beginning-of-item-list))))
  4968. (defun org-move-item-down ()
  4969. "Move the plain list item at point down, i.e. swap with following item.
  4970. Subitems (items with larger indentation) are considered part of the item,
  4971. so this really moves item trees."
  4972. (interactive)
  4973. (let (beg beg0 end end0 ind ind1 (pos (point)) txt ne-end ne-beg)
  4974. (org-beginning-of-item)
  4975. (setq beg0 (point))
  4976. (save-excursion
  4977. (setq ne-beg (org-back-over-empty-lines))
  4978. (setq beg (point)))
  4979. (goto-char beg0)
  4980. (setq ind (org-get-indentation))
  4981. (org-end-of-item)
  4982. (setq end0 (point))
  4983. (setq ind1 (org-get-indentation))
  4984. (setq ne-end (org-back-over-empty-lines))
  4985. (setq end (point))
  4986. (goto-char beg0)
  4987. (when (and (org-first-list-item-p) (< ne-end ne-beg))
  4988. ;; include less whitespace
  4989. (save-excursion
  4990. (goto-char beg)
  4991. (forward-line (- ne-beg ne-end))
  4992. (setq beg (point))))
  4993. (goto-char end0)
  4994. (if (and (org-at-item-p) (= ind ind1))
  4995. (progn
  4996. (org-end-of-item)
  4997. (org-back-over-empty-lines)
  4998. (setq txt (buffer-substring beg end))
  4999. (save-excursion
  5000. (delete-region beg end))
  5001. (setq pos (point))
  5002. (insert txt)
  5003. (goto-char pos) (org-skip-whitespace)
  5004. (org-maybe-renumber-ordered-list))
  5005. (goto-char pos)
  5006. (error "Cannot move this item further down"))))
  5007. (defun org-move-item-up (arg)
  5008. "Move the plain list item at point up, i.e. swap with previous item.
  5009. Subitems (items with larger indentation) are considered part of the item,
  5010. so this really moves item trees."
  5011. (interactive "p")
  5012. (let (beg beg0 end ind ind1 (pos (point)) txt
  5013. ne-beg ne-ins ins-end)
  5014. (org-beginning-of-item)
  5015. (setq beg0 (point))
  5016. (setq ind (org-get-indentation))
  5017. (save-excursion
  5018. (setq ne-beg (org-back-over-empty-lines))
  5019. (setq beg (point)))
  5020. (goto-char beg0)
  5021. (org-end-of-item)
  5022. (setq end (point))
  5023. (goto-char beg0)
  5024. (catch 'exit
  5025. (while t
  5026. (beginning-of-line 0)
  5027. (if (looking-at "[ \t]*$")
  5028. (if org-empty-line-terminates-plain-lists
  5029. (progn
  5030. (goto-char pos)
  5031. (error "Cannot move this item further up"))
  5032. nil)
  5033. (if (<= (setq ind1 (org-get-indentation)) ind)
  5034. (throw 'exit t)))))
  5035. (condition-case nil
  5036. (org-beginning-of-item)
  5037. (error (goto-char beg)
  5038. (error "Cannot move this item further up")))
  5039. (setq ind1 (org-get-indentation))
  5040. (if (and (org-at-item-p) (= ind ind1))
  5041. (progn
  5042. (setq ne-ins (org-back-over-empty-lines))
  5043. (setq txt (buffer-substring beg end))
  5044. (save-excursion
  5045. (delete-region beg end))
  5046. (setq pos (point))
  5047. (insert txt)
  5048. (setq ins-end (point))
  5049. (goto-char pos) (org-skip-whitespace)
  5050. (when (and (org-first-list-item-p) (> ne-ins ne-beg))
  5051. ;; Move whitespace back to beginning
  5052. (save-excursion
  5053. (goto-char ins-end)
  5054. (let ((kill-whole-line t))
  5055. (kill-line (- ne-ins ne-beg)) (point)))
  5056. (insert (make-string (- ne-ins ne-beg) ?\n)))
  5057. (org-maybe-renumber-ordered-list))
  5058. (goto-char pos)
  5059. (error "Cannot move this item further up"))))
  5060. (defun org-maybe-renumber-ordered-list ()
  5061. "Renumber the ordered list at point if setup allows it.
  5062. This tests the user option `org-auto-renumber-ordered-lists' before
  5063. doing the renumbering."
  5064. (interactive)
  5065. (when (and org-auto-renumber-ordered-lists
  5066. (org-at-item-p))
  5067. (if (match-beginning 3)
  5068. (org-renumber-ordered-list 1)
  5069. (org-fix-bullet-type))))
  5070. (defun org-maybe-renumber-ordered-list-safe ()
  5071. (condition-case nil
  5072. (save-excursion
  5073. (org-maybe-renumber-ordered-list))
  5074. (error nil)))
  5075. (defun org-cycle-list-bullet (&optional which)
  5076. "Cycle through the different itemize/enumerate bullets.
  5077. This cycle the entire list level through the sequence:
  5078. `-' -> `+' -> `*' -> `1.' -> `1)'
  5079. If WHICH is a string, use that as the new bullet. If WHICH is an integer,
  5080. 0 meand `-', 1 means `+' etc."
  5081. (interactive "P")
  5082. (org-preserve-lc
  5083. (org-beginning-of-item-list)
  5084. (org-at-item-p)
  5085. (beginning-of-line 1)
  5086. (let ((current (match-string 0))
  5087. (prevp (eq which 'previous))
  5088. new)
  5089. (setq new (cond
  5090. ((and (numberp which)
  5091. (nth (1- which) '("-" "+" "*" "1." "1)"))))
  5092. ((string-match "-" current) (if prevp "1)" "+"))
  5093. ((string-match "\\+" current)
  5094. (if prevp "-" (if (looking-at "\\S-") "1." "*")))
  5095. ((string-match "\\*" current) (if prevp "+" "1."))
  5096. ((string-match "\\." current) (if prevp "*" "1)"))
  5097. ((string-match ")" current) (if prevp "1." "-"))
  5098. (t (error "This should not happen"))))
  5099. (and (looking-at "\\([ \t]*\\)\\S-+") (replace-match (concat "\\1" new)))
  5100. (org-fix-bullet-type)
  5101. (org-maybe-renumber-ordered-list))))
  5102. (defun org-get-string-indentation (s)
  5103. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  5104. (let ((n -1) (i 0) (w tab-width) c)
  5105. (catch 'exit
  5106. (while (< (setq n (1+ n)) (length s))
  5107. (setq c (aref s n))
  5108. (cond ((= c ?\ ) (setq i (1+ i)))
  5109. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  5110. (t (throw 'exit t)))))
  5111. i))
  5112. (defun org-renumber-ordered-list (arg)
  5113. "Renumber an ordered plain list.
  5114. Cursor needs to be in the first line of an item, the line that starts
  5115. with something like \"1.\" or \"2)\"."
  5116. (interactive "p")
  5117. (unless (and (org-at-item-p)
  5118. (match-beginning 3))
  5119. (error "This is not an ordered list"))
  5120. (let ((line (org-current-line))
  5121. (col (current-column))
  5122. (ind (org-get-string-indentation
  5123. (buffer-substring (point-at-bol) (match-beginning 3))))
  5124. ;; (term (substring (match-string 3) -1))
  5125. ind1 (n (1- arg))
  5126. fmt)
  5127. ;; find where this list begins
  5128. (org-beginning-of-item-list)
  5129. (looking-at "[ \t]*[0-9]+\\([.)]\\)")
  5130. (setq fmt (concat "%d" (match-string 1)))
  5131. (beginning-of-line 0)
  5132. ;; walk forward and replace these numbers
  5133. (catch 'exit
  5134. (while t
  5135. (catch 'next
  5136. (beginning-of-line 2)
  5137. (if (eobp) (throw 'exit nil))
  5138. (if (looking-at "[ \t]*$") (throw 'next nil))
  5139. (skip-chars-forward " \t") (setq ind1 (current-column))
  5140. (if (> ind1 ind) (throw 'next t))
  5141. (if (< ind1 ind) (throw 'exit t))
  5142. (if (not (org-at-item-p)) (throw 'exit nil))
  5143. (delete-region (match-beginning 2) (match-end 2))
  5144. (goto-char (match-beginning 2))
  5145. (insert (format fmt (setq n (1+ n)))))))
  5146. (goto-line line)
  5147. (org-move-to-column col)))
  5148. (defun org-fix-bullet-type ()
  5149. "Make sure all items in this list have the same bullet as the firsst item."
  5150. (interactive)
  5151. (unless (org-at-item-p) (error "This is not a list"))
  5152. (let ((line (org-current-line))
  5153. (col (current-column))
  5154. (ind (current-indentation))
  5155. ind1 bullet)
  5156. ;; find where this list begins
  5157. (org-beginning-of-item-list)
  5158. (beginning-of-line 1)
  5159. ;; find out what the bullet type is
  5160. (looking-at "[ \t]*\\(\\S-+\\)")
  5161. (setq bullet (match-string 1))
  5162. ;; walk forward and replace these numbers
  5163. (beginning-of-line 0)
  5164. (catch 'exit
  5165. (while t
  5166. (catch 'next
  5167. (beginning-of-line 2)
  5168. (if (eobp) (throw 'exit nil))
  5169. (if (looking-at "[ \t]*$") (throw 'next nil))
  5170. (skip-chars-forward " \t") (setq ind1 (current-column))
  5171. (if (> ind1 ind) (throw 'next t))
  5172. (if (< ind1 ind) (throw 'exit t))
  5173. (if (not (org-at-item-p)) (throw 'exit nil))
  5174. (skip-chars-forward " \t")
  5175. (looking-at "\\S-+")
  5176. (replace-match bullet))))
  5177. (goto-line line)
  5178. (org-move-to-column col)
  5179. (if (string-match "[0-9]" bullet)
  5180. (org-renumber-ordered-list 1))))
  5181. (defun org-beginning-of-item-list ()
  5182. "Go to the beginning of the current item list.
  5183. I.e. to the first item in this list."
  5184. (interactive)
  5185. (org-beginning-of-item)
  5186. (let ((pos (point-at-bol))
  5187. (ind (org-get-indentation))
  5188. ind1)
  5189. ;; find where this list begins
  5190. (catch 'exit
  5191. (while t
  5192. (catch 'next
  5193. (beginning-of-line 0)
  5194. (if (looking-at "[ \t]*$")
  5195. (throw (if (bobp) 'exit 'next) t))
  5196. (skip-chars-forward " \t") (setq ind1 (current-column))
  5197. (if (or (< ind1 ind)
  5198. (and (= ind1 ind)
  5199. (not (org-at-item-p)))
  5200. (bobp))
  5201. (throw 'exit t)
  5202. (when (org-at-item-p) (setq pos (point-at-bol)))))))
  5203. (goto-char pos)))
  5204. (defun org-end-of-item-list ()
  5205. "Go to the end of the current item list.
  5206. I.e. to the text after the last item."
  5207. (interactive)
  5208. (org-beginning-of-item)
  5209. (let ((pos (point-at-bol))
  5210. (ind (org-get-indentation))
  5211. ind1)
  5212. ;; find where this list begins
  5213. (catch 'exit
  5214. (while t
  5215. (catch 'next
  5216. (beginning-of-line 2)
  5217. (if (looking-at "[ \t]*$")
  5218. (throw (if (eobp) 'exit 'next) t))
  5219. (skip-chars-forward " \t") (setq ind1 (current-column))
  5220. (if (or (< ind1 ind)
  5221. (and (= ind1 ind)
  5222. (not (org-at-item-p)))
  5223. (eobp))
  5224. (progn
  5225. (setq pos (point-at-bol))
  5226. (throw 'exit t))))))
  5227. (goto-char pos)))
  5228. (defvar org-last-indent-begin-marker (make-marker))
  5229. (defvar org-last-indent-end-marker (make-marker))
  5230. (defun org-outdent-item (arg)
  5231. "Outdent a local list item."
  5232. (interactive "p")
  5233. (org-indent-item (- arg)))
  5234. (defun org-indent-item (arg)
  5235. "Indent a local list item."
  5236. (interactive "p")
  5237. (unless (org-at-item-p)
  5238. (error "Not on an item"))
  5239. (save-excursion
  5240. (let (beg end ind ind1 tmp delta ind-down ind-up)
  5241. (if (memq last-command '(org-shiftmetaright org-shiftmetaleft))
  5242. (setq beg org-last-indent-begin-marker
  5243. end org-last-indent-end-marker)
  5244. (org-beginning-of-item)
  5245. (setq beg (move-marker org-last-indent-begin-marker (point)))
  5246. (org-end-of-item)
  5247. (setq end (move-marker org-last-indent-end-marker (point))))
  5248. (goto-char beg)
  5249. (setq tmp (org-item-indent-positions)
  5250. ind (car tmp)
  5251. ind-down (nth 2 tmp)
  5252. ind-up (nth 1 tmp)
  5253. delta (if (> arg 0)
  5254. (if ind-down (- ind-down ind) 2)
  5255. (if ind-up (- ind-up ind) -2)))
  5256. (if (< (+ delta ind) 0) (error "Cannot outdent beyond margin"))
  5257. (while (< (point) end)
  5258. (beginning-of-line 1)
  5259. (skip-chars-forward " \t") (setq ind1 (current-column))
  5260. (delete-region (point-at-bol) (point))
  5261. (or (eolp) (org-indent-to-column (+ ind1 delta)))
  5262. (beginning-of-line 2))))
  5263. (org-fix-bullet-type)
  5264. (org-maybe-renumber-ordered-list-safe)
  5265. (save-excursion
  5266. (beginning-of-line 0)
  5267. (condition-case nil (org-beginning-of-item) (error nil))
  5268. (org-maybe-renumber-ordered-list-safe)))
  5269. (defun org-item-indent-positions ()
  5270. "Return indentation for plain list items.
  5271. This returns a list with three values: The current indentation, the
  5272. parent indentation and the indentation a child should habe.
  5273. Assumes cursor in item line."
  5274. (let* ((bolpos (point-at-bol))
  5275. (ind (org-get-indentation))
  5276. ind-down ind-up pos)
  5277. (save-excursion
  5278. (org-beginning-of-item-list)
  5279. (skip-chars-backward "\n\r \t")
  5280. (when (org-in-item-p)
  5281. (org-beginning-of-item)
  5282. (setq ind-up (org-get-indentation))))
  5283. (setq pos (point))
  5284. (save-excursion
  5285. (cond
  5286. ((and (condition-case nil (progn (org-previous-item) t)
  5287. (error nil))
  5288. (or (forward-char 1) t)
  5289. (re-search-forward "^\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)" bolpos t))
  5290. (setq ind-down (org-get-indentation)))
  5291. ((and (goto-char pos)
  5292. (org-at-item-p))
  5293. (goto-char (match-end 0))
  5294. (skip-chars-forward " \t")
  5295. (setq ind-down (current-column)))))
  5296. (list ind ind-up ind-down)))
  5297. ;;; The orgstruct minor mode
  5298. ;; Define a minor mode which can be used in other modes in order to
  5299. ;; integrate the org-mode structure editing commands.
  5300. ;; This is really a hack, because the org-mode structure commands use
  5301. ;; keys which normally belong to the major mode. Here is how it
  5302. ;; works: The minor mode defines all the keys necessary to operate the
  5303. ;; structure commands, but wraps the commands into a function which
  5304. ;; tests if the cursor is currently at a headline or a plain list
  5305. ;; item. If that is the case, the structure command is used,
  5306. ;; temporarily setting many Org-mode variables like regular
  5307. ;; expressions for filling etc. However, when any of those keys is
  5308. ;; used at a different location, function uses `key-binding' to look
  5309. ;; up if the key has an associated command in another currently active
  5310. ;; keymap (minor modes, major mode, global), and executes that
  5311. ;; command. There might be problems if any of the keys is otherwise
  5312. ;; used as a prefix key.
  5313. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  5314. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  5315. ;; addresses this by checking explicitly for both bindings.
  5316. (defvar orgstruct-mode-map (make-sparse-keymap)
  5317. "Keymap for the minor `orgstruct-mode'.")
  5318. (defvar org-local-vars nil
  5319. "List of local variables, for use by `orgstruct-mode'")
  5320. ;;;###autoload
  5321. (define-minor-mode orgstruct-mode
  5322. "Toggle the minor more `orgstruct-mode'.
  5323. This mode is for using Org-mode structure commands in other modes.
  5324. The following key behave as if Org-mode was active, if the cursor
  5325. is on a headline, or on a plain list item (both in the definition
  5326. of Org-mode).
  5327. M-up Move entry/item up
  5328. M-down Move entry/item down
  5329. M-left Promote
  5330. M-right Demote
  5331. M-S-up Move entry/item up
  5332. M-S-down Move entry/item down
  5333. M-S-left Promote subtree
  5334. M-S-right Demote subtree
  5335. M-q Fill paragraph and items like in Org-mode
  5336. C-c ^ Sort entries
  5337. C-c - Cycle list bullet
  5338. TAB Cycle item visibility
  5339. M-RET Insert new heading/item
  5340. S-M-RET Insert new TODO heading / Chekbox item
  5341. C-c C-c Set tags / toggle checkbox"
  5342. nil " OrgStruct" nil
  5343. (org-load-modules-maybe)
  5344. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  5345. ;;;###autoload
  5346. (defun turn-on-orgstruct ()
  5347. "Unconditionally turn on `orgstruct-mode'."
  5348. (orgstruct-mode 1))
  5349. ;;;###autoload
  5350. (defun turn-on-orgstruct++ ()
  5351. "Unconditionally turn on `orgstruct-mode', and force org-mode indentations.
  5352. In addition to setting orgstruct-mode, this also exports all indentation and
  5353. autofilling variables from org-mode into the buffer. Note that turning
  5354. off orgstruct-mode will *not* remove these additional settings."
  5355. (orgstruct-mode 1)
  5356. (let (var val)
  5357. (mapc
  5358. (lambda (x)
  5359. (when (string-match
  5360. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5361. (symbol-name (car x)))
  5362. (setq var (car x) val (nth 1 x))
  5363. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  5364. org-local-vars)))
  5365. (defun orgstruct-error ()
  5366. "Error when there is no default binding for a structure key."
  5367. (interactive)
  5368. (error "This key has no function outside structure elements"))
  5369. (defun orgstruct-setup ()
  5370. "Setup orgstruct keymaps."
  5371. (let ((nfunc 0)
  5372. (bindings
  5373. (list
  5374. '([(meta up)] org-metaup)
  5375. '([(meta down)] org-metadown)
  5376. '([(meta left)] org-metaleft)
  5377. '([(meta right)] org-metaright)
  5378. '([(meta shift up)] org-shiftmetaup)
  5379. '([(meta shift down)] org-shiftmetadown)
  5380. '([(meta shift left)] org-shiftmetaleft)
  5381. '([(meta shift right)] org-shiftmetaright)
  5382. '([(shift up)] org-shiftup)
  5383. '([(shift down)] org-shiftdown)
  5384. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  5385. '("\M-q" fill-paragraph)
  5386. '("\C-c^" org-sort)
  5387. '("\C-c-" org-cycle-list-bullet)))
  5388. elt key fun cmd)
  5389. (while (setq elt (pop bindings))
  5390. (setq nfunc (1+ nfunc))
  5391. (setq key (org-key (car elt))
  5392. fun (nth 1 elt)
  5393. cmd (orgstruct-make-binding fun nfunc key))
  5394. (org-defkey orgstruct-mode-map key cmd))
  5395. ;; Special treatment needed for TAB and RET
  5396. (org-defkey orgstruct-mode-map [(tab)]
  5397. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  5398. (org-defkey orgstruct-mode-map "\C-i"
  5399. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  5400. (org-defkey orgstruct-mode-map "\M-\C-m"
  5401. (orgstruct-make-binding 'org-insert-heading 105
  5402. "\M-\C-m" [(meta return)]))
  5403. (org-defkey orgstruct-mode-map [(meta return)]
  5404. (orgstruct-make-binding 'org-insert-heading 106
  5405. [(meta return)] "\M-\C-m"))
  5406. (org-defkey orgstruct-mode-map [(shift meta return)]
  5407. (orgstruct-make-binding 'org-insert-todo-heading 107
  5408. [(meta return)] "\M-\C-m"))
  5409. (unless org-local-vars
  5410. (setq org-local-vars (org-get-local-variables)))
  5411. t))
  5412. (defun orgstruct-make-binding (fun n &rest keys)
  5413. "Create a function for binding in the structure minor mode.
  5414. FUN is the command to call inside a table. N is used to create a unique
  5415. command name. KEYS are keys that should be checked in for a command
  5416. to execute outside of tables."
  5417. (eval
  5418. (list 'defun
  5419. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  5420. '(arg)
  5421. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  5422. "Outside of structure, run the binding of `"
  5423. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  5424. "'.")
  5425. '(interactive "p")
  5426. (list 'if
  5427. '(org-context-p 'headline 'item)
  5428. (list 'org-run-like-in-org-mode (list 'quote fun))
  5429. (list 'let '(orgstruct-mode)
  5430. (list 'call-interactively
  5431. (append '(or)
  5432. (mapcar (lambda (k)
  5433. (list 'key-binding k))
  5434. keys)
  5435. '('orgstruct-error))))))))
  5436. (defun org-context-p (&rest contexts)
  5437. "Check if local context is and of CONTEXTS.
  5438. Possible values in the list of contexts are `table', `headline', and `item'."
  5439. (let ((pos (point)))
  5440. (goto-char (point-at-bol))
  5441. (prog1 (or (and (memq 'table contexts)
  5442. (looking-at "[ \t]*|"))
  5443. (and (memq 'headline contexts)
  5444. (looking-at "\\*+"))
  5445. (and (memq 'item contexts)
  5446. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)")))
  5447. (goto-char pos))))
  5448. (defun org-get-local-variables ()
  5449. "Return a list of all local variables in an org-mode buffer."
  5450. (let (varlist)
  5451. (with-current-buffer (get-buffer-create "*Org tmp*")
  5452. (erase-buffer)
  5453. (org-mode)
  5454. (setq varlist (buffer-local-variables)))
  5455. (kill-buffer "*Org tmp*")
  5456. (delq nil
  5457. (mapcar
  5458. (lambda (x)
  5459. (setq x
  5460. (if (symbolp x)
  5461. (list x)
  5462. (list (car x) (list 'quote (cdr x)))))
  5463. (if (string-match
  5464. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5465. (symbol-name (car x)))
  5466. x nil))
  5467. varlist))))
  5468. ;;;###autoload
  5469. (defun org-run-like-in-org-mode (cmd)
  5470. (org-load-modules-maybe)
  5471. (unless org-local-vars
  5472. (setq org-local-vars (org-get-local-variables)))
  5473. (eval (list 'let org-local-vars
  5474. (list 'call-interactively (list 'quote cmd)))))
  5475. ;;;; Archiving
  5476. (defun org-get-category (&optional pos)
  5477. "Get the category applying to position POS."
  5478. (get-text-property (or pos (point)) 'org-category))
  5479. (defun org-refresh-category-properties ()
  5480. "Refresh category text properties in the buffer."
  5481. (let ((def-cat (cond
  5482. ((null org-category)
  5483. (if buffer-file-name
  5484. (file-name-sans-extension
  5485. (file-name-nondirectory buffer-file-name))
  5486. "???"))
  5487. ((symbolp org-category) (symbol-name org-category))
  5488. (t org-category)))
  5489. beg end cat pos optionp)
  5490. (org-unmodified
  5491. (save-excursion
  5492. (save-restriction
  5493. (widen)
  5494. (goto-char (point-min))
  5495. (put-text-property (point) (point-max) 'org-category def-cat)
  5496. (while (re-search-forward
  5497. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  5498. (setq pos (match-end 0)
  5499. optionp (equal (char-after (match-beginning 0)) ?#)
  5500. cat (org-trim (match-string 2)))
  5501. (if optionp
  5502. (setq beg (point-at-bol) end (point-max))
  5503. (org-back-to-heading t)
  5504. (setq beg (point) end (org-end-of-subtree t t)))
  5505. (put-text-property beg end 'org-category cat)
  5506. (goto-char pos)))))))
  5507. ;;;; Link Stuff
  5508. ;;; Link abbreviations
  5509. (defun org-link-expand-abbrev (link)
  5510. "Apply replacements as defined in `org-link-abbrev-alist."
  5511. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  5512. (let* ((key (match-string 1 link))
  5513. (as (or (assoc key org-link-abbrev-alist-local)
  5514. (assoc key org-link-abbrev-alist)))
  5515. (tag (and (match-end 2) (match-string 3 link)))
  5516. rpl)
  5517. (if (not as)
  5518. link
  5519. (setq rpl (cdr as))
  5520. (cond
  5521. ((symbolp rpl) (funcall rpl tag))
  5522. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  5523. (t (concat rpl tag)))))
  5524. link))
  5525. ;;; Storing and inserting links
  5526. (defvar org-insert-link-history nil
  5527. "Minibuffer history for links inserted with `org-insert-link'.")
  5528. (defvar org-stored-links nil
  5529. "Contains the links stored with `org-store-link'.")
  5530. (defvar org-store-link-plist nil
  5531. "Plist with info about the most recently link created with `org-store-link'.")
  5532. (defvar org-link-protocols nil
  5533. "Link protocols added to Org-mode using `org-add-link-type'.")
  5534. (defvar org-store-link-functions nil
  5535. "List of functions that are called to create and store a link.
  5536. Each function will be called in turn until one returns a non-nil
  5537. value. Each function should check if it is responsible for creating
  5538. this link (for example by looking at the major mode).
  5539. If not, it must exit and return nil.
  5540. If yes, it should return a non-nil value after a calling
  5541. `org-store-link-props' with a list of properties and values.
  5542. Special properties are:
  5543. :type The link prefix. like \"http\". This must be given.
  5544. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  5545. This is obligatory as well.
  5546. :description Optional default description for the second pair
  5547. of brackets in an Org-mode link. The user can still change
  5548. this when inserting this link into an Org-mode buffer.
  5549. In addition to these, any additional properties can be specified
  5550. and then used in remember templates.")
  5551. (defun org-add-link-type (type &optional follow export)
  5552. "Add TYPE to the list of `org-link-types'.
  5553. Re-compute all regular expressions depending on `org-link-types'
  5554. FOLLOW and EXPORT are two functions.
  5555. FOLLOW should take the link path as the single argument and do whatever
  5556. is necessary to follow the link, for example find a file or display
  5557. a mail message.
  5558. EXPORT should format the link path for export to one of the export formats.
  5559. It should be a function accepting three arguments:
  5560. path the path of the link, the text after the prefix (like \"http:\")
  5561. desc the description of the link, if any, nil if there was no descripton
  5562. format the export format, a symbol like `html' or `latex'.
  5563. The function may use the FORMAT information to return different values
  5564. depending on the format. The return value will be put literally into
  5565. the exported file.
  5566. Org-mode has a built-in default for exporting links. If you are happy with
  5567. this default, there is no need to define an export function for the link
  5568. type. For a simple example of an export function, see `org-bbdb.el'."
  5569. (add-to-list 'org-link-types type t)
  5570. (org-make-link-regexps)
  5571. (if (assoc type org-link-protocols)
  5572. (setcdr (assoc type org-link-protocols) (list follow export))
  5573. (push (list type follow export) org-link-protocols)))
  5574. ;;;###autoload
  5575. (defun org-store-link (arg)
  5576. "\\<org-mode-map>Store an org-link to the current location.
  5577. This link is added to `org-stored-links' and can later be inserted
  5578. into an org-buffer with \\[org-insert-link].
  5579. For some link types, a prefix arg is interpreted:
  5580. For links to usenet articles, arg negates `org-usenet-links-prefer-google'.
  5581. For file links, arg negates `org-context-in-file-links'."
  5582. (interactive "P")
  5583. (org-load-modules-maybe)
  5584. (setq org-store-link-plist nil) ; reset
  5585. (let (link cpltxt desc description search txt)
  5586. (cond
  5587. ((run-hook-with-args-until-success 'org-store-link-functions)
  5588. (setq link (plist-get org-store-link-plist :link)
  5589. desc (or (plist-get org-store-link-plist :description) link)))
  5590. ((eq major-mode 'calendar-mode)
  5591. (let ((cd (calendar-cursor-to-date)))
  5592. (setq link
  5593. (format-time-string
  5594. (car org-time-stamp-formats)
  5595. (apply 'encode-time
  5596. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  5597. nil nil nil))))
  5598. (org-store-link-props :type "calendar" :date cd)))
  5599. ((eq major-mode 'w3-mode)
  5600. (setq cpltxt (url-view-url t)
  5601. link (org-make-link cpltxt))
  5602. (org-store-link-props :type "w3" :url (url-view-url t)))
  5603. ((eq major-mode 'w3m-mode)
  5604. (setq cpltxt (or w3m-current-title w3m-current-url)
  5605. link (org-make-link w3m-current-url))
  5606. (org-store-link-props :type "w3m" :url (url-view-url t)))
  5607. ((setq search (run-hook-with-args-until-success
  5608. 'org-create-file-search-functions))
  5609. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  5610. "::" search))
  5611. (setq cpltxt (or description link)))
  5612. ((eq major-mode 'image-mode)
  5613. (setq cpltxt (concat "file:"
  5614. (abbreviate-file-name buffer-file-name))
  5615. link (org-make-link cpltxt))
  5616. (org-store-link-props :type "image" :file buffer-file-name))
  5617. ((eq major-mode 'dired-mode)
  5618. ;; link to the file in the current line
  5619. (setq cpltxt (concat "file:"
  5620. (abbreviate-file-name
  5621. (expand-file-name
  5622. (dired-get-filename nil t))))
  5623. link (org-make-link cpltxt)))
  5624. ((and buffer-file-name (org-mode-p))
  5625. ;; Just link to current headline
  5626. (setq cpltxt (concat "file:"
  5627. (abbreviate-file-name buffer-file-name)))
  5628. ;; Add a context search string
  5629. (when (org-xor org-context-in-file-links arg)
  5630. ;; Check if we are on a target
  5631. (if (org-in-regexp "<<\\(.*?\\)>>")
  5632. (setq cpltxt (concat cpltxt "::" (match-string 1)))
  5633. (setq txt (cond
  5634. ((org-on-heading-p) nil)
  5635. ((org-region-active-p)
  5636. (buffer-substring (region-beginning) (region-end)))
  5637. (t nil)))
  5638. (when (or (null txt) (string-match "\\S-" txt))
  5639. (setq cpltxt
  5640. (concat cpltxt "::"
  5641. (condition-case nil
  5642. (org-make-org-heading-search-string txt)
  5643. (error "")))
  5644. desc "NONE"))))
  5645. (if (string-match "::\\'" cpltxt)
  5646. (setq cpltxt (substring cpltxt 0 -2)))
  5647. (setq link (org-make-link cpltxt)))
  5648. ((buffer-file-name (buffer-base-buffer))
  5649. ;; Just link to this file here.
  5650. (setq cpltxt (concat "file:"
  5651. (abbreviate-file-name
  5652. (buffer-file-name (buffer-base-buffer)))))
  5653. ;; Add a context string
  5654. (when (org-xor org-context-in-file-links arg)
  5655. (setq txt (if (org-region-active-p)
  5656. (buffer-substring (region-beginning) (region-end))
  5657. (buffer-substring (point-at-bol) (point-at-eol))))
  5658. ;; Only use search option if there is some text.
  5659. (when (string-match "\\S-" txt)
  5660. (setq cpltxt
  5661. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  5662. desc "NONE")))
  5663. (setq link (org-make-link cpltxt)))
  5664. ((interactive-p)
  5665. (error "Cannot link to a buffer which is not visiting a file"))
  5666. (t (setq link nil)))
  5667. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  5668. (setq link (or link cpltxt)
  5669. desc (or desc cpltxt))
  5670. (if (equal desc "NONE") (setq desc nil))
  5671. (if (and (interactive-p) link)
  5672. (progn
  5673. (setq org-stored-links
  5674. (cons (list link desc) org-stored-links))
  5675. (message "Stored: %s" (or desc link)))
  5676. (and link (org-make-link-string link desc)))))
  5677. (defun org-store-link-props (&rest plist)
  5678. "Store link properties, extract names and addresses."
  5679. (let (x adr)
  5680. (when (setq x (plist-get plist :from))
  5681. (setq adr (mail-extract-address-components x))
  5682. (plist-put plist :fromname (car adr))
  5683. (plist-put plist :fromaddress (nth 1 adr)))
  5684. (when (setq x (plist-get plist :to))
  5685. (setq adr (mail-extract-address-components x))
  5686. (plist-put plist :toname (car adr))
  5687. (plist-put plist :toaddress (nth 1 adr))))
  5688. (let ((from (plist-get plist :from))
  5689. (to (plist-get plist :to)))
  5690. (when (and from to org-from-is-user-regexp)
  5691. (plist-put plist :fromto
  5692. (if (string-match org-from-is-user-regexp from)
  5693. (concat "to %t")
  5694. (concat "from %f")))))
  5695. (setq org-store-link-plist plist))
  5696. (defun org-add-link-props (&rest plist)
  5697. "Add these properties to the link property list."
  5698. (let (key value)
  5699. (while plist
  5700. (setq key (pop plist) value (pop plist))
  5701. (setq org-store-link-plist
  5702. (plist-put org-store-link-plist key value)))))
  5703. (defun org-email-link-description (&optional fmt)
  5704. "Return the description part of an email link.
  5705. This takes information from `org-store-link-plist' and formats it
  5706. according to FMT (default from `org-email-link-description-format')."
  5707. (setq fmt (or fmt org-email-link-description-format))
  5708. (let* ((p org-store-link-plist)
  5709. (to (plist-get p :toaddress))
  5710. (from (plist-get p :fromaddress))
  5711. (table
  5712. (list
  5713. (cons "%c" (plist-get p :fromto))
  5714. (cons "%F" (plist-get p :from))
  5715. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  5716. (cons "%T" (plist-get p :to))
  5717. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  5718. (cons "%s" (plist-get p :subject))
  5719. (cons "%m" (plist-get p :message-id)))))
  5720. (when (string-match "%c" fmt)
  5721. ;; Check if the user wrote this message
  5722. (if (and org-from-is-user-regexp from to
  5723. (save-match-data (string-match org-from-is-user-regexp from)))
  5724. (setq fmt (replace-match "to %t" t t fmt))
  5725. (setq fmt (replace-match "from %f" t t fmt))))
  5726. (org-replace-escapes fmt table)))
  5727. (defun org-make-org-heading-search-string (&optional string heading)
  5728. "Make search string for STRING or current headline."
  5729. (interactive)
  5730. (let ((s (or string (org-get-heading))))
  5731. (unless (and string (not heading))
  5732. ;; We are using a headline, clean up garbage in there.
  5733. (if (string-match org-todo-regexp s)
  5734. (setq s (replace-match "" t t s)))
  5735. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  5736. (setq s (replace-match "" t t s)))
  5737. (setq s (org-trim s))
  5738. (if (string-match (concat "^\\(" org-quote-string "\\|"
  5739. org-comment-string "\\)") s)
  5740. (setq s (replace-match "" t t s)))
  5741. (while (string-match org-ts-regexp s)
  5742. (setq s (replace-match "" t t s))))
  5743. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  5744. (setq s (replace-match " " t t s)))
  5745. (or string (setq s (concat "*" s))) ; Add * for headlines
  5746. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  5747. (defun org-make-link (&rest strings)
  5748. "Concatenate STRINGS."
  5749. (apply 'concat strings))
  5750. (defun org-make-link-string (link &optional description)
  5751. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  5752. (unless (string-match "\\S-" link)
  5753. (error "Empty link"))
  5754. (when (stringp description)
  5755. ;; Remove brackets from the description, they are fatal.
  5756. (while (string-match "\\[" description)
  5757. (setq description (replace-match "{" t t description)))
  5758. (while (string-match "\\]" description)
  5759. (setq description (replace-match "}" t t description))))
  5760. (when (equal (org-link-escape link) description)
  5761. ;; No description needed, it is identical
  5762. (setq description nil))
  5763. (when (and (not description)
  5764. (not (equal link (org-link-escape link))))
  5765. (setq description link))
  5766. (concat "[[" (org-link-escape link) "]"
  5767. (if description (concat "[" description "]") "")
  5768. "]"))
  5769. (defconst org-link-escape-chars
  5770. '((?\ . "%20")
  5771. (?\[ . "%5B")
  5772. (?\] . "%5D")
  5773. (?\340 . "%E0") ; `a
  5774. (?\342 . "%E2") ; ^a
  5775. (?\347 . "%E7") ; ,c
  5776. (?\350 . "%E8") ; `e
  5777. (?\351 . "%E9") ; 'e
  5778. (?\352 . "%EA") ; ^e
  5779. (?\356 . "%EE") ; ^i
  5780. (?\364 . "%F4") ; ^o
  5781. (?\371 . "%F9") ; `u
  5782. (?\373 . "%FB") ; ^u
  5783. (?\; . "%3B")
  5784. (?? . "%3F")
  5785. (?= . "%3D")
  5786. (?+ . "%2B")
  5787. )
  5788. "Association list of escapes for some characters problematic in links.
  5789. This is the list that is used for internal purposes.")
  5790. (defconst org-link-escape-chars-browser
  5791. '((?\ . "%20")) ; 32 for the SPC char
  5792. "Association list of escapes for some characters problematic in links.
  5793. This is the list that is used before handing over to the browser.")
  5794. (defun org-link-escape (text &optional table)
  5795. "Escape charaters in TEXT that are problematic for links."
  5796. (setq table (or table org-link-escape-chars))
  5797. (when text
  5798. (let ((re (mapconcat (lambda (x) (regexp-quote
  5799. (char-to-string (car x))))
  5800. table "\\|")))
  5801. (while (string-match re text)
  5802. (setq text
  5803. (replace-match
  5804. (cdr (assoc (string-to-char (match-string 0 text))
  5805. table))
  5806. t t text)))
  5807. text)))
  5808. (defun org-link-unescape (text &optional table)
  5809. "Reverse the action of `org-link-escape'."
  5810. (setq table (or table org-link-escape-chars))
  5811. (when text
  5812. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  5813. table "\\|")))
  5814. (while (string-match re text)
  5815. (setq text
  5816. (replace-match
  5817. (char-to-string (car (rassoc (match-string 0 text) table)))
  5818. t t text)))
  5819. text)))
  5820. (defun org-xor (a b)
  5821. "Exclusive or."
  5822. (if a (not b) b))
  5823. (defun org-get-header (header)
  5824. "Find a header field in the current buffer."
  5825. (save-excursion
  5826. (goto-char (point-min))
  5827. (let ((case-fold-search t) s)
  5828. (cond
  5829. ((eq header 'from)
  5830. (if (re-search-forward "^From:\\s-+\\(.*\\)" nil t)
  5831. (setq s (match-string 1)))
  5832. (while (string-match "\"" s)
  5833. (setq s (replace-match "" t t s)))
  5834. (if (string-match "[<(].*" s)
  5835. (setq s (replace-match "" t t s))))
  5836. ((eq header 'message-id)
  5837. (if (re-search-forward "^message-id:\\s-+\\(.*\\)" nil t)
  5838. (setq s (match-string 1))))
  5839. ((eq header 'subject)
  5840. (if (re-search-forward "^subject:\\s-+\\(.*\\)" nil t)
  5841. (setq s (match-string 1)))))
  5842. (if (string-match "\\`[ \t\]+" s) (setq s (replace-match "" t t s)))
  5843. (if (string-match "[ \t\]+\\'" s) (setq s (replace-match "" t t s)))
  5844. s)))
  5845. (defun org-fixup-message-id-for-http (s)
  5846. "Replace special characters in a message id, so it can be used in an http query."
  5847. (while (string-match "<" s)
  5848. (setq s (replace-match "%3C" t t s)))
  5849. (while (string-match ">" s)
  5850. (setq s (replace-match "%3E" t t s)))
  5851. (while (string-match "@" s)
  5852. (setq s (replace-match "%40" t t s)))
  5853. s)
  5854. ;;;###autoload
  5855. (defun org-insert-link-global ()
  5856. "Insert a link like Org-mode does.
  5857. This command can be called in any mode to insert a link in Org-mode syntax."
  5858. (interactive)
  5859. (org-load-modules-maybe)
  5860. (org-run-like-in-org-mode 'org-insert-link))
  5861. (defun org-insert-link (&optional complete-file link-location)
  5862. "Insert a link. At the prompt, enter the link.
  5863. Completion can be used to select a link previously stored with
  5864. `org-store-link'. When the empty string is entered (i.e. if you just
  5865. press RET at the prompt), the link defaults to the most recently
  5866. stored link. As SPC triggers completion in the minibuffer, you need to
  5867. use M-SPC or C-q SPC to force the insertion of a space character.
  5868. You will also be prompted for a description, and if one is given, it will
  5869. be displayed in the buffer instead of the link.
  5870. If there is already a link at point, this command will allow you to edit link
  5871. and description parts.
  5872. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  5873. be selected using completion. The path to the file will be relative to the
  5874. current directory if the file is in the current directory or a subdirectory.
  5875. Otherwise, the link will be the absolute path as completed in the minibuffer
  5876. \(i.e. normally ~/path/to/file).
  5877. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  5878. the current directory or below. With three \\[universal-argument] prefixes, negate the meaning
  5879. of `org-keep-stored-link-after-insertion'.
  5880. If `org-make-link-description-function' is non-nil, this function will be
  5881. called with the link target, and the result will be the default
  5882. link description.
  5883. If the LINK-LOCATION parameter is non-nil, this value will be
  5884. used as the link location instead of reading one interactively."
  5885. (interactive "P")
  5886. (let* ((wcf (current-window-configuration))
  5887. (region (if (org-region-active-p)
  5888. (buffer-substring (region-beginning) (region-end))))
  5889. (remove (and region (list (region-beginning) (region-end))))
  5890. (desc region)
  5891. tmphist ; byte-compile incorrectly complains about this
  5892. (link link-location)
  5893. entry file)
  5894. (cond
  5895. (link-location) ; specified by arg, just use it.
  5896. ((org-in-regexp org-bracket-link-regexp 1)
  5897. ;; We do have a link at point, and we are going to edit it.
  5898. (setq remove (list (match-beginning 0) (match-end 0)))
  5899. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  5900. (setq link (read-string "Link: "
  5901. (org-link-unescape
  5902. (org-match-string-no-properties 1)))))
  5903. ((or (org-in-regexp org-angle-link-re)
  5904. (org-in-regexp org-plain-link-re))
  5905. ;; Convert to bracket link
  5906. (setq remove (list (match-beginning 0) (match-end 0))
  5907. link (read-string "Link: "
  5908. (org-remove-angle-brackets (match-string 0)))))
  5909. ((equal complete-file '(4))
  5910. ;; Completing read for file names.
  5911. (setq file (read-file-name "File: "))
  5912. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  5913. (pwd1 (file-name-as-directory (abbreviate-file-name
  5914. (expand-file-name ".")))))
  5915. (cond
  5916. ((equal complete-file '(16))
  5917. (setq link (org-make-link
  5918. "file:"
  5919. (abbreviate-file-name (expand-file-name file)))))
  5920. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  5921. (setq link (org-make-link "file:" (match-string 1 file))))
  5922. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  5923. (expand-file-name file))
  5924. (setq link (org-make-link
  5925. "file:" (match-string 1 (expand-file-name file)))))
  5926. (t (setq link (org-make-link "file:" file))))))
  5927. (t
  5928. ;; Read link, with completion for stored links.
  5929. (with-output-to-temp-buffer "*Org Links*"
  5930. (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
  5931. (when org-stored-links
  5932. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  5933. (princ (mapconcat
  5934. (lambda (x)
  5935. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  5936. (reverse org-stored-links) "\n"))))
  5937. (let ((cw (selected-window)))
  5938. (select-window (get-buffer-window "*Org Links*"))
  5939. (shrink-window-if-larger-than-buffer)
  5940. (setq truncate-lines t)
  5941. (select-window cw))
  5942. ;; Fake a link history, containing the stored links.
  5943. (setq tmphist (append (mapcar 'car org-stored-links)
  5944. org-insert-link-history))
  5945. (unwind-protect
  5946. (setq link (org-completing-read
  5947. "Link: "
  5948. (append
  5949. (mapcar (lambda (x) (list (concat (car x) ":")))
  5950. (append org-link-abbrev-alist-local org-link-abbrev-alist))
  5951. (mapcar (lambda (x) (list (concat x ":")))
  5952. org-link-types))
  5953. nil nil nil
  5954. 'tmphist
  5955. (or (car (car org-stored-links)))))
  5956. (set-window-configuration wcf)
  5957. (kill-buffer "*Org Links*"))
  5958. (setq entry (assoc link org-stored-links))
  5959. (or entry (push link org-insert-link-history))
  5960. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  5961. (not org-keep-stored-link-after-insertion))
  5962. (setq org-stored-links (delq (assoc link org-stored-links)
  5963. org-stored-links)))
  5964. (setq desc (or desc (nth 1 entry)))))
  5965. (if (string-match org-plain-link-re link)
  5966. ;; URL-like link, normalize the use of angular brackets.
  5967. (setq link (org-make-link (org-remove-angle-brackets link))))
  5968. ;; Check if we are linking to the current file with a search option
  5969. ;; If yes, simplify the link by using only the search option.
  5970. (when (and buffer-file-name
  5971. (string-match "\\<file:\\(.+?\\)::\\([^>]+\\)" link))
  5972. (let* ((path (match-string 1 link))
  5973. (case-fold-search nil)
  5974. (search (match-string 2 link)))
  5975. (save-match-data
  5976. (if (equal (file-truename buffer-file-name) (file-truename path))
  5977. ;; We are linking to this same file, with a search option
  5978. (setq link search)))))
  5979. ;; Check if we can/should use a relative path. If yes, simplify the link
  5980. (when (string-match "\\<file:\\(.*\\)" link)
  5981. (let* ((path (match-string 1 link))
  5982. (origpath path)
  5983. (case-fold-search nil))
  5984. (cond
  5985. ((eq org-link-file-path-type 'absolute)
  5986. (setq path (abbreviate-file-name (expand-file-name path))))
  5987. ((eq org-link-file-path-type 'noabbrev)
  5988. (setq path (expand-file-name path)))
  5989. ((eq org-link-file-path-type 'relative)
  5990. (setq path (file-relative-name path)))
  5991. (t
  5992. (save-match-data
  5993. (if (string-match (concat "^" (regexp-quote
  5994. (file-name-as-directory
  5995. (expand-file-name "."))))
  5996. (expand-file-name path))
  5997. ;; We are linking a file with relative path name.
  5998. (setq path (substring (expand-file-name path)
  5999. (match-end 0)))))))
  6000. (setq link (concat "file:" path))
  6001. (if (equal desc origpath)
  6002. (setq desc path))))
  6003. (if org-make-link-description-function
  6004. (setq desc (funcall org-make-link-description-function link desc)))
  6005. (setq desc (read-string "Description: " desc))
  6006. (unless (string-match "\\S-" desc) (setq desc nil))
  6007. (if remove (apply 'delete-region remove))
  6008. (insert (org-make-link-string link desc))))
  6009. (defun org-completing-read (&rest args)
  6010. (let ((minibuffer-local-completion-map
  6011. (copy-keymap minibuffer-local-completion-map)))
  6012. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  6013. (apply 'completing-read args)))
  6014. ;;; Opening/following a link
  6015. (defvar org-link-search-failed nil)
  6016. (defun org-next-link ()
  6017. "Move forward to the next link.
  6018. If the link is in hidden text, expose it."
  6019. (interactive)
  6020. (when (and org-link-search-failed (eq this-command last-command))
  6021. (goto-char (point-min))
  6022. (message "Link search wrapped back to beginning of buffer"))
  6023. (setq org-link-search-failed nil)
  6024. (let* ((pos (point))
  6025. (ct (org-context))
  6026. (a (assoc :link ct)))
  6027. (if a (goto-char (nth 2 a)))
  6028. (if (re-search-forward org-any-link-re nil t)
  6029. (progn
  6030. (goto-char (match-beginning 0))
  6031. (if (org-invisible-p) (org-show-context)))
  6032. (goto-char pos)
  6033. (setq org-link-search-failed t)
  6034. (error "No further link found"))))
  6035. (defun org-previous-link ()
  6036. "Move backward to the previous link.
  6037. If the link is in hidden text, expose it."
  6038. (interactive)
  6039. (when (and org-link-search-failed (eq this-command last-command))
  6040. (goto-char (point-max))
  6041. (message "Link search wrapped back to end of buffer"))
  6042. (setq org-link-search-failed nil)
  6043. (let* ((pos (point))
  6044. (ct (org-context))
  6045. (a (assoc :link ct)))
  6046. (if a (goto-char (nth 1 a)))
  6047. (if (re-search-backward org-any-link-re nil t)
  6048. (progn
  6049. (goto-char (match-beginning 0))
  6050. (if (org-invisible-p) (org-show-context)))
  6051. (goto-char pos)
  6052. (setq org-link-search-failed t)
  6053. (error "No further link found"))))
  6054. (defun org-find-file-at-mouse (ev)
  6055. "Open file link or URL at mouse."
  6056. (interactive "e")
  6057. (mouse-set-point ev)
  6058. (org-open-at-point 'in-emacs))
  6059. (defun org-open-at-mouse (ev)
  6060. "Open file link or URL at mouse."
  6061. (interactive "e")
  6062. (mouse-set-point ev)
  6063. (org-open-at-point))
  6064. (defvar org-window-config-before-follow-link nil
  6065. "The window configuration before following a link.
  6066. This is saved in case the need arises to restore it.")
  6067. (defvar org-open-link-marker (make-marker)
  6068. "Marker pointing to the location where `org-open-at-point; was called.")
  6069. ;;;###autoload
  6070. (defun org-open-at-point-global ()
  6071. "Follow a link like Org-mode does.
  6072. This command can be called in any mode to follow a link that has
  6073. Org-mode syntax."
  6074. (interactive)
  6075. (org-run-like-in-org-mode 'org-open-at-point))
  6076. ;;;###autoload
  6077. (defun org-open-link-from-string (s &optional arg)
  6078. "Open a link in the string S, as if it was in Org-mode."
  6079. (interactive "sLink: \nP")
  6080. (with-temp-buffer
  6081. (let ((org-inhibit-startup t))
  6082. (org-mode)
  6083. (insert s)
  6084. (goto-char (point-min))
  6085. (org-open-at-point arg))))
  6086. (defun org-open-at-point (&optional in-emacs)
  6087. "Open link at or after point.
  6088. If there is no link at point, this function will search forward up to
  6089. the end of the current subtree.
  6090. Normally, files will be opened by an appropriate application. If the
  6091. optional argument IN-EMACS is non-nil, Emacs will visit the file."
  6092. (interactive "P")
  6093. (org-load-modules-maybe)
  6094. (move-marker org-open-link-marker (point))
  6095. (setq org-window-config-before-follow-link (current-window-configuration))
  6096. (org-remove-occur-highlights nil nil t)
  6097. (if (org-at-timestamp-p t)
  6098. (org-follow-timestamp-link)
  6099. (let (type path link line search (pos (point)))
  6100. (catch 'match
  6101. (save-excursion
  6102. (skip-chars-forward "^]\n\r")
  6103. (when (org-in-regexp org-bracket-link-regexp)
  6104. (setq link (org-link-unescape (org-match-string-no-properties 1)))
  6105. (while (string-match " *\n *" link)
  6106. (setq link (replace-match " " t t link)))
  6107. (setq link (org-link-expand-abbrev link))
  6108. (if (string-match org-link-re-with-space2 link)
  6109. (setq type (match-string 1 link) path (match-string 2 link))
  6110. (setq type "thisfile" path link))
  6111. (throw 'match t)))
  6112. (when (get-text-property (point) 'org-linked-text)
  6113. (setq type "thisfile"
  6114. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  6115. (1+ (point)) (point))
  6116. path (buffer-substring
  6117. (previous-single-property-change pos 'org-linked-text)
  6118. (next-single-property-change pos 'org-linked-text)))
  6119. (throw 'match t))
  6120. (save-excursion
  6121. (when (or (org-in-regexp org-angle-link-re)
  6122. (org-in-regexp org-plain-link-re))
  6123. (setq type (match-string 1) path (match-string 2))
  6124. (throw 'match t)))
  6125. (when (org-in-regexp "\\<\\([^><\n]+\\)\\>")
  6126. (setq type "tree-match"
  6127. path (match-string 1))
  6128. (throw 'match t))
  6129. (save-excursion
  6130. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  6131. (setq type "tags"
  6132. path (match-string 1))
  6133. (while (string-match ":" path)
  6134. (setq path (replace-match "+" t t path)))
  6135. (throw 'match t))))
  6136. (unless path
  6137. (error "No link found"))
  6138. ;; Remove any trailing spaces in path
  6139. (if (string-match " +\\'" path)
  6140. (setq path (replace-match "" t t path)))
  6141. (cond
  6142. ((assoc type org-link-protocols)
  6143. (funcall (nth 1 (assoc type org-link-protocols)) path))
  6144. ((equal type "mailto")
  6145. (let ((cmd (car org-link-mailto-program))
  6146. (args (cdr org-link-mailto-program)) args1
  6147. (address path) (subject "") a)
  6148. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  6149. (setq address (match-string 1 path)
  6150. subject (org-link-escape (match-string 2 path))))
  6151. (while args
  6152. (cond
  6153. ((not (stringp (car args))) (push (pop args) args1))
  6154. (t (setq a (pop args))
  6155. (if (string-match "%a" a)
  6156. (setq a (replace-match address t t a)))
  6157. (if (string-match "%s" a)
  6158. (setq a (replace-match subject t t a)))
  6159. (push a args1))))
  6160. (apply cmd (nreverse args1))))
  6161. ((member type '("http" "https" "ftp" "news"))
  6162. (browse-url (concat type ":" (org-link-escape
  6163. path org-link-escape-chars-browser))))
  6164. ((member type '("message"))
  6165. (browse-url (concat type ":" path)))
  6166. ((string= type "tags")
  6167. (org-tags-view in-emacs path))
  6168. ((string= type "thisfile")
  6169. (if in-emacs
  6170. (switch-to-buffer-other-window
  6171. (org-get-buffer-for-internal-link (current-buffer)))
  6172. (org-mark-ring-push))
  6173. (let ((cmd `(org-link-search
  6174. ,path
  6175. ,(cond ((equal in-emacs '(4)) 'occur)
  6176. ((equal in-emacs '(16)) 'org-occur)
  6177. (t nil))
  6178. ,pos)))
  6179. (condition-case nil (eval cmd)
  6180. (error (progn (widen) (eval cmd))))))
  6181. ((string= type "tree-match")
  6182. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  6183. ((string= type "file")
  6184. (if (string-match "::\\([0-9]+\\)\\'" path)
  6185. (setq line (string-to-number (match-string 1 path))
  6186. path (substring path 0 (match-beginning 0)))
  6187. (if (string-match "::\\(.+\\)\\'" path)
  6188. (setq search (match-string 1 path)
  6189. path (substring path 0 (match-beginning 0)))))
  6190. (if (string-match "[*?{]" (file-name-nondirectory path))
  6191. (dired path)
  6192. (org-open-file path in-emacs line search)))
  6193. ((string= type "news")
  6194. (require 'org-gnus)
  6195. (org-gnus-follow-link path))
  6196. ((string= type "shell")
  6197. (let ((cmd path))
  6198. (if (or (not org-confirm-shell-link-function)
  6199. (funcall org-confirm-shell-link-function
  6200. (format "Execute \"%s\" in shell? "
  6201. (org-add-props cmd nil
  6202. 'face 'org-warning))))
  6203. (progn
  6204. (message "Executing %s" cmd)
  6205. (shell-command cmd))
  6206. (error "Abort"))))
  6207. ((string= type "elisp")
  6208. (let ((cmd path))
  6209. (if (or (not org-confirm-elisp-link-function)
  6210. (funcall org-confirm-elisp-link-function
  6211. (format "Execute \"%s\" as elisp? "
  6212. (org-add-props cmd nil
  6213. 'face 'org-warning))))
  6214. (message "%s => %s" cmd (eval (read cmd)))
  6215. (error "Abort"))))
  6216. (t
  6217. (browse-url-at-point)))))
  6218. (move-marker org-open-link-marker nil)
  6219. (run-hook-with-args 'org-follow-link-hook))
  6220. ;;;; Time estimates
  6221. (defun org-get-effort (&optional pom)
  6222. "Get the effort estimate for the current entry."
  6223. (org-entry-get pom org-effort-property))
  6224. ;;; File search
  6225. (defvar org-create-file-search-functions nil
  6226. "List of functions to construct the right search string for a file link.
  6227. These functions are called in turn with point at the location to
  6228. which the link should point.
  6229. A function in the hook should first test if it would like to
  6230. handle this file type, for example by checking the major-mode or
  6231. the file extension. If it decides not to handle this file, it
  6232. should just return nil to give other functions a chance. If it
  6233. does handle the file, it must return the search string to be used
  6234. when following the link. The search string will be part of the
  6235. file link, given after a double colon, and `org-open-at-point'
  6236. will automatically search for it. If special measures must be
  6237. taken to make the search successful, another function should be
  6238. added to the companion hook `org-execute-file-search-functions',
  6239. which see.
  6240. A function in this hook may also use `setq' to set the variable
  6241. `description' to provide a suggestion for the descriptive text to
  6242. be used for this link when it gets inserted into an Org-mode
  6243. buffer with \\[org-insert-link].")
  6244. (defvar org-execute-file-search-functions nil
  6245. "List of functions to execute a file search triggered by a link.
  6246. Functions added to this hook must accept a single argument, the
  6247. search string that was part of the file link, the part after the
  6248. double colon. The function must first check if it would like to
  6249. handle this search, for example by checking the major-mode or the
  6250. file extension. If it decides not to handle this search, it
  6251. should just return nil to give other functions a chance. If it
  6252. does handle the search, it must return a non-nil value to keep
  6253. other functions from trying.
  6254. Each function can access the current prefix argument through the
  6255. variable `current-prefix-argument'. Note that a single prefix is
  6256. used to force opening a link in Emacs, so it may be good to only
  6257. use a numeric or double prefix to guide the search function.
  6258. In case this is needed, a function in this hook can also restore
  6259. the window configuration before `org-open-at-point' was called using:
  6260. (set-window-configuration org-window-config-before-follow-link)")
  6261. (defun org-link-search (s &optional type avoid-pos)
  6262. "Search for a link search option.
  6263. If S is surrounded by forward slashes, it is interpreted as a
  6264. regular expression. In org-mode files, this will create an `org-occur'
  6265. sparse tree. In ordinary files, `occur' will be used to list matches.
  6266. If the current buffer is in `dired-mode', grep will be used to search
  6267. in all files. If AVOID-POS is given, ignore matches near that position."
  6268. (let ((case-fold-search t)
  6269. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  6270. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  6271. (append '(("") (" ") ("\t") ("\n"))
  6272. org-emphasis-alist)
  6273. "\\|") "\\)"))
  6274. (pos (point))
  6275. (pre nil) (post nil)
  6276. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  6277. (cond
  6278. ;; First check if there are any special
  6279. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  6280. ;; Now try the builtin stuff
  6281. ((save-excursion
  6282. (goto-char (point-min))
  6283. (and
  6284. (re-search-forward
  6285. (concat "<<" (regexp-quote s0) ">>") nil t)
  6286. (setq type 'dedicated
  6287. pos (match-beginning 0))))
  6288. ;; There is an exact target for this
  6289. (goto-char pos))
  6290. ((string-match "^/\\(.*\\)/$" s)
  6291. ;; A regular expression
  6292. (cond
  6293. ((org-mode-p)
  6294. (org-occur (match-string 1 s)))
  6295. ;;((eq major-mode 'dired-mode)
  6296. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  6297. (t (org-do-occur (match-string 1 s)))))
  6298. (t
  6299. ;; A normal search strings
  6300. (when (equal (string-to-char s) ?*)
  6301. ;; Anchor on headlines, post may include tags.
  6302. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  6303. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  6304. s (substring s 1)))
  6305. (remove-text-properties
  6306. 0 (length s)
  6307. '(face nil mouse-face nil keymap nil fontified nil) s)
  6308. ;; Make a series of regular expressions to find a match
  6309. (setq words (org-split-string s "[ \n\r\t]+")
  6310. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  6311. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  6312. "\\)" markers)
  6313. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  6314. re2a (concat "[ \t\r\n]" re2a_)
  6315. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  6316. re4 (concat "[^a-zA-Z_]" re4_)
  6317. re1 (concat pre re2 post)
  6318. re3 (concat pre (if pre re4_ re4) post)
  6319. re5 (concat pre ".*" re4)
  6320. re2 (concat pre re2)
  6321. re2a (concat pre (if pre re2a_ re2a))
  6322. re4 (concat pre (if pre re4_ re4))
  6323. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  6324. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  6325. re5 "\\)"
  6326. ))
  6327. (cond
  6328. ((eq type 'org-occur) (org-occur reall))
  6329. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  6330. (t (goto-char (point-min))
  6331. (setq type 'fuzzy)
  6332. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  6333. (org-search-not-self 1 re1 nil t)
  6334. (org-search-not-self 1 re2 nil t)
  6335. (org-search-not-self 1 re2a nil t)
  6336. (org-search-not-self 1 re3 nil t)
  6337. (org-search-not-self 1 re4 nil t)
  6338. (org-search-not-self 1 re5 nil t)
  6339. )
  6340. (goto-char (match-beginning 1))
  6341. (goto-char pos)
  6342. (error "No match")))))
  6343. (t
  6344. ;; Normal string-search
  6345. (goto-char (point-min))
  6346. (if (search-forward s nil t)
  6347. (goto-char (match-beginning 0))
  6348. (error "No match"))))
  6349. (and (org-mode-p) (org-show-context 'link-search))
  6350. type))
  6351. (defun org-search-not-self (group &rest args)
  6352. "Execute `re-search-forward', but only accept matches that do not
  6353. enclose the position of `org-open-link-marker'."
  6354. (let ((m org-open-link-marker))
  6355. (catch 'exit
  6356. (while (apply 're-search-forward args)
  6357. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  6358. (goto-char (match-end group))
  6359. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  6360. (> (match-beginning 0) (marker-position m))
  6361. (< (match-end 0) (marker-position m)))
  6362. (save-match-data
  6363. (or (not (org-in-regexp
  6364. org-bracket-link-analytic-regexp 1))
  6365. (not (match-end 4)) ; no description
  6366. (and (<= (match-beginning 4) (point))
  6367. (>= (match-end 4) (point))))))
  6368. (throw 'exit (point))))))))
  6369. (defun org-get-buffer-for-internal-link (buffer)
  6370. "Return a buffer to be used for displaying the link target of internal links."
  6371. (cond
  6372. ((not org-display-internal-link-with-indirect-buffer)
  6373. buffer)
  6374. ((string-match "(Clone)$" (buffer-name buffer))
  6375. (message "Buffer is already a clone, not making another one")
  6376. ;; we also do not modify visibility in this case
  6377. buffer)
  6378. (t ; make a new indirect buffer for displaying the link
  6379. (let* ((bn (buffer-name buffer))
  6380. (ibn (concat bn "(Clone)"))
  6381. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  6382. (with-current-buffer ib (org-overview))
  6383. ib))))
  6384. (defun org-do-occur (regexp &optional cleanup)
  6385. "Call the Emacs command `occur'.
  6386. If CLEANUP is non-nil, remove the printout of the regular expression
  6387. in the *Occur* buffer. This is useful if the regex is long and not useful
  6388. to read."
  6389. (occur regexp)
  6390. (when cleanup
  6391. (let ((cwin (selected-window)) win beg end)
  6392. (when (setq win (get-buffer-window "*Occur*"))
  6393. (select-window win))
  6394. (goto-char (point-min))
  6395. (when (re-search-forward "match[a-z]+" nil t)
  6396. (setq beg (match-end 0))
  6397. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  6398. (setq end (1- (match-beginning 0)))))
  6399. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  6400. (goto-char (point-min))
  6401. (select-window cwin))))
  6402. ;;; The mark ring for links jumps
  6403. (defvar org-mark-ring nil
  6404. "Mark ring for positions before jumps in Org-mode.")
  6405. (defvar org-mark-ring-last-goto nil
  6406. "Last position in the mark ring used to go back.")
  6407. ;; Fill and close the ring
  6408. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  6409. (loop for i from 1 to org-mark-ring-length do
  6410. (push (make-marker) org-mark-ring))
  6411. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  6412. org-mark-ring)
  6413. (defun org-mark-ring-push (&optional pos buffer)
  6414. "Put the current position or POS into the mark ring and rotate it."
  6415. (interactive)
  6416. (setq pos (or pos (point)))
  6417. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  6418. (move-marker (car org-mark-ring)
  6419. (or pos (point))
  6420. (or buffer (current-buffer)))
  6421. (message "%s"
  6422. (substitute-command-keys
  6423. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  6424. (defun org-mark-ring-goto (&optional n)
  6425. "Jump to the previous position in the mark ring.
  6426. With prefix arg N, jump back that many stored positions. When
  6427. called several times in succession, walk through the entire ring.
  6428. Org-mode commands jumping to a different position in the current file,
  6429. or to another Org-mode file, automatically push the old position
  6430. onto the ring."
  6431. (interactive "p")
  6432. (let (p m)
  6433. (if (eq last-command this-command)
  6434. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  6435. (setq p org-mark-ring))
  6436. (setq org-mark-ring-last-goto p)
  6437. (setq m (car p))
  6438. (switch-to-buffer (marker-buffer m))
  6439. (goto-char m)
  6440. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  6441. (defun org-remove-angle-brackets (s)
  6442. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  6443. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  6444. s)
  6445. (defun org-add-angle-brackets (s)
  6446. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  6447. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  6448. s)
  6449. ;;; Following specific links
  6450. (defun org-follow-timestamp-link ()
  6451. (cond
  6452. ((org-at-date-range-p t)
  6453. (let ((org-agenda-start-on-weekday)
  6454. (t1 (match-string 1))
  6455. (t2 (match-string 2)))
  6456. (setq t1 (time-to-days (org-time-string-to-time t1))
  6457. t2 (time-to-days (org-time-string-to-time t2)))
  6458. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  6459. ((org-at-timestamp-p t)
  6460. (org-agenda-list nil (time-to-days (org-time-string-to-time
  6461. (substring (match-string 1) 0 10)))
  6462. 1))
  6463. (t (error "This should not happen"))))
  6464. ;;; Following file links
  6465. (defvar org-wait nil)
  6466. (defun org-open-file (path &optional in-emacs line search)
  6467. "Open the file at PATH.
  6468. First, this expands any special file name abbreviations. Then the
  6469. configuration variable `org-file-apps' is checked if it contains an
  6470. entry for this file type, and if yes, the corresponding command is launched.
  6471. If no application is found, Emacs simply visits the file.
  6472. With optional argument IN-EMACS, Emacs will visit the file.
  6473. Optional LINE specifies a line to go to, optional SEARCH a string to
  6474. search for. If LINE or SEARCH is given, the file will always be
  6475. opened in Emacs.
  6476. If the file does not exist, an error is thrown."
  6477. (setq in-emacs (or in-emacs line search))
  6478. (let* ((file (if (equal path "")
  6479. buffer-file-name
  6480. (substitute-in-file-name (expand-file-name path))))
  6481. (apps (append org-file-apps (org-default-apps)))
  6482. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  6483. (dirp (if remp nil (file-directory-p file)))
  6484. (dfile (downcase file))
  6485. (old-buffer (current-buffer))
  6486. (old-pos (point))
  6487. (old-mode major-mode)
  6488. ext cmd)
  6489. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  6490. (setq ext (match-string 1 dfile))
  6491. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  6492. (setq ext (match-string 1 dfile))))
  6493. (if in-emacs
  6494. (setq cmd 'emacs)
  6495. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  6496. (and dirp (cdr (assoc 'directory apps)))
  6497. (cdr (assoc ext apps))
  6498. (cdr (assoc t apps)))))
  6499. (when (eq cmd 'mailcap)
  6500. (require 'mailcap)
  6501. (mailcap-parse-mailcaps)
  6502. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  6503. (command (mailcap-mime-info mime-type)))
  6504. (if (stringp command)
  6505. (setq cmd command)
  6506. (setq cmd 'emacs))))
  6507. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  6508. (not (file-exists-p file))
  6509. (not org-open-non-existing-files))
  6510. (error "No such file: %s" file))
  6511. (cond
  6512. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  6513. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  6514. (while (string-match "['\"]%s['\"]" cmd)
  6515. (setq cmd (replace-match "%s" t t cmd)))
  6516. (while (string-match "%s" cmd)
  6517. (setq cmd (replace-match
  6518. (save-match-data (shell-quote-argument file))
  6519. t t cmd)))
  6520. (save-window-excursion
  6521. (start-process-shell-command cmd nil cmd)
  6522. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  6523. ))
  6524. ((or (stringp cmd)
  6525. (eq cmd 'emacs))
  6526. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  6527. (widen)
  6528. (if line (goto-line line)
  6529. (if search (org-link-search search))))
  6530. ((consp cmd)
  6531. (eval cmd))
  6532. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  6533. (and (org-mode-p) (eq old-mode 'org-mode)
  6534. (or (not (equal old-buffer (current-buffer)))
  6535. (not (equal old-pos (point))))
  6536. (org-mark-ring-push old-pos old-buffer))))
  6537. (defun org-default-apps ()
  6538. "Return the default applications for this operating system."
  6539. (cond
  6540. ((eq system-type 'darwin)
  6541. org-file-apps-defaults-macosx)
  6542. ((eq system-type 'windows-nt)
  6543. org-file-apps-defaults-windowsnt)
  6544. (t org-file-apps-defaults-gnu)))
  6545. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  6546. (defun org-file-remote-p (file)
  6547. "Test whether FILE specifies a location on a remote system.
  6548. Return non-nil if the location is indeed remote.
  6549. For example, the filename \"/user@host:/foo\" specifies a location
  6550. on the system \"/user@host:\"."
  6551. (cond ((fboundp 'file-remote-p)
  6552. (file-remote-p file))
  6553. ((fboundp 'tramp-handle-file-remote-p)
  6554. (tramp-handle-file-remote-p file))
  6555. ((and (boundp 'ange-ftp-name-format)
  6556. (string-match (car ange-ftp-name-format) file))
  6557. t)
  6558. (t nil)))
  6559. ;;;; Refiling
  6560. (defun org-get-org-file ()
  6561. "Read a filename, with default directory `org-directory'."
  6562. (let ((default (or org-default-notes-file remember-data-file)))
  6563. (read-file-name (format "File name [%s]: " default)
  6564. (file-name-as-directory org-directory)
  6565. default)))
  6566. (defun org-notes-order-reversed-p ()
  6567. "Check if the current file should receive notes in reversed order."
  6568. (cond
  6569. ((not org-reverse-note-order) nil)
  6570. ((eq t org-reverse-note-order) t)
  6571. ((not (listp org-reverse-note-order)) nil)
  6572. (t (catch 'exit
  6573. (let ((all org-reverse-note-order)
  6574. entry)
  6575. (while (setq entry (pop all))
  6576. (if (string-match (car entry) buffer-file-name)
  6577. (throw 'exit (cdr entry))))
  6578. nil)))))
  6579. (defvar org-refile-target-table nil
  6580. "The list of refile targets, created by `org-refile'.")
  6581. (defvar org-agenda-new-buffers nil
  6582. "Buffers created to visit agenda files.")
  6583. (defun org-get-refile-targets (&optional default-buffer)
  6584. "Produce a table with refile targets."
  6585. (let ((entries (or org-refile-targets '((nil . (:level . 1)))))
  6586. targets txt re files f desc descre)
  6587. (with-current-buffer (or default-buffer (current-buffer))
  6588. (while (setq entry (pop entries))
  6589. (setq files (car entry) desc (cdr entry))
  6590. (cond
  6591. ((null files) (setq files (list (current-buffer))))
  6592. ((eq files 'org-agenda-files)
  6593. (setq files (org-agenda-files 'unrestricted)))
  6594. ((and (symbolp files) (fboundp files))
  6595. (setq files (funcall files)))
  6596. ((and (symbolp files) (boundp files))
  6597. (setq files (symbol-value files))))
  6598. (if (stringp files) (setq files (list files)))
  6599. (cond
  6600. ((eq (car desc) :tag)
  6601. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  6602. ((eq (car desc) :todo)
  6603. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  6604. ((eq (car desc) :regexp)
  6605. (setq descre (cdr desc)))
  6606. ((eq (car desc) :level)
  6607. (setq descre (concat "^\\*\\{" (number-to-string
  6608. (if org-odd-levels-only
  6609. (1- (* 2 (cdr desc)))
  6610. (cdr desc)))
  6611. "\\}[ \t]")))
  6612. ((eq (car desc) :maxlevel)
  6613. (setq descre (concat "^\\*\\{1," (number-to-string
  6614. (if org-odd-levels-only
  6615. (1- (* 2 (cdr desc)))
  6616. (cdr desc)))
  6617. "\\}[ \t]")))
  6618. (t (error "Bad refiling target description %s" desc)))
  6619. (while (setq f (pop files))
  6620. (save-excursion
  6621. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  6622. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  6623. (save-excursion
  6624. (save-restriction
  6625. (widen)
  6626. (goto-char (point-min))
  6627. (while (re-search-forward descre nil t)
  6628. (goto-char (point-at-bol))
  6629. (when (looking-at org-complex-heading-regexp)
  6630. (setq txt (match-string 4)
  6631. re (concat "^" (regexp-quote
  6632. (buffer-substring (match-beginning 1)
  6633. (match-end 4)))))
  6634. (if (match-end 5) (setq re (concat re "[ \t]+"
  6635. (regexp-quote
  6636. (match-string 5)))))
  6637. (setq re (concat re "[ \t]*$"))
  6638. (when org-refile-use-outline-path
  6639. (setq txt (mapconcat 'identity
  6640. (append
  6641. (if (eq org-refile-use-outline-path 'file)
  6642. (list (file-name-nondirectory
  6643. (buffer-file-name (buffer-base-buffer))))
  6644. (if (eq org-refile-use-outline-path 'full-file-path)
  6645. (list (buffer-file-name (buffer-base-buffer)))))
  6646. (org-get-outline-path)
  6647. (list txt))
  6648. "/")))
  6649. (push (list txt f re (point)) targets))
  6650. (goto-char (point-at-eol))))))))
  6651. (nreverse targets))))
  6652. (defun org-get-outline-path ()
  6653. "Return the outline path to the current entry, as a list."
  6654. (let (rtn)
  6655. (save-excursion
  6656. (while (org-up-heading-safe)
  6657. (when (looking-at org-complex-heading-regexp)
  6658. (push (org-match-string-no-properties 4) rtn)))
  6659. rtn)))
  6660. (defvar org-refile-history nil
  6661. "History for refiling operations.")
  6662. (defun org-refile (&optional goto default-buffer)
  6663. "Move the entry at point to another heading.
  6664. The list of target headings is compiled using the information in
  6665. `org-refile-targets', which see. This list is created before each use
  6666. and will therefore always be up-to-date.
  6667. At the target location, the entry is filed as a subitem of the target heading.
  6668. Depending on `org-reverse-note-order', the new subitem will either be the
  6669. first of the last subitem.
  6670. With prefix arg GOTO, the command will only visit the target location,
  6671. not actually move anything.
  6672. With a double prefix `C-c C-c', go to the location where the last refiling
  6673. operation has put the subtree."
  6674. (interactive "P")
  6675. (let* ((cbuf (current-buffer))
  6676. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  6677. pos it nbuf file re level reversed)
  6678. (if (equal goto '(16))
  6679. (org-refile-goto-last-stored)
  6680. (when (setq it (org-refile-get-location
  6681. (if goto "Goto: " "Refile to: ") default-buffer))
  6682. (setq file (nth 1 it)
  6683. re (nth 2 it)
  6684. pos (nth 3 it))
  6685. (setq nbuf (or (find-buffer-visiting file)
  6686. (find-file-noselect file)))
  6687. (if goto
  6688. (progn
  6689. (switch-to-buffer nbuf)
  6690. (goto-char pos)
  6691. (org-show-context 'org-goto))
  6692. (org-copy-subtree 1 nil t)
  6693. (save-excursion
  6694. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  6695. (find-file-noselect file))))
  6696. (setq reversed (org-notes-order-reversed-p))
  6697. (save-excursion
  6698. (save-restriction
  6699. (widen)
  6700. (goto-char pos)
  6701. (looking-at outline-regexp)
  6702. (setq level (org-get-valid-level (funcall outline-level) 1))
  6703. (goto-char
  6704. (if reversed
  6705. (outline-next-heading)
  6706. (or (save-excursion (outline-get-next-sibling))
  6707. (org-end-of-subtree t t)
  6708. (point-max))))
  6709. (bookmark-set "org-refile-last-stored")
  6710. (org-paste-subtree level))))
  6711. (org-cut-subtree)
  6712. (setq org-markers-to-move nil)
  6713. (message "Entry refiled to \"%s\"" (car it)))))))
  6714. (defun org-refile-goto-last-stored ()
  6715. "Go to the location where the last refile was stored."
  6716. (interactive)
  6717. (bookmark-jump "org-refile-last-stored")
  6718. (message "This is the location of the last refile"))
  6719. (defun org-refile-get-location (&optional prompt default-buffer)
  6720. "Prompt the user for a refile location, using PROMPT."
  6721. (let ((org-refile-targets org-refile-targets)
  6722. (org-refile-use-outline-path org-refile-use-outline-path))
  6723. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  6724. (unless org-refile-target-table
  6725. (error "No refile targets"))
  6726. (let* ((cbuf (current-buffer))
  6727. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  6728. (fname (and filename (file-truename filename)))
  6729. (tbl (mapcar
  6730. (lambda (x)
  6731. (if (not (equal fname (file-truename (nth 1 x))))
  6732. (cons (concat (car x) " (" (file-name-nondirectory
  6733. (nth 1 x)) ")")
  6734. (cdr x))
  6735. x))
  6736. org-refile-target-table))
  6737. (completion-ignore-case t))
  6738. (assoc (completing-read prompt tbl nil t nil 'org-refile-history)
  6739. tbl)))
  6740. ;;;; Dynamic blocks
  6741. (defun org-find-dblock (name)
  6742. "Find the first dynamic block with name NAME in the buffer.
  6743. If not found, stay at current position and return nil."
  6744. (let (pos)
  6745. (save-excursion
  6746. (goto-char (point-min))
  6747. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  6748. nil t)
  6749. (match-beginning 0))))
  6750. (if pos (goto-char pos))
  6751. pos))
  6752. (defconst org-dblock-start-re
  6753. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  6754. "Matches the startline of a dynamic block, with parameters.")
  6755. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  6756. "Matches the end of a dyhamic block.")
  6757. (defun org-create-dblock (plist)
  6758. "Create a dynamic block section, with parameters taken from PLIST.
  6759. PLIST must containe a :name entry which is used as name of the block."
  6760. (unless (bolp) (newline))
  6761. (let ((name (plist-get plist :name)))
  6762. (insert "#+BEGIN: " name)
  6763. (while plist
  6764. (if (eq (car plist) :name)
  6765. (setq plist (cddr plist))
  6766. (insert " " (prin1-to-string (pop plist)))))
  6767. (insert "\n\n#+END:\n")
  6768. (beginning-of-line -2)))
  6769. (defun org-prepare-dblock ()
  6770. "Prepare dynamic block for refresh.
  6771. This empties the block, puts the cursor at the insert position and returns
  6772. the property list including an extra property :name with the block name."
  6773. (unless (looking-at org-dblock-start-re)
  6774. (error "Not at a dynamic block"))
  6775. (let* ((begdel (1+ (match-end 0)))
  6776. (name (org-no-properties (match-string 1)))
  6777. (params (append (list :name name)
  6778. (read (concat "(" (match-string 3) ")")))))
  6779. (unless (re-search-forward org-dblock-end-re nil t)
  6780. (error "Dynamic block not terminated"))
  6781. (setq params
  6782. (append params
  6783. (list :content (buffer-substring
  6784. begdel (match-beginning 0)))))
  6785. (delete-region begdel (match-beginning 0))
  6786. (goto-char begdel)
  6787. (open-line 1)
  6788. params))
  6789. (defun org-map-dblocks (&optional command)
  6790. "Apply COMMAND to all dynamic blocks in the current buffer.
  6791. If COMMAND is not given, use `org-update-dblock'."
  6792. (let ((cmd (or command 'org-update-dblock))
  6793. pos)
  6794. (save-excursion
  6795. (goto-char (point-min))
  6796. (while (re-search-forward org-dblock-start-re nil t)
  6797. (goto-char (setq pos (match-beginning 0)))
  6798. (condition-case nil
  6799. (funcall cmd)
  6800. (error (message "Error during update of dynamic block")))
  6801. (goto-char pos)
  6802. (unless (re-search-forward org-dblock-end-re nil t)
  6803. (error "Dynamic block not terminated"))))))
  6804. (defun org-dblock-update (&optional arg)
  6805. "User command for updating dynamic blocks.
  6806. Update the dynamic block at point. With prefix ARG, update all dynamic
  6807. blocks in the buffer."
  6808. (interactive "P")
  6809. (if arg
  6810. (org-update-all-dblocks)
  6811. (or (looking-at org-dblock-start-re)
  6812. (org-beginning-of-dblock))
  6813. (org-update-dblock)))
  6814. (defun org-update-dblock ()
  6815. "Update the dynamic block at point
  6816. This means to empty the block, parse for parameters and then call
  6817. the correct writing function."
  6818. (save-window-excursion
  6819. (let* ((pos (point))
  6820. (line (org-current-line))
  6821. (params (org-prepare-dblock))
  6822. (name (plist-get params :name))
  6823. (cmd (intern (concat "org-dblock-write:" name))))
  6824. (message "Updating dynamic block `%s' at line %d..." name line)
  6825. (funcall cmd params)
  6826. (message "Updating dynamic block `%s' at line %d...done" name line)
  6827. (goto-char pos))))
  6828. (defun org-beginning-of-dblock ()
  6829. "Find the beginning of the dynamic block at point.
  6830. Error if there is no scuh block at point."
  6831. (let ((pos (point))
  6832. beg)
  6833. (end-of-line 1)
  6834. (if (and (re-search-backward org-dblock-start-re nil t)
  6835. (setq beg (match-beginning 0))
  6836. (re-search-forward org-dblock-end-re nil t)
  6837. (> (match-end 0) pos))
  6838. (goto-char beg)
  6839. (goto-char pos)
  6840. (error "Not in a dynamic block"))))
  6841. (defun org-update-all-dblocks ()
  6842. "Update all dynamic blocks in the buffer.
  6843. This function can be used in a hook."
  6844. (when (org-mode-p)
  6845. (org-map-dblocks 'org-update-dblock)))
  6846. ;;;; Completion
  6847. (defconst org-additional-option-like-keywords
  6848. '("BEGIN_HTML" "BEGIN_LaTeX" "END_HTML" "END_LaTeX"
  6849. "ORGTBL" "HTML:" "LaTeX:" "BEGIN:" "END:" "TBLFM"
  6850. "BEGIN_EXAMPLE" "END_EXAMPLE"))
  6851. (defun org-complete (&optional arg)
  6852. "Perform completion on word at point.
  6853. At the beginning of a headline, this completes TODO keywords as given in
  6854. `org-todo-keywords'.
  6855. If the current word is preceded by a backslash, completes the TeX symbols
  6856. that are supported for HTML support.
  6857. If the current word is preceded by \"#+\", completes special words for
  6858. setting file options.
  6859. In the line after \"#+STARTUP:, complete valid keywords.\"
  6860. At all other locations, this simply calls the value of
  6861. `org-completion-fallback-command'."
  6862. (interactive "P")
  6863. (org-without-partial-completion
  6864. (catch 'exit
  6865. (let* ((end (point))
  6866. (beg1 (save-excursion
  6867. (skip-chars-backward (org-re "[:alnum:]_@"))
  6868. (point)))
  6869. (beg (save-excursion
  6870. (skip-chars-backward "a-zA-Z0-9_:$")
  6871. (point)))
  6872. (confirm (lambda (x) (stringp (car x))))
  6873. (searchhead (equal (char-before beg) ?*))
  6874. (tag (and (equal (char-before beg1) ?:)
  6875. (equal (char-after (point-at-bol)) ?*)))
  6876. (prop (and (equal (char-before beg1) ?:)
  6877. (not (equal (char-after (point-at-bol)) ?*))))
  6878. (texp (equal (char-before beg) ?\\))
  6879. (link (equal (char-before beg) ?\[))
  6880. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  6881. beg)
  6882. "#+"))
  6883. (startup (string-match "^#\\+STARTUP:.*"
  6884. (buffer-substring (point-at-bol) (point))))
  6885. (completion-ignore-case opt)
  6886. (type nil)
  6887. (tbl nil)
  6888. (table (cond
  6889. (opt
  6890. (setq type :opt)
  6891. (require 'org-exp)
  6892. (append
  6893. (mapcar
  6894. (lambda (x)
  6895. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  6896. (cons (match-string 2 x) (match-string 1 x)))
  6897. (org-split-string (org-get-current-options) "\n"))
  6898. (mapcar 'list org-additional-option-like-keywords)))
  6899. (startup
  6900. (setq type :startup)
  6901. org-startup-options)
  6902. (link (append org-link-abbrev-alist-local
  6903. org-link-abbrev-alist))
  6904. (texp
  6905. (setq type :tex)
  6906. org-html-entities)
  6907. ((string-match "\\`\\*+[ \t]+\\'"
  6908. (buffer-substring (point-at-bol) beg))
  6909. (setq type :todo)
  6910. (mapcar 'list org-todo-keywords-1))
  6911. (searchhead
  6912. (setq type :searchhead)
  6913. (save-excursion
  6914. (goto-char (point-min))
  6915. (while (re-search-forward org-todo-line-regexp nil t)
  6916. (push (list
  6917. (org-make-org-heading-search-string
  6918. (match-string 3) t))
  6919. tbl)))
  6920. tbl)
  6921. (tag (setq type :tag beg beg1)
  6922. (or org-tag-alist (org-get-buffer-tags)))
  6923. (prop (setq type :prop beg beg1)
  6924. (mapcar 'list (org-buffer-property-keys nil t t)))
  6925. (t (progn
  6926. (call-interactively org-completion-fallback-command)
  6927. (throw 'exit nil)))))
  6928. (pattern (buffer-substring-no-properties beg end))
  6929. (completion (try-completion pattern table confirm)))
  6930. (cond ((eq completion t)
  6931. (if (not (assoc (upcase pattern) table))
  6932. (message "Already complete")
  6933. (if (and (equal type :opt)
  6934. (not (member (car (assoc (upcase pattern) table))
  6935. org-additional-option-like-keywords)))
  6936. (insert (substring (cdr (assoc (upcase pattern) table))
  6937. (length pattern)))
  6938. (if (memq type '(:tag :prop)) (insert ":")))))
  6939. ((null completion)
  6940. (message "Can't find completion for \"%s\"" pattern)
  6941. (ding))
  6942. ((not (string= pattern completion))
  6943. (delete-region beg end)
  6944. (if (string-match " +$" completion)
  6945. (setq completion (replace-match "" t t completion)))
  6946. (insert completion)
  6947. (if (get-buffer-window "*Completions*")
  6948. (delete-window (get-buffer-window "*Completions*")))
  6949. (if (assoc completion table)
  6950. (if (eq type :todo) (insert " ")
  6951. (if (memq type '(:tag :prop)) (insert ":"))))
  6952. (if (and (equal type :opt) (assoc completion table))
  6953. (message "%s" (substitute-command-keys
  6954. "Press \\[org-complete] again to insert example settings"))))
  6955. (t
  6956. (message "Making completion list...")
  6957. (let ((list (sort (all-completions pattern table confirm)
  6958. 'string<)))
  6959. (with-output-to-temp-buffer "*Completions*"
  6960. (condition-case nil
  6961. ;; Protection needed for XEmacs and emacs 21
  6962. (display-completion-list list pattern)
  6963. (error (display-completion-list list)))))
  6964. (message "Making completion list...%s" "done")))))))
  6965. ;;;; TODO, DEADLINE, Comments
  6966. (defun org-toggle-comment ()
  6967. "Change the COMMENT state of an entry."
  6968. (interactive)
  6969. (save-excursion
  6970. (org-back-to-heading)
  6971. (let (case-fold-search)
  6972. (if (looking-at (concat outline-regexp
  6973. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  6974. (replace-match "" t t nil 1)
  6975. (if (looking-at outline-regexp)
  6976. (progn
  6977. (goto-char (match-end 0))
  6978. (insert org-comment-string " ")))))))
  6979. (defvar org-last-todo-state-is-todo nil
  6980. "This is non-nil when the last TODO state change led to a TODO state.
  6981. If the last change removed the TODO tag or switched to DONE, then
  6982. this is nil.")
  6983. (defvar org-setting-tags nil) ; dynamically skiped
  6984. (defun org-parse-local-options (string var)
  6985. "Parse STRING for startup setting relevant for variable VAR."
  6986. (let ((rtn (symbol-value var))
  6987. e opts)
  6988. (save-match-data
  6989. (if (or (not string) (not (string-match "\\S-" string)))
  6990. rtn
  6991. (setq opts (delq nil (mapcar (lambda (x)
  6992. (setq e (assoc x org-startup-options))
  6993. (if (eq (nth 1 e) var) e nil))
  6994. (org-split-string string "[ \t]+"))))
  6995. (if (not opts)
  6996. rtn
  6997. (setq rtn nil)
  6998. (while (setq e (pop opts))
  6999. (if (not (nth 3 e))
  7000. (setq rtn (nth 2 e))
  7001. (if (not (listp rtn)) (setq rtn nil))
  7002. (push (nth 2 e) rtn)))
  7003. rtn)))))
  7004. (defvar org-blocker-hook nil
  7005. "Hook for functions that are allowed to block a state change.
  7006. Each function gets as its single argument a property list, see
  7007. `org-trigger-hook' for more information about this list.
  7008. If any of the functions in this hook returns nil, the state change
  7009. is blocked.")
  7010. (defvar org-trigger-hook nil
  7011. "Hook for functions that are triggered by a state change.
  7012. Each function gets as its single argument a property list with at least
  7013. the following elements:
  7014. (:type type-of-change :position pos-at-entry-start
  7015. :from old-state :to new-state)
  7016. Depending on the type, more properties may be present.
  7017. This mechanism is currently implemented for:
  7018. TODO state changes
  7019. ------------------
  7020. :type todo-state-change
  7021. :from previous state (keyword as a string), or nil
  7022. :to new state (keyword as a string), or nil")
  7023. (defun org-todo (&optional arg)
  7024. "Change the TODO state of an item.
  7025. The state of an item is given by a keyword at the start of the heading,
  7026. like
  7027. *** TODO Write paper
  7028. *** DONE Call mom
  7029. The different keywords are specified in the variable `org-todo-keywords'.
  7030. By default the available states are \"TODO\" and \"DONE\".
  7031. So for this example: when the item starts with TODO, it is changed to DONE.
  7032. When it starts with DONE, the DONE is removed. And when neither TODO nor
  7033. DONE are present, add TODO at the beginning of the heading.
  7034. With C-u prefix arg, use completion to determine the new state.
  7035. With numeric prefix arg, switch to that state.
  7036. For calling through lisp, arg is also interpreted in the following way:
  7037. 'none -> empty state
  7038. \"\"(empty string) -> switch to empty state
  7039. 'done -> switch to DONE
  7040. 'nextset -> switch to the next set of keywords
  7041. 'previousset -> switch to the previous set of keywords
  7042. \"WAITING\" -> switch to the specified keyword, but only if it
  7043. really is a member of `org-todo-keywords'."
  7044. (interactive "P")
  7045. (save-excursion
  7046. (catch 'exit
  7047. (org-back-to-heading)
  7048. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  7049. (or (looking-at (concat " +" org-todo-regexp " *"))
  7050. (looking-at " *"))
  7051. (let* ((match-data (match-data))
  7052. (startpos (point-at-bol))
  7053. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  7054. (org-log-done org-log-done)
  7055. (org-log-repeat org-log-repeat)
  7056. (org-todo-log-states org-todo-log-states)
  7057. (this (match-string 1))
  7058. (hl-pos (match-beginning 0))
  7059. (head (org-get-todo-sequence-head this))
  7060. (ass (assoc head org-todo-kwd-alist))
  7061. (interpret (nth 1 ass))
  7062. (done-word (nth 3 ass))
  7063. (final-done-word (nth 4 ass))
  7064. (last-state (or this ""))
  7065. (completion-ignore-case t)
  7066. (member (member this org-todo-keywords-1))
  7067. (tail (cdr member))
  7068. (state (cond
  7069. ((and org-todo-key-trigger
  7070. (or (and (equal arg '(4)) (eq org-use-fast-todo-selection 'prefix))
  7071. (and (not arg) org-use-fast-todo-selection
  7072. (not (eq org-use-fast-todo-selection 'prefix)))))
  7073. ;; Use fast selection
  7074. (org-fast-todo-selection))
  7075. ((and (equal arg '(4))
  7076. (or (not org-use-fast-todo-selection)
  7077. (not org-todo-key-trigger)))
  7078. ;; Read a state with completion
  7079. (completing-read "State: " (mapcar (lambda(x) (list x))
  7080. org-todo-keywords-1)
  7081. nil t))
  7082. ((eq arg 'right)
  7083. (if this
  7084. (if tail (car tail) nil)
  7085. (car org-todo-keywords-1)))
  7086. ((eq arg 'left)
  7087. (if (equal member org-todo-keywords-1)
  7088. nil
  7089. (if this
  7090. (nth (- (length org-todo-keywords-1) (length tail) 2)
  7091. org-todo-keywords-1)
  7092. (org-last org-todo-keywords-1))))
  7093. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  7094. (setq arg nil))) ; hack to fall back to cycling
  7095. (arg
  7096. ;; user or caller requests a specific state
  7097. (cond
  7098. ((equal arg "") nil)
  7099. ((eq arg 'none) nil)
  7100. ((eq arg 'done) (or done-word (car org-done-keywords)))
  7101. ((eq arg 'nextset)
  7102. (or (car (cdr (member head org-todo-heads)))
  7103. (car org-todo-heads)))
  7104. ((eq arg 'previousset)
  7105. (let ((org-todo-heads (reverse org-todo-heads)))
  7106. (or (car (cdr (member head org-todo-heads)))
  7107. (car org-todo-heads))))
  7108. ((car (member arg org-todo-keywords-1)))
  7109. ((nth (1- (prefix-numeric-value arg))
  7110. org-todo-keywords-1))))
  7111. ((null member) (or head (car org-todo-keywords-1)))
  7112. ((equal this final-done-word) nil) ;; -> make empty
  7113. ((null tail) nil) ;; -> first entry
  7114. ((eq interpret 'sequence)
  7115. (car tail))
  7116. ((memq interpret '(type priority))
  7117. (if (eq this-command last-command)
  7118. (car tail)
  7119. (if (> (length tail) 0)
  7120. (or done-word (car org-done-keywords))
  7121. nil)))
  7122. (t nil)))
  7123. (next (if state (concat " " state " ") " "))
  7124. (change-plist (list :type 'todo-state-change :from this :to state
  7125. :position startpos))
  7126. dolog now-done-p)
  7127. (when org-blocker-hook
  7128. (unless (save-excursion
  7129. (save-match-data
  7130. (run-hook-with-args-until-failure
  7131. 'org-blocker-hook change-plist)))
  7132. (if (interactive-p)
  7133. (error "TODO state change from %s to %s blocked" this state)
  7134. ;; fail silently
  7135. (message "TODO state change from %s to %s blocked" this state)
  7136. (throw 'exit nil))))
  7137. (store-match-data match-data)
  7138. (replace-match next t t)
  7139. (unless (pos-visible-in-window-p hl-pos)
  7140. (message "TODO state changed to %s" (org-trim next)))
  7141. (unless head
  7142. (setq head (org-get-todo-sequence-head state)
  7143. ass (assoc head org-todo-kwd-alist)
  7144. interpret (nth 1 ass)
  7145. done-word (nth 3 ass)
  7146. final-done-word (nth 4 ass)))
  7147. (when (memq arg '(nextset previousset))
  7148. (message "Keyword-Set %d/%d: %s"
  7149. (- (length org-todo-sets) -1
  7150. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  7151. (length org-todo-sets)
  7152. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  7153. (setq org-last-todo-state-is-todo
  7154. (not (member state org-done-keywords)))
  7155. (setq now-done-p (and (member state org-done-keywords)
  7156. (not (member this org-done-keywords))))
  7157. (and logging (org-local-logging logging))
  7158. (when (and (or org-todo-log-states org-log-done)
  7159. (not (memq arg '(nextset previousset))))
  7160. ;; we need to look at recording a time and note
  7161. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  7162. (nth 2 (assoc this org-todo-log-states))))
  7163. (when (and state
  7164. (member state org-not-done-keywords)
  7165. (not (member this org-not-done-keywords)))
  7166. ;; This is now a todo state and was not one before
  7167. ;; If there was a CLOSED time stamp, get rid of it.
  7168. (org-add-planning-info nil nil 'closed))
  7169. (when (and now-done-p org-log-done)
  7170. ;; It is now done, and it was not done before
  7171. (org-add-planning-info 'closed (org-current-time))
  7172. (if (and (not dolog) (eq 'note org-log-done))
  7173. (org-add-log-setup 'done state 'findpos 'note)))
  7174. (when (and state dolog)
  7175. ;; This is a non-nil state, and we need to log it
  7176. (org-add-log-setup 'state state 'findpos dolog)))
  7177. ;; Fixup tag positioning
  7178. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  7179. (run-hooks 'org-after-todo-state-change-hook)
  7180. (if (and arg (not (member state org-done-keywords)))
  7181. (setq head (org-get-todo-sequence-head state)))
  7182. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  7183. ;; Do we need to trigger a repeat?
  7184. (when now-done-p (org-auto-repeat-maybe state))
  7185. ;; Fixup cursor location if close to the keyword
  7186. (if (and (outline-on-heading-p)
  7187. (not (bolp))
  7188. (save-excursion (beginning-of-line 1)
  7189. (looking-at org-todo-line-regexp))
  7190. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  7191. (progn
  7192. (goto-char (or (match-end 2) (match-end 1)))
  7193. (just-one-space)))
  7194. (when org-trigger-hook
  7195. (save-excursion
  7196. (run-hook-with-args 'org-trigger-hook change-plist)))))))
  7197. (defun org-local-logging (value)
  7198. "Get logging settings from a property VALUE."
  7199. (let* (words w a)
  7200. ;; directly set the variables, they are already local.
  7201. (setq org-log-done nil
  7202. org-log-repeat nil
  7203. org-todo-log-states nil)
  7204. (setq words (org-split-string value))
  7205. (while (setq w (pop words))
  7206. (cond
  7207. ((setq a (assoc w org-startup-options))
  7208. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  7209. (set (nth 1 a) (nth 2 a))))
  7210. ((setq a (org-extract-log-state-settings w))
  7211. (and (member (car a) org-todo-keywords-1)
  7212. (push a org-todo-log-states)))))))
  7213. (defun org-get-todo-sequence-head (kwd)
  7214. "Return the head of the TODO sequence to which KWD belongs.
  7215. If KWD is not set, check if there is a text property remembering the
  7216. right sequence."
  7217. (let (p)
  7218. (cond
  7219. ((not kwd)
  7220. (or (get-text-property (point-at-bol) 'org-todo-head)
  7221. (progn
  7222. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  7223. nil (point-at-eol)))
  7224. (get-text-property p 'org-todo-head))))
  7225. ((not (member kwd org-todo-keywords-1))
  7226. (car org-todo-keywords-1))
  7227. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  7228. (defun org-fast-todo-selection ()
  7229. "Fast TODO keyword selection with single keys.
  7230. Returns the new TODO keyword, or nil if no state change should occur."
  7231. (let* ((fulltable org-todo-key-alist)
  7232. (done-keywords org-done-keywords) ;; needed for the faces.
  7233. (maxlen (apply 'max (mapcar
  7234. (lambda (x)
  7235. (if (stringp (car x)) (string-width (car x)) 0))
  7236. fulltable)))
  7237. (expert nil)
  7238. (fwidth (+ maxlen 3 1 3))
  7239. (ncol (/ (- (window-width) 4) fwidth))
  7240. tg cnt e c tbl
  7241. groups ingroup)
  7242. (save-window-excursion
  7243. (if expert
  7244. (set-buffer (get-buffer-create " *Org todo*"))
  7245. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  7246. (erase-buffer)
  7247. (org-set-local 'org-done-keywords done-keywords)
  7248. (setq tbl fulltable cnt 0)
  7249. (while (setq e (pop tbl))
  7250. (cond
  7251. ((equal e '(:startgroup))
  7252. (push '() groups) (setq ingroup t)
  7253. (when (not (= cnt 0))
  7254. (setq cnt 0)
  7255. (insert "\n"))
  7256. (insert "{ "))
  7257. ((equal e '(:endgroup))
  7258. (setq ingroup nil cnt 0)
  7259. (insert "}\n"))
  7260. (t
  7261. (setq tg (car e) c (cdr e))
  7262. (if ingroup (push tg (car groups)))
  7263. (setq tg (org-add-props tg nil 'face
  7264. (org-get-todo-face tg)))
  7265. (if (and (= cnt 0) (not ingroup)) (insert " "))
  7266. (insert "[" c "] " tg (make-string
  7267. (- fwidth 4 (length tg)) ?\ ))
  7268. (when (= (setq cnt (1+ cnt)) ncol)
  7269. (insert "\n")
  7270. (if ingroup (insert " "))
  7271. (setq cnt 0)))))
  7272. (insert "\n")
  7273. (goto-char (point-min))
  7274. (if (and (not expert) (fboundp 'fit-window-to-buffer))
  7275. (fit-window-to-buffer))
  7276. (message "[a-z..]:Set [SPC]:clear")
  7277. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  7278. (cond
  7279. ((or (= c ?\C-g)
  7280. (and (= c ?q) (not (rassoc c fulltable))))
  7281. (setq quit-flag t))
  7282. ((= c ?\ ) nil)
  7283. ((setq e (rassoc c fulltable) tg (car e))
  7284. tg)
  7285. (t (setq quit-flag t))))))
  7286. (defun org-entry-is-todo-p ()
  7287. (member (org-get-todo-state) org-not-done-keywords))
  7288. (defun org-entry-is-done-p ()
  7289. (member (org-get-todo-state) org-done-keywords))
  7290. (defun org-get-todo-state ()
  7291. (save-excursion
  7292. (org-back-to-heading t)
  7293. (and (looking-at org-todo-line-regexp)
  7294. (match-end 2)
  7295. (match-string 2))))
  7296. (defun org-at-date-range-p (&optional inactive-ok)
  7297. "Is the cursor inside a date range?"
  7298. (interactive)
  7299. (save-excursion
  7300. (catch 'exit
  7301. (let ((pos (point)))
  7302. (skip-chars-backward "^[<\r\n")
  7303. (skip-chars-backward "<[")
  7304. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  7305. (>= (match-end 0) pos)
  7306. (throw 'exit t))
  7307. (skip-chars-backward "^<[\r\n")
  7308. (skip-chars-backward "<[")
  7309. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  7310. (>= (match-end 0) pos)
  7311. (throw 'exit t)))
  7312. nil)))
  7313. (defun org-get-repeat ()
  7314. "Check if tere is a deadline/schedule with repeater in this entry."
  7315. (save-match-data
  7316. (save-excursion
  7317. (org-back-to-heading t)
  7318. (if (re-search-forward
  7319. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  7320. (match-string 1)))))
  7321. (defvar org-last-changed-timestamp)
  7322. (defvar org-log-post-message)
  7323. (defvar org-log-note-purpose)
  7324. (defvar org-log-note-how)
  7325. (defun org-auto-repeat-maybe (done-word)
  7326. "Check if the current headline contains a repeated deadline/schedule.
  7327. If yes, set TODO state back to what it was and change the base date
  7328. of repeating deadline/scheduled time stamps to new date.
  7329. This function is run automatically after each state change to a DONE state."
  7330. ;; last-state is dynamically scoped into this function
  7331. (let* ((repeat (org-get-repeat))
  7332. (aa (assoc last-state org-todo-kwd-alist))
  7333. (interpret (nth 1 aa))
  7334. (head (nth 2 aa))
  7335. (whata '(("d" . day) ("m" . month) ("y" . year)))
  7336. (msg "Entry repeats: ")
  7337. (org-log-done nil)
  7338. (org-todo-log-states nil)
  7339. (nshiftmax 10) (nshift 0)
  7340. re type n what ts mb0 time)
  7341. (when repeat
  7342. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  7343. (org-todo (if (eq interpret 'type) last-state head))
  7344. (when org-log-repeat
  7345. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  7346. (memq 'org-add-log-note post-command-hook))
  7347. ;; OK, we are already setup for some record
  7348. (if (eq org-log-repeat 'note)
  7349. ;; make sure we take a note, not only a time stamp
  7350. (setq org-log-note-how 'note))
  7351. ;; Set up for taking a record
  7352. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  7353. 'findpos org-log-repeat)))
  7354. (org-back-to-heading t)
  7355. (org-add-planning-info nil nil 'closed)
  7356. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  7357. org-deadline-time-regexp "\\)\\|\\("
  7358. org-ts-regexp "\\)"))
  7359. (while (re-search-forward
  7360. re (save-excursion (outline-next-heading) (point)) t)
  7361. (setq type (if (match-end 1) org-scheduled-string
  7362. (if (match-end 3) org-deadline-string "Plain:"))
  7363. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0)))
  7364. mb0 (match-beginning 0))
  7365. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  7366. (setq n (string-to-number (match-string 2 ts))
  7367. what (match-string 3 ts))
  7368. (if (equal what "w") (setq n (* n 7) what "d"))
  7369. ;; Preparation, see if we need to modify the start date for the change
  7370. (when (match-end 1)
  7371. (setq time (save-match-data (org-time-string-to-time ts)))
  7372. (cond
  7373. ((equal (match-string 1 ts) ".")
  7374. ;; Shift starting date to today
  7375. (org-timestamp-change
  7376. (- (time-to-days (current-time)) (time-to-days time))
  7377. 'day))
  7378. ((equal (match-string 1 ts) "+")
  7379. (while (or (= nshift 0)
  7380. (<= (time-to-days time) (time-to-days (current-time))))
  7381. (when (= (incf nshift) nshiftmax)
  7382. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  7383. (error "Abort")))
  7384. (org-timestamp-change n (cdr (assoc what whata)))
  7385. (org-at-timestamp-p t)
  7386. (setq ts (match-string 1))
  7387. (setq time (save-match-data (org-time-string-to-time ts))))
  7388. (org-timestamp-change (- n) (cdr (assoc what whata)))
  7389. ;; rematch, so that we have everything in place for the real shift
  7390. (org-at-timestamp-p t)
  7391. (setq ts (match-string 1))
  7392. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  7393. (org-timestamp-change n (cdr (assoc what whata)))
  7394. (setq msg (concat msg type org-last-changed-timestamp " "))))
  7395. (setq org-log-post-message msg)
  7396. (message "%s" msg))))
  7397. (defun org-show-todo-tree (arg)
  7398. "Make a compact tree which shows all headlines marked with TODO.
  7399. The tree will show the lines where the regexp matches, and all higher
  7400. headlines above the match.
  7401. With a \\[universal-argument] prefix, also show the DONE entries.
  7402. With a numeric prefix N, construct a sparse tree for the Nth element
  7403. of `org-todo-keywords-1'."
  7404. (interactive "P")
  7405. (let ((case-fold-search nil)
  7406. (kwd-re
  7407. (cond ((null arg) org-not-done-regexp)
  7408. ((equal arg '(4))
  7409. (let ((kwd (completing-read "Keyword (or KWD1|KWD2|...): "
  7410. (mapcar 'list org-todo-keywords-1))))
  7411. (concat "\\("
  7412. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  7413. "\\)\\>")))
  7414. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  7415. (regexp-quote (nth (1- (prefix-numeric-value arg))
  7416. org-todo-keywords-1)))
  7417. (t (error "Invalid prefix argument: %s" arg)))))
  7418. (message "%d TODO entries found"
  7419. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  7420. (defun org-deadline (&optional remove)
  7421. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  7422. With argument REMOVE, remove any deadline from the item."
  7423. (interactive "P")
  7424. (if remove
  7425. (progn
  7426. (org-remove-timestamp-with-keyword org-deadline-string)
  7427. (message "Item no longer has a deadline."))
  7428. (org-add-planning-info 'deadline nil 'closed)))
  7429. (defun org-schedule (&optional remove)
  7430. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  7431. With argument REMOVE, remove any scheduling date from the item."
  7432. (interactive "P")
  7433. (if remove
  7434. (progn
  7435. (org-remove-timestamp-with-keyword org-scheduled-string)
  7436. (message "Item is no longer scheduled."))
  7437. (org-add-planning-info 'scheduled nil 'closed)))
  7438. (defun org-remove-timestamp-with-keyword (keyword)
  7439. "Remove all time stamps with KEYWORD in the current entry."
  7440. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  7441. beg)
  7442. (save-excursion
  7443. (org-back-to-heading t)
  7444. (setq beg (point))
  7445. (org-end-of-subtree t t)
  7446. (while (re-search-backward re beg t)
  7447. (replace-match "")
  7448. (unless (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  7449. (delete-region (point-at-bol) (min (1+ (point)) (point-max))))))))
  7450. (defun org-add-planning-info (what &optional time &rest remove)
  7451. "Insert new timestamp with keyword in the line directly after the headline.
  7452. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  7453. If non is given, the user is prompted for a date.
  7454. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  7455. be removed."
  7456. (interactive)
  7457. (let (org-time-was-given org-end-time-was-given ts
  7458. end default-time default-input)
  7459. (when (and (not time) (memq what '(scheduled deadline)))
  7460. ;; Try to get a default date/time from existing timestamp
  7461. (save-excursion
  7462. (org-back-to-heading t)
  7463. (setq end (save-excursion (outline-next-heading) (point)))
  7464. (when (re-search-forward (if (eq what 'scheduled)
  7465. org-scheduled-time-regexp
  7466. org-deadline-time-regexp)
  7467. end t)
  7468. (setq ts (match-string 1)
  7469. default-time
  7470. (apply 'encode-time (org-parse-time-string ts))
  7471. default-input (and ts (org-get-compact-tod ts))))))
  7472. (when what
  7473. ;; If necessary, get the time from the user
  7474. (setq time (or time (org-read-date nil 'to-time nil nil
  7475. default-time default-input))))
  7476. (when (and org-insert-labeled-timestamps-at-point
  7477. (member what '(scheduled deadline)))
  7478. (insert
  7479. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  7480. (org-insert-time-stamp time org-time-was-given
  7481. nil nil nil (list org-end-time-was-given))
  7482. (setq what nil))
  7483. (save-excursion
  7484. (save-restriction
  7485. (let (col list elt ts buffer-invisibility-spec)
  7486. (org-back-to-heading t)
  7487. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  7488. (goto-char (match-end 1))
  7489. (setq col (current-column))
  7490. (goto-char (match-end 0))
  7491. (if (eobp) (insert "\n") (forward-char 1))
  7492. (if (and (not (looking-at outline-regexp))
  7493. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  7494. "[^\r\n]*"))
  7495. (not (equal (match-string 1) org-clock-string)))
  7496. (narrow-to-region (match-beginning 0) (match-end 0))
  7497. (insert-before-markers "\n")
  7498. (backward-char 1)
  7499. (narrow-to-region (point) (point))
  7500. (org-indent-to-column col))
  7501. ;; Check if we have to remove something.
  7502. (setq list (cons what remove))
  7503. (while list
  7504. (setq elt (pop list))
  7505. (goto-char (point-min))
  7506. (when (or (and (eq elt 'scheduled)
  7507. (re-search-forward org-scheduled-time-regexp nil t))
  7508. (and (eq elt 'deadline)
  7509. (re-search-forward org-deadline-time-regexp nil t))
  7510. (and (eq elt 'closed)
  7511. (re-search-forward org-closed-time-regexp nil t)))
  7512. (replace-match "")
  7513. (if (looking-at "--+<[^>]+>") (replace-match ""))
  7514. (if (looking-at " +") (replace-match ""))))
  7515. (goto-char (point-max))
  7516. (when what
  7517. (insert
  7518. (if (not (equal (char-before) ?\ )) " " "")
  7519. (cond ((eq what 'scheduled) org-scheduled-string)
  7520. ((eq what 'deadline) org-deadline-string)
  7521. ((eq what 'closed) org-closed-string))
  7522. " ")
  7523. (setq ts (org-insert-time-stamp
  7524. time
  7525. (or org-time-was-given
  7526. (and (eq what 'closed) org-log-done-with-time))
  7527. (eq what 'closed)
  7528. nil nil (list org-end-time-was-given)))
  7529. (end-of-line 1))
  7530. (goto-char (point-min))
  7531. (widen)
  7532. (if (and (looking-at "[ \t]+\n")
  7533. (equal (char-before) ?\n))
  7534. (backward-delete-char 1))
  7535. ts)))))
  7536. (defvar org-log-note-marker (make-marker))
  7537. (defvar org-log-note-purpose nil)
  7538. (defvar org-log-note-state nil)
  7539. (defvar org-log-note-how nil)
  7540. (defvar org-log-note-window-configuration nil)
  7541. (defvar org-log-note-return-to (make-marker))
  7542. (defvar org-log-post-message nil
  7543. "Message to be displayed after a log note has been stored.
  7544. The auto-repeater uses this.")
  7545. (defun org-add-note ()
  7546. "Add a note to the current entry.
  7547. This is done in the same way as adding a state change note."
  7548. (interactive)
  7549. (org-add-log-setup 'note nil t nil))
  7550. (defun org-add-log-setup (&optional purpose state findpos how)
  7551. "Set up the post command hook to take a note.
  7552. If this is about to TODO state change, the new state is expected in STATE.
  7553. When FINDPOS is non-nil, find the correct position for the note in
  7554. the current entry. If not, assume that it can be inserted at point."
  7555. (save-excursion
  7556. (when findpos
  7557. (org-back-to-heading t)
  7558. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  7559. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  7560. "[^\r\n]*\\)?"))
  7561. (goto-char (match-end 0))
  7562. (unless org-log-states-order-reversed
  7563. (and (= (char-after) ?\n) (forward-char 1))
  7564. (org-skip-over-state-notes)
  7565. (skip-chars-backward " \t\n\r")))
  7566. (move-marker org-log-note-marker (point))
  7567. (setq org-log-note-purpose purpose
  7568. org-log-note-state state
  7569. org-log-note-how how)
  7570. (add-hook 'post-command-hook 'org-add-log-note 'append)))
  7571. (defun org-skip-over-state-notes ()
  7572. "Skip past the list of State notes in an entry."
  7573. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  7574. (while (looking-at "[ \t]*- State")
  7575. (condition-case nil
  7576. (org-next-item)
  7577. (error (org-end-of-item)))))
  7578. (defun org-add-log-note (&optional purpose)
  7579. "Pop up a window for taking a note, and add this note later at point."
  7580. (remove-hook 'post-command-hook 'org-add-log-note)
  7581. (setq org-log-note-window-configuration (current-window-configuration))
  7582. (delete-other-windows)
  7583. (move-marker org-log-note-return-to (point))
  7584. (switch-to-buffer (marker-buffer org-log-note-marker))
  7585. (goto-char org-log-note-marker)
  7586. (org-switch-to-buffer-other-window "*Org Note*")
  7587. (erase-buffer)
  7588. (if (memq org-log-note-how '(time state))
  7589. (org-store-log-note)
  7590. (let ((org-inhibit-startup t)) (org-mode))
  7591. (insert (format "# Insert note for %s.
  7592. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  7593. (cond
  7594. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  7595. ((eq org-log-note-purpose 'done) "closed todo item")
  7596. ((eq org-log-note-purpose 'state)
  7597. (format "state change to \"%s\"" org-log-note-state))
  7598. ((eq org-log-note-purpose 'note)
  7599. "this entry")
  7600. (t (error "This should not happen")))))
  7601. (org-set-local 'org-finish-function 'org-store-log-note)))
  7602. (defvar org-note-abort nil) ; dynamically scoped
  7603. (defun org-store-log-note ()
  7604. "Finish taking a log note, and insert it to where it belongs."
  7605. (let ((txt (buffer-string))
  7606. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  7607. lines ind)
  7608. (kill-buffer (current-buffer))
  7609. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  7610. (setq txt (replace-match "" t t txt)))
  7611. (if (string-match "\\s-+\\'" txt)
  7612. (setq txt (replace-match "" t t txt)))
  7613. (setq lines (org-split-string txt "\n"))
  7614. (when (and note (string-match "\\S-" note))
  7615. (setq note
  7616. (org-replace-escapes
  7617. note
  7618. (list (cons "%u" (user-login-name))
  7619. (cons "%U" user-full-name)
  7620. (cons "%t" (format-time-string
  7621. (org-time-stamp-format 'long 'inactive)
  7622. (current-time)))
  7623. (cons "%s" (if org-log-note-state
  7624. (concat "\"" org-log-note-state "\"")
  7625. "")))))
  7626. (if lines (setq note (concat note " \\\\")))
  7627. (push note lines))
  7628. (when (or current-prefix-arg org-note-abort) (setq lines nil))
  7629. (when lines
  7630. (save-excursion
  7631. (set-buffer (marker-buffer org-log-note-marker))
  7632. (save-excursion
  7633. (goto-char org-log-note-marker)
  7634. (move-marker org-log-note-marker nil)
  7635. (end-of-line 1)
  7636. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  7637. (indent-relative nil)
  7638. (insert "- " (pop lines))
  7639. (org-indent-line-function)
  7640. (beginning-of-line 1)
  7641. (looking-at "[ \t]*")
  7642. (setq ind (concat (match-string 0) " "))
  7643. (end-of-line 1)
  7644. (while lines (insert "\n" ind (pop lines)))))))
  7645. (set-window-configuration org-log-note-window-configuration)
  7646. (with-current-buffer (marker-buffer org-log-note-return-to)
  7647. (goto-char org-log-note-return-to))
  7648. (move-marker org-log-note-return-to nil)
  7649. (and org-log-post-message (message "%s" org-log-post-message)))
  7650. (defun org-sparse-tree (&optional arg)
  7651. "Create a sparse tree, prompt for the details.
  7652. This command can create sparse trees. You first need to select the type
  7653. of match used to create the tree:
  7654. t Show entries with a specific TODO keyword.
  7655. T Show entries selected by a tags match.
  7656. p Enter a property name and its value (both with completion on existing
  7657. names/values) and show entries with that property.
  7658. r Show entries matching a regular expression
  7659. d Show deadlines due within `org-deadline-warning-days'."
  7660. (interactive "P")
  7661. (let (ans kwd value)
  7662. (message "Sparse tree: [/]regexp [t]odo-kwd [T]ag [p]roperty [d]eadlines [b]efore-date")
  7663. (setq ans (read-char-exclusive))
  7664. (cond
  7665. ((equal ans ?d)
  7666. (call-interactively 'org-check-deadlines))
  7667. ((equal ans ?b)
  7668. (call-interactively 'org-check-before-date))
  7669. ((equal ans ?t)
  7670. (org-show-todo-tree '(4)))
  7671. ((equal ans ?T)
  7672. (call-interactively 'org-tags-sparse-tree))
  7673. ((member ans '(?p ?P))
  7674. (setq kwd (completing-read "Property: "
  7675. (mapcar 'list (org-buffer-property-keys))))
  7676. (setq value (completing-read "Value: "
  7677. (mapcar 'list (org-property-values kwd))))
  7678. (unless (string-match "\\`{.*}\\'" value)
  7679. (setq value (concat "\"" value "\"")))
  7680. (org-tags-sparse-tree arg (concat kwd "=" value)))
  7681. ((member ans '(?r ?R ?/))
  7682. (call-interactively 'org-occur))
  7683. (t (error "No such sparse tree command \"%c\"" ans)))))
  7684. (defvar org-occur-highlights nil
  7685. "List of overlays used for occur matches.")
  7686. (make-variable-buffer-local 'org-occur-highlights)
  7687. (defvar org-occur-parameters nil
  7688. "Parameters of the active org-occur calls.
  7689. This is a list, each call to org-occur pushes as cons cell,
  7690. containing the regular expression and the callback, onto the list.
  7691. The list can contain several entries if `org-occur' has been called
  7692. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  7693. will only contain one set of parameters. When the highlights are
  7694. removed (for example with `C-c C-c', or with the next edit (depending
  7695. on `org-remove-highlights-with-change'), this variable is emptied
  7696. as well.")
  7697. (make-variable-buffer-local 'org-occur-parameters)
  7698. (defun org-occur (regexp &optional keep-previous callback)
  7699. "Make a compact tree which shows all matches of REGEXP.
  7700. The tree will show the lines where the regexp matches, and all higher
  7701. headlines above the match. It will also show the heading after the match,
  7702. to make sure editing the matching entry is easy.
  7703. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  7704. call to `org-occur' will be kept, to allow stacking of calls to this
  7705. command.
  7706. If CALLBACK is non-nil, it is a function which is called to confirm
  7707. that the match should indeed be shown."
  7708. (interactive "sRegexp: \nP")
  7709. (unless keep-previous
  7710. (org-remove-occur-highlights nil nil t))
  7711. (push (cons regexp callback) org-occur-parameters)
  7712. (let ((cnt 0))
  7713. (save-excursion
  7714. (goto-char (point-min))
  7715. (if (or (not keep-previous) ; do not want to keep
  7716. (not org-occur-highlights)) ; no previous matches
  7717. ;; hide everything
  7718. (org-overview))
  7719. (while (re-search-forward regexp nil t)
  7720. (when (or (not callback)
  7721. (save-match-data (funcall callback)))
  7722. (setq cnt (1+ cnt))
  7723. (when org-highlight-sparse-tree-matches
  7724. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  7725. (org-show-context 'occur-tree))))
  7726. (when org-remove-highlights-with-change
  7727. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  7728. nil 'local))
  7729. (unless org-sparse-tree-open-archived-trees
  7730. (org-hide-archived-subtrees (point-min) (point-max)))
  7731. (run-hooks 'org-occur-hook)
  7732. (if (interactive-p)
  7733. (message "%d match(es) for regexp %s" cnt regexp))
  7734. cnt))
  7735. (defun org-show-context (&optional key)
  7736. "Make sure point and context and visible.
  7737. How much context is shown depends upon the variables
  7738. `org-show-hierarchy-above', `org-show-following-heading'. and
  7739. `org-show-siblings'."
  7740. (let ((heading-p (org-on-heading-p t))
  7741. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  7742. (following-p (org-get-alist-option org-show-following-heading key))
  7743. (entry-p (org-get-alist-option org-show-entry-below key))
  7744. (siblings-p (org-get-alist-option org-show-siblings key)))
  7745. (catch 'exit
  7746. ;; Show heading or entry text
  7747. (if (and heading-p (not entry-p))
  7748. (org-flag-heading nil) ; only show the heading
  7749. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  7750. (org-show-hidden-entry))) ; show entire entry
  7751. (when following-p
  7752. ;; Show next sibling, or heading below text
  7753. (save-excursion
  7754. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  7755. (org-flag-heading nil))))
  7756. (when siblings-p (org-show-siblings))
  7757. (when hierarchy-p
  7758. ;; show all higher headings, possibly with siblings
  7759. (save-excursion
  7760. (while (and (condition-case nil
  7761. (progn (org-up-heading-all 1) t)
  7762. (error nil))
  7763. (not (bobp)))
  7764. (org-flag-heading nil)
  7765. (when siblings-p (org-show-siblings))))))))
  7766. (defun org-reveal (&optional siblings)
  7767. "Show current entry, hierarchy above it, and the following headline.
  7768. This can be used to show a consistent set of context around locations
  7769. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  7770. not t for the search context.
  7771. With optional argument SIBLINGS, on each level of the hierarchy all
  7772. siblings are shown. This repairs the tree structure to what it would
  7773. look like when opened with hierarchical calls to `org-cycle'."
  7774. (interactive "P")
  7775. (let ((org-show-hierarchy-above t)
  7776. (org-show-following-heading t)
  7777. (org-show-siblings (if siblings t org-show-siblings)))
  7778. (org-show-context nil)))
  7779. (defun org-highlight-new-match (beg end)
  7780. "Highlight from BEG to END and mark the highlight is an occur headline."
  7781. (let ((ov (org-make-overlay beg end)))
  7782. (org-overlay-put ov 'face 'secondary-selection)
  7783. (push ov org-occur-highlights)))
  7784. (defun org-remove-occur-highlights (&optional beg end noremove)
  7785. "Remove the occur highlights from the buffer.
  7786. BEG and END are ignored. If NOREMOVE is nil, remove this function
  7787. from the `before-change-functions' in the current buffer."
  7788. (interactive)
  7789. (unless org-inhibit-highlight-removal
  7790. (mapc 'org-delete-overlay org-occur-highlights)
  7791. (setq org-occur-highlights nil)
  7792. (setq org-occur-parameters nil)
  7793. (unless noremove
  7794. (remove-hook 'before-change-functions
  7795. 'org-remove-occur-highlights 'local))))
  7796. ;;;; Priorities
  7797. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  7798. "Regular expression matching the priority indicator.")
  7799. (defvar org-remove-priority-next-time nil)
  7800. (defun org-priority-up ()
  7801. "Increase the priority of the current item."
  7802. (interactive)
  7803. (org-priority 'up))
  7804. (defun org-priority-down ()
  7805. "Decrease the priority of the current item."
  7806. (interactive)
  7807. (org-priority 'down))
  7808. (defun org-priority (&optional action)
  7809. "Change the priority of an item by ARG.
  7810. ACTION can be `set', `up', `down', or a character."
  7811. (interactive)
  7812. (setq action (or action 'set))
  7813. (let (current new news have remove)
  7814. (save-excursion
  7815. (org-back-to-heading)
  7816. (if (looking-at org-priority-regexp)
  7817. (setq current (string-to-char (match-string 2))
  7818. have t)
  7819. (setq current org-default-priority))
  7820. (cond
  7821. ((or (eq action 'set)
  7822. (if (featurep 'xemacs) (characterp action) (integerp action)))
  7823. (if (not (eq action 'set))
  7824. (setq new action)
  7825. (message "Priority %c-%c, SPC to remove: "
  7826. org-highest-priority org-lowest-priority)
  7827. (setq new (read-char-exclusive)))
  7828. (if (and (= (upcase org-highest-priority) org-highest-priority)
  7829. (= (upcase org-lowest-priority) org-lowest-priority))
  7830. (setq new (upcase new)))
  7831. (cond ((equal new ?\ ) (setq remove t))
  7832. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  7833. (error "Priority must be between `%c' and `%c'"
  7834. org-highest-priority org-lowest-priority))))
  7835. ((eq action 'up)
  7836. (if (and (not have) (eq last-command this-command))
  7837. (setq new org-lowest-priority)
  7838. (setq new (if (and org-priority-start-cycle-with-default (not have))
  7839. org-default-priority (1- current)))))
  7840. ((eq action 'down)
  7841. (if (and (not have) (eq last-command this-command))
  7842. (setq new org-highest-priority)
  7843. (setq new (if (and org-priority-start-cycle-with-default (not have))
  7844. org-default-priority (1+ current)))))
  7845. (t (error "Invalid action")))
  7846. (if (or (< (upcase new) org-highest-priority)
  7847. (> (upcase new) org-lowest-priority))
  7848. (setq remove t))
  7849. (setq news (format "%c" new))
  7850. (if have
  7851. (if remove
  7852. (replace-match "" t t nil 1)
  7853. (replace-match news t t nil 2))
  7854. (if remove
  7855. (error "No priority cookie found in line")
  7856. (looking-at org-todo-line-regexp)
  7857. (if (match-end 2)
  7858. (progn
  7859. (goto-char (match-end 2))
  7860. (insert " [#" news "]"))
  7861. (goto-char (match-beginning 3))
  7862. (insert "[#" news "] ")))))
  7863. (org-preserve-lc (org-set-tags nil 'align))
  7864. (if remove
  7865. (message "Priority removed")
  7866. (message "Priority of current item set to %s" news))))
  7867. (defun org-get-priority (s)
  7868. "Find priority cookie and return priority."
  7869. (save-match-data
  7870. (if (not (string-match org-priority-regexp s))
  7871. (* 1000 (- org-lowest-priority org-default-priority))
  7872. (* 1000 (- org-lowest-priority
  7873. (string-to-char (match-string 2 s)))))))
  7874. ;;;; Tags
  7875. (defun org-scan-tags (action matcher &optional todo-only)
  7876. "Scan headline tags with inheritance and produce output ACTION.
  7877. ACTION can be `sparse-tree' or `agenda'. MATCHER is a Lisp form to be
  7878. evaluated, testing if a given set of tags qualifies a headline for
  7879. inclusion. When TODO-ONLY is non-nil, only lines with a TODO keyword
  7880. are included in the output."
  7881. (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
  7882. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  7883. (org-re
  7884. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  7885. (props (list 'face nil
  7886. 'done-face 'org-done
  7887. 'undone-face nil
  7888. 'mouse-face 'highlight
  7889. 'org-not-done-regexp org-not-done-regexp
  7890. 'org-todo-regexp org-todo-regexp
  7891. 'keymap org-agenda-keymap
  7892. 'help-echo
  7893. (format "mouse-2 or RET jump to org file %s"
  7894. (abbreviate-file-name
  7895. (or (buffer-file-name (buffer-base-buffer))
  7896. (buffer-name (buffer-base-buffer)))))))
  7897. (case-fold-search nil)
  7898. lspos
  7899. tags tags-list tags-alist (llast 0) rtn level category i txt
  7900. todo marker entry priority)
  7901. (save-excursion
  7902. (goto-char (point-min))
  7903. (when (eq action 'sparse-tree)
  7904. (org-overview)
  7905. (org-remove-occur-highlights))
  7906. (while (re-search-forward re nil t)
  7907. (catch :skip
  7908. (setq todo (if (match-end 1) (match-string 2))
  7909. tags (if (match-end 4) (match-string 4)))
  7910. (goto-char (setq lspos (1+ (match-beginning 0))))
  7911. (setq level (org-reduced-level (funcall outline-level))
  7912. category (org-get-category))
  7913. (setq i llast llast level)
  7914. ;; remove tag lists from same and sublevels
  7915. (while (>= i level)
  7916. (when (setq entry (assoc i tags-alist))
  7917. (setq tags-alist (delete entry tags-alist)))
  7918. (setq i (1- i)))
  7919. ;; add the next tags
  7920. (when tags
  7921. (setq tags (mapcar 'downcase (org-split-string tags ":"))
  7922. tags-alist
  7923. (cons (cons level tags) tags-alist)))
  7924. ;; compile tags for current headline
  7925. (setq tags-list
  7926. (if org-use-tag-inheritance
  7927. (apply 'append (mapcar 'cdr tags-alist))
  7928. tags))
  7929. (when (and tags org-use-tag-inheritance
  7930. (not (eq t org-use-tag-inheritance)))
  7931. ;; selective inheritance, remove uninherited ones
  7932. (setcdr (car tags-alist)
  7933. (org-remove-uniherited-tags (cdar tags-alist))))
  7934. (when (and (or (not todo-only) (member todo org-not-done-keywords))
  7935. (eval matcher)
  7936. (or (not org-agenda-skip-archived-trees)
  7937. (not (member org-archive-tag tags-list))))
  7938. (and (eq action 'agenda) (org-agenda-skip))
  7939. ;; list this headline
  7940. (if (eq action 'sparse-tree)
  7941. (progn
  7942. (and org-highlight-sparse-tree-matches
  7943. (org-get-heading) (match-end 0)
  7944. (org-highlight-new-match
  7945. (match-beginning 0) (match-beginning 1)))
  7946. (org-show-context 'tags-tree))
  7947. (setq txt (org-format-agenda-item
  7948. ""
  7949. (concat
  7950. (if org-tags-match-list-sublevels
  7951. (make-string (1- level) ?.) "")
  7952. (org-get-heading))
  7953. category tags-list)
  7954. priority (org-get-priority txt))
  7955. (goto-char lspos)
  7956. (setq marker (org-agenda-new-marker))
  7957. (org-add-props txt props
  7958. 'org-marker marker 'org-hd-marker marker 'org-category category
  7959. 'priority priority 'type "tagsmatch")
  7960. (push txt rtn))
  7961. ;; if we are to skip sublevels, jump to end of subtree
  7962. (or org-tags-match-list-sublevels (org-end-of-subtree t))))))
  7963. (when (and (eq action 'sparse-tree)
  7964. (not org-sparse-tree-open-archived-trees))
  7965. (org-hide-archived-subtrees (point-min) (point-max)))
  7966. (nreverse rtn)))
  7967. (defun org-remove-uniherited-tags (tags)
  7968. "Remove all tags that are not inherited from the list TAGS."
  7969. (cond
  7970. ((eq org-use-tag-inheritance t) tags)
  7971. ((not org-use-tag-inheritance) nil)
  7972. ((stringp org-use-tag-inheritance)
  7973. (delq nil (mapcar
  7974. (lambda (x) (if (string-match org-use-tag-inheritance x) x nil))
  7975. tags)))
  7976. ((listp org-use-tag-inheritance)
  7977. (org-delete-all org-use-tag-inheritance tags))))
  7978. (defvar todo-only) ;; dynamically scoped
  7979. (defun org-tags-sparse-tree (&optional todo-only match)
  7980. "Create a sparse tree according to tags string MATCH.
  7981. MATCH can contain positive and negative selection of tags, like
  7982. \"+WORK+URGENT-WITHBOSS\".
  7983. If optional argument TODO_ONLY is non-nil, only select lines that are
  7984. also TODO lines."
  7985. (interactive "P")
  7986. (org-prepare-agenda-buffers (list (current-buffer)))
  7987. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  7988. (defvar org-cached-props nil)
  7989. (defun org-cached-entry-get (pom property)
  7990. (if (or (eq t org-use-property-inheritance)
  7991. (and (stringp org-use-property-inheritance)
  7992. (string-match org-use-property-inheritance property))
  7993. (and (listp org-use-property-inheritance)
  7994. (member property org-use-property-inheritance)))
  7995. ;; Caching is not possible, check it directly
  7996. (org-entry-get pom property 'inherit)
  7997. ;; Get all properties, so that we can do complicated checks easily
  7998. (cdr (assoc property (or org-cached-props
  7999. (setq org-cached-props
  8000. (org-entry-properties pom)))))))
  8001. (defun org-global-tags-completion-table (&optional files)
  8002. "Return the list of all tags in all agenda buffer/files."
  8003. (save-excursion
  8004. (org-uniquify
  8005. (delq nil
  8006. (apply 'append
  8007. (mapcar
  8008. (lambda (file)
  8009. (set-buffer (find-file-noselect file))
  8010. (append (org-get-buffer-tags)
  8011. (mapcar (lambda (x) (if (stringp (car-safe x))
  8012. (list (car-safe x)) nil))
  8013. org-tag-alist)))
  8014. (if (and files (car files))
  8015. files
  8016. (org-agenda-files))))))))
  8017. (defun org-make-tags-matcher (match)
  8018. "Create the TAGS//TODO matcher form for the selection string MATCH."
  8019. ;; todo-only is scoped dynamically into this function, and the function
  8020. ;; may change it it the matcher asksk for it.
  8021. (unless match
  8022. ;; Get a new match request, with completion
  8023. (let ((org-last-tags-completion-table
  8024. (org-global-tags-completion-table)))
  8025. (setq match (completing-read
  8026. "Match: " 'org-tags-completion-function nil nil nil
  8027. 'org-tags-history))))
  8028. ;; Parse the string and create a lisp form
  8029. (let ((match0 match)
  8030. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
  8031. minus tag mm
  8032. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  8033. orterms term orlist re-p str-p level-p level-op
  8034. prop-p pn pv po cat-p gv)
  8035. (if (string-match "/+" match)
  8036. ;; match contains also a todo-matching request
  8037. (progn
  8038. (setq tagsmatch (substring match 0 (match-beginning 0))
  8039. todomatch (substring match (match-end 0)))
  8040. (if (string-match "^!" todomatch)
  8041. (setq todo-only t todomatch (substring todomatch 1)))
  8042. (if (string-match "^\\s-*$" todomatch)
  8043. (setq todomatch nil)))
  8044. ;; only matching tags
  8045. (setq tagsmatch match todomatch nil))
  8046. ;; Make the tags matcher
  8047. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  8048. (setq tagsmatcher t)
  8049. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  8050. (while (setq term (pop orterms))
  8051. (while (and (equal (substring term -1) "\\") orterms)
  8052. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  8053. (while (string-match re term)
  8054. (setq minus (and (match-end 1)
  8055. (equal (match-string 1 term) "-"))
  8056. tag (match-string 2 term)
  8057. re-p (equal (string-to-char tag) ?{)
  8058. level-p (match-end 4)
  8059. prop-p (match-end 5)
  8060. mm (cond
  8061. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  8062. (level-p
  8063. (setq level-op (org-op-to-function (match-string 3 term)))
  8064. `(,level-op level ,(string-to-number
  8065. (match-string 4 term))))
  8066. (prop-p
  8067. (setq pn (match-string 5 term)
  8068. po (match-string 6 term)
  8069. pv (match-string 7 term)
  8070. cat-p (equal pn "CATEGORY")
  8071. re-p (equal (string-to-char pv) ?{)
  8072. str-p (equal (string-to-char pv) ?\")
  8073. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  8074. (setq po (org-op-to-function po str-p))
  8075. (if (equal pn "CATEGORY")
  8076. (setq gv '(get-text-property (point) 'org-category))
  8077. (setq gv `(org-cached-entry-get nil ,pn)))
  8078. (if re-p
  8079. (if (eq po 'org<>)
  8080. `(not (string-match ,pv (or ,gv "")))
  8081. `(string-match ,pv (or ,gv "")))
  8082. (if str-p
  8083. `(,po (or ,gv "") ,pv)
  8084. `(,po (string-to-number (or ,gv ""))
  8085. ,(string-to-number pv) ))))
  8086. (t `(member ,(downcase tag) tags-list)))
  8087. mm (if minus (list 'not mm) mm)
  8088. term (substring term (match-end 0)))
  8089. (push mm tagsmatcher))
  8090. (push (if (> (length tagsmatcher) 1)
  8091. (cons 'and tagsmatcher)
  8092. (car tagsmatcher))
  8093. orlist)
  8094. (setq tagsmatcher nil))
  8095. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  8096. (setq tagsmatcher
  8097. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  8098. ;; Make the todo matcher
  8099. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  8100. (setq todomatcher t)
  8101. (setq orterms (org-split-string todomatch "|") orlist nil)
  8102. (while (setq term (pop orterms))
  8103. (while (string-match re term)
  8104. (setq minus (and (match-end 1)
  8105. (equal (match-string 1 term) "-"))
  8106. kwd (match-string 2 term)
  8107. re-p (equal (string-to-char kwd) ?{)
  8108. term (substring term (match-end 0))
  8109. mm (if re-p
  8110. `(string-match ,(substring kwd 1 -1) todo)
  8111. (list 'equal 'todo kwd))
  8112. mm (if minus (list 'not mm) mm))
  8113. (push mm todomatcher))
  8114. (push (if (> (length todomatcher) 1)
  8115. (cons 'and todomatcher)
  8116. (car todomatcher))
  8117. orlist)
  8118. (setq todomatcher nil))
  8119. (setq todomatcher (if (> (length orlist) 1)
  8120. (cons 'or orlist) (car orlist))))
  8121. ;; Return the string and lisp forms of the matcher
  8122. (setq matcher (if todomatcher
  8123. (list 'and tagsmatcher todomatcher)
  8124. tagsmatcher))
  8125. (cons match0 matcher)))
  8126. (defun org-op-to-function (op &optional stringp)
  8127. (setq op
  8128. (cond
  8129. ((equal op "<" ) '(< string< ))
  8130. ((equal op ">" ) '(> org-string> ))
  8131. ((member op '("<=" "=<")) '(<= org-string<= ))
  8132. ((member op '(">=" "=>")) '(>= org-string>= ))
  8133. ((member op '("=" "==")) '(= string= ))
  8134. ((member op '("<>" "!=")) '(org<> org-string<> ))))
  8135. (nth (if stringp 1 0) op))
  8136. (defun org<> (a b) (not (= a b)))
  8137. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  8138. (defun org-string>= (a b) (not (string< a b)))
  8139. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  8140. (defun org-string<> (a b) (not (string= a b)))
  8141. (defun org-match-any-p (re list)
  8142. "Does re match any element of list?"
  8143. (setq list (mapcar (lambda (x) (string-match re x)) list))
  8144. (delq nil list))
  8145. (defvar org-add-colon-after-tag-completion nil) ;; dynamically skoped param
  8146. (defvar org-tags-overlay (org-make-overlay 1 1))
  8147. (org-detach-overlay org-tags-overlay)
  8148. (defun org-get-tags-at (&optional pos)
  8149. "Get a list of all headline tags applicable at POS.
  8150. POS defaults to point. If tags are inherited, the list contains
  8151. the targets in the same sequence as the headlines appear, i.e.
  8152. sthe tags of the current headline come last."
  8153. (interactive)
  8154. (let (tags ltags lastpos parent)
  8155. (save-excursion
  8156. (save-restriction
  8157. (widen)
  8158. (goto-char (or pos (point)))
  8159. (save-match-data
  8160. (condition-case nil
  8161. (progn
  8162. (org-back-to-heading t)
  8163. (while (not (equal lastpos (point)))
  8164. (setq lastpos (point))
  8165. (when (looking-at (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  8166. (setq ltags (org-split-string
  8167. (org-match-string-no-properties 1) ":"))
  8168. (setq tags (append (org-remove-uniherited-tags ltags)
  8169. tags)))
  8170. (or org-use-tag-inheritance (error ""))
  8171. (org-up-heading-all 1)
  8172. (setq parent t)))
  8173. (error nil))))
  8174. tags)))
  8175. (defun org-toggle-tag (tag &optional onoff)
  8176. "Toggle the tag TAG for the current line.
  8177. If ONOFF is `on' or `off', don't toggle but set to this state."
  8178. (unless (org-on-heading-p t) (error "Not on headling"))
  8179. (let (res current)
  8180. (save-excursion
  8181. (beginning-of-line)
  8182. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  8183. (point-at-eol) t)
  8184. (progn
  8185. (setq current (match-string 1))
  8186. (replace-match ""))
  8187. (setq current ""))
  8188. (setq current (nreverse (org-split-string current ":")))
  8189. (cond
  8190. ((eq onoff 'on)
  8191. (setq res t)
  8192. (or (member tag current) (push tag current)))
  8193. ((eq onoff 'off)
  8194. (or (not (member tag current)) (setq current (delete tag current))))
  8195. (t (if (member tag current)
  8196. (setq current (delete tag current))
  8197. (setq res t)
  8198. (push tag current))))
  8199. (end-of-line 1)
  8200. (if current
  8201. (progn
  8202. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  8203. (org-set-tags nil t))
  8204. (delete-horizontal-space))
  8205. (run-hooks 'org-after-tags-change-hook))
  8206. res))
  8207. (defun org-align-tags-here (to-col)
  8208. ;; Assumes that this is a headline
  8209. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  8210. (beginning-of-line 1)
  8211. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  8212. (< pos (match-beginning 2)))
  8213. (progn
  8214. (setq tags-l (- (match-end 2) (match-beginning 2)))
  8215. (goto-char (match-beginning 1))
  8216. (insert " ")
  8217. (delete-region (point) (1+ (match-beginning 2)))
  8218. (setq ncol (max (1+ (current-column))
  8219. (1+ col)
  8220. (if (> to-col 0)
  8221. to-col
  8222. (- (abs to-col) tags-l))))
  8223. (setq p (point))
  8224. (insert (make-string (- ncol (current-column)) ?\ ))
  8225. (setq ncol (current-column))
  8226. (tabify p (point-at-eol))
  8227. (org-move-to-column (min ncol col) t))
  8228. (goto-char pos))))
  8229. (defun org-set-tags (&optional arg just-align)
  8230. "Set the tags for the current headline.
  8231. With prefix ARG, realign all tags in headings in the current buffer."
  8232. (interactive "P")
  8233. (let* ((re (concat "^" outline-regexp))
  8234. (current (org-get-tags-string))
  8235. (col (current-column))
  8236. (org-setting-tags t)
  8237. table current-tags inherited-tags ; computed below when needed
  8238. tags p0 c0 c1 rpl)
  8239. (if arg
  8240. (save-excursion
  8241. (goto-char (point-min))
  8242. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  8243. (while (re-search-forward re nil t)
  8244. (org-set-tags nil t)
  8245. (end-of-line 1)))
  8246. (message "All tags realigned to column %d" org-tags-column))
  8247. (if just-align
  8248. (setq tags current)
  8249. ;; Get a new set of tags from the user
  8250. (save-excursion
  8251. (setq table (or org-tag-alist (org-get-buffer-tags))
  8252. org-last-tags-completion-table table
  8253. current-tags (org-split-string current ":")
  8254. inherited-tags (nreverse
  8255. (nthcdr (length current-tags)
  8256. (nreverse (org-get-tags-at))))
  8257. tags
  8258. (if (or (eq t org-use-fast-tag-selection)
  8259. (and org-use-fast-tag-selection
  8260. (delq nil (mapcar 'cdr table))))
  8261. (org-fast-tag-selection
  8262. current-tags inherited-tags table
  8263. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  8264. (let ((org-add-colon-after-tag-completion t))
  8265. (org-trim
  8266. (org-without-partial-completion
  8267. (completing-read "Tags: " 'org-tags-completion-function
  8268. nil nil current 'org-tags-history)))))))
  8269. (while (string-match "[-+&]+" tags)
  8270. ;; No boolean logic, just a list
  8271. (setq tags (replace-match ":" t t tags))))
  8272. (if (string-match "\\`[\t ]*\\'" tags)
  8273. (setq tags "")
  8274. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  8275. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  8276. ;; Insert new tags at the correct column
  8277. (beginning-of-line 1)
  8278. (cond
  8279. ((and (equal current "") (equal tags "")))
  8280. ((re-search-forward
  8281. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  8282. (point-at-eol) t)
  8283. (if (equal tags "")
  8284. (setq rpl "")
  8285. (goto-char (match-beginning 0))
  8286. (setq c0 (current-column) p0 (point)
  8287. c1 (max (1+ c0) (if (> org-tags-column 0)
  8288. org-tags-column
  8289. (- (- org-tags-column) (length tags))))
  8290. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  8291. (replace-match rpl t t)
  8292. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  8293. tags)
  8294. (t (error "Tags alignment failed")))
  8295. (org-move-to-column col)
  8296. (unless just-align
  8297. (run-hooks 'org-after-tags-change-hook)))))
  8298. (defun org-change-tag-in-region (beg end tag off)
  8299. "Add or remove TAG for each entry in the region.
  8300. This works in the agenda, and also in an org-mode buffer."
  8301. (interactive
  8302. (list (region-beginning) (region-end)
  8303. (let ((org-last-tags-completion-table
  8304. (if (org-mode-p)
  8305. (org-get-buffer-tags)
  8306. (org-global-tags-completion-table))))
  8307. (completing-read
  8308. "Tag: " 'org-tags-completion-function nil nil nil
  8309. 'org-tags-history))
  8310. (progn
  8311. (message "[s]et or [r]emove? ")
  8312. (equal (read-char-exclusive) ?r))))
  8313. (if (fboundp 'deactivate-mark) (deactivate-mark))
  8314. (let ((agendap (equal major-mode 'org-agenda-mode))
  8315. l1 l2 m buf pos newhead (cnt 0))
  8316. (goto-char end)
  8317. (setq l2 (1- (org-current-line)))
  8318. (goto-char beg)
  8319. (setq l1 (org-current-line))
  8320. (loop for l from l1 to l2 do
  8321. (goto-line l)
  8322. (setq m (get-text-property (point) 'org-hd-marker))
  8323. (when (or (and (org-mode-p) (org-on-heading-p))
  8324. (and agendap m))
  8325. (setq buf (if agendap (marker-buffer m) (current-buffer))
  8326. pos (if agendap m (point)))
  8327. (with-current-buffer buf
  8328. (save-excursion
  8329. (save-restriction
  8330. (goto-char pos)
  8331. (setq cnt (1+ cnt))
  8332. (org-toggle-tag tag (if off 'off 'on))
  8333. (setq newhead (org-get-heading)))))
  8334. (and agendap (org-agenda-change-all-lines newhead m))))
  8335. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  8336. (defun org-tags-completion-function (string predicate &optional flag)
  8337. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  8338. (confirm (lambda (x) (stringp (car x)))))
  8339. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  8340. (setq s1 (match-string 1 string)
  8341. s2 (match-string 2 string))
  8342. (setq s1 "" s2 string))
  8343. (cond
  8344. ((eq flag nil)
  8345. ;; try completion
  8346. (setq rtn (try-completion s2 ctable confirm))
  8347. (if (stringp rtn)
  8348. (setq rtn
  8349. (concat s1 s2 (substring rtn (length s2))
  8350. (if (and org-add-colon-after-tag-completion
  8351. (assoc rtn ctable))
  8352. ":" ""))))
  8353. rtn)
  8354. ((eq flag t)
  8355. ;; all-completions
  8356. (all-completions s2 ctable confirm)
  8357. )
  8358. ((eq flag 'lambda)
  8359. ;; exact match?
  8360. (assoc s2 ctable)))
  8361. ))
  8362. (defun org-fast-tag-insert (kwd tags face &optional end)
  8363. "Insert KDW, and the TAGS, the latter with face FACE. Also inser END."
  8364. (insert (format "%-12s" (concat kwd ":"))
  8365. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  8366. (or end "")))
  8367. (defun org-fast-tag-show-exit (flag)
  8368. (save-excursion
  8369. (goto-line 3)
  8370. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  8371. (replace-match ""))
  8372. (when flag
  8373. (end-of-line 1)
  8374. (org-move-to-column (- (window-width) 19) t)
  8375. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  8376. (defun org-set-current-tags-overlay (current prefix)
  8377. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  8378. (if (featurep 'xemacs)
  8379. (org-overlay-display org-tags-overlay (concat prefix s)
  8380. 'secondary-selection)
  8381. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  8382. (org-overlay-display org-tags-overlay (concat prefix s)))))
  8383. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  8384. "Fast tag selection with single keys.
  8385. CURRENT is the current list of tags in the headline, INHERITED is the
  8386. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  8387. possibly with grouping information. TODO-TABLE is a similar table with
  8388. TODO keywords, should these have keys assigned to them.
  8389. If the keys are nil, a-z are automatically assigned.
  8390. Returns the new tags string, or nil to not change the current settings."
  8391. (let* ((fulltable (append table todo-table))
  8392. (maxlen (apply 'max (mapcar
  8393. (lambda (x)
  8394. (if (stringp (car x)) (string-width (car x)) 0))
  8395. fulltable)))
  8396. (buf (current-buffer))
  8397. (expert (eq org-fast-tag-selection-single-key 'expert))
  8398. (buffer-tags nil)
  8399. (fwidth (+ maxlen 3 1 3))
  8400. (ncol (/ (- (window-width) 4) fwidth))
  8401. (i-face 'org-done)
  8402. (c-face 'org-todo)
  8403. tg cnt e c char c1 c2 ntable tbl rtn
  8404. ov-start ov-end ov-prefix
  8405. (exit-after-next org-fast-tag-selection-single-key)
  8406. (done-keywords org-done-keywords)
  8407. groups ingroup)
  8408. (save-excursion
  8409. (beginning-of-line 1)
  8410. (if (looking-at
  8411. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  8412. (setq ov-start (match-beginning 1)
  8413. ov-end (match-end 1)
  8414. ov-prefix "")
  8415. (setq ov-start (1- (point-at-eol))
  8416. ov-end (1+ ov-start))
  8417. (skip-chars-forward "^\n\r")
  8418. (setq ov-prefix
  8419. (concat
  8420. (buffer-substring (1- (point)) (point))
  8421. (if (> (current-column) org-tags-column)
  8422. " "
  8423. (make-string (- org-tags-column (current-column)) ?\ ))))))
  8424. (org-move-overlay org-tags-overlay ov-start ov-end)
  8425. (save-window-excursion
  8426. (if expert
  8427. (set-buffer (get-buffer-create " *Org tags*"))
  8428. (delete-other-windows)
  8429. (split-window-vertically)
  8430. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  8431. (erase-buffer)
  8432. (org-set-local 'org-done-keywords done-keywords)
  8433. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  8434. (org-fast-tag-insert "Current" current c-face "\n\n")
  8435. (org-fast-tag-show-exit exit-after-next)
  8436. (org-set-current-tags-overlay current ov-prefix)
  8437. (setq tbl fulltable char ?a cnt 0)
  8438. (while (setq e (pop tbl))
  8439. (cond
  8440. ((equal e '(:startgroup))
  8441. (push '() groups) (setq ingroup t)
  8442. (when (not (= cnt 0))
  8443. (setq cnt 0)
  8444. (insert "\n"))
  8445. (insert "{ "))
  8446. ((equal e '(:endgroup))
  8447. (setq ingroup nil cnt 0)
  8448. (insert "}\n"))
  8449. (t
  8450. (setq tg (car e) c2 nil)
  8451. (if (cdr e)
  8452. (setq c (cdr e))
  8453. ;; automatically assign a character.
  8454. (setq c1 (string-to-char
  8455. (downcase (substring
  8456. tg (if (= (string-to-char tg) ?@) 1 0)))))
  8457. (if (or (rassoc c1 ntable) (rassoc c1 table))
  8458. (while (or (rassoc char ntable) (rassoc char table))
  8459. (setq char (1+ char)))
  8460. (setq c2 c1))
  8461. (setq c (or c2 char)))
  8462. (if ingroup (push tg (car groups)))
  8463. (setq tg (org-add-props tg nil 'face
  8464. (cond
  8465. ((not (assoc tg table))
  8466. (org-get-todo-face tg))
  8467. ((member tg current) c-face)
  8468. ((member tg inherited) i-face)
  8469. (t nil))))
  8470. (if (and (= cnt 0) (not ingroup)) (insert " "))
  8471. (insert "[" c "] " tg (make-string
  8472. (- fwidth 4 (length tg)) ?\ ))
  8473. (push (cons tg c) ntable)
  8474. (when (= (setq cnt (1+ cnt)) ncol)
  8475. (insert "\n")
  8476. (if ingroup (insert " "))
  8477. (setq cnt 0)))))
  8478. (setq ntable (nreverse ntable))
  8479. (insert "\n")
  8480. (goto-char (point-min))
  8481. (if (and (not expert) (fboundp 'fit-window-to-buffer))
  8482. (fit-window-to-buffer))
  8483. (setq rtn
  8484. (catch 'exit
  8485. (while t
  8486. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  8487. (if groups " [!] no groups" " [!]groups")
  8488. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  8489. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  8490. (cond
  8491. ((= c ?\r) (throw 'exit t))
  8492. ((= c ?!)
  8493. (setq groups (not groups))
  8494. (goto-char (point-min))
  8495. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  8496. ((= c ?\C-c)
  8497. (if (not expert)
  8498. (org-fast-tag-show-exit
  8499. (setq exit-after-next (not exit-after-next)))
  8500. (setq expert nil)
  8501. (delete-other-windows)
  8502. (split-window-vertically)
  8503. (org-switch-to-buffer-other-window " *Org tags*")
  8504. (and (fboundp 'fit-window-to-buffer)
  8505. (fit-window-to-buffer))))
  8506. ((or (= c ?\C-g)
  8507. (and (= c ?q) (not (rassoc c ntable))))
  8508. (org-detach-overlay org-tags-overlay)
  8509. (setq quit-flag t))
  8510. ((= c ?\ )
  8511. (setq current nil)
  8512. (if exit-after-next (setq exit-after-next 'now)))
  8513. ((= c ?\t)
  8514. (condition-case nil
  8515. (setq tg (completing-read
  8516. "Tag: "
  8517. (or buffer-tags
  8518. (with-current-buffer buf
  8519. (org-get-buffer-tags)))))
  8520. (quit (setq tg "")))
  8521. (when (string-match "\\S-" tg)
  8522. (add-to-list 'buffer-tags (list tg))
  8523. (if (member tg current)
  8524. (setq current (delete tg current))
  8525. (push tg current)))
  8526. (if exit-after-next (setq exit-after-next 'now)))
  8527. ((setq e (rassoc c todo-table) tg (car e))
  8528. (with-current-buffer buf
  8529. (save-excursion (org-todo tg)))
  8530. (if exit-after-next (setq exit-after-next 'now)))
  8531. ((setq e (rassoc c ntable) tg (car e))
  8532. (if (member tg current)
  8533. (setq current (delete tg current))
  8534. (loop for g in groups do
  8535. (if (member tg g)
  8536. (mapc (lambda (x)
  8537. (setq current (delete x current)))
  8538. g)))
  8539. (push tg current))
  8540. (if exit-after-next (setq exit-after-next 'now))))
  8541. ;; Create a sorted list
  8542. (setq current
  8543. (sort current
  8544. (lambda (a b)
  8545. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  8546. (if (eq exit-after-next 'now) (throw 'exit t))
  8547. (goto-char (point-min))
  8548. (beginning-of-line 2)
  8549. (delete-region (point) (point-at-eol))
  8550. (org-fast-tag-insert "Current" current c-face)
  8551. (org-set-current-tags-overlay current ov-prefix)
  8552. (while (re-search-forward
  8553. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  8554. (setq tg (match-string 1))
  8555. (add-text-properties
  8556. (match-beginning 1) (match-end 1)
  8557. (list 'face
  8558. (cond
  8559. ((member tg current) c-face)
  8560. ((member tg inherited) i-face)
  8561. (t (get-text-property (match-beginning 1) 'face))))))
  8562. (goto-char (point-min)))))
  8563. (org-detach-overlay org-tags-overlay)
  8564. (if rtn
  8565. (mapconcat 'identity current ":")
  8566. nil))))
  8567. (defun org-get-tags-string ()
  8568. "Get the TAGS string in the current headline."
  8569. (unless (org-on-heading-p t)
  8570. (error "Not on a heading"))
  8571. (save-excursion
  8572. (beginning-of-line 1)
  8573. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  8574. (org-match-string-no-properties 1)
  8575. "")))
  8576. (defun org-get-tags ()
  8577. "Get the list of tags specified in the current headline."
  8578. (org-split-string (org-get-tags-string) ":"))
  8579. (defun org-get-buffer-tags ()
  8580. "Get a table of all tags used in the buffer, for completion."
  8581. (let (tags)
  8582. (save-excursion
  8583. (goto-char (point-min))
  8584. (while (re-search-forward
  8585. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  8586. (when (equal (char-after (point-at-bol 0)) ?*)
  8587. (mapc (lambda (x) (add-to-list 'tags x))
  8588. (org-split-string (org-match-string-no-properties 1) ":")))))
  8589. (mapcar 'list tags)))
  8590. ;;;; Properties
  8591. ;;; Setting and retrieving properties
  8592. (defconst org-special-properties
  8593. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "PRIORITY"
  8594. "TIMESTAMP" "TIMESTAMP_IA")
  8595. "The special properties valid in Org-mode.
  8596. These are properties that are not defined in the property drawer,
  8597. but in some other way.")
  8598. (defconst org-default-properties
  8599. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION"
  8600. "LOCATION" "LOGGING" "COLUMNS")
  8601. "Some properties that are used by Org-mode for various purposes.
  8602. Being in this list makes sure that they are offered for completion.")
  8603. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  8604. "Regular expression matching the first line of a property drawer.")
  8605. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  8606. "Regular expression matching the first line of a property drawer.")
  8607. (defun org-property-action ()
  8608. "Do an action on properties."
  8609. (interactive)
  8610. (let (c)
  8611. (org-at-property-p)
  8612. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  8613. (setq c (read-char-exclusive))
  8614. (cond
  8615. ((equal c ?s)
  8616. (call-interactively 'org-set-property))
  8617. ((equal c ?d)
  8618. (call-interactively 'org-delete-property))
  8619. ((equal c ?D)
  8620. (call-interactively 'org-delete-property-globally))
  8621. ((equal c ?c)
  8622. (call-interactively 'org-compute-property-at-point))
  8623. (t (error "No such property action %c" c)))))
  8624. (defun org-at-property-p ()
  8625. "Is the cursor in a property line?"
  8626. ;; FIXME: Does not check if we are actually in the drawer.
  8627. ;; FIXME: also returns true on any drawers.....
  8628. ;; This is used by C-c C-c for property action.
  8629. (save-excursion
  8630. (beginning-of-line 1)
  8631. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  8632. (defun org-get-property-block (&optional beg end force)
  8633. "Return the (beg . end) range of the body of the property drawer.
  8634. BEG and END can be beginning and end of subtree, if not given
  8635. they will be found.
  8636. If the drawer does not exist and FORCE is non-nil, create the drawer."
  8637. (catch 'exit
  8638. (save-excursion
  8639. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  8640. (end (or end (progn (outline-next-heading) (point)))))
  8641. (goto-char beg)
  8642. (if (re-search-forward org-property-start-re end t)
  8643. (setq beg (1+ (match-end 0)))
  8644. (if force
  8645. (save-excursion
  8646. (org-insert-property-drawer)
  8647. (setq end (progn (outline-next-heading) (point))))
  8648. (throw 'exit nil))
  8649. (goto-char beg)
  8650. (if (re-search-forward org-property-start-re end t)
  8651. (setq beg (1+ (match-end 0)))))
  8652. (if (re-search-forward org-property-end-re end t)
  8653. (setq end (match-beginning 0))
  8654. (or force (throw 'exit nil))
  8655. (goto-char beg)
  8656. (setq end beg)
  8657. (org-indent-line-function)
  8658. (insert ":END:\n"))
  8659. (cons beg end)))))
  8660. (defun org-entry-properties (&optional pom which)
  8661. "Get all properties of the entry at point-or-marker POM.
  8662. This includes the TODO keyword, the tags, time strings for deadline,
  8663. scheduled, and clocking, and any additional properties defined in the
  8664. entry. The return value is an alist, keys may occur multiple times
  8665. if the property key was used several times.
  8666. POM may also be nil, in which case the current entry is used.
  8667. If WHICH is nil or `all', get all properties. If WHICH is
  8668. `special' or `standard', only get that subclass."
  8669. (setq which (or which 'all))
  8670. (org-with-point-at pom
  8671. (let ((clockstr (substring org-clock-string 0 -1))
  8672. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  8673. beg end range props sum-props key value string clocksum)
  8674. (save-excursion
  8675. (when (condition-case nil (org-back-to-heading t) (error nil))
  8676. (setq beg (point))
  8677. (setq sum-props (get-text-property (point) 'org-summaries))
  8678. (setq clocksum (get-text-property (point) :org-clock-minutes))
  8679. (outline-next-heading)
  8680. (setq end (point))
  8681. (when (memq which '(all special))
  8682. ;; Get the special properties, like TODO and tags
  8683. (goto-char beg)
  8684. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  8685. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  8686. (when (looking-at org-priority-regexp)
  8687. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  8688. (when (and (setq value (org-get-tags-string))
  8689. (string-match "\\S-" value))
  8690. (push (cons "TAGS" value) props))
  8691. (when (setq value (org-get-tags-at))
  8692. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  8693. props))
  8694. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  8695. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  8696. string (if (equal key clockstr)
  8697. (org-no-properties
  8698. (org-trim
  8699. (buffer-substring
  8700. (match-beginning 3) (goto-char (point-at-eol)))))
  8701. (substring (org-match-string-no-properties 3) 1 -1)))
  8702. (unless key
  8703. (if (= (char-after (match-beginning 3)) ?\[)
  8704. (setq key "TIMESTAMP_IA")
  8705. (setq key "TIMESTAMP")))
  8706. (when (or (equal key clockstr) (not (assoc key props)))
  8707. (push (cons key string) props)))
  8708. )
  8709. (when (memq which '(all standard))
  8710. ;; Get the standard properties, like :PORP: ...
  8711. (setq range (org-get-property-block beg end))
  8712. (when range
  8713. (goto-char (car range))
  8714. (while (re-search-forward
  8715. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  8716. (cdr range) t)
  8717. (setq key (org-match-string-no-properties 1)
  8718. value (org-trim (or (org-match-string-no-properties 2) "")))
  8719. (unless (member key excluded)
  8720. (push (cons key (or value "")) props)))))
  8721. (if clocksum
  8722. (push (cons "CLOCKSUM"
  8723. (org-columns-number-to-string (/ (float clocksum) 60.)
  8724. 'add_times))
  8725. props))
  8726. (append sum-props (nreverse props)))))))
  8727. (defun org-entry-get (pom property &optional inherit)
  8728. "Get value of PROPERTY for entry at point-or-marker POM.
  8729. If INHERIT is non-nil and the entry does not have the property,
  8730. then also check higher levels of the hierarchy.
  8731. If INHERIT is the symbol `selective', use inheritance only if the setting
  8732. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  8733. If the property is present but empty, the return value is the empty string.
  8734. If the property is not present at all, nil is returned."
  8735. (org-with-point-at pom
  8736. (if (and inherit (if (eq inherit 'selective)
  8737. (org-property-inherit-p property)
  8738. t))
  8739. (org-entry-get-with-inheritance property)
  8740. (if (member property org-special-properties)
  8741. ;; We need a special property. Use brute force, get all properties.
  8742. (cdr (assoc property (org-entry-properties nil 'special)))
  8743. (let ((range (org-get-property-block)))
  8744. (if (and range
  8745. (goto-char (car range))
  8746. (re-search-forward
  8747. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)?")
  8748. (cdr range) t))
  8749. ;; Found the property, return it.
  8750. (if (match-end 1)
  8751. (org-match-string-no-properties 1)
  8752. "")))))))
  8753. (defun org-property-or-variable-value (var &optional inherit)
  8754. "Check if there is a property fixing the value of VAR.
  8755. If yes, return this value. If not, return the current value of the variable."
  8756. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  8757. (if (and prop (stringp prop) (string-match "\\S-" prop))
  8758. (read prop)
  8759. (symbol-value var))))
  8760. (defun org-entry-delete (pom property)
  8761. "Delete the property PROPERTY from entry at point-or-marker POM."
  8762. (org-with-point-at pom
  8763. (if (member property org-special-properties)
  8764. nil ; cannot delete these properties.
  8765. (let ((range (org-get-property-block)))
  8766. (if (and range
  8767. (goto-char (car range))
  8768. (re-search-forward
  8769. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)")
  8770. (cdr range) t))
  8771. (progn
  8772. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  8773. t)
  8774. nil)))))
  8775. ;; Multi-values properties are properties that contain multiple values
  8776. ;; These values are assumed to be single words, separated by whitespace.
  8777. (defun org-entry-add-to-multivalued-property (pom property value)
  8778. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  8779. (let* ((old (org-entry-get pom property))
  8780. (values (and old (org-split-string old "[ \t]"))))
  8781. (unless (member value values)
  8782. (setq values (cons value values))
  8783. (org-entry-put pom property
  8784. (mapconcat 'identity values " ")))))
  8785. (defun org-entry-remove-from-multivalued-property (pom property value)
  8786. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  8787. (let* ((old (org-entry-get pom property))
  8788. (values (and old (org-split-string old "[ \t]"))))
  8789. (when (member value values)
  8790. (setq values (delete value values))
  8791. (org-entry-put pom property
  8792. (mapconcat 'identity values " ")))))
  8793. (defun org-entry-member-in-multivalued-property (pom property value)
  8794. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  8795. (let* ((old (org-entry-get pom property))
  8796. (values (and old (org-split-string old "[ \t]"))))
  8797. (member value values)))
  8798. (defvar org-entry-property-inherited-from (make-marker))
  8799. (defun org-entry-get-with-inheritance (property)
  8800. "Get entry property, and search higher levels if not present."
  8801. (let (tmp)
  8802. (save-excursion
  8803. (save-restriction
  8804. (widen)
  8805. (catch 'ex
  8806. (while t
  8807. (when (setq tmp (org-entry-get nil property))
  8808. (org-back-to-heading t)
  8809. (move-marker org-entry-property-inherited-from (point))
  8810. (throw 'ex tmp))
  8811. (or (org-up-heading-safe) (throw 'ex nil)))))
  8812. (or tmp (cdr (assoc property org-local-properties))
  8813. (cdr (assoc property org-global-properties))))))
  8814. (defun org-entry-put (pom property value)
  8815. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  8816. (org-with-point-at pom
  8817. (org-back-to-heading t)
  8818. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  8819. range)
  8820. (cond
  8821. ((equal property "TODO")
  8822. (when (and (stringp value) (string-match "\\S-" value)
  8823. (not (member value org-todo-keywords-1)))
  8824. (error "\"%s\" is not a valid TODO state" value))
  8825. (if (or (not value)
  8826. (not (string-match "\\S-" value)))
  8827. (setq value 'none))
  8828. (org-todo value)
  8829. (org-set-tags nil 'align))
  8830. ((equal property "PRIORITY")
  8831. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  8832. (string-to-char value) ?\ ))
  8833. (org-set-tags nil 'align))
  8834. ((equal property "SCHEDULED")
  8835. (if (re-search-forward org-scheduled-time-regexp end t)
  8836. (cond
  8837. ((eq value 'earlier) (org-timestamp-change -1 'day))
  8838. ((eq value 'later) (org-timestamp-change 1 'day))
  8839. (t (call-interactively 'org-schedule)))
  8840. (call-interactively 'org-schedule)))
  8841. ((equal property "DEADLINE")
  8842. (if (re-search-forward org-deadline-time-regexp end t)
  8843. (cond
  8844. ((eq value 'earlier) (org-timestamp-change -1 'day))
  8845. ((eq value 'later) (org-timestamp-change 1 'day))
  8846. (t (call-interactively 'org-deadline)))
  8847. (call-interactively 'org-deadline)))
  8848. ((member property org-special-properties)
  8849. (error "The %s property can not yet be set with `org-entry-put'"
  8850. property))
  8851. (t ; a non-special property
  8852. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  8853. (setq range (org-get-property-block beg end 'force))
  8854. (goto-char (car range))
  8855. (if (re-search-forward
  8856. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  8857. (progn
  8858. (delete-region (match-beginning 1) (match-end 1))
  8859. (goto-char (match-beginning 1)))
  8860. (goto-char (cdr range))
  8861. (insert "\n")
  8862. (backward-char 1)
  8863. (org-indent-line-function)
  8864. (insert ":" property ":"))
  8865. (and value (insert " " value))
  8866. (org-indent-line-function)))))))
  8867. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  8868. "Get all property keys in the current buffer.
  8869. With INCLUDE-SPECIALS, also list the special properties that relect things
  8870. like tags and TODO state.
  8871. With INCLUDE-DEFAULTS, also include properties that has special meaning
  8872. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  8873. With INCLUDE-COLUMNS, also include property names given in COLUMN
  8874. formats in the current buffer."
  8875. (let (rtn range cfmt cols s p)
  8876. (save-excursion
  8877. (save-restriction
  8878. (widen)
  8879. (goto-char (point-min))
  8880. (while (re-search-forward org-property-start-re nil t)
  8881. (setq range (org-get-property-block))
  8882. (goto-char (car range))
  8883. (while (re-search-forward
  8884. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  8885. (cdr range) t)
  8886. (add-to-list 'rtn (org-match-string-no-properties 1)))
  8887. (outline-next-heading))))
  8888. (when include-specials
  8889. (setq rtn (append org-special-properties rtn)))
  8890. (when include-defaults
  8891. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties))
  8892. (when include-columns
  8893. (save-excursion
  8894. (save-restriction
  8895. (widen)
  8896. (goto-char (point-min))
  8897. (while (re-search-forward
  8898. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  8899. nil t)
  8900. (setq cfmt (match-string 2) s 0)
  8901. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  8902. cfmt s)
  8903. (setq s (match-end 0)
  8904. p (match-string 1 cfmt))
  8905. (unless (or (equal p "ITEM")
  8906. (member p org-special-properties))
  8907. (add-to-list 'rtn (match-string 1 cfmt))))))))
  8908. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  8909. (defun org-property-values (key)
  8910. "Return a list of all values of property KEY."
  8911. (save-excursion
  8912. (save-restriction
  8913. (widen)
  8914. (goto-char (point-min))
  8915. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  8916. values)
  8917. (while (re-search-forward re nil t)
  8918. (add-to-list 'values (org-trim (match-string 1))))
  8919. (delete "" values)))))
  8920. (defun org-insert-property-drawer ()
  8921. "Insert a property drawer into the current entry."
  8922. (interactive)
  8923. (org-back-to-heading t)
  8924. (looking-at outline-regexp)
  8925. (let ((indent (- (match-end 0)(match-beginning 0)))
  8926. (beg (point))
  8927. (re (concat "^[ \t]*" org-keyword-time-regexp))
  8928. end hiddenp)
  8929. (outline-next-heading)
  8930. (setq end (point))
  8931. (goto-char beg)
  8932. (while (re-search-forward re end t))
  8933. (setq hiddenp (org-invisible-p))
  8934. (end-of-line 1)
  8935. (and (equal (char-after) ?\n) (forward-char 1))
  8936. (while (looking-at "^[ \t]*\\(:CLOCK:\\|CLOCK\\|:END:\\)")
  8937. (beginning-of-line 2))
  8938. (org-skip-over-state-notes)
  8939. (skip-chars-backward " \t\n\r")
  8940. (if (eq (char-before) ?*) (forward-char 1))
  8941. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  8942. (beginning-of-line 0)
  8943. (org-indent-to-column indent)
  8944. (beginning-of-line 2)
  8945. (org-indent-to-column indent)
  8946. (beginning-of-line 0)
  8947. (if hiddenp
  8948. (save-excursion
  8949. (org-back-to-heading t)
  8950. (hide-entry))
  8951. (org-flag-drawer t))))
  8952. (defun org-set-property (property value)
  8953. "In the current entry, set PROPERTY to VALUE.
  8954. When called interactively, this will prompt for a property name, offering
  8955. completion on existing and default properties. And then it will prompt
  8956. for a value, offering competion either on allowed values (via an inherited
  8957. xxx_ALL property) or on existing values in other instances of this property
  8958. in the current file."
  8959. (interactive
  8960. (let* ((prop (completing-read
  8961. "Property: " (mapcar 'list (org-buffer-property-keys nil t t))))
  8962. (cur (org-entry-get nil prop))
  8963. (allowed (org-property-get-allowed-values nil prop 'table))
  8964. (existing (mapcar 'list (org-property-values prop)))
  8965. (val (if allowed
  8966. (completing-read "Value: " allowed nil 'req-match)
  8967. (completing-read
  8968. (concat "Value" (if (and cur (string-match "\\S-" cur))
  8969. (concat "[" cur "]") "")
  8970. ": ")
  8971. existing nil nil "" nil cur))))
  8972. (list prop (if (equal val "") cur val))))
  8973. (unless (equal (org-entry-get nil property) value)
  8974. (org-entry-put nil property value)))
  8975. (defun org-delete-property (property)
  8976. "In the current entry, delete PROPERTY."
  8977. (interactive
  8978. (let* ((prop (completing-read
  8979. "Property: " (org-entry-properties nil 'standard))))
  8980. (list prop)))
  8981. (message "Property %s %s" property
  8982. (if (org-entry-delete nil property)
  8983. "deleted"
  8984. "was not present in the entry")))
  8985. (defun org-delete-property-globally (property)
  8986. "Remove PROPERTY globally, from all entries."
  8987. (interactive
  8988. (let* ((prop (completing-read
  8989. "Globally remove property: "
  8990. (mapcar 'list (org-buffer-property-keys)))))
  8991. (list prop)))
  8992. (save-excursion
  8993. (save-restriction
  8994. (widen)
  8995. (goto-char (point-min))
  8996. (let ((cnt 0))
  8997. (while (re-search-forward
  8998. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  8999. nil t)
  9000. (setq cnt (1+ cnt))
  9001. (replace-match ""))
  9002. (message "Property \"%s\" removed from %d entries" property cnt)))))
  9003. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  9004. (defun org-compute-property-at-point ()
  9005. "Compute the property at point.
  9006. This looks for an enclosing column format, extracts the operator and
  9007. then applies it to the proerty in the column format's scope."
  9008. (interactive)
  9009. (unless (org-at-property-p)
  9010. (error "Not at a property"))
  9011. (let ((prop (org-match-string-no-properties 2)))
  9012. (org-columns-get-format-and-top-level)
  9013. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  9014. (error "No operator defined for property %s" prop))
  9015. (org-columns-compute prop)))
  9016. (defun org-property-get-allowed-values (pom property &optional table)
  9017. "Get allowed values for the property PROPERTY.
  9018. When TABLE is non-nil, return an alist that can directly be used for
  9019. completion."
  9020. (let (vals)
  9021. (cond
  9022. ((equal property "TODO")
  9023. (setq vals (org-with-point-at pom
  9024. (append org-todo-keywords-1 '("")))))
  9025. ((equal property "PRIORITY")
  9026. (let ((n org-lowest-priority))
  9027. (while (>= n org-highest-priority)
  9028. (push (char-to-string n) vals)
  9029. (setq n (1- n)))))
  9030. ((member property org-special-properties))
  9031. (t
  9032. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  9033. (when (and vals (string-match "\\S-" vals))
  9034. (setq vals (car (read-from-string (concat "(" vals ")"))))
  9035. (setq vals (mapcar (lambda (x)
  9036. (cond ((stringp x) x)
  9037. ((numberp x) (number-to-string x))
  9038. ((symbolp x) (symbol-name x))
  9039. (t "???")))
  9040. vals)))))
  9041. (if table (mapcar 'list vals) vals)))
  9042. (defun org-property-previous-allowed-value (&optional previous)
  9043. "Switch to the next allowed value for this property."
  9044. (interactive)
  9045. (org-property-next-allowed-value t))
  9046. (defun org-property-next-allowed-value (&optional previous)
  9047. "Switch to the next allowed value for this property."
  9048. (interactive)
  9049. (unless (org-at-property-p)
  9050. (error "Not at a property"))
  9051. (let* ((key (match-string 2))
  9052. (value (match-string 3))
  9053. (allowed (or (org-property-get-allowed-values (point) key)
  9054. (and (member value '("[ ]" "[-]" "[X]"))
  9055. '("[ ]" "[X]"))))
  9056. nval)
  9057. (unless allowed
  9058. (error "Allowed values for this property have not been defined"))
  9059. (if previous (setq allowed (reverse allowed)))
  9060. (if (member value allowed)
  9061. (setq nval (car (cdr (member value allowed)))))
  9062. (setq nval (or nval (car allowed)))
  9063. (if (equal nval value)
  9064. (error "Only one allowed value for this property"))
  9065. (org-at-property-p)
  9066. (replace-match (concat " :" key ": " nval) t t)
  9067. (org-indent-line-function)
  9068. (beginning-of-line 1)
  9069. (skip-chars-forward " \t")))
  9070. (defun org-find-entry-with-id (ident)
  9071. "Locate the entry that contains the ID property with exact value IDENT.
  9072. IDENT can be a string, a symbol or a number, this function will search for
  9073. the string representation of it.
  9074. Return the position where this entry starts, or nil if there is no such entry."
  9075. (let ((id (cond
  9076. ((stringp ident) ident)
  9077. ((symbol-name ident) (symbol-name ident))
  9078. ((numberp ident) (number-to-string ident))
  9079. (t (error "IDENT %s must be a string, symbol or number" ident))))
  9080. (case-fold-search nil))
  9081. (save-excursion
  9082. (save-restriction
  9083. (widen)
  9084. (goto-char (point-min))
  9085. (when (re-search-forward
  9086. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  9087. nil t)
  9088. (org-back-to-heading)
  9089. (point))))))
  9090. ;;;; Timestamps
  9091. (defvar org-last-changed-timestamp nil)
  9092. (defvar org-time-was-given) ; dynamically scoped parameter
  9093. (defvar org-end-time-was-given) ; dynamically scoped parameter
  9094. (defvar org-ts-what) ; dynamically scoped parameter
  9095. (defun org-time-stamp (arg)
  9096. "Prompt for a date/time and insert a time stamp.
  9097. If the user specifies a time like HH:MM, or if this command is called
  9098. with a prefix argument, the time stamp will contain date and time.
  9099. Otherwise, only the date will be included. All parts of a date not
  9100. specified by the user will be filled in from the current date/time.
  9101. So if you press just return without typing anything, the time stamp
  9102. will represent the current date/time. If there is already a timestamp
  9103. at the cursor, it will be modified."
  9104. (interactive "P")
  9105. (let* ((ts nil)
  9106. (default-time
  9107. ;; Default time is either today, or, when entering a range,
  9108. ;; the range start.
  9109. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  9110. (save-excursion
  9111. (re-search-backward
  9112. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  9113. (- (point) 20) t)))
  9114. (apply 'encode-time (org-parse-time-string (match-string 1)))
  9115. (current-time)))
  9116. (default-input (and ts (org-get-compact-tod ts)))
  9117. org-time-was-given org-end-time-was-given time)
  9118. (cond
  9119. ((and (org-at-timestamp-p)
  9120. (eq last-command 'org-time-stamp)
  9121. (eq this-command 'org-time-stamp))
  9122. (insert "--")
  9123. (setq time (let ((this-command this-command))
  9124. (org-read-date arg 'totime nil nil default-time default-input)))
  9125. (org-insert-time-stamp time (or org-time-was-given arg)))
  9126. ((org-at-timestamp-p)
  9127. (setq time (let ((this-command this-command))
  9128. (org-read-date arg 'totime nil nil default-time default-input)))
  9129. (when (org-at-timestamp-p) ; just to get the match data
  9130. (replace-match "")
  9131. (setq org-last-changed-timestamp
  9132. (org-insert-time-stamp
  9133. time (or org-time-was-given arg)
  9134. nil nil nil (list org-end-time-was-given))))
  9135. (message "Timestamp updated"))
  9136. (t
  9137. (setq time (let ((this-command this-command))
  9138. (org-read-date arg 'totime nil nil default-time default-input)))
  9139. (org-insert-time-stamp time (or org-time-was-given arg)
  9140. nil nil nil (list org-end-time-was-given))))))
  9141. ;; FIXME: can we use this for something else, like computing time differences?
  9142. (defun org-get-compact-tod (s)
  9143. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  9144. (let* ((t1 (match-string 1 s))
  9145. (h1 (string-to-number (match-string 2 s)))
  9146. (m1 (string-to-number (match-string 3 s)))
  9147. (t2 (and (match-end 4) (match-string 5 s)))
  9148. (h2 (and t2 (string-to-number (match-string 6 s))))
  9149. (m2 (and t2 (string-to-number (match-string 7 s))))
  9150. dh dm)
  9151. (if (not t2)
  9152. t1
  9153. (setq dh (- h2 h1) dm (- m2 m1))
  9154. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  9155. (concat t1 "+" (number-to-string dh)
  9156. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  9157. (defun org-time-stamp-inactive (&optional arg)
  9158. "Insert an inactive time stamp.
  9159. An inactive time stamp is enclosed in square brackets instead of angle
  9160. brackets. It is inactive in the sense that it does not trigger agenda entries,
  9161. does not link to the calendar and cannot be changed with the S-cursor keys.
  9162. So these are more for recording a certain time/date."
  9163. (interactive "P")
  9164. (let (org-time-was-given org-end-time-was-given time)
  9165. (setq time (org-read-date arg 'totime))
  9166. (org-insert-time-stamp time (or org-time-was-given arg) 'inactive
  9167. nil nil (list org-end-time-was-given))))
  9168. (defvar org-date-ovl (org-make-overlay 1 1))
  9169. (org-overlay-put org-date-ovl 'face 'org-warning)
  9170. (org-detach-overlay org-date-ovl)
  9171. (defvar org-ans1) ; dynamically scoped parameter
  9172. (defvar org-ans2) ; dynamically scoped parameter
  9173. (defvar org-plain-time-of-day-regexp) ; defined below
  9174. (defvar org-read-date-overlay nil)
  9175. (defvar org-dcst nil) ; dynamically scoped
  9176. (defun org-read-date (&optional with-time to-time from-string prompt
  9177. default-time default-input)
  9178. "Read a date, possibly a time, and make things smooth for the user.
  9179. The prompt will suggest to enter an ISO date, but you can also enter anything
  9180. which will at least partially be understood by `parse-time-string'.
  9181. Unrecognized parts of the date will default to the current day, month, year,
  9182. hour and minute. If this command is called to replace a timestamp at point,
  9183. of to enter the second timestamp of a range, the default time is taken from the
  9184. existing stamp. For example,
  9185. 3-2-5 --> 2003-02-05
  9186. feb 15 --> currentyear-02-15
  9187. sep 12 9 --> 2009-09-12
  9188. 12:45 --> today 12:45
  9189. 22 sept 0:34 --> currentyear-09-22 0:34
  9190. 12 --> currentyear-currentmonth-12
  9191. Fri --> nearest Friday (today or later)
  9192. etc.
  9193. Furthermore you can specify a relative date by giving, as the *first* thing
  9194. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  9195. change in days weeks, months, years.
  9196. With a single plus or minus, the date is relative to today. With a double
  9197. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  9198. +4d --> four days from today
  9199. +4 --> same as above
  9200. +2w --> two weeks from today
  9201. ++5 --> five days from default date
  9202. The function understands only English month and weekday abbreviations,
  9203. but this can be configured with the variables `parse-time-months' and
  9204. `parse-time-weekdays'.
  9205. While prompting, a calendar is popped up - you can also select the
  9206. date with the mouse (button 1). The calendar shows a period of three
  9207. months. To scroll it to other months, use the keys `>' and `<'.
  9208. If you don't like the calendar, turn it off with
  9209. \(setq org-read-date-popup-calendar nil)
  9210. With optional argument TO-TIME, the date will immediately be converted
  9211. to an internal time.
  9212. With an optional argument WITH-TIME, the prompt will suggest to also
  9213. insert a time. Note that when WITH-TIME is not set, you can still
  9214. enter a time, and this function will inform the calling routine about
  9215. this change. The calling routine may then choose to change the format
  9216. used to insert the time stamp into the buffer to include the time.
  9217. With optional argument FROM-STRING, read from this string instead from
  9218. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  9219. the time/date that is used for everything that is not specified by the
  9220. user."
  9221. (require 'parse-time)
  9222. (let* ((org-time-stamp-rounding-minutes
  9223. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  9224. (org-dcst org-display-custom-times)
  9225. (ct (org-current-time))
  9226. (def (or default-time ct))
  9227. (defdecode (decode-time def))
  9228. (dummy (progn
  9229. (when (< (nth 2 defdecode) org-extend-today-until)
  9230. (setcar (nthcdr 2 defdecode) -1)
  9231. (setcar (nthcdr 1 defdecode) 59)
  9232. (setq def (apply 'encode-time defdecode)
  9233. defdecode (decode-time def)))))
  9234. (calendar-move-hook nil)
  9235. (calendar-view-diary-initially-flag nil)
  9236. (view-diary-entries-initially nil)
  9237. (calendar-view-holidays-initially-flag nil)
  9238. (view-calendar-holidays-initially nil)
  9239. (timestr (format-time-string
  9240. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  9241. (prompt (concat (if prompt (concat prompt " ") "")
  9242. (format "Date+time [%s]: " timestr)))
  9243. ans (org-ans0 "") org-ans1 org-ans2 final)
  9244. (cond
  9245. (from-string (setq ans from-string))
  9246. (org-read-date-popup-calendar
  9247. (save-excursion
  9248. (save-window-excursion
  9249. (calendar)
  9250. (calendar-forward-day (- (time-to-days def)
  9251. (calendar-absolute-from-gregorian
  9252. (calendar-current-date))))
  9253. (org-eval-in-calendar nil t)
  9254. (let* ((old-map (current-local-map))
  9255. (map (copy-keymap calendar-mode-map))
  9256. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  9257. (org-defkey map (kbd "RET") 'org-calendar-select)
  9258. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  9259. 'org-calendar-select-mouse)
  9260. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  9261. 'org-calendar-select-mouse)
  9262. (org-defkey minibuffer-local-map [(meta shift left)]
  9263. (lambda () (interactive)
  9264. (org-eval-in-calendar '(calendar-backward-month 1))))
  9265. (org-defkey minibuffer-local-map [(meta shift right)]
  9266. (lambda () (interactive)
  9267. (org-eval-in-calendar '(calendar-forward-month 1))))
  9268. (org-defkey minibuffer-local-map [(meta shift up)]
  9269. (lambda () (interactive)
  9270. (org-eval-in-calendar '(calendar-backward-year 1))))
  9271. (org-defkey minibuffer-local-map [(meta shift down)]
  9272. (lambda () (interactive)
  9273. (org-eval-in-calendar '(calendar-forward-year 1))))
  9274. (org-defkey minibuffer-local-map [(shift up)]
  9275. (lambda () (interactive)
  9276. (org-eval-in-calendar '(calendar-backward-week 1))))
  9277. (org-defkey minibuffer-local-map [(shift down)]
  9278. (lambda () (interactive)
  9279. (org-eval-in-calendar '(calendar-forward-week 1))))
  9280. (org-defkey minibuffer-local-map [(shift left)]
  9281. (lambda () (interactive)
  9282. (org-eval-in-calendar '(calendar-backward-day 1))))
  9283. (org-defkey minibuffer-local-map [(shift right)]
  9284. (lambda () (interactive)
  9285. (org-eval-in-calendar '(calendar-forward-day 1))))
  9286. (org-defkey minibuffer-local-map ">"
  9287. (lambda () (interactive)
  9288. (org-eval-in-calendar '(scroll-calendar-left 1))))
  9289. (org-defkey minibuffer-local-map "<"
  9290. (lambda () (interactive)
  9291. (org-eval-in-calendar '(scroll-calendar-right 1))))
  9292. (unwind-protect
  9293. (progn
  9294. (use-local-map map)
  9295. (add-hook 'post-command-hook 'org-read-date-display)
  9296. (setq org-ans0 (read-string prompt default-input nil nil))
  9297. ;; org-ans0: from prompt
  9298. ;; org-ans1: from mouse click
  9299. ;; org-ans2: from calendar motion
  9300. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  9301. (remove-hook 'post-command-hook 'org-read-date-display)
  9302. (use-local-map old-map)
  9303. (when org-read-date-overlay
  9304. (org-delete-overlay org-read-date-overlay)
  9305. (setq org-read-date-overlay nil)))))))
  9306. (t ; Naked prompt only
  9307. (unwind-protect
  9308. (setq ans (read-string prompt default-input nil timestr))
  9309. (when org-read-date-overlay
  9310. (org-delete-overlay org-read-date-overlay)
  9311. (setq org-read-date-overlay nil)))))
  9312. (setq final (org-read-date-analyze ans def defdecode))
  9313. (if to-time
  9314. (apply 'encode-time final)
  9315. (if (and (boundp 'org-time-was-given) org-time-was-given)
  9316. (format "%04d-%02d-%02d %02d:%02d"
  9317. (nth 5 final) (nth 4 final) (nth 3 final)
  9318. (nth 2 final) (nth 1 final))
  9319. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  9320. (defvar def)
  9321. (defvar defdecode)
  9322. (defvar with-time)
  9323. (defun org-read-date-display ()
  9324. "Display the currrent date prompt interpretation in the minibuffer."
  9325. (when org-read-date-display-live
  9326. (when org-read-date-overlay
  9327. (org-delete-overlay org-read-date-overlay))
  9328. (let ((p (point)))
  9329. (end-of-line 1)
  9330. (while (not (equal (buffer-substring
  9331. (max (point-min) (- (point) 4)) (point))
  9332. " "))
  9333. (insert " "))
  9334. (goto-char p))
  9335. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  9336. " " (or org-ans1 org-ans2)))
  9337. (org-end-time-was-given nil)
  9338. (f (org-read-date-analyze ans def defdecode))
  9339. (fmts (if org-dcst
  9340. org-time-stamp-custom-formats
  9341. org-time-stamp-formats))
  9342. (fmt (if (or with-time
  9343. (and (boundp 'org-time-was-given) org-time-was-given))
  9344. (cdr fmts)
  9345. (car fmts)))
  9346. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  9347. (when (and org-end-time-was-given
  9348. (string-match org-plain-time-of-day-regexp txt))
  9349. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  9350. org-end-time-was-given
  9351. (substring txt (match-end 0)))))
  9352. (setq org-read-date-overlay
  9353. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  9354. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  9355. (defun org-read-date-analyze (ans def defdecode)
  9356. "Analyze the combined answer of the date prompt."
  9357. ;; FIXME: cleanup and comment
  9358. (let (delta deltan deltaw deltadef year month day
  9359. hour minute second wday pm h2 m2 tl wday1
  9360. iso-year iso-weekday iso-week iso-year iso-date)
  9361. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  9362. (setq ans (replace-match "" t t ans)
  9363. deltan (car delta)
  9364. deltaw (nth 1 delta)
  9365. deltadef (nth 2 delta)))
  9366. ;; Check if there is an iso week date in there
  9367. ;; If yes, sore the info and ostpone interpreting it until the rest
  9368. ;; of the parsing is done
  9369. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  9370. (setq iso-year (if (match-end 1) (org-small-year-to-year (string-to-number (match-string 1 ans))))
  9371. iso-weekday (if (match-end 3) (string-to-number (match-string 3 ans)))
  9372. iso-week (string-to-number (match-string 2 ans)))
  9373. (setq ans (replace-match "" t t ans)))
  9374. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  9375. (when (string-match
  9376. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  9377. (setq year (if (match-end 2)
  9378. (string-to-number (match-string 2 ans))
  9379. (string-to-number (format-time-string "%Y")))
  9380. month (string-to-number (match-string 3 ans))
  9381. day (string-to-number (match-string 4 ans)))
  9382. (if (< year 100) (setq year (+ 2000 year)))
  9383. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  9384. t nil ans)))
  9385. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  9386. ;; If there is a time with am/pm, and *no* time without it, we convert
  9387. ;; so that matching will be successful.
  9388. (loop for i from 1 to 2 do ; twice, for end time as well
  9389. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  9390. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  9391. (setq hour (string-to-number (match-string 1 ans))
  9392. minute (if (match-end 3)
  9393. (string-to-number (match-string 3 ans))
  9394. 0)
  9395. pm (equal ?p
  9396. (string-to-char (downcase (match-string 4 ans)))))
  9397. (if (and (= hour 12) (not pm))
  9398. (setq hour 0)
  9399. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  9400. (setq ans (replace-match (format "%02d:%02d" hour minute)
  9401. t t ans))))
  9402. ;; Check if a time range is given as a duration
  9403. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  9404. (setq hour (string-to-number (match-string 1 ans))
  9405. h2 (+ hour (string-to-number (match-string 3 ans)))
  9406. minute (string-to-number (match-string 2 ans))
  9407. m2 (+ minute (if (match-end 5) (string-to-number
  9408. (match-string 5 ans))0)))
  9409. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  9410. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  9411. t t ans)))
  9412. ;; Check if there is a time range
  9413. (when (boundp 'org-end-time-was-given)
  9414. (setq org-time-was-given nil)
  9415. (when (and (string-match org-plain-time-of-day-regexp ans)
  9416. (match-end 8))
  9417. (setq org-end-time-was-given (match-string 8 ans))
  9418. (setq ans (concat (substring ans 0 (match-beginning 7))
  9419. (substring ans (match-end 7))))))
  9420. (setq tl (parse-time-string ans)
  9421. day (or (nth 3 tl) (nth 3 defdecode))
  9422. month (or (nth 4 tl)
  9423. (if (and org-read-date-prefer-future
  9424. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  9425. (1+ (nth 4 defdecode))
  9426. (nth 4 defdecode)))
  9427. year (or (nth 5 tl)
  9428. (if (and org-read-date-prefer-future
  9429. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  9430. (1+ (nth 5 defdecode))
  9431. (nth 5 defdecode)))
  9432. hour (or (nth 2 tl) (nth 2 defdecode))
  9433. minute (or (nth 1 tl) (nth 1 defdecode))
  9434. second (or (nth 0 tl) 0)
  9435. wday (nth 6 tl))
  9436. ;; Special date definitions below
  9437. (cond
  9438. (iso-week
  9439. ;; There was an iso week
  9440. (setq year (or iso-year year)
  9441. day (or iso-weekday wday 1)
  9442. wday nil ; to make sure that the trigger below does not match
  9443. iso-date (calendar-gregorian-from-absolute
  9444. (calendar-absolute-from-iso
  9445. (list iso-week day year))))
  9446. ; FIXME: Should we also push ISO weeks into the future?
  9447. ; (when (and org-read-date-prefer-future
  9448. ; (not iso-year)
  9449. ; (< (calendar-absolute-from-gregorian iso-date)
  9450. ; (time-to-days (current-time))))
  9451. ; (setq year (1+ year)
  9452. ; iso-date (calendar-gregorian-from-absolute
  9453. ; (calendar-absolute-from-iso
  9454. ; (list iso-week day year)))))
  9455. (setq month (car iso-date)
  9456. year (nth 2 iso-date)
  9457. day (nth 1 iso-date)))
  9458. (deltan
  9459. (unless deltadef
  9460. (let ((now (decode-time (current-time))))
  9461. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  9462. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  9463. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  9464. ((equal deltaw "m") (setq month (+ month deltan)))
  9465. ((equal deltaw "y") (setq year (+ year deltan)))))
  9466. ((and wday (not (nth 3 tl)))
  9467. ;; Weekday was given, but no day, so pick that day in the week
  9468. ;; on or after the derived date.
  9469. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  9470. (unless (equal wday wday1)
  9471. (setq day (+ day (% (- wday wday1 -7) 7))))))
  9472. (if (and (boundp 'org-time-was-given)
  9473. (nth 2 tl))
  9474. (setq org-time-was-given t))
  9475. (if (< year 100) (setq year (+ 2000 year)))
  9476. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  9477. (list second minute hour day month year)))
  9478. (defvar parse-time-weekdays)
  9479. (defun org-read-date-get-relative (s today default)
  9480. "Check string S for special relative date string.
  9481. TODAY and DEFAULT are internal times, for today and for a default.
  9482. Return shift list (N what def-flag)
  9483. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  9484. N is the number of WHATs to shift.
  9485. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  9486. the DEFAULT date rather than TODAY."
  9487. (when (string-match
  9488. (concat
  9489. "\\`[ \t]*\\([-+]\\{1,2\\}\\)"
  9490. "\\([0-9]+\\)?"
  9491. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  9492. "\\([ \t]\\|$\\)") s)
  9493. (let* ((dir (if (match-end 1)
  9494. (string-to-char (substring (match-string 1 s) -1))
  9495. ?+))
  9496. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  9497. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  9498. (what (if (match-end 3) (match-string 3 s) "d"))
  9499. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  9500. (date (if rel default today))
  9501. (wday (nth 6 (decode-time date)))
  9502. delta)
  9503. (if wday1
  9504. (progn
  9505. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  9506. (if (= dir ?-) (setq delta (- delta 7)))
  9507. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  9508. (list delta "d" rel))
  9509. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  9510. (defun org-eval-in-calendar (form &optional keepdate)
  9511. "Eval FORM in the calendar window and return to current window.
  9512. Also, store the cursor date in variable org-ans2."
  9513. (let ((sw (selected-window)))
  9514. (select-window (get-buffer-window "*Calendar*"))
  9515. (eval form)
  9516. (when (and (not keepdate) (calendar-cursor-to-date))
  9517. (let* ((date (calendar-cursor-to-date))
  9518. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  9519. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  9520. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  9521. (select-window sw)))
  9522. ; ;; Update the prompt to show new default date
  9523. ; (save-excursion
  9524. ; (goto-char (point-min))
  9525. ; (when (and org-ans2
  9526. ; (re-search-forward "\\[[-0-9]+\\]" nil t)
  9527. ; (get-text-property (match-end 0) 'field))
  9528. ; (let ((inhibit-read-only t))
  9529. ; (replace-match (concat "[" org-ans2 "]") t t)
  9530. ; (add-text-properties (point-min) (1+ (match-end 0))
  9531. ; (text-properties-at (1+ (point-min)))))))))
  9532. (defun org-calendar-select ()
  9533. "Return to `org-read-date' with the date currently selected.
  9534. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  9535. (interactive)
  9536. (when (calendar-cursor-to-date)
  9537. (let* ((date (calendar-cursor-to-date))
  9538. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  9539. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  9540. (if (active-minibuffer-window) (exit-minibuffer))))
  9541. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  9542. "Insert a date stamp for the date given by the internal TIME.
  9543. WITH-HM means, use the stamp format that includes the time of the day.
  9544. INACTIVE means use square brackets instead of angular ones, so that the
  9545. stamp will not contribute to the agenda.
  9546. PRE and POST are optional strings to be inserted before and after the
  9547. stamp.
  9548. The command returns the inserted time stamp."
  9549. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  9550. stamp)
  9551. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  9552. (insert-before-markers (or pre ""))
  9553. (insert-before-markers (setq stamp (format-time-string fmt time)))
  9554. (when (listp extra)
  9555. (setq extra (car extra))
  9556. (if (and (stringp extra)
  9557. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  9558. (setq extra (format "-%02d:%02d"
  9559. (string-to-number (match-string 1 extra))
  9560. (string-to-number (match-string 2 extra))))
  9561. (setq extra nil)))
  9562. (when extra
  9563. (backward-char 1)
  9564. (insert-before-markers extra)
  9565. (forward-char 1))
  9566. (insert-before-markers (or post ""))
  9567. stamp))
  9568. (defun org-toggle-time-stamp-overlays ()
  9569. "Toggle the use of custom time stamp formats."
  9570. (interactive)
  9571. (setq org-display-custom-times (not org-display-custom-times))
  9572. (unless org-display-custom-times
  9573. (let ((p (point-min)) (bmp (buffer-modified-p)))
  9574. (while (setq p (next-single-property-change p 'display))
  9575. (if (and (get-text-property p 'display)
  9576. (eq (get-text-property p 'face) 'org-date))
  9577. (remove-text-properties
  9578. p (setq p (next-single-property-change p 'display))
  9579. '(display t))))
  9580. (set-buffer-modified-p bmp)))
  9581. (if (featurep 'xemacs)
  9582. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  9583. (org-restart-font-lock)
  9584. (setq org-table-may-need-update t)
  9585. (if org-display-custom-times
  9586. (message "Time stamps are overlayed with custom format")
  9587. (message "Time stamp overlays removed")))
  9588. (defun org-display-custom-time (beg end)
  9589. "Overlay modified time stamp format over timestamp between BEG and END."
  9590. (let* ((ts (buffer-substring beg end))
  9591. t1 w1 with-hm tf time str w2 (off 0))
  9592. (save-match-data
  9593. (setq t1 (org-parse-time-string ts t))
  9594. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\)?\\'" ts)
  9595. (setq off (- (match-end 0) (match-beginning 0)))))
  9596. (setq end (- end off))
  9597. (setq w1 (- end beg)
  9598. with-hm (and (nth 1 t1) (nth 2 t1))
  9599. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  9600. time (org-fix-decoded-time t1)
  9601. str (org-add-props
  9602. (format-time-string
  9603. (substring tf 1 -1) (apply 'encode-time time))
  9604. nil 'mouse-face 'highlight)
  9605. w2 (length str))
  9606. (if (not (= w2 w1))
  9607. (add-text-properties (1+ beg) (+ 2 beg)
  9608. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  9609. (if (featurep 'xemacs)
  9610. (progn
  9611. (put-text-property beg end 'invisible t)
  9612. (put-text-property beg end 'end-glyph (make-glyph str)))
  9613. (put-text-property beg end 'display str))))
  9614. (defun org-translate-time (string)
  9615. "Translate all timestamps in STRING to custom format.
  9616. But do this only if the variable `org-display-custom-times' is set."
  9617. (when org-display-custom-times
  9618. (save-match-data
  9619. (let* ((start 0)
  9620. (re org-ts-regexp-both)
  9621. t1 with-hm inactive tf time str beg end)
  9622. (while (setq start (string-match re string start))
  9623. (setq beg (match-beginning 0)
  9624. end (match-end 0)
  9625. t1 (save-match-data
  9626. (org-parse-time-string (substring string beg end) t))
  9627. with-hm (and (nth 1 t1) (nth 2 t1))
  9628. inactive (equal (substring string beg (1+ beg)) "[")
  9629. tf (funcall (if with-hm 'cdr 'car)
  9630. org-time-stamp-custom-formats)
  9631. time (org-fix-decoded-time t1)
  9632. str (format-time-string
  9633. (concat
  9634. (if inactive "[" "<") (substring tf 1 -1)
  9635. (if inactive "]" ">"))
  9636. (apply 'encode-time time))
  9637. string (replace-match str t t string)
  9638. start (+ start (length str)))))))
  9639. string)
  9640. (defun org-fix-decoded-time (time)
  9641. "Set 0 instead of nil for the first 6 elements of time.
  9642. Don't touch the rest."
  9643. (let ((n 0))
  9644. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  9645. (defun org-days-to-time (timestamp-string)
  9646. "Difference between TIMESTAMP-STRING and now in days."
  9647. (- (time-to-days (org-time-string-to-time timestamp-string))
  9648. (time-to-days (current-time))))
  9649. (defun org-deadline-close (timestamp-string &optional ndays)
  9650. "Is the time in TIMESTAMP-STRING close to the current date?"
  9651. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  9652. (and (< (org-days-to-time timestamp-string) ndays)
  9653. (not (org-entry-is-done-p))))
  9654. (defun org-get-wdays (ts)
  9655. "Get the deadline lead time appropriate for timestring TS."
  9656. (cond
  9657. ((<= org-deadline-warning-days 0)
  9658. ;; 0 or negative, enforce this value no matter what
  9659. (- org-deadline-warning-days))
  9660. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\)" ts)
  9661. ;; lead time is specified.
  9662. (floor (* (string-to-number (match-string 1 ts))
  9663. (cdr (assoc (match-string 2 ts)
  9664. '(("d" . 1) ("w" . 7)
  9665. ("m" . 30.4) ("y" . 365.25)))))))
  9666. ;; go for the default.
  9667. (t org-deadline-warning-days)))
  9668. (defun org-calendar-select-mouse (ev)
  9669. "Return to `org-read-date' with the date currently selected.
  9670. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  9671. (interactive "e")
  9672. (mouse-set-point ev)
  9673. (when (calendar-cursor-to-date)
  9674. (let* ((date (calendar-cursor-to-date))
  9675. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  9676. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  9677. (if (active-minibuffer-window) (exit-minibuffer))))
  9678. (defun org-check-deadlines (ndays)
  9679. "Check if there are any deadlines due or past due.
  9680. A deadline is considered due if it happens within `org-deadline-warning-days'
  9681. days from today's date. If the deadline appears in an entry marked DONE,
  9682. it is not shown. The prefix arg NDAYS can be used to test that many
  9683. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  9684. (interactive "P")
  9685. (let* ((org-warn-days
  9686. (cond
  9687. ((equal ndays '(4)) 100000)
  9688. (ndays (prefix-numeric-value ndays))
  9689. (t (abs org-deadline-warning-days))))
  9690. (case-fold-search nil)
  9691. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  9692. (callback
  9693. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  9694. (message "%d deadlines past-due or due within %d days"
  9695. (org-occur regexp nil callback)
  9696. org-warn-days)))
  9697. (defun org-check-before-date (date)
  9698. "Check if there are deadlines or scheduled entries before DATE."
  9699. (interactive (list (org-read-date)))
  9700. (let ((case-fold-search nil)
  9701. (regexp (concat "\\<\\(" org-deadline-string
  9702. "\\|" org-scheduled-string
  9703. "\\) *<\\([^>]+\\)>"))
  9704. (callback
  9705. (lambda () (time-less-p
  9706. (org-time-string-to-time (match-string 2))
  9707. (org-time-string-to-time date)))))
  9708. (message "%d entries before %s"
  9709. (org-occur regexp nil callback) date)))
  9710. (defun org-evaluate-time-range (&optional to-buffer)
  9711. "Evaluate a time range by computing the difference between start and end.
  9712. Normally the result is just printed in the echo area, but with prefix arg
  9713. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  9714. If the time range is actually in a table, the result is inserted into the
  9715. next column.
  9716. For time difference computation, a year is assumed to be exactly 365
  9717. days in order to avoid rounding problems."
  9718. (interactive "P")
  9719. (or
  9720. (org-clock-update-time-maybe)
  9721. (save-excursion
  9722. (unless (org-at-date-range-p t)
  9723. (goto-char (point-at-bol))
  9724. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  9725. (if (not (org-at-date-range-p t))
  9726. (error "Not at a time-stamp range, and none found in current line")))
  9727. (let* ((ts1 (match-string 1))
  9728. (ts2 (match-string 2))
  9729. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  9730. (match-end (match-end 0))
  9731. (time1 (org-time-string-to-time ts1))
  9732. (time2 (org-time-string-to-time ts2))
  9733. (t1 (time-to-seconds time1))
  9734. (t2 (time-to-seconds time2))
  9735. (diff (abs (- t2 t1)))
  9736. (negative (< (- t2 t1) 0))
  9737. ;; (ys (floor (* 365 24 60 60)))
  9738. (ds (* 24 60 60))
  9739. (hs (* 60 60))
  9740. (fy "%dy %dd %02d:%02d")
  9741. (fy1 "%dy %dd")
  9742. (fd "%dd %02d:%02d")
  9743. (fd1 "%dd")
  9744. (fh "%02d:%02d")
  9745. y d h m align)
  9746. (if havetime
  9747. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  9748. y 0
  9749. d (floor (/ diff ds)) diff (mod diff ds)
  9750. h (floor (/ diff hs)) diff (mod diff hs)
  9751. m (floor (/ diff 60)))
  9752. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  9753. y 0
  9754. d (floor (+ (/ diff ds) 0.5))
  9755. h 0 m 0))
  9756. (if (not to-buffer)
  9757. (message "%s" (org-make-tdiff-string y d h m))
  9758. (if (org-at-table-p)
  9759. (progn
  9760. (goto-char match-end)
  9761. (setq align t)
  9762. (and (looking-at " *|") (goto-char (match-end 0))))
  9763. (goto-char match-end))
  9764. (if (looking-at
  9765. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  9766. (replace-match ""))
  9767. (if negative (insert " -"))
  9768. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  9769. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  9770. (insert " " (format fh h m))))
  9771. (if align (org-table-align))
  9772. (message "Time difference inserted")))))
  9773. (defun org-make-tdiff-string (y d h m)
  9774. (let ((fmt "")
  9775. (l nil))
  9776. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  9777. l (push y l)))
  9778. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  9779. l (push d l)))
  9780. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  9781. l (push h l)))
  9782. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  9783. l (push m l)))
  9784. (apply 'format fmt (nreverse l))))
  9785. (defun org-time-string-to-time (s)
  9786. (apply 'encode-time (org-parse-time-string s)))
  9787. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  9788. "Convert a time stamp to an absolute day number.
  9789. If there is a specifyer for a cyclic time stamp, get the closest date to
  9790. DAYNR.
  9791. PREFER and SHOW_ALL are passed through to `org-closest-date'."
  9792. (cond
  9793. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  9794. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  9795. daynr
  9796. (+ daynr 1000)))
  9797. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  9798. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  9799. (time-to-days (current-time))) (match-string 0 s)
  9800. prefer show-all))
  9801. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  9802. (defun org-days-to-iso-week (days)
  9803. "Return the iso week number."
  9804. (require 'cal-iso)
  9805. (car (calendar-iso-from-absolute days)))
  9806. (defun org-small-year-to-year (year)
  9807. "Convert 2-digit years into 4-digit years.
  9808. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  9809. The year 2000 cannot be abbreviated. Any year lager than 99
  9810. is retrned unchanged."
  9811. (if (< year 38)
  9812. (setq year (+ 2000 year))
  9813. (if (< year 100)
  9814. (setq year (+ 1900 year))))
  9815. year)
  9816. (defun org-time-from-absolute (d)
  9817. "Return the time corresponding to date D.
  9818. D may be an absolute day number, or a calendar-type list (month day year)."
  9819. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  9820. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  9821. (defun org-calendar-holiday ()
  9822. "List of holidays, for Diary display in Org-mode."
  9823. (require 'holidays)
  9824. (let ((hl (funcall
  9825. (if (fboundp 'calendar-check-holidays)
  9826. 'calendar-check-holidays 'check-calendar-holidays) date)))
  9827. (if hl (mapconcat 'identity hl "; "))))
  9828. (defun org-diary-sexp-entry (sexp entry date)
  9829. "Process a SEXP diary ENTRY for DATE."
  9830. (require 'diary-lib)
  9831. (let ((result (if calendar-debug-sexp
  9832. (let ((stack-trace-on-error t))
  9833. (eval (car (read-from-string sexp))))
  9834. (condition-case nil
  9835. (eval (car (read-from-string sexp)))
  9836. (error
  9837. (beep)
  9838. (message "Bad sexp at line %d in %s: %s"
  9839. (org-current-line)
  9840. (buffer-file-name) sexp)
  9841. (sleep-for 2))))))
  9842. (cond ((stringp result) result)
  9843. ((and (consp result)
  9844. (stringp (cdr result))) (cdr result))
  9845. (result entry)
  9846. (t nil))))
  9847. (defun org-diary-to-ical-string (frombuf)
  9848. "Get iCalendar entries from diary entries in buffer FROMBUF.
  9849. This uses the icalendar.el library."
  9850. (let* ((tmpdir (if (featurep 'xemacs)
  9851. (temp-directory)
  9852. temporary-file-directory))
  9853. (tmpfile (make-temp-name
  9854. (expand-file-name "orgics" tmpdir)))
  9855. buf rtn b e)
  9856. (save-excursion
  9857. (set-buffer frombuf)
  9858. (icalendar-export-region (point-min) (point-max) tmpfile)
  9859. (setq buf (find-buffer-visiting tmpfile))
  9860. (set-buffer buf)
  9861. (goto-char (point-min))
  9862. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  9863. (setq b (match-beginning 0)))
  9864. (goto-char (point-max))
  9865. (if (re-search-backward "^END:VEVENT" nil t)
  9866. (setq e (match-end 0)))
  9867. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  9868. (kill-buffer buf)
  9869. (kill-buffer frombuf)
  9870. (delete-file tmpfile)
  9871. rtn))
  9872. (defun org-closest-date (start current change prefer show-all)
  9873. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  9874. When PREFER is `past' return a date that is either CURRENT or past.
  9875. When PREFER is `future', return a date that is either CURRENT or future.
  9876. When SHOW-ALL is nil, only return the current occurence of a time stamp."
  9877. ;; Make the proper lists from the dates
  9878. (catch 'exit
  9879. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  9880. dn dw sday cday n1 n2
  9881. d m y y1 y2 date1 date2 nmonths nm ny m2)
  9882. (setq start (org-date-to-gregorian start)
  9883. current (org-date-to-gregorian
  9884. (if show-all
  9885. current
  9886. (time-to-days (current-time))))
  9887. sday (calendar-absolute-from-gregorian start)
  9888. cday (calendar-absolute-from-gregorian current))
  9889. (if (<= cday sday) (throw 'exit sday))
  9890. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  9891. (setq dn (string-to-number (match-string 1 change))
  9892. dw (cdr (assoc (match-string 2 change) a1)))
  9893. (error "Invalid change specifyer: %s" change))
  9894. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  9895. (cond
  9896. ((eq dw 'day)
  9897. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  9898. n2 (+ n1 dn)))
  9899. ((eq dw 'year)
  9900. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  9901. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  9902. (setq date1 (list m d y1)
  9903. n1 (calendar-absolute-from-gregorian date1)
  9904. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  9905. n2 (calendar-absolute-from-gregorian date2)))
  9906. ((eq dw 'month)
  9907. ;; approx number of month between the tow dates
  9908. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  9909. ;; How often does dn fit in there?
  9910. (setq d (nth 1 start) m (car start) y (nth 2 start)
  9911. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  9912. m (+ m nm)
  9913. ny (floor (/ m 12))
  9914. y (+ y ny)
  9915. m (- m (* ny 12)))
  9916. (while (> m 12) (setq m (- m 12) y (1+ y)))
  9917. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  9918. (setq m2 (+ m dn) y2 y)
  9919. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  9920. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  9921. (while (< n2 cday)
  9922. (setq n1 n2 m m2 y y2)
  9923. (setq m2 (+ m dn) y2 y)
  9924. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  9925. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  9926. (if show-all
  9927. (cond
  9928. ((eq prefer 'past) n1)
  9929. ((eq prefer 'future) (if (= cday n1) n1 n2))
  9930. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  9931. (cond
  9932. ((eq prefer 'past) n1)
  9933. ((eq prefer 'future) (if (= cday n1) n1 n2))
  9934. (t (if (= cday n1) n1 n2)))))))
  9935. (defun org-date-to-gregorian (date)
  9936. "Turn any specification of DATE into a gregorian date for the calendar."
  9937. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  9938. ((and (listp date) (= (length date) 3)) date)
  9939. ((stringp date)
  9940. (setq date (org-parse-time-string date))
  9941. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  9942. ((listp date)
  9943. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  9944. (defun org-parse-time-string (s &optional nodefault)
  9945. "Parse the standard Org-mode time string.
  9946. This should be a lot faster than the normal `parse-time-string'.
  9947. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  9948. hour and minute fields will be nil if not given."
  9949. (if (string-match org-ts-regexp0 s)
  9950. (list 0
  9951. (if (or (match-beginning 8) (not nodefault))
  9952. (string-to-number (or (match-string 8 s) "0")))
  9953. (if (or (match-beginning 7) (not nodefault))
  9954. (string-to-number (or (match-string 7 s) "0")))
  9955. (string-to-number (match-string 4 s))
  9956. (string-to-number (match-string 3 s))
  9957. (string-to-number (match-string 2 s))
  9958. nil nil nil)
  9959. (make-list 9 0)))
  9960. (defun org-timestamp-up (&optional arg)
  9961. "Increase the date item at the cursor by one.
  9962. If the cursor is on the year, change the year. If it is on the month or
  9963. the day, change that.
  9964. With prefix ARG, change by that many units."
  9965. (interactive "p")
  9966. (org-timestamp-change (prefix-numeric-value arg)))
  9967. (defun org-timestamp-down (&optional arg)
  9968. "Decrease the date item at the cursor by one.
  9969. If the cursor is on the year, change the year. If it is on the month or
  9970. the day, change that.
  9971. With prefix ARG, change by that many units."
  9972. (interactive "p")
  9973. (org-timestamp-change (- (prefix-numeric-value arg))))
  9974. (defun org-timestamp-up-day (&optional arg)
  9975. "Increase the date in the time stamp by one day.
  9976. With prefix ARG, change that many days."
  9977. (interactive "p")
  9978. (if (and (not (org-at-timestamp-p t))
  9979. (org-on-heading-p))
  9980. (org-todo 'up)
  9981. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  9982. (defun org-timestamp-down-day (&optional arg)
  9983. "Decrease the date in the time stamp by one day.
  9984. With prefix ARG, change that many days."
  9985. (interactive "p")
  9986. (if (and (not (org-at-timestamp-p t))
  9987. (org-on-heading-p))
  9988. (org-todo 'down)
  9989. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  9990. (defun org-at-timestamp-p (&optional inactive-ok)
  9991. "Determine if the cursor is in or at a timestamp."
  9992. (interactive)
  9993. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  9994. (pos (point))
  9995. (ans (or (looking-at tsr)
  9996. (save-excursion
  9997. (skip-chars-backward "^[<\n\r\t")
  9998. (if (> (point) (point-min)) (backward-char 1))
  9999. (and (looking-at tsr)
  10000. (> (- (match-end 0) pos) -1))))))
  10001. (and ans
  10002. (boundp 'org-ts-what)
  10003. (setq org-ts-what
  10004. (cond
  10005. ((= pos (match-beginning 0)) 'bracket)
  10006. ((= pos (1- (match-end 0))) 'bracket)
  10007. ((org-pos-in-match-range pos 2) 'year)
  10008. ((org-pos-in-match-range pos 3) 'month)
  10009. ((org-pos-in-match-range pos 7) 'hour)
  10010. ((org-pos-in-match-range pos 8) 'minute)
  10011. ((or (org-pos-in-match-range pos 4)
  10012. (org-pos-in-match-range pos 5)) 'day)
  10013. ((and (> pos (or (match-end 8) (match-end 5)))
  10014. (< pos (match-end 0)))
  10015. (- pos (or (match-end 8) (match-end 5))))
  10016. (t 'day))))
  10017. ans))
  10018. (defun org-toggle-timestamp-type ()
  10019. "Toggle the type (<active> or [inactive]) of a time stamp."
  10020. (interactive)
  10021. (when (org-at-timestamp-p t)
  10022. (save-excursion
  10023. (goto-char (match-beginning 0))
  10024. (insert (if (equal (char-after) ?<) "[" "<")) (delete-char 1)
  10025. (goto-char (1- (match-end 0)))
  10026. (insert (if (equal (char-after) ?>) "]" ">")) (delete-char 1))
  10027. (message "Timestamp is now %sactive"
  10028. (if (equal (char-before) ?>) "in" ""))))
  10029. (defun org-timestamp-change (n &optional what)
  10030. "Change the date in the time stamp at point.
  10031. The date will be changed by N times WHAT. WHAT can be `day', `month',
  10032. `year', `minute', `second'. If WHAT is not given, the cursor position
  10033. in the timestamp determines what will be changed."
  10034. (let ((pos (point))
  10035. with-hm inactive
  10036. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  10037. org-ts-what
  10038. extra rem
  10039. ts time time0)
  10040. (if (not (org-at-timestamp-p t))
  10041. (error "Not at a timestamp"))
  10042. (if (and (not what) (eq org-ts-what 'bracket))
  10043. (org-toggle-timestamp-type)
  10044. (if (and (not what) (not (eq org-ts-what 'day))
  10045. org-display-custom-times
  10046. (get-text-property (point) 'display)
  10047. (not (get-text-property (1- (point)) 'display)))
  10048. (setq org-ts-what 'day))
  10049. (setq org-ts-what (or what org-ts-what)
  10050. inactive (= (char-after (match-beginning 0)) ?\[)
  10051. ts (match-string 0))
  10052. (replace-match "")
  10053. (if (string-match
  10054. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\)*\\)[]>]"
  10055. ts)
  10056. (setq extra (match-string 1 ts)))
  10057. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  10058. (setq with-hm t))
  10059. (setq time0 (org-parse-time-string ts))
  10060. (when (and (eq org-ts-what 'minute)
  10061. (eq current-prefix-arg nil))
  10062. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  10063. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  10064. (setcar (cdr time0) (+ (nth 1 time0)
  10065. (if (> n 0) (- rem) (- dm rem))))))
  10066. (setq time
  10067. (encode-time (or (car time0) 0)
  10068. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  10069. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  10070. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  10071. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  10072. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  10073. (nthcdr 6 time0)))
  10074. (when (integerp org-ts-what)
  10075. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  10076. (if (eq what 'calendar)
  10077. (let ((cal-date (org-get-date-from-calendar)))
  10078. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  10079. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  10080. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  10081. (setcar time0 (or (car time0) 0))
  10082. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  10083. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  10084. (setq time (apply 'encode-time time0))))
  10085. (setq org-last-changed-timestamp
  10086. (org-insert-time-stamp time with-hm inactive nil nil extra))
  10087. (org-clock-update-time-maybe)
  10088. (goto-char pos)
  10089. ;; Try to recenter the calendar window, if any
  10090. (if (and org-calendar-follow-timestamp-change
  10091. (get-buffer-window "*Calendar*" t)
  10092. (memq org-ts-what '(day month year)))
  10093. (org-recenter-calendar (time-to-days time))))))
  10094. (defun org-modify-ts-extra (s pos n dm)
  10095. "Change the different parts of the lead-time and repeat fields in timestamp."
  10096. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  10097. ng h m new rem)
  10098. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  10099. (cond
  10100. ((or (org-pos-in-match-range pos 2)
  10101. (org-pos-in-match-range pos 3))
  10102. (setq m (string-to-number (match-string 3 s))
  10103. h (string-to-number (match-string 2 s)))
  10104. (if (org-pos-in-match-range pos 2)
  10105. (setq h (+ h n))
  10106. (setq n (* dm (org-no-warnings (signum n))))
  10107. (when (not (= 0 (setq rem (% m dm))))
  10108. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  10109. (setq m (+ m n)))
  10110. (if (< m 0) (setq m (+ m 60) h (1- h)))
  10111. (if (> m 59) (setq m (- m 60) h (1+ h)))
  10112. (setq h (min 24 (max 0 h)))
  10113. (setq ng 1 new (format "-%02d:%02d" h m)))
  10114. ((org-pos-in-match-range pos 6)
  10115. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  10116. ((org-pos-in-match-range pos 5)
  10117. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  10118. ((org-pos-in-match-range pos 9)
  10119. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  10120. ((org-pos-in-match-range pos 8)
  10121. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  10122. (when ng
  10123. (setq s (concat
  10124. (substring s 0 (match-beginning ng))
  10125. new
  10126. (substring s (match-end ng))))))
  10127. s))
  10128. (defun org-recenter-calendar (date)
  10129. "If the calendar is visible, recenter it to DATE."
  10130. (let* ((win (selected-window))
  10131. (cwin (get-buffer-window "*Calendar*" t))
  10132. (calendar-move-hook nil))
  10133. (when cwin
  10134. (select-window cwin)
  10135. (calendar-goto-date (if (listp date) date
  10136. (calendar-gregorian-from-absolute date)))
  10137. (select-window win))))
  10138. (defun org-goto-calendar (&optional arg)
  10139. "Go to the Emacs calendar at the current date.
  10140. If there is a time stamp in the current line, go to that date.
  10141. A prefix ARG can be used to force the current date."
  10142. (interactive "P")
  10143. (let ((tsr org-ts-regexp) diff
  10144. (calendar-move-hook nil)
  10145. (calendar-view-holidays-initially-flag nil)
  10146. (view-calendar-holidays-initially nil)
  10147. (calendar-view-diary-initially-flag nil)
  10148. (view-diary-entries-initially nil))
  10149. (if (or (org-at-timestamp-p)
  10150. (save-excursion
  10151. (beginning-of-line 1)
  10152. (looking-at (concat ".*" tsr))))
  10153. (let ((d1 (time-to-days (current-time)))
  10154. (d2 (time-to-days
  10155. (org-time-string-to-time (match-string 1)))))
  10156. (setq diff (- d2 d1))))
  10157. (calendar)
  10158. (calendar-goto-today)
  10159. (if (and diff (not arg)) (calendar-forward-day diff))))
  10160. (defun org-get-date-from-calendar ()
  10161. "Return a list (month day year) of date at point in calendar."
  10162. (with-current-buffer "*Calendar*"
  10163. (save-match-data
  10164. (calendar-cursor-to-date))))
  10165. (defun org-date-from-calendar ()
  10166. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  10167. If there is already a time stamp at the cursor position, update it."
  10168. (interactive)
  10169. (if (org-at-timestamp-p t)
  10170. (org-timestamp-change 0 'calendar)
  10171. (let ((cal-date (org-get-date-from-calendar)))
  10172. (org-insert-time-stamp
  10173. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  10174. (defun org-minutes-to-hh:mm-string (m)
  10175. "Compute H:MM from a number of minutes."
  10176. (let ((h (/ m 60)))
  10177. (setq m (- m (* 60 h)))
  10178. (format "%d:%02d" h m)))
  10179. (defun org-hh:mm-string-to-minutes (s)
  10180. "Convert a string H:MM to a number of minutes."
  10181. (if (string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  10182. (+ (* (string-to-number (match-string 1 s)) 60)
  10183. (string-to-number (match-string 2 s)))
  10184. 0))
  10185. ;;;; Agenda files
  10186. ;;;###autoload
  10187. (defun org-iswitchb (&optional arg)
  10188. "Use `iswitchb-read-buffer' to prompt for an Org buffer to switch to.
  10189. With a prefix argument, restrict available to files.
  10190. With two prefix arguments, restrict available buffers to agenda files.
  10191. Due to some yet unresolved reason, global function
  10192. `iswitchb-mode' needs to be active for this function to work."
  10193. (interactive "P")
  10194. (require 'iswitchb)
  10195. (let ((enabled iswitchb-mode) blist)
  10196. (or enabled (iswitchb-mode 1))
  10197. (setq blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  10198. ((equal arg '(16)) (org-buffer-list 'agenda))
  10199. (t (org-buffer-list))))
  10200. (unwind-protect
  10201. (let ((iswitchb-make-buflist-hook
  10202. (lambda ()
  10203. (setq iswitchb-temp-buflist
  10204. (mapcar 'buffer-name blist)))))
  10205. (switch-to-buffer
  10206. (iswitchb-read-buffer
  10207. "Switch-to: " nil t))
  10208. (or enabled (iswitchb-mode -1))))))
  10209. (defun org-buffer-list (&optional predicate tmp)
  10210. "Return a list of Org buffers.
  10211. PREDICATE can be either 'export, 'files or 'agenda.
  10212. 'export restrict the list to Export buffers.
  10213. 'files restrict the list to buffers visiting Org files.
  10214. 'agenda restrict the list to buffers visiting agenda files.
  10215. If TMP is non-nil, don't include temporary buffers."
  10216. (let (filter blist)
  10217. (setq filter
  10218. (cond ((eq predicate 'files) "\.org$")
  10219. ((eq predicate 'export) "\*Org .*Export")
  10220. (t "\*Org \\|\.org$")))
  10221. (setq blist
  10222. (mapcar
  10223. (lambda(b)
  10224. (let ((bname (buffer-name b))
  10225. (bfile (buffer-file-name b)))
  10226. (if (and (string-match filter bname)
  10227. (if (eq predicate 'agenda)
  10228. (member bfile
  10229. (mapcar (lambda(f) (file-truename f))
  10230. org-agenda-files)) t)
  10231. (if tmp (not (string-match "tmp" bname)) t)) b)))
  10232. (buffer-list)))
  10233. (delete nil blist)))
  10234. (defun org-agenda-files (&optional unrestricted ext)
  10235. "Get the list of agenda files.
  10236. Optional UNRESTRICTED means return the full list even if a restriction
  10237. is currently in place.
  10238. When EXT is non-nil, try to add all files that are created by adding EXT
  10239. to the file nemes. Basically, this is a way to add the archive files
  10240. to the list, by setting EXT to \"_archive\" If EXT is non-nil, but not
  10241. a string, \"_archive\" will be used."
  10242. (let ((files
  10243. (cond
  10244. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  10245. ((stringp org-agenda-files) (org-read-agenda-file-list))
  10246. ((listp org-agenda-files) org-agenda-files)
  10247. (t (error "Invalid value of `org-agenda-files'")))))
  10248. (setq files (apply 'append
  10249. (mapcar (lambda (f)
  10250. (if (file-directory-p f)
  10251. (directory-files
  10252. f t org-agenda-file-regexp)
  10253. (list f)))
  10254. files)))
  10255. (when org-agenda-skip-unavailable-files
  10256. (setq files (delq nil
  10257. (mapcar (function
  10258. (lambda (file)
  10259. (and (file-readable-p file) file)))
  10260. files))))
  10261. (when ext
  10262. (setq ext (if (and (stringp ext) (string-match "\\S-" ext))
  10263. ext "_archive"))
  10264. (setq files (apply 'append
  10265. (mapcar
  10266. (lambda (f)
  10267. (if (file-exists-p (concat f ext))
  10268. (list f (concat f ext))
  10269. (list f)))
  10270. files))))
  10271. files))
  10272. (defun org-edit-agenda-file-list ()
  10273. "Edit the list of agenda files.
  10274. Depending on setup, this either uses customize to edit the variable
  10275. `org-agenda-files', or it visits the file that is holding the list. In the
  10276. latter case, the buffer is set up in a way that saving it automatically kills
  10277. the buffer and restores the previous window configuration."
  10278. (interactive)
  10279. (if (stringp org-agenda-files)
  10280. (let ((cw (current-window-configuration)))
  10281. (find-file org-agenda-files)
  10282. (org-set-local 'org-window-configuration cw)
  10283. (org-add-hook 'after-save-hook
  10284. (lambda ()
  10285. (set-window-configuration
  10286. (prog1 org-window-configuration
  10287. (kill-buffer (current-buffer))))
  10288. (org-install-agenda-files-menu)
  10289. (message "New agenda file list installed"))
  10290. nil 'local)
  10291. (message "%s" (substitute-command-keys
  10292. "Edit list and finish with \\[save-buffer]")))
  10293. (customize-variable 'org-agenda-files)))
  10294. (defun org-store-new-agenda-file-list (list)
  10295. "Set new value for the agenda file list and save it correcly."
  10296. (if (stringp org-agenda-files)
  10297. (let ((f org-agenda-files) b)
  10298. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  10299. (with-temp-file f
  10300. (insert (mapconcat 'identity list "\n") "\n")))
  10301. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  10302. (setq org-agenda-files list)
  10303. (customize-save-variable 'org-agenda-files org-agenda-files))))
  10304. (defun org-read-agenda-file-list ()
  10305. "Read the list of agenda files from a file."
  10306. (when (file-directory-p org-agenda-files)
  10307. (error "`org-agenda-files' cannot be a single directory"))
  10308. (when (stringp org-agenda-files)
  10309. (with-temp-buffer
  10310. (insert-file-contents org-agenda-files)
  10311. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  10312. ;;;###autoload
  10313. (defun org-cycle-agenda-files ()
  10314. "Cycle through the files in `org-agenda-files'.
  10315. If the current buffer visits an agenda file, find the next one in the list.
  10316. If the current buffer does not, find the first agenda file."
  10317. (interactive)
  10318. (let* ((fs (org-agenda-files t))
  10319. (files (append fs (list (car fs))))
  10320. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  10321. file)
  10322. (unless files (error "No agenda files"))
  10323. (catch 'exit
  10324. (while (setq file (pop files))
  10325. (if (equal (file-truename file) tcf)
  10326. (when (car files)
  10327. (find-file (car files))
  10328. (throw 'exit t))))
  10329. (find-file (car fs)))
  10330. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  10331. (defun org-agenda-file-to-front (&optional to-end)
  10332. "Move/add the current file to the top of the agenda file list.
  10333. If the file is not present in the list, it is added to the front. If it is
  10334. present, it is moved there. With optional argument TO-END, add/move to the
  10335. end of the list."
  10336. (interactive "P")
  10337. (let ((org-agenda-skip-unavailable-files nil)
  10338. (file-alist (mapcar (lambda (x)
  10339. (cons (file-truename x) x))
  10340. (org-agenda-files t)))
  10341. (ctf (file-truename buffer-file-name))
  10342. x had)
  10343. (setq x (assoc ctf file-alist) had x)
  10344. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  10345. (if to-end
  10346. (setq file-alist (append (delq x file-alist) (list x)))
  10347. (setq file-alist (cons x (delq x file-alist))))
  10348. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  10349. (org-install-agenda-files-menu)
  10350. (message "File %s to %s of agenda file list"
  10351. (if had "moved" "added") (if to-end "end" "front"))))
  10352. (defun org-remove-file (&optional file)
  10353. "Remove current file from the list of files in variable `org-agenda-files'.
  10354. These are the files which are being checked for agenda entries.
  10355. Optional argument FILE means, use this file instead of the current."
  10356. (interactive)
  10357. (let* ((org-agenda-skip-unavailable-files nil)
  10358. (file (or file buffer-file-name))
  10359. (true-file (file-truename file))
  10360. (afile (abbreviate-file-name file))
  10361. (files (delq nil (mapcar
  10362. (lambda (x)
  10363. (if (equal true-file
  10364. (file-truename x))
  10365. nil x))
  10366. (org-agenda-files t)))))
  10367. (if (not (= (length files) (length (org-agenda-files t))))
  10368. (progn
  10369. (org-store-new-agenda-file-list files)
  10370. (org-install-agenda-files-menu)
  10371. (message "Removed file: %s" afile))
  10372. (message "File was not in list: %s (not removed)" afile))))
  10373. (defun org-file-menu-entry (file)
  10374. (vector file (list 'find-file file) t))
  10375. (defun org-check-agenda-file (file)
  10376. "Make sure FILE exists. If not, ask user what to do."
  10377. (when (not (file-exists-p file))
  10378. (message "non-existent file %s. [R]emove from list or [A]bort?"
  10379. (abbreviate-file-name file))
  10380. (let ((r (downcase (read-char-exclusive))))
  10381. (cond
  10382. ((equal r ?r)
  10383. (org-remove-file file)
  10384. (throw 'nextfile t))
  10385. (t (error "Abort"))))))
  10386. (defun org-get-agenda-file-buffer (file)
  10387. "Get a buffer visiting FILE. If the buffer needs to be created, add
  10388. it to the list of buffers which might be released later."
  10389. (let ((buf (org-find-base-buffer-visiting file)))
  10390. (if buf
  10391. buf ; just return it
  10392. ;; Make a new buffer and remember it
  10393. (setq buf (find-file-noselect file))
  10394. (if buf (push buf org-agenda-new-buffers))
  10395. buf)))
  10396. (defun org-release-buffers (blist)
  10397. "Release all buffers in list, asking the user for confirmation when needed.
  10398. When a buffer is unmodified, it is just killed. When modified, it is saved
  10399. \(if the user agrees) and then killed."
  10400. (let (buf file)
  10401. (while (setq buf (pop blist))
  10402. (setq file (buffer-file-name buf))
  10403. (when (and (buffer-modified-p buf)
  10404. file
  10405. (y-or-n-p (format "Save file %s? " file)))
  10406. (with-current-buffer buf (save-buffer)))
  10407. (kill-buffer buf))))
  10408. (defun org-prepare-agenda-buffers (files)
  10409. "Create buffers for all agenda files, protect archived trees and comments."
  10410. (interactive)
  10411. (let ((pa '(:org-archived t))
  10412. (pc '(:org-comment t))
  10413. (pall '(:org-archived t :org-comment t))
  10414. (inhibit-read-only t)
  10415. (rea (concat ":" org-archive-tag ":"))
  10416. bmp file re)
  10417. (save-excursion
  10418. (save-restriction
  10419. (while (setq file (pop files))
  10420. (if (bufferp file)
  10421. (set-buffer file)
  10422. (org-check-agenda-file file)
  10423. (set-buffer (org-get-agenda-file-buffer file)))
  10424. (widen)
  10425. (setq bmp (buffer-modified-p))
  10426. (org-refresh-category-properties)
  10427. (setq org-todo-keywords-for-agenda
  10428. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  10429. (setq org-done-keywords-for-agenda
  10430. (append org-done-keywords-for-agenda org-done-keywords))
  10431. (save-excursion
  10432. (remove-text-properties (point-min) (point-max) pall)
  10433. (when org-agenda-skip-archived-trees
  10434. (goto-char (point-min))
  10435. (while (re-search-forward rea nil t)
  10436. (if (org-on-heading-p t)
  10437. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  10438. (goto-char (point-min))
  10439. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  10440. (while (re-search-forward re nil t)
  10441. (add-text-properties
  10442. (match-beginning 0) (org-end-of-subtree t) pc)))
  10443. (set-buffer-modified-p bmp))))))
  10444. ;;;; Embedded LaTeX
  10445. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  10446. "Keymap for the minor `org-cdlatex-mode'.")
  10447. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  10448. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  10449. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  10450. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  10451. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  10452. (defvar org-cdlatex-texmathp-advice-is-done nil
  10453. "Flag remembering if we have applied the advice to texmathp already.")
  10454. (define-minor-mode org-cdlatex-mode
  10455. "Toggle the minor `org-cdlatex-mode'.
  10456. This mode supports entering LaTeX environment and math in LaTeX fragments
  10457. in Org-mode.
  10458. \\{org-cdlatex-mode-map}"
  10459. nil " OCDL" nil
  10460. (when org-cdlatex-mode (require 'cdlatex))
  10461. (unless org-cdlatex-texmathp-advice-is-done
  10462. (setq org-cdlatex-texmathp-advice-is-done t)
  10463. (defadvice texmathp (around org-math-always-on activate)
  10464. "Always return t in org-mode buffers.
  10465. This is because we want to insert math symbols without dollars even outside
  10466. the LaTeX math segments. If Orgmode thinks that point is actually inside
  10467. en embedded LaTeX fragement, let texmathp do its job.
  10468. \\[org-cdlatex-mode-map]"
  10469. (interactive)
  10470. (let (p)
  10471. (cond
  10472. ((not (org-mode-p)) ad-do-it)
  10473. ((eq this-command 'cdlatex-math-symbol)
  10474. (setq ad-return-value t
  10475. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  10476. (t
  10477. (let ((p (org-inside-LaTeX-fragment-p)))
  10478. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  10479. (setq ad-return-value t
  10480. texmathp-why '("Org-mode embedded math" . 0))
  10481. (if p ad-do-it)))))))))
  10482. (defun turn-on-org-cdlatex ()
  10483. "Unconditionally turn on `org-cdlatex-mode'."
  10484. (org-cdlatex-mode 1))
  10485. (defun org-inside-LaTeX-fragment-p ()
  10486. "Test if point is inside a LaTeX fragment.
  10487. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  10488. sequence appearing also before point.
  10489. Even though the matchers for math are configurable, this function assumes
  10490. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  10491. delimiters are skipped when they have been removed by customization.
  10492. The return value is nil, or a cons cell with the delimiter and
  10493. and the position of this delimiter.
  10494. This function does a reasonably good job, but can locally be fooled by
  10495. for example currency specifications. For example it will assume being in
  10496. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  10497. fragments that are properly closed, but during editing, we have to live
  10498. with the uncertainty caused by missing closing delimiters. This function
  10499. looks only before point, not after."
  10500. (catch 'exit
  10501. (let ((pos (point))
  10502. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  10503. (lim (progn
  10504. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  10505. (point)))
  10506. dd-on str (start 0) m re)
  10507. (goto-char pos)
  10508. (when dodollar
  10509. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  10510. re (nth 1 (assoc "$" org-latex-regexps)))
  10511. (while (string-match re str start)
  10512. (cond
  10513. ((= (match-end 0) (length str))
  10514. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  10515. ((= (match-end 0) (- (length str) 5))
  10516. (throw 'exit nil))
  10517. (t (setq start (match-end 0))))))
  10518. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  10519. (goto-char pos)
  10520. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  10521. (and (match-beginning 2) (throw 'exit nil))
  10522. ;; count $$
  10523. (while (re-search-backward "\\$\\$" lim t)
  10524. (setq dd-on (not dd-on)))
  10525. (goto-char pos)
  10526. (if dd-on (cons "$$" m))))))
  10527. (defun org-try-cdlatex-tab ()
  10528. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  10529. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  10530. - inside a LaTeX fragment, or
  10531. - after the first word in a line, where an abbreviation expansion could
  10532. insert a LaTeX environment."
  10533. (when org-cdlatex-mode
  10534. (cond
  10535. ((save-excursion
  10536. (skip-chars-backward "a-zA-Z0-9*")
  10537. (skip-chars-backward " \t")
  10538. (bolp))
  10539. (cdlatex-tab) t)
  10540. ((org-inside-LaTeX-fragment-p)
  10541. (cdlatex-tab) t)
  10542. (t nil))))
  10543. (defun org-cdlatex-underscore-caret (&optional arg)
  10544. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  10545. Revert to the normal definition outside of these fragments."
  10546. (interactive "P")
  10547. (if (org-inside-LaTeX-fragment-p)
  10548. (call-interactively 'cdlatex-sub-superscript)
  10549. (let (org-cdlatex-mode)
  10550. (call-interactively (key-binding (vector last-input-event))))))
  10551. (defun org-cdlatex-math-modify (&optional arg)
  10552. "Execute `cdlatex-math-modify' in LaTeX fragments.
  10553. Revert to the normal definition outside of these fragments."
  10554. (interactive "P")
  10555. (if (org-inside-LaTeX-fragment-p)
  10556. (call-interactively 'cdlatex-math-modify)
  10557. (let (org-cdlatex-mode)
  10558. (call-interactively (key-binding (vector last-input-event))))))
  10559. (defvar org-latex-fragment-image-overlays nil
  10560. "List of overlays carrying the images of latex fragments.")
  10561. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  10562. (defun org-remove-latex-fragment-image-overlays ()
  10563. "Remove all overlays with LaTeX fragment images in current buffer."
  10564. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  10565. (setq org-latex-fragment-image-overlays nil))
  10566. (defun org-preview-latex-fragment (&optional subtree)
  10567. "Preview the LaTeX fragment at point, or all locally or globally.
  10568. If the cursor is in a LaTeX fragment, create the image and overlay
  10569. it over the source code. If there is no fragment at point, display
  10570. all fragments in the current text, from one headline to the next. With
  10571. prefix SUBTREE, display all fragments in the current subtree. With a
  10572. double prefix `C-u C-u', or when the cursor is before the first headline,
  10573. display all fragments in the buffer.
  10574. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  10575. (interactive "P")
  10576. (org-remove-latex-fragment-image-overlays)
  10577. (save-excursion
  10578. (save-restriction
  10579. (let (beg end at msg)
  10580. (cond
  10581. ((or (equal subtree '(16))
  10582. (not (save-excursion
  10583. (re-search-backward (concat "^" outline-regexp) nil t))))
  10584. (setq beg (point-min) end (point-max)
  10585. msg "Creating images for buffer...%s"))
  10586. ((equal subtree '(4))
  10587. (org-back-to-heading)
  10588. (setq beg (point) end (org-end-of-subtree t)
  10589. msg "Creating images for subtree...%s"))
  10590. (t
  10591. (if (setq at (org-inside-LaTeX-fragment-p))
  10592. (goto-char (max (point-min) (- (cdr at) 2)))
  10593. (org-back-to-heading))
  10594. (setq beg (point) end (progn (outline-next-heading) (point))
  10595. msg (if at "Creating image...%s"
  10596. "Creating images for entry...%s"))))
  10597. (message msg "")
  10598. (narrow-to-region beg end)
  10599. (goto-char beg)
  10600. (org-format-latex
  10601. (concat "ltxpng/" (file-name-sans-extension
  10602. (file-name-nondirectory
  10603. buffer-file-name)))
  10604. default-directory 'overlays msg at 'forbuffer)
  10605. (message msg "done. Use `C-c C-c' to remove images.")))))
  10606. (defvar org-latex-regexps
  10607. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  10608. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  10609. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  10610. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([ .,?;:'\")\000]\\|$\\)" 2 nil)
  10611. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  10612. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  10613. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  10614. "Regular expressions for matching embedded LaTeX.")
  10615. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  10616. "Replace LaTeX fragments with links to an image, and produce images."
  10617. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  10618. (let* ((prefixnodir (file-name-nondirectory prefix))
  10619. (absprefix (expand-file-name prefix dir))
  10620. (todir (file-name-directory absprefix))
  10621. (opt org-format-latex-options)
  10622. (matchers (plist-get opt :matchers))
  10623. (re-list org-latex-regexps)
  10624. (cnt 0) txt link beg end re e checkdir
  10625. m n block linkfile movefile ov)
  10626. ;; Check if there are old images files with this prefix, and remove them
  10627. (when (file-directory-p todir)
  10628. (mapc 'delete-file
  10629. (directory-files
  10630. todir 'full
  10631. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  10632. ;; Check the different regular expressions
  10633. (while (setq e (pop re-list))
  10634. (setq m (car e) re (nth 1 e) n (nth 2 e)
  10635. block (if (nth 3 e) "\n\n" ""))
  10636. (when (member m matchers)
  10637. (goto-char (point-min))
  10638. (while (re-search-forward re nil t)
  10639. (when (or (not at) (equal (cdr at) (match-beginning n)))
  10640. (setq txt (match-string n)
  10641. beg (match-beginning n) end (match-end n)
  10642. cnt (1+ cnt)
  10643. linkfile (format "%s_%04d.png" prefix cnt)
  10644. movefile (format "%s_%04d.png" absprefix cnt)
  10645. link (concat block "[[file:" linkfile "]]" block))
  10646. (if msg (message msg cnt))
  10647. (goto-char beg)
  10648. (unless checkdir ; make sure the directory exists
  10649. (setq checkdir t)
  10650. (or (file-directory-p todir) (make-directory todir)))
  10651. (org-create-formula-image
  10652. txt movefile opt forbuffer)
  10653. (if overlays
  10654. (progn
  10655. (setq ov (org-make-overlay beg end))
  10656. (if (featurep 'xemacs)
  10657. (progn
  10658. (org-overlay-put ov 'invisible t)
  10659. (org-overlay-put
  10660. ov 'end-glyph
  10661. (make-glyph (vector 'png :file movefile))))
  10662. (org-overlay-put
  10663. ov 'display
  10664. (list 'image :type 'png :file movefile :ascent 'center)))
  10665. (push ov org-latex-fragment-image-overlays)
  10666. (goto-char end))
  10667. (delete-region beg end)
  10668. (insert link))))))))
  10669. ;; This function borrows from Ganesh Swami's latex2png.el
  10670. (defun org-create-formula-image (string tofile options buffer)
  10671. (let* ((tmpdir (if (featurep 'xemacs)
  10672. (temp-directory)
  10673. temporary-file-directory))
  10674. (texfilebase (make-temp-name
  10675. (expand-file-name "orgtex" tmpdir)))
  10676. (texfile (concat texfilebase ".tex"))
  10677. (dvifile (concat texfilebase ".dvi"))
  10678. (pngfile (concat texfilebase ".png"))
  10679. (fnh (if (featurep 'xemacs)
  10680. (font-height (get-face-font 'default))
  10681. (face-attribute 'default :height nil)))
  10682. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  10683. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  10684. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  10685. "Black"))
  10686. (bg (or (plist-get options (if buffer :background :html-background))
  10687. "Transparent")))
  10688. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  10689. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  10690. (with-temp-file texfile
  10691. (insert org-format-latex-header
  10692. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  10693. (let ((dir default-directory))
  10694. (condition-case nil
  10695. (progn
  10696. (cd tmpdir)
  10697. (call-process "latex" nil nil nil texfile))
  10698. (error nil))
  10699. (cd dir))
  10700. (if (not (file-exists-p dvifile))
  10701. (progn (message "Failed to create dvi file from %s" texfile) nil)
  10702. (call-process "dvipng" nil nil nil
  10703. "-E" "-fg" fg "-bg" bg
  10704. "-D" dpi
  10705. ;;"-x" scale "-y" scale
  10706. "-T" "tight"
  10707. "-o" pngfile
  10708. dvifile)
  10709. (if (not (file-exists-p pngfile))
  10710. (progn (message "Failed to create png file from %s" texfile) nil)
  10711. ;; Use the requested file name and clean up
  10712. (copy-file pngfile tofile 'replace)
  10713. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  10714. (delete-file (concat texfilebase e)))
  10715. pngfile))))
  10716. (defun org-dvipng-color (attr)
  10717. "Return an rgb color specification for dvipng."
  10718. (apply 'format "rgb %s %s %s"
  10719. (mapcar 'org-normalize-color
  10720. (color-values (face-attribute 'default attr nil)))))
  10721. (defun org-normalize-color (value)
  10722. "Return string to be used as color value for an RGB component."
  10723. (format "%g" (/ value 65535.0)))
  10724. ;;;; Key bindings
  10725. ;; Make `C-c C-x' a prefix key
  10726. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  10727. ;; TAB key with modifiers
  10728. (org-defkey org-mode-map "\C-i" 'org-cycle)
  10729. (org-defkey org-mode-map [(tab)] 'org-cycle)
  10730. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  10731. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  10732. (org-defkey org-mode-map "\M-\t" 'org-complete)
  10733. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  10734. ;; The following line is necessary under Suse GNU/Linux
  10735. (unless (featurep 'xemacs)
  10736. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  10737. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  10738. (define-key org-mode-map [backtab] 'org-shifttab)
  10739. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  10740. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  10741. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  10742. ;; Cursor keys with modifiers
  10743. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  10744. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  10745. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  10746. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  10747. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  10748. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  10749. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  10750. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  10751. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  10752. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  10753. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  10754. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  10755. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  10756. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  10757. ;;; Extra keys for tty access.
  10758. ;; We only set them when really needed because otherwise the
  10759. ;; menus don't show the simple keys
  10760. (when (or (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  10761. (not window-system))
  10762. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  10763. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  10764. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  10765. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  10766. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  10767. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  10768. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  10769. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  10770. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  10771. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  10772. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  10773. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  10774. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  10775. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  10776. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  10777. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  10778. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  10779. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  10780. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  10781. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  10782. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  10783. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
  10784. ;; All the other keys
  10785. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  10786. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  10787. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree)
  10788. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  10789. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  10790. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  10791. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  10792. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  10793. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  10794. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  10795. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  10796. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  10797. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  10798. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  10799. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  10800. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  10801. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  10802. (org-defkey org-mode-map "\C-c\\" 'org-tags-sparse-tree) ; Minor-mode res.
  10803. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  10804. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  10805. (org-defkey org-mode-map [(control return)] 'org-insert-heading-after-current)
  10806. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  10807. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  10808. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  10809. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  10810. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  10811. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  10812. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  10813. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  10814. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  10815. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  10816. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  10817. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  10818. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  10819. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  10820. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  10821. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  10822. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  10823. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  10824. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  10825. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  10826. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  10827. (org-defkey org-mode-map "\C-c^" 'org-sort)
  10828. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  10829. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  10830. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  10831. (org-defkey org-mode-map "\C-m" 'org-return)
  10832. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  10833. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  10834. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  10835. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  10836. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  10837. (org-defkey org-mode-map "\C-c'" 'org-table-edit-formulas)
  10838. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  10839. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  10840. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  10841. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  10842. (org-defkey org-mode-map "\C-c\C-q" 'org-table-wrap-region)
  10843. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  10844. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  10845. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  10846. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  10847. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  10848. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-cut-special)
  10849. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  10850. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  10851. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  10852. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  10853. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  10854. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  10855. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  10856. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  10857. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  10858. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  10859. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  10860. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  10861. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  10862. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  10863. (org-defkey org-mode-map "\C-c\C-xr" 'org-insert-columns-dblock)
  10864. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  10865. (when (featurep 'xemacs)
  10866. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  10867. (defvar org-table-auto-blank-field) ; defined in org-table.el
  10868. (defun org-self-insert-command (N)
  10869. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  10870. If the cursor is in a table looking at whitespace, the whitespace is
  10871. overwritten, and the table is not marked as requiring realignment."
  10872. (interactive "p")
  10873. (if (and (org-table-p)
  10874. (progn
  10875. ;; check if we blank the field, and if that triggers align
  10876. (and (featurep 'org-table) org-table-auto-blank-field
  10877. (member last-command
  10878. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  10879. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  10880. ;; got extra space, this field does not determine column width
  10881. (let (org-table-may-need-update) (org-table-blank-field))
  10882. ;; no extra space, this field may determine column width
  10883. (org-table-blank-field)))
  10884. t)
  10885. (eq N 1)
  10886. (looking-at "[^|\n]* |"))
  10887. (let (org-table-may-need-update)
  10888. (goto-char (1- (match-end 0)))
  10889. (delete-backward-char 1)
  10890. (goto-char (match-beginning 0))
  10891. (self-insert-command N))
  10892. (setq org-table-may-need-update t)
  10893. (self-insert-command N)
  10894. (org-fix-tags-on-the-fly)))
  10895. (defun org-fix-tags-on-the-fly ()
  10896. (when (and (equal (char-after (point-at-bol)) ?*)
  10897. (org-on-heading-p))
  10898. (org-align-tags-here org-tags-column)))
  10899. (defun org-delete-backward-char (N)
  10900. "Like `delete-backward-char', insert whitespace at field end in tables.
  10901. When deleting backwards, in tables this function will insert whitespace in
  10902. front of the next \"|\" separator, to keep the table aligned. The table will
  10903. still be marked for re-alignment if the field did fill the entire column,
  10904. because, in this case the deletion might narrow the column."
  10905. (interactive "p")
  10906. (if (and (org-table-p)
  10907. (eq N 1)
  10908. (string-match "|" (buffer-substring (point-at-bol) (point)))
  10909. (looking-at ".*?|"))
  10910. (let ((pos (point))
  10911. (noalign (looking-at "[^|\n\r]* |"))
  10912. (c org-table-may-need-update))
  10913. (backward-delete-char N)
  10914. (skip-chars-forward "^|")
  10915. (insert " ")
  10916. (goto-char (1- pos))
  10917. ;; noalign: if there were two spaces at the end, this field
  10918. ;; does not determine the width of the column.
  10919. (if noalign (setq org-table-may-need-update c)))
  10920. (backward-delete-char N)
  10921. (org-fix-tags-on-the-fly)))
  10922. (defun org-delete-char (N)
  10923. "Like `delete-char', but insert whitespace at field end in tables.
  10924. When deleting characters, in tables this function will insert whitespace in
  10925. front of the next \"|\" separator, to keep the table aligned. The table will
  10926. still be marked for re-alignment if the field did fill the entire column,
  10927. because, in this case the deletion might narrow the column."
  10928. (interactive "p")
  10929. (if (and (org-table-p)
  10930. (not (bolp))
  10931. (not (= (char-after) ?|))
  10932. (eq N 1))
  10933. (if (looking-at ".*?|")
  10934. (let ((pos (point))
  10935. (noalign (looking-at "[^|\n\r]* |"))
  10936. (c org-table-may-need-update))
  10937. (replace-match (concat
  10938. (substring (match-string 0) 1 -1)
  10939. " |"))
  10940. (goto-char pos)
  10941. ;; noalign: if there were two spaces at the end, this field
  10942. ;; does not determine the width of the column.
  10943. (if noalign (setq org-table-may-need-update c)))
  10944. (delete-char N))
  10945. (delete-char N)
  10946. (org-fix-tags-on-the-fly)))
  10947. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  10948. (put 'org-self-insert-command 'delete-selection t)
  10949. (put 'orgtbl-self-insert-command 'delete-selection t)
  10950. (put 'org-delete-char 'delete-selection 'supersede)
  10951. (put 'org-delete-backward-char 'delete-selection 'supersede)
  10952. ;; Make `flyspell-mode' delay after some commands
  10953. (put 'org-self-insert-command 'flyspell-delayed t)
  10954. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  10955. (put 'org-delete-char 'flyspell-delayed t)
  10956. (put 'org-delete-backward-char 'flyspell-delayed t)
  10957. ;; Make pabbrev-mode expand after org-mode commands
  10958. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  10959. (put 'orgybl-self-insert-command 'pabbrev-expand-after-command t)
  10960. ;; How to do this: Measure non-white length of current string
  10961. ;; If equal to column width, we should realign.
  10962. (defun org-remap (map &rest commands)
  10963. "In MAP, remap the functions given in COMMANDS.
  10964. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  10965. (let (new old)
  10966. (while commands
  10967. (setq old (pop commands) new (pop commands))
  10968. (if (fboundp 'command-remapping)
  10969. (org-defkey map (vector 'remap old) new)
  10970. (substitute-key-definition old new map global-map)))))
  10971. (when (eq org-enable-table-editor 'optimized)
  10972. ;; If the user wants maximum table support, we need to hijack
  10973. ;; some standard editing functions
  10974. (org-remap org-mode-map
  10975. 'self-insert-command 'org-self-insert-command
  10976. 'delete-char 'org-delete-char
  10977. 'delete-backward-char 'org-delete-backward-char)
  10978. (org-defkey org-mode-map "|" 'org-force-self-insert))
  10979. (defun org-shiftcursor-error ()
  10980. "Throw an error because Shift-Cursor command was applied in wrong context."
  10981. (error "This command is active in special context like tables, headlines or timestamps"))
  10982. (defun org-shifttab (&optional arg)
  10983. "Global visibility cycling or move to previous table field.
  10984. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  10985. on context.
  10986. See the individual commands for more information."
  10987. (interactive "P")
  10988. (cond
  10989. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  10990. (arg (message "Content view to level: ")
  10991. (org-content (prefix-numeric-value arg))
  10992. (setq org-cycle-global-status 'overview))
  10993. (t (call-interactively 'org-global-cycle))))
  10994. (defun org-shiftmetaleft ()
  10995. "Promote subtree or delete table column.
  10996. Calls `org-promote-subtree', `org-outdent-item',
  10997. or `org-table-delete-column', depending on context.
  10998. See the individual commands for more information."
  10999. (interactive)
  11000. (cond
  11001. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  11002. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  11003. ((org-at-item-p) (call-interactively 'org-outdent-item))
  11004. (t (org-shiftcursor-error))))
  11005. (defun org-shiftmetaright ()
  11006. "Demote subtree or insert table column.
  11007. Calls `org-demote-subtree', `org-indent-item',
  11008. or `org-table-insert-column', depending on context.
  11009. See the individual commands for more information."
  11010. (interactive)
  11011. (cond
  11012. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  11013. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  11014. ((org-at-item-p) (call-interactively 'org-indent-item))
  11015. (t (org-shiftcursor-error))))
  11016. (defun org-shiftmetaup (&optional arg)
  11017. "Move subtree up or kill table row.
  11018. Calls `org-move-subtree-up' or `org-table-kill-row' or
  11019. `org-move-item-up' depending on context. See the individual commands
  11020. for more information."
  11021. (interactive "P")
  11022. (cond
  11023. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  11024. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  11025. ((org-at-item-p) (call-interactively 'org-move-item-up))
  11026. (t (org-shiftcursor-error))))
  11027. (defun org-shiftmetadown (&optional arg)
  11028. "Move subtree down or insert table row.
  11029. Calls `org-move-subtree-down' or `org-table-insert-row' or
  11030. `org-move-item-down', depending on context. See the individual
  11031. commands for more information."
  11032. (interactive "P")
  11033. (cond
  11034. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  11035. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  11036. ((org-at-item-p) (call-interactively 'org-move-item-down))
  11037. (t (org-shiftcursor-error))))
  11038. (defun org-metaleft (&optional arg)
  11039. "Promote heading or move table column to left.
  11040. Calls `org-do-promote' or `org-table-move-column', depending on context.
  11041. With no specific context, calls the Emacs default `backward-word'.
  11042. See the individual commands for more information."
  11043. (interactive "P")
  11044. (cond
  11045. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  11046. ((or (org-on-heading-p) (org-region-active-p))
  11047. (call-interactively 'org-do-promote))
  11048. ((org-at-item-p) (call-interactively 'org-outdent-item))
  11049. (t (call-interactively 'backward-word))))
  11050. (defun org-metaright (&optional arg)
  11051. "Demote subtree or move table column to right.
  11052. Calls `org-do-demote' or `org-table-move-column', depending on context.
  11053. With no specific context, calls the Emacs default `forward-word'.
  11054. See the individual commands for more information."
  11055. (interactive "P")
  11056. (cond
  11057. ((org-at-table-p) (call-interactively 'org-table-move-column))
  11058. ((or (org-on-heading-p) (org-region-active-p))
  11059. (call-interactively 'org-do-demote))
  11060. ((org-at-item-p) (call-interactively 'org-indent-item))
  11061. (t (call-interactively 'forward-word))))
  11062. (defun org-metaup (&optional arg)
  11063. "Move subtree up or move table row up.
  11064. Calls `org-move-subtree-up' or `org-table-move-row' or
  11065. `org-move-item-up', depending on context. See the individual commands
  11066. for more information."
  11067. (interactive "P")
  11068. (cond
  11069. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  11070. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  11071. ((org-at-item-p) (call-interactively 'org-move-item-up))
  11072. (t (transpose-lines 1) (beginning-of-line -1))))
  11073. (defun org-metadown (&optional arg)
  11074. "Move subtree down or move table row down.
  11075. Calls `org-move-subtree-down' or `org-table-move-row' or
  11076. `org-move-item-down', depending on context. See the individual
  11077. commands for more information."
  11078. (interactive "P")
  11079. (cond
  11080. ((org-at-table-p) (call-interactively 'org-table-move-row))
  11081. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  11082. ((org-at-item-p) (call-interactively 'org-move-item-down))
  11083. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  11084. (defun org-shiftup (&optional arg)
  11085. "Increase item in timestamp or increase priority of current headline.
  11086. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  11087. depending on context. See the individual commands for more information."
  11088. (interactive "P")
  11089. (cond
  11090. ((org-at-timestamp-p t)
  11091. (call-interactively (if org-edit-timestamp-down-means-later
  11092. 'org-timestamp-down 'org-timestamp-up)))
  11093. ((org-on-heading-p) (call-interactively 'org-priority-up))
  11094. ((org-at-item-p) (call-interactively 'org-previous-item))
  11095. ((org-clocktable-try-shift 'up arg))
  11096. (t (call-interactively 'org-beginning-of-item) (beginning-of-line 1))))
  11097. (defun org-shiftdown (&optional arg)
  11098. "Decrease item in timestamp or decrease priority of current headline.
  11099. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  11100. depending on context. See the individual commands for more information."
  11101. (interactive "P")
  11102. (cond
  11103. ((org-at-timestamp-p t)
  11104. (call-interactively (if org-edit-timestamp-down-means-later
  11105. 'org-timestamp-up 'org-timestamp-down)))
  11106. ((org-on-heading-p) (call-interactively 'org-priority-down))
  11107. ((org-clocktable-try-shift 'down arg))
  11108. (t (call-interactively 'org-next-item))))
  11109. (defun org-shiftright (&optional arg)
  11110. "Next TODO keyword or timestamp one day later, depending on context."
  11111. (interactive "P")
  11112. (cond
  11113. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  11114. ((org-on-heading-p) (org-call-with-arg 'org-todo 'right))
  11115. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet nil))
  11116. ((org-at-property-p) (call-interactively 'org-property-next-allowed-value))
  11117. ((org-clocktable-try-shift 'right arg))
  11118. (t (org-shiftcursor-error))))
  11119. (defun org-shiftleft (&optional arg)
  11120. "Previous TODO keyword or timestamp one day earlier, depending on context."
  11121. (interactive "P")
  11122. (cond
  11123. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  11124. ((org-on-heading-p) (org-call-with-arg 'org-todo 'left))
  11125. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet 'previous))
  11126. ((org-at-property-p)
  11127. (call-interactively 'org-property-previous-allowed-value))
  11128. ((org-clocktable-try-shift 'left arg))
  11129. (t (org-shiftcursor-error))))
  11130. (defun org-shiftcontrolright ()
  11131. "Switch to next TODO set."
  11132. (interactive)
  11133. (cond
  11134. ((org-on-heading-p) (org-call-with-arg 'org-todo 'nextset))
  11135. (t (org-shiftcursor-error))))
  11136. (defun org-shiftcontrolleft ()
  11137. "Switch to previous TODO set."
  11138. (interactive)
  11139. (cond
  11140. ((org-on-heading-p) (org-call-with-arg 'org-todo 'previousset))
  11141. (t (org-shiftcursor-error))))
  11142. (defun org-ctrl-c-ret ()
  11143. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  11144. (interactive)
  11145. (cond
  11146. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  11147. (t (call-interactively 'org-insert-heading))))
  11148. (defun org-copy-special ()
  11149. "Copy region in table or copy current subtree.
  11150. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  11151. See the individual commands for more information."
  11152. (interactive)
  11153. (call-interactively
  11154. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  11155. (defun org-cut-special ()
  11156. "Cut region in table or cut current subtree.
  11157. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  11158. See the individual commands for more information."
  11159. (interactive)
  11160. (call-interactively
  11161. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  11162. (defun org-paste-special (arg)
  11163. "Paste rectangular region into table, or past subtree relative to level.
  11164. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  11165. See the individual commands for more information."
  11166. (interactive "P")
  11167. (if (org-at-table-p)
  11168. (org-table-paste-rectangle)
  11169. (org-paste-subtree arg)))
  11170. (defun org-ctrl-c-ctrl-c (&optional arg)
  11171. "Set tags in headline, or update according to changed information at point.
  11172. This command does many different things, depending on context:
  11173. - If the cursor is in a headline, prompt for tags and insert them
  11174. into the current line, aligned to `org-tags-column'. When called
  11175. with prefix arg, realign all tags in the current buffer.
  11176. - If the cursor is in one of the special #+KEYWORD lines, this
  11177. triggers scanning the buffer for these lines and updating the
  11178. information.
  11179. - If the cursor is inside a table, realign the table. This command
  11180. works even if the automatic table editor has been turned off.
  11181. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  11182. the entire table.
  11183. - If the cursor is a the beginning of a dynamic block, update it.
  11184. - If the cursor is inside a table created by the table.el package,
  11185. activate that table.
  11186. - If the current buffer is a remember buffer, close note and file it.
  11187. with a prefix argument, file it without further interaction to the default
  11188. location.
  11189. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  11190. links in this buffer.
  11191. - If the cursor is on a numbered item in a plain list, renumber the
  11192. ordered list.
  11193. - If the cursor is on a checkbox, toggle it."
  11194. (interactive "P")
  11195. (let ((org-enable-table-editor t))
  11196. (cond
  11197. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  11198. org-occur-highlights
  11199. org-latex-fragment-image-overlays)
  11200. (and (boundp 'org-clock-overlays) (org-remove-clock-overlays))
  11201. (org-remove-occur-highlights)
  11202. (org-remove-latex-fragment-image-overlays)
  11203. (message "Temporary highlights/overlays removed from current buffer"))
  11204. ((and (local-variable-p 'org-finish-function (current-buffer))
  11205. (fboundp org-finish-function))
  11206. (funcall org-finish-function))
  11207. ((org-at-property-p)
  11208. (call-interactively 'org-property-action))
  11209. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  11210. ((org-on-heading-p) (call-interactively 'org-set-tags))
  11211. ((org-at-table.el-p)
  11212. (require 'table)
  11213. (beginning-of-line 1)
  11214. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  11215. (call-interactively 'table-recognize-table))
  11216. ((org-at-table-p)
  11217. (org-table-maybe-eval-formula)
  11218. (if arg
  11219. (call-interactively 'org-table-recalculate)
  11220. (org-table-maybe-recalculate-line))
  11221. (call-interactively 'org-table-align))
  11222. ((org-at-item-checkbox-p)
  11223. (call-interactively 'org-toggle-checkbox))
  11224. ((org-at-item-p)
  11225. (call-interactively 'org-maybe-renumber-ordered-list))
  11226. ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
  11227. ;; Dynamic block
  11228. (beginning-of-line 1)
  11229. (org-update-dblock))
  11230. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  11231. (cond
  11232. ((equal (match-string 1) "TBLFM")
  11233. ;; Recalculate the table before this line
  11234. (save-excursion
  11235. (beginning-of-line 1)
  11236. (skip-chars-backward " \r\n\t")
  11237. (if (org-at-table-p)
  11238. (org-call-with-arg 'org-table-recalculate t))))
  11239. (t
  11240. (call-interactively 'org-mode-restart))))
  11241. (t (error "C-c C-c can do nothing useful at this location.")))))
  11242. (defun org-mode-restart ()
  11243. "Restart Org-mode, to scan again for special lines.
  11244. Also updates the keyword regular expressions."
  11245. (interactive)
  11246. (let ((org-inhibit-startup t)) (org-mode))
  11247. (message "Org-mode restarted to refresh keyword and special line setup"))
  11248. (defun org-kill-note-or-show-branches ()
  11249. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  11250. (interactive)
  11251. (if (not org-finish-function)
  11252. (call-interactively 'show-branches)
  11253. (let ((org-note-abort t))
  11254. (funcall org-finish-function))))
  11255. (defun org-return (&optional indent)
  11256. "Goto next table row or insert a newline.
  11257. Calls `org-table-next-row' or `newline', depending on context.
  11258. See the individual commands for more information."
  11259. (interactive)
  11260. (cond
  11261. ((bobp) (if indent (newline-and-indent) (newline)))
  11262. ((and (org-at-heading-p)
  11263. (looking-at
  11264. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  11265. (org-show-entry)
  11266. (end-of-line 1)
  11267. (newline))
  11268. ((org-at-table-p)
  11269. (org-table-justify-field-maybe)
  11270. (call-interactively 'org-table-next-row))
  11271. (t (if indent (newline-and-indent) (newline)))))
  11272. (defun org-return-indent ()
  11273. "Goto next table row or insert a newline and indent.
  11274. Calls `org-table-next-row' or `newline-and-indent', depending on
  11275. context. See the individual commands for more information."
  11276. (interactive)
  11277. (org-return t))
  11278. (defun org-ctrl-c-star ()
  11279. "Compute table, or change heading status of lines.
  11280. Calls `org-table-recalculate' or `org-toggle-region-headlines',
  11281. depending on context. This will also turn a plain list item or a normal
  11282. line into a subheading."
  11283. (interactive)
  11284. (cond
  11285. ((org-at-table-p)
  11286. (call-interactively 'org-table-recalculate))
  11287. ((org-region-active-p)
  11288. ;; Convert all lines in region to list items
  11289. (call-interactively 'org-toggle-region-headings))
  11290. ((org-on-heading-p)
  11291. (org-toggle-region-headings (point-at-bol)
  11292. (min (1+ (point-at-eol)) (point-max))))
  11293. ((org-at-item-p)
  11294. ;; Convert to heading
  11295. (let ((level (save-match-data
  11296. (save-excursion
  11297. (condition-case nil
  11298. (progn
  11299. (org-back-to-heading t)
  11300. (funcall outline-level))
  11301. (error 0))))))
  11302. (replace-match
  11303. (concat (make-string (org-get-valid-level level 1) ?*) " ") t t)))
  11304. (t (org-toggle-region-headings (point-at-bol)
  11305. (min (1+ (point-at-eol)) (point-max))))))
  11306. (defun org-ctrl-c-minus ()
  11307. "Insert separator line in table or modify bullet status of line.
  11308. Also turns a plain line or a region of lines into list items.
  11309. Calls `org-table-insert-hline', `org-toggle-region-items', or
  11310. `org-cycle-list-bullet', depending on context."
  11311. (interactive)
  11312. (cond
  11313. ((org-at-table-p)
  11314. (call-interactively 'org-table-insert-hline))
  11315. ((org-on-heading-p)
  11316. ;; Convert to item
  11317. (save-excursion
  11318. (beginning-of-line 1)
  11319. (if (looking-at "\\*+ ")
  11320. (replace-match (concat (make-string (- (match-end 0) (point) 1) ?\ ) "- ")))))
  11321. ((org-region-active-p)
  11322. ;; Convert all lines in region to list items
  11323. (call-interactively 'org-toggle-region-items))
  11324. ((org-in-item-p)
  11325. (call-interactively 'org-cycle-list-bullet))
  11326. (t (org-toggle-region-items (point-at-bol)
  11327. (min (1+ (point-at-eol)) (point-max))))))
  11328. (defun org-toggle-region-items (beg end)
  11329. "Convert all lines in region to list items.
  11330. If the first line is already an item, convert all list items in the region
  11331. to normal lines."
  11332. (interactive "r")
  11333. (let (l2 l)
  11334. (save-excursion
  11335. (goto-char end)
  11336. (setq l2 (org-current-line))
  11337. (goto-char beg)
  11338. (beginning-of-line 1)
  11339. (setq l (1- (org-current-line)))
  11340. (if (org-at-item-p)
  11341. ;; We already have items, de-itemize
  11342. (while (< (setq l (1+ l)) l2)
  11343. (when (org-at-item-p)
  11344. (goto-char (match-beginning 2))
  11345. (delete-region (match-beginning 2) (match-end 2))
  11346. (and (looking-at "[ \t]+") (replace-match "")))
  11347. (beginning-of-line 2))
  11348. (while (< (setq l (1+ l)) l2)
  11349. (unless (org-at-item-p)
  11350. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  11351. (replace-match "\\1- \\2")))
  11352. (beginning-of-line 2))))))
  11353. (defun org-toggle-region-headings (beg end)
  11354. "Convert all lines in region to list items.
  11355. If the first line is already an item, convert all list items in the region
  11356. to normal lines."
  11357. (interactive "r")
  11358. (let (l2 l)
  11359. (save-excursion
  11360. (goto-char end)
  11361. (setq l2 (org-current-line))
  11362. (goto-char beg)
  11363. (beginning-of-line 1)
  11364. (setq l (1- (org-current-line)))
  11365. (if (org-on-heading-p)
  11366. ;; We already have headlines, de-star them
  11367. (while (< (setq l (1+ l)) l2)
  11368. (when (org-on-heading-p t)
  11369. (and (looking-at outline-regexp) (replace-match "")))
  11370. (beginning-of-line 2))
  11371. (let* ((stars (save-excursion
  11372. (re-search-backward org-complex-heading-regexp nil t)
  11373. (or (match-string 1) "*")))
  11374. (add-stars (if org-odd-levels-only "**" "*"))
  11375. (rpl (concat stars add-stars " \\2")))
  11376. (while (< (setq l (1+ l)) l2)
  11377. (unless (org-on-heading-p)
  11378. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  11379. (replace-match rpl)))
  11380. (beginning-of-line 2)))))))
  11381. (defun org-meta-return (&optional arg)
  11382. "Insert a new heading or wrap a region in a table.
  11383. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  11384. See the individual commands for more information."
  11385. (interactive "P")
  11386. (cond
  11387. ((org-at-table-p)
  11388. (call-interactively 'org-table-wrap-region))
  11389. (t (call-interactively 'org-insert-heading))))
  11390. ;;; Menu entries
  11391. ;; Define the Org-mode menus
  11392. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  11393. '("Tbl"
  11394. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  11395. ["Next Field" org-cycle (org-at-table-p)]
  11396. ["Previous Field" org-shifttab (org-at-table-p)]
  11397. ["Next Row" org-return (org-at-table-p)]
  11398. "--"
  11399. ["Blank Field" org-table-blank-field (org-at-table-p)]
  11400. ["Edit Field" org-table-edit-field (org-at-table-p)]
  11401. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  11402. "--"
  11403. ("Column"
  11404. ["Move Column Left" org-metaleft (org-at-table-p)]
  11405. ["Move Column Right" org-metaright (org-at-table-p)]
  11406. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  11407. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  11408. ("Row"
  11409. ["Move Row Up" org-metaup (org-at-table-p)]
  11410. ["Move Row Down" org-metadown (org-at-table-p)]
  11411. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  11412. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  11413. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  11414. "--"
  11415. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  11416. ("Rectangle"
  11417. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  11418. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  11419. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  11420. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  11421. "--"
  11422. ("Calculate"
  11423. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  11424. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  11425. ["Edit Formulas" org-table-edit-formulas (org-at-table-p)]
  11426. "--"
  11427. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  11428. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  11429. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  11430. "--"
  11431. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  11432. "--"
  11433. ["Sum Column/Rectangle" org-table-sum
  11434. (or (org-at-table-p) (org-region-active-p))]
  11435. ["Which Column?" org-table-current-column (org-at-table-p)])
  11436. ["Debug Formulas"
  11437. org-table-toggle-formula-debugger
  11438. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  11439. ["Show Col/Row Numbers"
  11440. org-table-toggle-coordinate-overlays
  11441. :style toggle
  11442. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  11443. "--"
  11444. ["Create" org-table-create (and (not (org-at-table-p))
  11445. org-enable-table-editor)]
  11446. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  11447. ["Import from File" org-table-import (not (org-at-table-p))]
  11448. ["Export to File" org-table-export (org-at-table-p)]
  11449. "--"
  11450. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  11451. (easy-menu-define org-org-menu org-mode-map "Org menu"
  11452. '("Org"
  11453. ("Show/Hide"
  11454. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  11455. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  11456. ["Sparse Tree..." org-sparse-tree t]
  11457. ["Reveal Context" org-reveal t]
  11458. ["Show All" show-all t]
  11459. "--"
  11460. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  11461. "--"
  11462. ["New Heading" org-insert-heading t]
  11463. ("Navigate Headings"
  11464. ["Up" outline-up-heading t]
  11465. ["Next" outline-next-visible-heading t]
  11466. ["Previous" outline-previous-visible-heading t]
  11467. ["Next Same Level" outline-forward-same-level t]
  11468. ["Previous Same Level" outline-backward-same-level t]
  11469. "--"
  11470. ["Jump" org-goto t])
  11471. ("Edit Structure"
  11472. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  11473. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  11474. "--"
  11475. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  11476. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  11477. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  11478. "--"
  11479. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  11480. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  11481. ["Demote Heading" org-metaright (not (org-at-table-p))]
  11482. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  11483. "--"
  11484. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  11485. "--"
  11486. ["Convert to odd levels" org-convert-to-odd-levels t]
  11487. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  11488. ("Editing"
  11489. ["Emphasis..." org-emphasize t])
  11490. ("Archive"
  11491. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  11492. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  11493. ; :active t :keys "C-u C-c C-x C-a"]
  11494. ["Sparse trees open ARCHIVE trees"
  11495. (setq org-sparse-tree-open-archived-trees
  11496. (not org-sparse-tree-open-archived-trees))
  11497. :style toggle :selected org-sparse-tree-open-archived-trees]
  11498. ["Cycling opens ARCHIVE trees"
  11499. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  11500. :style toggle :selected org-cycle-open-archived-trees]
  11501. ["Agenda includes ARCHIVE trees"
  11502. (setq org-agenda-skip-archived-trees (not org-agenda-skip-archived-trees))
  11503. :style toggle :selected (not org-agenda-skip-archived-trees)]
  11504. "--"
  11505. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  11506. ; ["Check and Move Children" (org-archive-subtree '(4))
  11507. ; :active t :keys "C-u C-c C-x C-s"]
  11508. )
  11509. "--"
  11510. ("TODO Lists"
  11511. ["TODO/DONE/-" org-todo t]
  11512. ("Select keyword"
  11513. ["Next keyword" org-shiftright (org-on-heading-p)]
  11514. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  11515. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  11516. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  11517. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  11518. ["Show TODO Tree" org-show-todo-tree t]
  11519. ["Global TODO list" org-todo-list t]
  11520. "--"
  11521. ["Set Priority" org-priority t]
  11522. ["Priority Up" org-shiftup t]
  11523. ["Priority Down" org-shiftdown t])
  11524. ("TAGS and Properties"
  11525. ["Set Tags" 'org-ctrl-c-ctrl-c (org-at-heading-p)]
  11526. ["Change tag in region" 'org-change-tag-in-region (org-region-active-p)]
  11527. "--"
  11528. ["Set property" 'org-set-property t]
  11529. ["Column view of properties" org-columns t]
  11530. ["Insert Column View DBlock" org-insert-columns-dblock t])
  11531. ("Dates and Scheduling"
  11532. ["Timestamp" org-time-stamp t]
  11533. ["Timestamp (inactive)" org-time-stamp-inactive t]
  11534. ("Change Date"
  11535. ["1 Day Later" org-shiftright t]
  11536. ["1 Day Earlier" org-shiftleft t]
  11537. ["1 ... Later" org-shiftup t]
  11538. ["1 ... Earlier" org-shiftdown t])
  11539. ["Compute Time Range" org-evaluate-time-range t]
  11540. ["Schedule Item" org-schedule t]
  11541. ["Deadline" org-deadline t]
  11542. "--"
  11543. ["Custom time format" org-toggle-time-stamp-overlays
  11544. :style radio :selected org-display-custom-times]
  11545. "--"
  11546. ["Goto Calendar" org-goto-calendar t]
  11547. ["Date from Calendar" org-date-from-calendar t])
  11548. ("Logging work"
  11549. ["Clock in" org-clock-in t]
  11550. ["Clock out" org-clock-out t]
  11551. ["Clock cancel" org-clock-cancel t]
  11552. ["Goto running clock" org-clock-goto t]
  11553. ["Display times" org-clock-display t]
  11554. ["Create clock table" org-clock-report t]
  11555. "--"
  11556. ["Record DONE time"
  11557. (progn (setq org-log-done (not org-log-done))
  11558. (message "Switching to %s will %s record a timestamp"
  11559. (car org-done-keywords)
  11560. (if org-log-done "automatically" "not")))
  11561. :style toggle :selected org-log-done])
  11562. "--"
  11563. ["Agenda Command..." org-agenda t]
  11564. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  11565. ("File List for Agenda")
  11566. ("Special views current file"
  11567. ["TODO Tree" org-show-todo-tree t]
  11568. ["Check Deadlines" org-check-deadlines t]
  11569. ["Timeline" org-timeline t]
  11570. ["Tags Tree" org-tags-sparse-tree t])
  11571. "--"
  11572. ("Hyperlinks"
  11573. ["Store Link (Global)" org-store-link t]
  11574. ["Insert Link" org-insert-link t]
  11575. ["Follow Link" org-open-at-point t]
  11576. "--"
  11577. ["Next link" org-next-link t]
  11578. ["Previous link" org-previous-link t]
  11579. "--"
  11580. ["Descriptive Links"
  11581. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  11582. :style radio
  11583. :selected (member '(org-link) buffer-invisibility-spec)]
  11584. ["Literal Links"
  11585. (progn
  11586. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  11587. :style radio
  11588. :selected (not (member '(org-link) buffer-invisibility-spec))])
  11589. "--"
  11590. ["Export/Publish..." org-export t]
  11591. ("LaTeX"
  11592. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  11593. :selected org-cdlatex-mode]
  11594. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  11595. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  11596. ["Modify math symbol" org-cdlatex-math-modify
  11597. (org-inside-LaTeX-fragment-p)]
  11598. ["Export LaTeX fragments as images"
  11599. (if (featurep 'org-exp)
  11600. (setq org-export-with-LaTeX-fragments
  11601. (not org-export-with-LaTeX-fragments))
  11602. (require 'org-exp))
  11603. :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
  11604. org-export-with-LaTeX-fragments)])
  11605. "--"
  11606. ("Documentation"
  11607. ["Show Version" org-version t]
  11608. ["Info Documentation" org-info t])
  11609. ("Customize"
  11610. ["Browse Org Group" org-customize t]
  11611. "--"
  11612. ["Expand This Menu" org-create-customize-menu
  11613. (fboundp 'customize-menu-create)])
  11614. "--"
  11615. ["Refresh setup" org-mode-restart t]
  11616. ))
  11617. (defun org-info (&optional node)
  11618. "Read documentation for Org-mode in the info system.
  11619. With optional NODE, go directly to that node."
  11620. (interactive)
  11621. (info (format "(org)%s" (or node ""))))
  11622. (defun org-install-agenda-files-menu ()
  11623. (let ((bl (buffer-list)))
  11624. (save-excursion
  11625. (while bl
  11626. (set-buffer (pop bl))
  11627. (if (org-mode-p) (setq bl nil)))
  11628. (when (org-mode-p)
  11629. (easy-menu-change
  11630. '("Org") "File List for Agenda"
  11631. (append
  11632. (list
  11633. ["Edit File List" (org-edit-agenda-file-list) t]
  11634. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  11635. ["Remove Current File from List" org-remove-file t]
  11636. ["Cycle through agenda files" org-cycle-agenda-files t]
  11637. ["Occur in all agenda files" org-occur-in-agenda-files t]
  11638. "--")
  11639. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  11640. ;;;; Documentation
  11641. (defun org-require-autoloaded-modules ()
  11642. (interactive)
  11643. (mapc 'require
  11644. '(org-agenda org-archive org-clock org-colview
  11645. org-exp org-export-latex org-publish
  11646. org-remember org-table)))
  11647. (defun org-customize ()
  11648. "Call the customize function with org as argument."
  11649. (interactive)
  11650. (org-load-modules-maybe)
  11651. (org-require-autoloaded-modules)
  11652. (customize-browse 'org))
  11653. (defun org-create-customize-menu ()
  11654. "Create a full customization menu for Org-mode, insert it into the menu."
  11655. (interactive)
  11656. (org-load-modules-maybe)
  11657. (org-require-autoloaded-modules)
  11658. (if (fboundp 'customize-menu-create)
  11659. (progn
  11660. (easy-menu-change
  11661. '("Org") "Customize"
  11662. `(["Browse Org group" org-customize t]
  11663. "--"
  11664. ,(customize-menu-create 'org)
  11665. ["Set" Custom-set t]
  11666. ["Save" Custom-save t]
  11667. ["Reset to Current" Custom-reset-current t]
  11668. ["Reset to Saved" Custom-reset-saved t]
  11669. ["Reset to Standard Settings" Custom-reset-standard t]))
  11670. (message "\"Org\"-menu now contains full customization menu"))
  11671. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  11672. ;;;; Miscellaneous stuff
  11673. ;;; Generally useful functions
  11674. (defun org-plist-delete (plist property)
  11675. "Delete PROPERTY from PLIST.
  11676. This is in contrast to merely setting it to 0."
  11677. (let (p)
  11678. (while plist
  11679. (if (not (eq property (car plist)))
  11680. (setq p (plist-put p (car plist) (nth 1 plist))))
  11681. (setq plist (cddr plist)))
  11682. p))
  11683. (defun org-force-self-insert (N)
  11684. "Needed to enforce self-insert under remapping."
  11685. (interactive "p")
  11686. (self-insert-command N))
  11687. (defun org-string-width (s)
  11688. "Compute width of string, ignoring invisible characters.
  11689. This ignores character with invisibility property `org-link', and also
  11690. characters with property `org-cwidth', because these will become invisible
  11691. upon the next fontification round."
  11692. (let (b l)
  11693. (when (or (eq t buffer-invisibility-spec)
  11694. (assq 'org-link buffer-invisibility-spec))
  11695. (while (setq b (text-property-any 0 (length s)
  11696. 'invisible 'org-link s))
  11697. (setq s (concat (substring s 0 b)
  11698. (substring s (or (next-single-property-change
  11699. b 'invisible s) (length s)))))))
  11700. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  11701. (setq s (concat (substring s 0 b)
  11702. (substring s (or (next-single-property-change
  11703. b 'org-cwidth s) (length s))))))
  11704. (setq l (string-width s) b -1)
  11705. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  11706. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  11707. l))
  11708. (defun org-base-buffer (buffer)
  11709. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  11710. (if (not buffer)
  11711. buffer
  11712. (or (buffer-base-buffer buffer)
  11713. buffer)))
  11714. (defun org-trim (s)
  11715. "Remove whitespace at beginning and end of string."
  11716. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  11717. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  11718. s)
  11719. (defun org-wrap (string &optional width lines)
  11720. "Wrap string to either a number of lines, or a width in characters.
  11721. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  11722. that costs. If there is a word longer than WIDTH, the text is actually
  11723. wrapped to the length of that word.
  11724. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  11725. many lines, whatever width that takes.
  11726. The return value is a list of lines, without newlines at the end."
  11727. (let* ((words (org-split-string string "[ \t\n]+"))
  11728. (maxword (apply 'max (mapcar 'org-string-width words)))
  11729. w ll)
  11730. (cond (width
  11731. (org-do-wrap words (max maxword width)))
  11732. (lines
  11733. (setq w maxword)
  11734. (setq ll (org-do-wrap words maxword))
  11735. (if (<= (length ll) lines)
  11736. ll
  11737. (setq ll words)
  11738. (while (> (length ll) lines)
  11739. (setq w (1+ w))
  11740. (setq ll (org-do-wrap words w)))
  11741. ll))
  11742. (t (error "Cannot wrap this")))))
  11743. (defun org-do-wrap (words width)
  11744. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  11745. (let (lines line)
  11746. (while words
  11747. (setq line (pop words))
  11748. (while (and words (< (+ (length line) (length (car words))) width))
  11749. (setq line (concat line " " (pop words))))
  11750. (setq lines (push line lines)))
  11751. (nreverse lines)))
  11752. (defun org-split-string (string &optional separators)
  11753. "Splits STRING into substrings at SEPARATORS.
  11754. No empty strings are returned if there are matches at the beginning
  11755. and end of string."
  11756. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  11757. (start 0)
  11758. notfirst
  11759. (list nil))
  11760. (while (and (string-match rexp string
  11761. (if (and notfirst
  11762. (= start (match-beginning 0))
  11763. (< start (length string)))
  11764. (1+ start) start))
  11765. (< (match-beginning 0) (length string)))
  11766. (setq notfirst t)
  11767. (or (eq (match-beginning 0) 0)
  11768. (and (eq (match-beginning 0) (match-end 0))
  11769. (eq (match-beginning 0) start))
  11770. (setq list
  11771. (cons (substring string start (match-beginning 0))
  11772. list)))
  11773. (setq start (match-end 0)))
  11774. (or (eq start (length string))
  11775. (setq list
  11776. (cons (substring string start)
  11777. list)))
  11778. (nreverse list)))
  11779. (defun org-context ()
  11780. "Return a list of contexts of the current cursor position.
  11781. If several contexts apply, all are returned.
  11782. Each context entry is a list with a symbol naming the context, and
  11783. two positions indicating start and end of the context. Possible
  11784. contexts are:
  11785. :headline anywhere in a headline
  11786. :headline-stars on the leading stars in a headline
  11787. :todo-keyword on a TODO keyword (including DONE) in a headline
  11788. :tags on the TAGS in a headline
  11789. :priority on the priority cookie in a headline
  11790. :item on the first line of a plain list item
  11791. :item-bullet on the bullet/number of a plain list item
  11792. :checkbox on the checkbox in a plain list item
  11793. :table in an org-mode table
  11794. :table-special on a special filed in a table
  11795. :table-table in a table.el table
  11796. :link on a hyperlink
  11797. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  11798. :target on a <<target>>
  11799. :radio-target on a <<<radio-target>>>
  11800. :latex-fragment on a LaTeX fragment
  11801. :latex-preview on a LaTeX fragment with overlayed preview image
  11802. This function expects the position to be visible because it uses font-lock
  11803. faces as a help to recognize the following contexts: :table-special, :link,
  11804. and :keyword."
  11805. (let* ((f (get-text-property (point) 'face))
  11806. (faces (if (listp f) f (list f)))
  11807. (p (point)) clist o)
  11808. ;; First the large context
  11809. (cond
  11810. ((org-on-heading-p t)
  11811. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  11812. (when (progn
  11813. (beginning-of-line 1)
  11814. (looking-at org-todo-line-tags-regexp))
  11815. (push (org-point-in-group p 1 :headline-stars) clist)
  11816. (push (org-point-in-group p 2 :todo-keyword) clist)
  11817. (push (org-point-in-group p 4 :tags) clist))
  11818. (goto-char p)
  11819. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  11820. (if (looking-at "\\[#[A-Z0-9]\\]")
  11821. (push (org-point-in-group p 0 :priority) clist)))
  11822. ((org-at-item-p)
  11823. (push (org-point-in-group p 2 :item-bullet) clist)
  11824. (push (list :item (point-at-bol)
  11825. (save-excursion (org-end-of-item) (point)))
  11826. clist)
  11827. (and (org-at-item-checkbox-p)
  11828. (push (org-point-in-group p 0 :checkbox) clist)))
  11829. ((org-at-table-p)
  11830. (push (list :table (org-table-begin) (org-table-end)) clist)
  11831. (if (memq 'org-formula faces)
  11832. (push (list :table-special
  11833. (previous-single-property-change p 'face)
  11834. (next-single-property-change p 'face)) clist)))
  11835. ((org-at-table-p 'any)
  11836. (push (list :table-table) clist)))
  11837. (goto-char p)
  11838. ;; Now the small context
  11839. (cond
  11840. ((org-at-timestamp-p)
  11841. (push (org-point-in-group p 0 :timestamp) clist))
  11842. ((memq 'org-link faces)
  11843. (push (list :link
  11844. (previous-single-property-change p 'face)
  11845. (next-single-property-change p 'face)) clist))
  11846. ((memq 'org-special-keyword faces)
  11847. (push (list :keyword
  11848. (previous-single-property-change p 'face)
  11849. (next-single-property-change p 'face)) clist))
  11850. ((org-on-target-p)
  11851. (push (org-point-in-group p 0 :target) clist)
  11852. (goto-char (1- (match-beginning 0)))
  11853. (if (looking-at org-radio-target-regexp)
  11854. (push (org-point-in-group p 0 :radio-target) clist))
  11855. (goto-char p))
  11856. ((setq o (car (delq nil
  11857. (mapcar
  11858. (lambda (x)
  11859. (if (memq x org-latex-fragment-image-overlays) x))
  11860. (org-overlays-at (point))))))
  11861. (push (list :latex-fragment
  11862. (org-overlay-start o) (org-overlay-end o)) clist)
  11863. (push (list :latex-preview
  11864. (org-overlay-start o) (org-overlay-end o)) clist))
  11865. ((org-inside-LaTeX-fragment-p)
  11866. ;; FIXME: positions wrong.
  11867. (push (list :latex-fragment (point) (point)) clist)))
  11868. (setq clist (nreverse (delq nil clist)))
  11869. clist))
  11870. ;; FIXME: Compare with at-regexp-p Do we need both?
  11871. (defun org-in-regexp (re &optional nlines visually)
  11872. "Check if point is inside a match of regexp.
  11873. Normally only the current line is checked, but you can include NLINES extra
  11874. lines both before and after point into the search.
  11875. If VISUALLY is set, require that the cursor is not after the match but
  11876. really on, so that the block visually is on the match."
  11877. (catch 'exit
  11878. (let ((pos (point))
  11879. (eol (point-at-eol (+ 1 (or nlines 0))))
  11880. (inc (if visually 1 0)))
  11881. (save-excursion
  11882. (beginning-of-line (- 1 (or nlines 0)))
  11883. (while (re-search-forward re eol t)
  11884. (if (and (<= (match-beginning 0) pos)
  11885. (>= (+ inc (match-end 0)) pos))
  11886. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  11887. (defun org-at-regexp-p (regexp)
  11888. "Is point inside a match of REGEXP in the current line?"
  11889. (catch 'exit
  11890. (save-excursion
  11891. (let ((pos (point)) (end (point-at-eol)))
  11892. (beginning-of-line 1)
  11893. (while (re-search-forward regexp end t)
  11894. (if (and (<= (match-beginning 0) pos)
  11895. (>= (match-end 0) pos))
  11896. (throw 'exit t)))
  11897. nil))))
  11898. (defun org-occur-in-agenda-files (regexp &optional nlines)
  11899. "Call `multi-occur' with buffers for all agenda files."
  11900. (interactive "sOrg-files matching: \np")
  11901. (let* ((files (org-agenda-files))
  11902. (tnames (mapcar 'file-truename files))
  11903. (extra org-agenda-text-search-extra-files)
  11904. f)
  11905. (when (eq (car extra) 'agenda-archives)
  11906. (setq extra (cdr extra))
  11907. (setq files (org-add-archive-files files)))
  11908. (while (setq f (pop extra))
  11909. (unless (member (file-truename f) tnames)
  11910. (add-to-list 'files f 'append)
  11911. (add-to-list 'tnames (file-truename f) 'append)))
  11912. (multi-occur
  11913. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  11914. regexp)))
  11915. (if (boundp 'occur-mode-find-occurrence-hook)
  11916. ;; Emacs 23
  11917. (add-hook 'occur-mode-find-occurrence-hook
  11918. (lambda ()
  11919. (when (org-mode-p)
  11920. (org-reveal))))
  11921. ;; Emacs 22
  11922. (defadvice occur-mode-goto-occurrence
  11923. (after org-occur-reveal activate)
  11924. (and (org-mode-p) (org-reveal)))
  11925. (defadvice occur-mode-goto-occurrence-other-window
  11926. (after org-occur-reveal activate)
  11927. (and (org-mode-p) (org-reveal)))
  11928. (defadvice occur-mode-display-occurrence
  11929. (after org-occur-reveal activate)
  11930. (when (org-mode-p)
  11931. (let ((pos (occur-mode-find-occurrence)))
  11932. (with-current-buffer (marker-buffer pos)
  11933. (save-excursion
  11934. (goto-char pos)
  11935. (org-reveal)))))))
  11936. (defun org-uniquify (list)
  11937. "Remove duplicate elements from LIST."
  11938. (let (res)
  11939. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  11940. res))
  11941. (defun org-delete-all (elts list)
  11942. "Remove all elements in ELTS from LIST."
  11943. (while elts
  11944. (setq list (delete (pop elts) list)))
  11945. list)
  11946. (defun org-back-over-empty-lines ()
  11947. "Move backwards over witespace, to the beginning of the first empty line.
  11948. Returns the number of empty lines passed."
  11949. (let ((pos (point)))
  11950. (skip-chars-backward " \t\n\r")
  11951. (beginning-of-line 2)
  11952. (goto-char (min (point) pos))
  11953. (count-lines (point) pos)))
  11954. (defun org-skip-whitespace ()
  11955. (skip-chars-forward " \t\n\r"))
  11956. (defun org-point-in-group (point group &optional context)
  11957. "Check if POINT is in match-group GROUP.
  11958. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  11959. match. If the match group does ot exist or point is not inside it,
  11960. return nil."
  11961. (and (match-beginning group)
  11962. (>= point (match-beginning group))
  11963. (<= point (match-end group))
  11964. (if context
  11965. (list context (match-beginning group) (match-end group))
  11966. t)))
  11967. (defun org-switch-to-buffer-other-window (&rest args)
  11968. "Switch to buffer in a second window on the current frame.
  11969. In particular, do not allow pop-up frames."
  11970. (let (pop-up-frames special-display-buffer-names special-display-regexps
  11971. special-display-function)
  11972. (apply 'switch-to-buffer-other-window args)))
  11973. (defun org-combine-plists (&rest plists)
  11974. "Create a single property list from all plists in PLISTS.
  11975. The process starts by copying the first list, and then setting properties
  11976. from the other lists. Settings in the last list are the most significant
  11977. ones and overrule settings in the other lists."
  11978. (let ((rtn (copy-sequence (pop plists)))
  11979. p v ls)
  11980. (while plists
  11981. (setq ls (pop plists))
  11982. (while ls
  11983. (setq p (pop ls) v (pop ls))
  11984. (setq rtn (plist-put rtn p v))))
  11985. rtn))
  11986. (defun org-move-line-down (arg)
  11987. "Move the current line down. With prefix argument, move it past ARG lines."
  11988. (interactive "p")
  11989. (let ((col (current-column))
  11990. beg end pos)
  11991. (beginning-of-line 1) (setq beg (point))
  11992. (beginning-of-line 2) (setq end (point))
  11993. (beginning-of-line (+ 1 arg))
  11994. (setq pos (move-marker (make-marker) (point)))
  11995. (insert (delete-and-extract-region beg end))
  11996. (goto-char pos)
  11997. (org-move-to-column col)))
  11998. (defun org-move-line-up (arg)
  11999. "Move the current line up. With prefix argument, move it past ARG lines."
  12000. (interactive "p")
  12001. (let ((col (current-column))
  12002. beg end pos)
  12003. (beginning-of-line 1) (setq beg (point))
  12004. (beginning-of-line 2) (setq end (point))
  12005. (beginning-of-line (- arg))
  12006. (setq pos (move-marker (make-marker) (point)))
  12007. (insert (delete-and-extract-region beg end))
  12008. (goto-char pos)
  12009. (org-move-to-column col)))
  12010. (defun org-replace-escapes (string table)
  12011. "Replace %-escapes in STRING with values in TABLE.
  12012. TABLE is an association list with keys like \"%a\" and string values.
  12013. The sequences in STRING may contain normal field width and padding information,
  12014. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  12015. so values can contain further %-escapes if they are define later in TABLE."
  12016. (let ((case-fold-search nil)
  12017. e re rpl)
  12018. (while (setq e (pop table))
  12019. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  12020. (while (string-match re string)
  12021. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  12022. (cdr e)))
  12023. (setq string (replace-match rpl t t string))))
  12024. string))
  12025. (defun org-sublist (list start end)
  12026. "Return a section of LIST, from START to END.
  12027. Counting starts at 1."
  12028. (let (rtn (c start))
  12029. (setq list (nthcdr (1- start) list))
  12030. (while (and list (<= c end))
  12031. (push (pop list) rtn)
  12032. (setq c (1+ c)))
  12033. (nreverse rtn)))
  12034. (defun org-find-base-buffer-visiting (file)
  12035. "Like `find-buffer-visiting' but alway return the base buffer and
  12036. not an indirect buffer."
  12037. (let ((buf (find-buffer-visiting file)))
  12038. (if buf
  12039. (or (buffer-base-buffer buf) buf)
  12040. nil)))
  12041. (defun org-image-file-name-regexp ()
  12042. "Return regexp matching the file names of images."
  12043. (if (fboundp 'image-file-name-regexp)
  12044. (image-file-name-regexp)
  12045. (let ((image-file-name-extensions
  12046. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  12047. "xbm" "xpm" "pbm" "pgm" "ppm")))
  12048. (concat "\\."
  12049. (regexp-opt (nconc (mapcar 'upcase
  12050. image-file-name-extensions)
  12051. image-file-name-extensions)
  12052. t)
  12053. "\\'"))))
  12054. (defun org-file-image-p (file)
  12055. "Return non-nil if FILE is an image."
  12056. (save-match-data
  12057. (string-match (org-image-file-name-regexp) file)))
  12058. ;;; Paragraph filling stuff.
  12059. ;; We want this to be just right, so use the full arsenal.
  12060. (defun org-indent-line-function ()
  12061. "Indent line like previous, but further if previous was headline or item."
  12062. (interactive)
  12063. (let* ((pos (point))
  12064. (itemp (org-at-item-p))
  12065. column bpos bcol tpos tcol bullet btype bullet-type)
  12066. ;; Find the previous relevant line
  12067. (beginning-of-line 1)
  12068. (cond
  12069. ((looking-at "#") (setq column 0))
  12070. ((looking-at "\\*+ ") (setq column 0))
  12071. (t
  12072. (beginning-of-line 0)
  12073. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]"))
  12074. (beginning-of-line 0))
  12075. (cond
  12076. ((looking-at "\\*+[ \t]+")
  12077. (goto-char (match-end 0))
  12078. (setq column (current-column)))
  12079. ((org-in-item-p)
  12080. (org-beginning-of-item)
  12081. ; (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  12082. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\)?")
  12083. (setq bpos (match-beginning 1) tpos (match-end 0)
  12084. bcol (progn (goto-char bpos) (current-column))
  12085. tcol (progn (goto-char tpos) (current-column))
  12086. bullet (match-string 1)
  12087. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  12088. (if (not itemp)
  12089. (setq column tcol)
  12090. (goto-char pos)
  12091. (beginning-of-line 1)
  12092. (if (looking-at "\\S-")
  12093. (progn
  12094. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  12095. (setq bullet (match-string 1)
  12096. btype (if (string-match "[0-9]" bullet) "n" bullet))
  12097. (setq column (if (equal btype bullet-type) bcol tcol)))
  12098. (setq column (org-get-indentation)))))
  12099. (t (setq column (org-get-indentation))))))
  12100. (goto-char pos)
  12101. (if (<= (current-column) (current-indentation))
  12102. (org-indent-line-to column)
  12103. (save-excursion (org-indent-line-to column)))
  12104. (setq column (current-column))
  12105. (beginning-of-line 1)
  12106. (if (looking-at
  12107. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  12108. (replace-match (concat "\\1" (format org-property-format
  12109. (match-string 2) (match-string 3)))
  12110. t nil))
  12111. (org-move-to-column column)))
  12112. (defun org-set-autofill-regexps ()
  12113. (interactive)
  12114. ;; In the paragraph separator we include headlines, because filling
  12115. ;; text in a line directly attached to a headline would otherwise
  12116. ;; fill the headline as well.
  12117. (org-set-local 'comment-start-skip "^#+[ \t]*")
  12118. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  12119. ;; The paragraph starter includes hand-formatted lists.
  12120. (org-set-local 'paragraph-start
  12121. "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  12122. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  12123. ;; But only if the user has not turned off tables or fixed-width regions
  12124. (org-set-local
  12125. 'auto-fill-inhibit-regexp
  12126. (concat "\\*+ \\|#\\+"
  12127. "\\|[ \t]*" org-keyword-time-regexp
  12128. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  12129. (concat
  12130. "\\|[ \t]*["
  12131. (if org-enable-table-editor "|" "")
  12132. (if org-enable-fixed-width-editor ":" "")
  12133. "]"))))
  12134. ;; We use our own fill-paragraph function, to make sure that tables
  12135. ;; and fixed-width regions are not wrapped. That function will pass
  12136. ;; through to `fill-paragraph' when appropriate.
  12137. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  12138. ; Adaptive filling: To get full control, first make sure that
  12139. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  12140. (org-set-local 'adaptive-fill-regexp "\000")
  12141. (org-set-local 'adaptive-fill-function
  12142. 'org-adaptive-fill-function)
  12143. (org-set-local
  12144. 'align-mode-rules-list
  12145. '((org-in-buffer-settings
  12146. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  12147. (modes . '(org-mode))))))
  12148. (defun org-fill-paragraph (&optional justify)
  12149. "Re-align a table, pass through to fill-paragraph if no table."
  12150. (let ((table-p (org-at-table-p))
  12151. (table.el-p (org-at-table.el-p)))
  12152. (cond ((and (equal (char-after (point-at-bol)) ?*)
  12153. (save-excursion (goto-char (point-at-bol))
  12154. (looking-at outline-regexp)))
  12155. t) ; skip headlines
  12156. (table.el-p t) ; skip table.el tables
  12157. (table-p (org-table-align) t) ; align org-mode tables
  12158. (t nil)))) ; call paragraph-fill
  12159. ;; For reference, this is the default value of adaptive-fill-regexp
  12160. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  12161. (defun org-adaptive-fill-function ()
  12162. "Return a fill prefix for org-mode files.
  12163. In particular, this makes sure hanging paragraphs for hand-formatted lists
  12164. work correctly."
  12165. (cond ((looking-at "#[ \t]+")
  12166. (match-string 0))
  12167. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] \\)?")
  12168. (save-excursion
  12169. (goto-char (match-end 0))
  12170. (make-string (current-column) ?\ )))
  12171. (t nil)))
  12172. ;;; Other stuff.
  12173. (defun org-toggle-fixed-width-section (arg)
  12174. "Toggle the fixed-width export.
  12175. If there is no active region, the QUOTE keyword at the current headline is
  12176. inserted or removed. When present, it causes the text between this headline
  12177. and the next to be exported as fixed-width text, and unmodified.
  12178. If there is an active region, this command adds or removes a colon as the
  12179. first character of this line. If the first character of a line is a colon,
  12180. this line is also exported in fixed-width font."
  12181. (interactive "P")
  12182. (let* ((cc 0)
  12183. (regionp (org-region-active-p))
  12184. (beg (if regionp (region-beginning) (point)))
  12185. (end (if regionp (region-end)))
  12186. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  12187. (case-fold-search nil)
  12188. (re "[ \t]*\\(:\\)")
  12189. off)
  12190. (if regionp
  12191. (save-excursion
  12192. (goto-char beg)
  12193. (setq cc (current-column))
  12194. (beginning-of-line 1)
  12195. (setq off (looking-at re))
  12196. (while (> nlines 0)
  12197. (setq nlines (1- nlines))
  12198. (beginning-of-line 1)
  12199. (cond
  12200. (arg
  12201. (org-move-to-column cc t)
  12202. (insert ":\n")
  12203. (forward-line -1))
  12204. ((and off (looking-at re))
  12205. (replace-match "" t t nil 1))
  12206. ((not off) (org-move-to-column cc t) (insert ":")))
  12207. (forward-line 1)))
  12208. (save-excursion
  12209. (org-back-to-heading)
  12210. (if (looking-at (concat outline-regexp
  12211. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  12212. (replace-match "" t t nil 1)
  12213. (if (looking-at outline-regexp)
  12214. (progn
  12215. (goto-char (match-end 0))
  12216. (insert org-quote-string " "))))))))
  12217. ;;;; Functions extending outline functionality
  12218. (defun org-beginning-of-line (&optional arg)
  12219. "Go to the beginning of the current line. If that is invisible, continue
  12220. to a visible line beginning. This makes the function of C-a more intuitive.
  12221. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  12222. first attempt, and only move to after the tags when the cursor is already
  12223. beyond the end of the headline."
  12224. (interactive "P")
  12225. (let ((pos (point)))
  12226. (beginning-of-line 1)
  12227. (if (bobp)
  12228. nil
  12229. (backward-char 1)
  12230. (if (org-invisible-p)
  12231. (while (and (not (bobp)) (org-invisible-p))
  12232. (backward-char 1)
  12233. (beginning-of-line 1))
  12234. (forward-char 1)))
  12235. (when org-special-ctrl-a/e
  12236. (cond
  12237. ((and (looking-at org-todo-line-regexp)
  12238. (= (char-after (match-end 1)) ?\ ))
  12239. (goto-char
  12240. (if (eq org-special-ctrl-a/e t)
  12241. (cond ((> pos (match-beginning 3)) (match-beginning 3))
  12242. ((= pos (point)) (match-beginning 3))
  12243. (t (point)))
  12244. (cond ((> pos (point)) (point))
  12245. ((not (eq last-command this-command)) (point))
  12246. (t (match-beginning 3))))))
  12247. ((org-at-item-p)
  12248. (goto-char
  12249. (if (eq org-special-ctrl-a/e t)
  12250. (cond ((> pos (match-end 4)) (match-end 4))
  12251. ((= pos (point)) (match-end 4))
  12252. (t (point)))
  12253. (cond ((> pos (point)) (point))
  12254. ((not (eq last-command this-command)) (point))
  12255. (t (match-end 4))))))))))
  12256. (defun org-end-of-line (&optional arg)
  12257. "Go to the end of the line.
  12258. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  12259. first attempt, and only move to after the tags when the cursor is already
  12260. beyond the end of the headline."
  12261. (interactive "P")
  12262. (if (or (not org-special-ctrl-a/e)
  12263. (not (org-on-heading-p)))
  12264. (end-of-line arg)
  12265. (let ((pos (point)))
  12266. (beginning-of-line 1)
  12267. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  12268. (if (eq org-special-ctrl-a/e t)
  12269. (if (or (< pos (match-beginning 1))
  12270. (= pos (match-end 0)))
  12271. (goto-char (match-beginning 1))
  12272. (goto-char (match-end 0)))
  12273. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  12274. (goto-char (match-end 0))
  12275. (goto-char (match-beginning 1))))
  12276. (end-of-line arg)))))
  12277. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  12278. (define-key org-mode-map "\C-e" 'org-end-of-line)
  12279. (defun org-kill-line (&optional arg)
  12280. "Kill line, to tags or end of line."
  12281. (interactive "P")
  12282. (cond
  12283. ((or (not org-special-ctrl-k)
  12284. (bolp)
  12285. (not (org-on-heading-p)))
  12286. (call-interactively 'kill-line))
  12287. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  12288. (kill-region (point) (match-beginning 1))
  12289. (org-set-tags nil t))
  12290. (t (kill-region (point) (point-at-eol)))))
  12291. (define-key org-mode-map "\C-k" 'org-kill-line)
  12292. (defun org-invisible-p ()
  12293. "Check if point is at a character currently not visible."
  12294. ;; Early versions of noutline don't have `outline-invisible-p'.
  12295. (if (fboundp 'outline-invisible-p)
  12296. (outline-invisible-p)
  12297. (get-char-property (point) 'invisible)))
  12298. (defun org-invisible-p2 ()
  12299. "Check if point is at a character currently not visible."
  12300. (save-excursion
  12301. (if (and (eolp) (not (bobp))) (backward-char 1))
  12302. ;; Early versions of noutline don't have `outline-invisible-p'.
  12303. (if (fboundp 'outline-invisible-p)
  12304. (outline-invisible-p)
  12305. (get-char-property (point) 'invisible))))
  12306. (defalias 'org-back-to-heading 'outline-back-to-heading)
  12307. (defalias 'org-on-heading-p 'outline-on-heading-p)
  12308. (defalias 'org-at-heading-p 'outline-on-heading-p)
  12309. (defun org-at-heading-or-item-p ()
  12310. (or (org-on-heading-p) (org-at-item-p)))
  12311. (defun org-on-target-p ()
  12312. (or (org-in-regexp org-radio-target-regexp)
  12313. (org-in-regexp org-target-regexp)))
  12314. (defun org-up-heading-all (arg)
  12315. "Move to the heading line of which the present line is a subheading.
  12316. This function considers both visible and invisible heading lines.
  12317. With argument, move up ARG levels."
  12318. (if (fboundp 'outline-up-heading-all)
  12319. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  12320. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  12321. (defun org-up-heading-safe ()
  12322. "Move to the heading line of which the present line is a subheading.
  12323. This version will not throw an error. It will return the level of the
  12324. headline found, or nil if no higher level is found."
  12325. (let ((pos (point)) start-level level
  12326. (re (concat "^" outline-regexp)))
  12327. (catch 'exit
  12328. (outline-back-to-heading t)
  12329. (setq start-level (funcall outline-level))
  12330. (if (equal start-level 1) (throw 'exit nil))
  12331. (while (re-search-backward re nil t)
  12332. (setq level (funcall outline-level))
  12333. (if (< level start-level) (throw 'exit level)))
  12334. nil)))
  12335. (defun org-first-sibling-p ()
  12336. "Is this heading the first child of its parents?"
  12337. (interactive)
  12338. (let ((re (concat "^" outline-regexp))
  12339. level l)
  12340. (unless (org-at-heading-p t)
  12341. (error "Not at a heading"))
  12342. (setq level (funcall outline-level))
  12343. (save-excursion
  12344. (if (not (re-search-backward re nil t))
  12345. t
  12346. (setq l (funcall outline-level))
  12347. (< l level)))))
  12348. (defun org-goto-sibling (&optional previous)
  12349. "Goto the next sibling, even if it is invisible.
  12350. When PREVIOUS is set, go to the previous sibling instead. Returns t
  12351. when a sibling was found. When none is found, return nil and don't
  12352. move point."
  12353. (let ((fun (if previous 're-search-backward 're-search-forward))
  12354. (pos (point))
  12355. (re (concat "^" outline-regexp))
  12356. level l)
  12357. (when (condition-case nil (org-back-to-heading t) (error nil))
  12358. (setq level (funcall outline-level))
  12359. (catch 'exit
  12360. (or previous (forward-char 1))
  12361. (while (funcall fun re nil t)
  12362. (setq l (funcall outline-level))
  12363. (when (< l level) (goto-char pos) (throw 'exit nil))
  12364. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  12365. (goto-char pos)
  12366. nil))))
  12367. (defun org-show-siblings ()
  12368. "Show all siblings of the current headline."
  12369. (save-excursion
  12370. (while (org-goto-sibling) (org-flag-heading nil)))
  12371. (save-excursion
  12372. (while (org-goto-sibling 'previous)
  12373. (org-flag-heading nil))))
  12374. (defun org-show-hidden-entry ()
  12375. "Show an entry where even the heading is hidden."
  12376. (save-excursion
  12377. (org-show-entry)))
  12378. (defun org-flag-heading (flag &optional entry)
  12379. "Flag the current heading. FLAG non-nil means make invisible.
  12380. When ENTRY is non-nil, show the entire entry."
  12381. (save-excursion
  12382. (org-back-to-heading t)
  12383. ;; Check if we should show the entire entry
  12384. (if entry
  12385. (progn
  12386. (org-show-entry)
  12387. (save-excursion
  12388. (and (outline-next-heading)
  12389. (org-flag-heading nil))))
  12390. (outline-flag-region (max (point-min) (1- (point)))
  12391. (save-excursion (outline-end-of-heading) (point))
  12392. flag))))
  12393. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  12394. ;; This is an exact copy of the original function, but it uses
  12395. ;; `org-back-to-heading', to make it work also in invisible
  12396. ;; trees. And is uses an invisible-OK argument.
  12397. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  12398. (org-back-to-heading invisible-OK)
  12399. (let ((first t)
  12400. (level (funcall outline-level)))
  12401. (while (and (not (eobp))
  12402. (or first (> (funcall outline-level) level)))
  12403. (setq first nil)
  12404. (outline-next-heading))
  12405. (unless to-heading
  12406. (if (memq (preceding-char) '(?\n ?\^M))
  12407. (progn
  12408. ;; Go to end of line before heading
  12409. (forward-char -1)
  12410. (if (memq (preceding-char) '(?\n ?\^M))
  12411. ;; leave blank line before heading
  12412. (forward-char -1))))))
  12413. (point))
  12414. (defun org-show-subtree ()
  12415. "Show everything after this heading at deeper levels."
  12416. (outline-flag-region
  12417. (point)
  12418. (save-excursion
  12419. (outline-end-of-subtree) (outline-next-heading) (point))
  12420. nil))
  12421. (defun org-show-entry ()
  12422. "Show the body directly following this heading.
  12423. Show the heading too, if it is currently invisible."
  12424. (interactive)
  12425. (save-excursion
  12426. (condition-case nil
  12427. (progn
  12428. (org-back-to-heading t)
  12429. (outline-flag-region
  12430. (max (point-min) (1- (point)))
  12431. (save-excursion
  12432. (re-search-forward
  12433. (concat "[\r\n]\\(" outline-regexp "\\)") nil 'move)
  12434. (or (match-beginning 1) (point-max)))
  12435. nil))
  12436. (error nil))))
  12437. (defun org-make-options-regexp (kwds)
  12438. "Make a regular expression for keyword lines."
  12439. (concat
  12440. "^"
  12441. "#?[ \t]*\\+\\("
  12442. (mapconcat 'regexp-quote kwds "\\|")
  12443. "\\):[ \t]*"
  12444. "\\(.+\\)"))
  12445. ;; Make isearch reveal the necessary context
  12446. (defun org-isearch-end ()
  12447. "Reveal context after isearch exits."
  12448. (when isearch-success ; only if search was successful
  12449. (if (featurep 'xemacs)
  12450. ;; Under XEmacs, the hook is run in the correct place,
  12451. ;; we directly show the context.
  12452. (org-show-context 'isearch)
  12453. ;; In Emacs the hook runs *before* restoring the overlays.
  12454. ;; So we have to use a one-time post-command-hook to do this.
  12455. ;; (Emacs 22 has a special variable, see function `org-mode')
  12456. (unless (and (boundp 'isearch-mode-end-hook-quit)
  12457. isearch-mode-end-hook-quit)
  12458. ;; Only when the isearch was not quitted.
  12459. (org-add-hook 'post-command-hook 'org-isearch-post-command
  12460. 'append 'local)))))
  12461. (defun org-isearch-post-command ()
  12462. "Remove self from hook, and show context."
  12463. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  12464. (org-show-context 'isearch))
  12465. ;;;; Integration with and fixes for other packages
  12466. ;;; Imenu support
  12467. (defvar org-imenu-markers nil
  12468. "All markers currently used by Imenu.")
  12469. (make-variable-buffer-local 'org-imenu-markers)
  12470. (defun org-imenu-new-marker (&optional pos)
  12471. "Return a new marker for use by Imenu, and remember the marker."
  12472. (let ((m (make-marker)))
  12473. (move-marker m (or pos (point)))
  12474. (push m org-imenu-markers)
  12475. m))
  12476. (defun org-imenu-get-tree ()
  12477. "Produce the index for Imenu."
  12478. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  12479. (setq org-imenu-markers nil)
  12480. (let* ((n org-imenu-depth)
  12481. (re (concat "^" outline-regexp))
  12482. (subs (make-vector (1+ n) nil))
  12483. (last-level 0)
  12484. m tree level head)
  12485. (save-excursion
  12486. (save-restriction
  12487. (widen)
  12488. (goto-char (point-max))
  12489. (while (re-search-backward re nil t)
  12490. (setq level (org-reduced-level (funcall outline-level)))
  12491. (when (<= level n)
  12492. (looking-at org-complex-heading-regexp)
  12493. (setq head (org-match-string-no-properties 4)
  12494. m (org-imenu-new-marker))
  12495. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  12496. (if (>= level last-level)
  12497. (push (cons head m) (aref subs level))
  12498. (push (cons head (aref subs (1+ level))) (aref subs level))
  12499. (loop for i from (1+ level) to n do (aset subs i nil)))
  12500. (setq last-level level)))))
  12501. (aref subs 1)))
  12502. (eval-after-load "imenu"
  12503. '(progn
  12504. (add-hook 'imenu-after-jump-hook
  12505. (lambda () (org-show-context 'org-goto)))))
  12506. ;; Speedbar support
  12507. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  12508. "Overlay marking the agenda restriction line in speedbar.")
  12509. (org-overlay-put org-speedbar-restriction-lock-overlay
  12510. 'face 'org-agenda-restriction-lock)
  12511. (org-overlay-put org-speedbar-restriction-lock-overlay
  12512. 'help-echo "Agendas are currently limited to this item.")
  12513. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  12514. (defun org-speedbar-set-agenda-restriction ()
  12515. "Restrict future agenda commands to the location at point in speedbar.
  12516. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  12517. (interactive)
  12518. (require 'org-agenda)
  12519. (let (p m tp np dir txt w)
  12520. (cond
  12521. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  12522. 'org-imenu t))
  12523. (setq m (get-text-property p 'org-imenu-marker))
  12524. (save-excursion
  12525. (save-restriction
  12526. (set-buffer (marker-buffer m))
  12527. (goto-char m)
  12528. (org-agenda-set-restriction-lock 'subtree))))
  12529. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  12530. 'speedbar-function 'speedbar-find-file))
  12531. (setq tp (previous-single-property-change
  12532. (1+ p) 'speedbar-function)
  12533. np (next-single-property-change
  12534. tp 'speedbar-function)
  12535. dir (speedbar-line-directory)
  12536. txt (buffer-substring-no-properties (or tp (point-min))
  12537. (or np (point-max))))
  12538. (save-excursion
  12539. (save-restriction
  12540. (set-buffer (find-file-noselect
  12541. (let ((default-directory dir))
  12542. (expand-file-name txt))))
  12543. (unless (org-mode-p)
  12544. (error "Cannot restrict to non-Org-mode file"))
  12545. (org-agenda-set-restriction-lock 'file))))
  12546. (t (error "Don't know how to restrict Org-mode's agenda")))
  12547. (org-move-overlay org-speedbar-restriction-lock-overlay
  12548. (point-at-bol) (point-at-eol))
  12549. (setq current-prefix-arg nil)
  12550. (org-agenda-maybe-redo)))
  12551. (eval-after-load "speedbar"
  12552. '(progn
  12553. (speedbar-add-supported-extension ".org")
  12554. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  12555. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  12556. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  12557. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  12558. (add-hook 'speedbar-visiting-tag-hook
  12559. (lambda () (org-show-context 'org-goto)))))
  12560. ;;; Fixes and Hacks for problems with other packages
  12561. ;; Make flyspell not check words in links, to not mess up our keymap
  12562. (defun org-mode-flyspell-verify ()
  12563. "Don't let flyspell put overlays at active buttons."
  12564. (not (get-text-property (point) 'keymap)))
  12565. ;; Make `bookmark-jump' show the jump location if it was hidden.
  12566. (eval-after-load "bookmark"
  12567. '(if (boundp 'bookmark-after-jump-hook)
  12568. ;; We can use the hook
  12569. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  12570. ;; Hook not available, use advice
  12571. (defadvice bookmark-jump (after org-make-visible activate)
  12572. "Make the position visible."
  12573. (org-bookmark-jump-unhide))))
  12574. (defun org-bookmark-jump-unhide ()
  12575. "Unhide the current position, to show the bookmark location."
  12576. (and (org-mode-p)
  12577. (or (org-invisible-p)
  12578. (save-excursion (goto-char (max (point-min) (1- (point))))
  12579. (org-invisible-p)))
  12580. (org-show-context 'bookmark-jump)))
  12581. ;; Make session.el ignore our circular variable
  12582. (eval-after-load "session"
  12583. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  12584. ;;;; Experimental code
  12585. (defun org-closed-in-range ()
  12586. "Sparse tree of items closed in a certain time range.
  12587. Still experimental, may disappear in the future."
  12588. (interactive)
  12589. ;; Get the time interval from the user.
  12590. (let* ((time1 (time-to-seconds
  12591. (org-read-date nil 'to-time nil "Starting date: ")))
  12592. (time2 (time-to-seconds
  12593. (org-read-date nil 'to-time nil "End date:")))
  12594. ;; callback function
  12595. (callback (lambda ()
  12596. (let ((time
  12597. (time-to-seconds
  12598. (apply 'encode-time
  12599. (org-parse-time-string
  12600. (match-string 1))))))
  12601. ;; check if time in interval
  12602. (and (>= time time1) (<= time time2))))))
  12603. ;; make tree, check each match with the callback
  12604. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  12605. ;;;; Finish up
  12606. (provide 'org)
  12607. (run-hooks 'org-load-hook)
  12608. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  12609. ;;; org.el ends here