org.el 506 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784
  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 of the License, or
  15. ;; (at your option) any later version.
  16. ;; GNU Emacs is distributed in the hope that it will be useful,
  17. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. ;; GNU General Public License for more details.
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  22. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  23. ;;
  24. ;;; Commentary:
  25. ;;
  26. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  27. ;; project planning with a fast and effective plain-text system.
  28. ;;
  29. ;; Org-mode develops organizational tasks around NOTES files that contain
  30. ;; information about projects as plain text. Org-mode is implemented on
  31. ;; top of outline-mode, which makes it possible to keep the content of
  32. ;; large files well structured. Visibility cycling and structure editing
  33. ;; help to work with the tree. Tables are easily created with a built-in
  34. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  35. ;; and scheduling. It dynamically compiles entries into an agenda that
  36. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  37. ;; Plain text URL-like links connect to websites, emails, Usenet
  38. ;; messages, BBDB entries, and any files related to the projects. For
  39. ;; printing and sharing of notes, an Org-mode file can be exported as a
  40. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  41. ;; iCalendar file. It can also serve as a publishing tool for a set of
  42. ;; linked webpages.
  43. ;;
  44. ;; Installation and Activation
  45. ;; ---------------------------
  46. ;; See the corresponding sections in the manual at
  47. ;;
  48. ;; http://orgmode.org/org.html#Installation
  49. ;;
  50. ;; Documentation
  51. ;; -------------
  52. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  53. ;; distribution also contains a PDF version of it. At the homepage of
  54. ;; Org-mode, you can read the same text online as HTML. There is also an
  55. ;; excellent reference card made by Philip Rooke. This card can be found
  56. ;; in the etc/ directory of Emacs 22.
  57. ;;
  58. ;; A list of recent changes can be found at
  59. ;; http://orgmode.org/Changes.html
  60. ;;
  61. ;;; Code:
  62. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  63. (defvar org-table-formula-constants-local nil
  64. "Local version of `org-table-formula-constants'.")
  65. (make-variable-buffer-local 'org-table-formula-constants-local)
  66. ;;;; Require other packages
  67. (eval-when-compile
  68. (require 'cl)
  69. (require 'gnus-sum)
  70. (require 'calendar))
  71. ;; For XEmacs, noutline is not yet provided by outline.el, so arrange for
  72. ;; the file noutline.el being loaded.
  73. (if (featurep 'xemacs) (condition-case nil (require 'noutline)))
  74. ;; We require noutline, which might be provided in outline.el
  75. (require 'outline) (require 'noutline)
  76. ;; Other stuff we need.
  77. (require 'time-date)
  78. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  79. (require 'easymenu)
  80. (require 'org-macs)
  81. (require 'org-compat)
  82. (require 'org-faces)
  83. ;;;; Customization variables
  84. ;;; Version
  85. (defconst org-version "6.02b"
  86. "The version number of the file org.el.")
  87. (defun org-version (&optional here)
  88. "Show the org-mode version in the echo area.
  89. With prefix arg HERE, insert it at point."
  90. (interactive "P")
  91. (let ((version (format "Org-mode version %s" org-version)))
  92. (message version)
  93. (if here
  94. (insert version))))
  95. ;;; Compatibility constants
  96. ;;; The custom variables
  97. (defgroup org nil
  98. "Outline-based notes management and organizer."
  99. :tag "Org"
  100. :group 'outlines
  101. :group 'hypermedia
  102. :group 'calendar)
  103. (defcustom org-load-hook nil
  104. "Hook that is run after org.el has been loaded."
  105. :group 'org
  106. :type 'hook)
  107. (defvar org-modules) ; defined below
  108. (defvar org-modules-loaded nil
  109. "Have the modules been loaded already?")
  110. (defun org-load-modules-maybe (&optional force)
  111. "Load all extensions listed in `org-default-extensions'."
  112. (when (or force (not org-modules-loaded))
  113. (mapc (lambda (ext)
  114. (condition-case nil (require ext)
  115. (error (message "Problems while trying to load feature `%s'" ext))))
  116. org-modules)
  117. (setq org-modules-loaded t)))
  118. (defun org-set-modules (var value)
  119. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  120. (set var value)
  121. (when (featurep 'org)
  122. (org-load-modules-maybe 'force)))
  123. (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)
  124. "Modules that should always be loaded together with org.el.
  125. If a description starts with <C>, the file is not part of emacs
  126. and loading it will require that you have downloaded and properly installed
  127. the org-mode distribution.
  128. You can also use this system to load external packages (i.e. neither Org
  129. core modules, not modules from the CONTRIB directory). Just add symbols
  130. to the end of the list. If the package is called org-xyz.e, then you need
  131. to add the symbol `xyz', and the package must have a call to
  132. (provide 'org-xyz)"
  133. :group 'org
  134. :set 'org-set-modules
  135. :type
  136. '(set :greedy t
  137. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  138. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  139. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  140. (const :tag " info: Links to Info nodes" org-info)
  141. (const :tag " infojs: Set up Sebastian Rose's JavaScript org-info.js" org-infojs)
  142. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  143. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  144. (const :tag " mew Links to Mew folders/messages" org-mew)
  145. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  146. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  147. (const :tag " vm: Links to VM folders/messages" org-vm)
  148. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  149. (const :tag " mouse: Additional mouse support" org-mouse)
  150. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  151. (const :tag "C bookmark: Org links to bookmarks" org-bookmark)
  152. (const :tag "C depend: TODO dependencies for Org-mode" org-depend)
  153. (const :tag "C elisp-symbol: Org links to emacs-lisp symbols" org-elisp-symbol)
  154. (const :tag "C eval: Include command output as text" org-eval)
  155. (const :tag "C expiry: Expiry mechanism for Org entries" org-expiry)
  156. (const :tag "C id: Global id's for identifying entries" org-id)
  157. (const :tag "C interactive-query: Interactive modification of tags query" org-interactive-query)
  158. (const :tag "C mairix: Hook mairix search into Org for different MUAs" org-mairix)
  159. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  160. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  161. (const :tag "C registry: A registry for Org links" org-registry)
  162. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  163. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  164. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  165. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  166. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  167. (defgroup org-startup nil
  168. "Options concerning startup of Org-mode."
  169. :tag "Org Startup"
  170. :group 'org)
  171. (defcustom org-startup-folded t
  172. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  173. This can also be configured on a per-file basis by adding one of
  174. the following lines anywhere in the buffer:
  175. #+STARTUP: fold
  176. #+STARTUP: nofold
  177. #+STARTUP: content"
  178. :group 'org-startup
  179. :type '(choice
  180. (const :tag "nofold: show all" nil)
  181. (const :tag "fold: overview" t)
  182. (const :tag "content: all headlines" content)))
  183. (defcustom org-startup-truncated t
  184. "Non-nil means, entering Org-mode will set `truncate-lines'.
  185. This is useful since some lines containing links can be very long and
  186. uninteresting. Also tables look terrible when wrapped."
  187. :group 'org-startup
  188. :type 'boolean)
  189. (defcustom org-startup-align-all-tables nil
  190. "Non-nil means, align all tables when visiting a file.
  191. This is useful when the column width in tables is forced with <N> cookies
  192. in table fields. Such tables will look correct only after the first re-align.
  193. This can also be configured on a per-file basis by adding one of
  194. the following lines anywhere in the buffer:
  195. #+STARTUP: align
  196. #+STARTUP: noalign"
  197. :group 'org-startup
  198. :type 'boolean)
  199. (defcustom org-insert-mode-line-in-empty-file nil
  200. "Non-nil means insert the first line setting Org-mode in empty files.
  201. When the function `org-mode' is called interactively in an empty file, this
  202. normally means that the file name does not automatically trigger Org-mode.
  203. To ensure that the file will always be in Org-mode in the future, a
  204. line enforcing Org-mode will be inserted into the buffer, if this option
  205. has been set."
  206. :group 'org-startup
  207. :type 'boolean)
  208. (defcustom org-replace-disputed-keys nil
  209. "Non-nil means use alternative key bindings for some keys.
  210. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  211. These keys are also used by other packages like `CUA-mode' or `windmove.el'.
  212. If you want to use Org-mode together with one of these other modes,
  213. or more generally if you would like to move some Org-mode commands to
  214. other keys, set this variable and configure the keys with the variable
  215. `org-disputed-keys'.
  216. This option is only relevant at load-time of Org-mode, and must be set
  217. *before* org.el is loaded. Changing it requires a restart of Emacs to
  218. become effective."
  219. :group 'org-startup
  220. :type 'boolean)
  221. (if (fboundp 'defvaralias)
  222. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  223. (defcustom org-disputed-keys
  224. '(([(shift up)] . [(meta p)])
  225. ([(shift down)] . [(meta n)])
  226. ([(shift left)] . [(meta -)])
  227. ([(shift right)] . [(meta +)])
  228. ([(control shift right)] . [(meta shift +)])
  229. ([(control shift left)] . [(meta shift -)]))
  230. "Keys for which Org-mode and other modes compete.
  231. This is an alist, cars are the default keys, second element specifies
  232. the alternative to use when `org-replace-disputed-keys' is t.
  233. Keys can be specified in any syntax supported by `define-key'.
  234. The value of this option takes effect only at Org-mode's startup,
  235. therefore you'll have to restart Emacs to apply it after changing."
  236. :group 'org-startup
  237. :type 'alist)
  238. (defun org-key (key)
  239. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  240. Or return the original if not disputed."
  241. (if org-replace-disputed-keys
  242. (let* ((nkey (key-description key))
  243. (x (org-find-if (lambda (x)
  244. (equal (key-description (car x)) nkey))
  245. org-disputed-keys)))
  246. (if x (cdr x) key))
  247. key))
  248. (defun org-find-if (predicate seq)
  249. (catch 'exit
  250. (while seq
  251. (if (funcall predicate (car seq))
  252. (throw 'exit (car seq))
  253. (pop seq)))))
  254. (defun org-defkey (keymap key def)
  255. "Define a key, possibly translated, as returned by `org-key'."
  256. (define-key keymap (org-key key) def))
  257. (defcustom org-ellipsis nil
  258. "The ellipsis to use in the Org-mode outline.
  259. When nil, just use the standard three dots. When a string, use that instead,
  260. When a face, use the standart 3 dots, but with the specified face.
  261. The change affects only Org-mode (which will then use its own display table).
  262. Changing this requires executing `M-x org-mode' in a buffer to become
  263. effective."
  264. :group 'org-startup
  265. :type '(choice (const :tag "Default" nil)
  266. (face :tag "Face" :value org-warning)
  267. (string :tag "String" :value "...#")))
  268. (defvar org-display-table nil
  269. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  270. (defgroup org-keywords nil
  271. "Keywords in Org-mode."
  272. :tag "Org Keywords"
  273. :group 'org)
  274. (defcustom org-deadline-string "DEADLINE:"
  275. "String to mark deadline entries.
  276. A deadline is this string, followed by a time stamp. Should be a word,
  277. terminated by a colon. You can insert a schedule keyword and
  278. a timestamp with \\[org-deadline].
  279. Changes become only effective after restarting Emacs."
  280. :group 'org-keywords
  281. :type 'string)
  282. (defcustom org-scheduled-string "SCHEDULED:"
  283. "String to mark scheduled TODO entries.
  284. A schedule is this string, followed by a time stamp. Should be a word,
  285. terminated by a colon. You can insert a schedule keyword and
  286. a timestamp with \\[org-schedule].
  287. Changes become only effective after restarting Emacs."
  288. :group 'org-keywords
  289. :type 'string)
  290. (defcustom org-closed-string "CLOSED:"
  291. "String used as the prefix for timestamps logging closing a TODO entry."
  292. :group 'org-keywords
  293. :type 'string)
  294. (defcustom org-clock-string "CLOCK:"
  295. "String used as prefix for timestamps clocking work hours on an item."
  296. :group 'org-keywords
  297. :type 'string)
  298. (defcustom org-comment-string "COMMENT"
  299. "Entries starting with this keyword will never be exported.
  300. An entry can be toggled between COMMENT and normal with
  301. \\[org-toggle-comment].
  302. Changes become only effective after restarting Emacs."
  303. :group 'org-keywords
  304. :type 'string)
  305. (defcustom org-quote-string "QUOTE"
  306. "Entries starting with this keyword will be exported in fixed-width font.
  307. Quoting applies only to the text in the entry following the headline, and does
  308. not extend beyond the next headline, even if that is lower level.
  309. An entry can be toggled between QUOTE and normal with
  310. \\[org-toggle-fixed-width-section]."
  311. :group 'org-keywords
  312. :type 'string)
  313. (defconst org-repeat-re
  314. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\([.+]?\\+[0-9]+[dwmy]\\)"
  315. "Regular expression for specifying repeated events.
  316. After a match, group 1 contains the repeat expression.")
  317. (defgroup org-structure nil
  318. "Options concerning the general structure of Org-mode files."
  319. :tag "Org Structure"
  320. :group 'org)
  321. (defgroup org-reveal-location nil
  322. "Options about how to make context of a location visible."
  323. :tag "Org Reveal Location"
  324. :group 'org-structure)
  325. (defconst org-context-choice
  326. '(choice
  327. (const :tag "Always" t)
  328. (const :tag "Never" nil)
  329. (repeat :greedy t :tag "Individual contexts"
  330. (cons
  331. (choice :tag "Context"
  332. (const agenda)
  333. (const org-goto)
  334. (const occur-tree)
  335. (const tags-tree)
  336. (const link-search)
  337. (const mark-goto)
  338. (const bookmark-jump)
  339. (const isearch)
  340. (const default))
  341. (boolean))))
  342. "Contexts for the reveal options.")
  343. (defcustom org-show-hierarchy-above '((default . t))
  344. "Non-nil means, show full hierarchy when revealing a location.
  345. Org-mode often shows locations in an org-mode file which might have
  346. been invisible before. When this is set, the hierarchy of headings
  347. above the exposed location is shown.
  348. Turning this off for example for sparse trees makes them very compact.
  349. Instead of t, this can also be an alist specifying this option for different
  350. contexts. Valid contexts are
  351. agenda when exposing an entry from the agenda
  352. org-goto when using the command `org-goto' on key C-c C-j
  353. occur-tree when using the command `org-occur' on key C-c /
  354. tags-tree when constructing a sparse tree based on tags matches
  355. link-search when exposing search matches associated with a link
  356. mark-goto when exposing the jump goal of a mark
  357. bookmark-jump when exposing a bookmark location
  358. isearch when exiting from an incremental search
  359. default default for all contexts not set explicitly"
  360. :group 'org-reveal-location
  361. :type org-context-choice)
  362. (defcustom org-show-following-heading '((default . nil))
  363. "Non-nil means, show following heading when revealing a location.
  364. Org-mode often shows locations in an org-mode file which might have
  365. been invisible before. When this is set, the heading following the
  366. match is shown.
  367. Turning this off for example for sparse trees makes them very compact,
  368. but makes it harder to edit the location of the match. In such a case,
  369. use the command \\[org-reveal] to show more context.
  370. Instead of t, this can also be an alist specifying this option for different
  371. contexts. See `org-show-hierarchy-above' for valid contexts."
  372. :group 'org-reveal-location
  373. :type org-context-choice)
  374. (defcustom org-show-siblings '((default . nil) (isearch t))
  375. "Non-nil means, show all sibling heading when revealing a location.
  376. Org-mode often shows locations in an org-mode file which might have
  377. been invisible before. When this is set, the sibling of the current entry
  378. heading are all made visible. If `org-show-hierarchy-above' is t,
  379. the same happens on each level of the hierarchy above the current entry.
  380. By default this is on for the isearch context, off for all other contexts.
  381. Turning this off for example for sparse trees makes them very compact,
  382. but makes it harder to edit the location of the match. In such a case,
  383. use the command \\[org-reveal] to show more context.
  384. Instead of t, this can also be an alist specifying this option for different
  385. contexts. See `org-show-hierarchy-above' for valid contexts."
  386. :group 'org-reveal-location
  387. :type org-context-choice)
  388. (defcustom org-show-entry-below '((default . nil))
  389. "Non-nil means, show the entry below a headline when revealing a location.
  390. Org-mode often shows locations in an org-mode file which might have
  391. been invisible before. When this is set, the text below the headline that is
  392. exposed is also shown.
  393. By default this is off for all contexts.
  394. Instead of t, this can also be an alist specifying this option for different
  395. contexts. See `org-show-hierarchy-above' for valid contexts."
  396. :group 'org-reveal-location
  397. :type org-context-choice)
  398. (defcustom org-indirect-buffer-display 'other-window
  399. "How should indirect tree buffers be displayed?
  400. This applies to indirect buffers created with the commands
  401. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  402. Valid values are:
  403. current-window Display in the current window
  404. other-window Just display in another window.
  405. dedicated-frame Create one new frame, and re-use it each time.
  406. new-frame Make a new frame each time. Note that in this case
  407. previously-made indirect buffers are kept, and you need to
  408. kill these buffers yourself."
  409. :group 'org-structure
  410. :group 'org-agenda-windows
  411. :type '(choice
  412. (const :tag "In current window" current-window)
  413. (const :tag "In current frame, other window" other-window)
  414. (const :tag "Each time a new frame" new-frame)
  415. (const :tag "One dedicated frame" dedicated-frame)))
  416. (defgroup org-cycle nil
  417. "Options concerning visibility cycling in Org-mode."
  418. :tag "Org Cycle"
  419. :group 'org-structure)
  420. (defcustom org-drawers '("PROPERTIES" "CLOCK")
  421. "Names of drawers. Drawers are not opened by cycling on the headline above.
  422. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  423. this:
  424. :DRAWERNAME:
  425. .....
  426. :END:
  427. The drawer \"PROPERTIES\" is special for capturing properties through
  428. the property API.
  429. Drawers can be defined on the per-file basis with a line like:
  430. #+DRAWERS: HIDDEN STATE PROPERTIES"
  431. :group 'org-structure
  432. :type '(repeat (string :tag "Drawer Name")))
  433. (defcustom org-cycle-global-at-bob nil
  434. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  435. This makes it possible to do global cycling without having to use S-TAB or
  436. C-u TAB. For this special case to work, the first line of the buffer
  437. must not be a headline - it may be empty ot some other text. When used in
  438. this way, `org-cycle-hook' is disables temporarily, to make sure the
  439. cursor stays at the beginning of the buffer.
  440. When this option is nil, don't do anything special at the beginning
  441. of the buffer."
  442. :group 'org-cycle
  443. :type 'boolean)
  444. (defcustom org-cycle-emulate-tab t
  445. "Where should `org-cycle' emulate TAB.
  446. nil Never
  447. white Only in completely white lines
  448. whitestart Only at the beginning of lines, before the first non-white char
  449. t Everywhere except in headlines
  450. exc-hl-bol Everywhere except at the start of a headline
  451. If TAB is used in a place where it does not emulate TAB, the current subtree
  452. visibility is cycled."
  453. :group 'org-cycle
  454. :type '(choice (const :tag "Never" nil)
  455. (const :tag "Only in completely white lines" white)
  456. (const :tag "Before first char in a line" whitestart)
  457. (const :tag "Everywhere except in headlines" t)
  458. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  459. ))
  460. (defcustom org-cycle-separator-lines 2
  461. "Number of empty lines needed to keep an empty line between collapsed trees.
  462. If you leave an empty line between the end of a subtree and the following
  463. headline, this empty line is hidden when the subtree is folded.
  464. Org-mode will leave (exactly) one empty line visible if the number of
  465. empty lines is equal or larger to the number given in this variable.
  466. So the default 2 means, at least 2 empty lines after the end of a subtree
  467. are needed to produce free space between a collapsed subtree and the
  468. following headline.
  469. Special case: when 0, never leave empty lines in collapsed view."
  470. :group 'org-cycle
  471. :type 'integer)
  472. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  473. org-cycle-hide-drawers
  474. org-cycle-show-empty-lines
  475. org-optimize-window-after-visibility-change)
  476. "Hook that is run after `org-cycle' has changed the buffer visibility.
  477. The function(s) in this hook must accept a single argument which indicates
  478. the new state that was set by the most recent `org-cycle' command. The
  479. argument is a symbol. After a global state change, it can have the values
  480. `overview', `content', or `all'. After a local state change, it can have
  481. the values `folded', `children', or `subtree'."
  482. :group 'org-cycle
  483. :type 'hook)
  484. (defgroup org-edit-structure nil
  485. "Options concerning structure editing in Org-mode."
  486. :tag "Org Edit Structure"
  487. :group 'org-structure)
  488. (defcustom org-odd-levels-only nil
  489. "Non-nil means, skip even levels and only use odd levels for the outline.
  490. This has the effect that two stars are being added/taken away in
  491. promotion/demotion commands. It also influences how levels are
  492. handled by the exporters.
  493. Changing it requires restart of `font-lock-mode' to become effective
  494. for fontification also in regions already fontified.
  495. You may also set this on a per-file basis by adding one of the following
  496. lines to the buffer:
  497. #+STARTUP: odd
  498. #+STARTUP: oddeven"
  499. :group 'org-edit-structure
  500. :group 'org-font-lock
  501. :type 'boolean)
  502. (defcustom org-adapt-indentation t
  503. "Non-nil means, adapt indentation when promoting and demoting.
  504. When this is set and the *entire* text in an entry is indented, the
  505. indentation is increased by one space in a demotion command, and
  506. decreased by one in a promotion command. If any line in the entry
  507. body starts at column 0, indentation is not changed at all."
  508. :group 'org-edit-structure
  509. :type 'boolean)
  510. (defcustom org-special-ctrl-a/e nil
  511. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  512. When t, `C-a' will bring back the cursor to the beginning of the
  513. headline text, i.e. after the stars and after a possible TODO keyword.
  514. In an item, this will be the position after the bullet.
  515. When the cursor is already at that position, another `C-a' will bring
  516. it to the beginning of the line.
  517. `C-e' will jump to the end of the headline, ignoring the presence of tags
  518. in the headline. A second `C-e' will then jump to the true end of the
  519. line, after any tags.
  520. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  521. and only a directly following, identical keypress will bring the cursor
  522. to the special positions."
  523. :group 'org-edit-structure
  524. :type '(choice
  525. (const :tag "off" nil)
  526. (const :tag "after bullet first" t)
  527. (const :tag "border first" reversed)))
  528. (if (fboundp 'defvaralias)
  529. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  530. (defcustom org-special-ctrl-k nil
  531. "Non-nil means `C-k' will behave specially in headlines.
  532. When nil, `C-k' will call the default `kill-line' command.
  533. When t, the following will happen while the cursor is in the headline:
  534. - When the cursor is at the beginning of a headline, kill the entire
  535. line and possible the folded subtree below the line.
  536. - When in the middle of the headline text, kill the headline up to the tags.
  537. - When after the headline text, kill the tags."
  538. :group 'org-edit-structure
  539. :type 'boolean)
  540. (defcustom org-M-RET-may-split-line '((default . t))
  541. "Non-nil means, M-RET will split the line at the cursor position.
  542. When nil, it will go to the end of the line before making a
  543. new line.
  544. You may also set this option in a different way for different
  545. contexts. Valid contexts are:
  546. headline when creating a new headline
  547. item when creating a new item
  548. table in a table field
  549. default the value to be used for all contexts not explicitly
  550. customized"
  551. :group 'org-structure
  552. :group 'org-table
  553. :type '(choice
  554. (const :tag "Always" t)
  555. (const :tag "Never" nil)
  556. (repeat :greedy t :tag "Individual contexts"
  557. (cons
  558. (choice :tag "Context"
  559. (const headline)
  560. (const item)
  561. (const table)
  562. (const default))
  563. (boolean)))))
  564. (defcustom org-blank-before-new-entry '((heading . nil)
  565. (plain-list-item . nil))
  566. "Should `org-insert-heading' leave a blank line before new heading/item?
  567. The value is an alist, with `heading' and `plain-list-item' as car,
  568. and a boolean flag as cdr."
  569. :group 'org-edit-structure
  570. :type '(list
  571. (cons (const heading) (boolean))
  572. (cons (const plain-list-item) (boolean))))
  573. (defcustom org-insert-heading-hook nil
  574. "Hook being run after inserting a new heading."
  575. :group 'org-edit-structure
  576. :type 'hook)
  577. (defcustom org-enable-fixed-width-editor t
  578. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  579. This currently only means, they are never auto-wrapped.
  580. When nil, such lines will be treated like ordinary lines.
  581. See also the QUOTE keyword."
  582. :group 'org-edit-structure
  583. :type 'boolean)
  584. (defcustom org-goto-auto-isearch t
  585. "Non-nil means, typing characters in org-goto starts incremental search."
  586. :group 'org-edit-structure
  587. :type 'boolean)
  588. (defgroup org-sparse-trees nil
  589. "Options concerning sparse trees in Org-mode."
  590. :tag "Org Sparse Trees"
  591. :group 'org-structure)
  592. (defcustom org-highlight-sparse-tree-matches t
  593. "Non-nil means, highlight all matches that define a sparse tree.
  594. The highlights will automatically disappear the next time the buffer is
  595. changed by an edit command."
  596. :group 'org-sparse-trees
  597. :type 'boolean)
  598. (defcustom org-remove-highlights-with-change t
  599. "Non-nil means, any change to the buffer will remove temporary highlights.
  600. Such highlights are created by `org-occur' and `org-clock-display'.
  601. When nil, `C-c C-c needs to be used to get rid of the highlights.
  602. The highlights created by `org-preview-latex-fragment' always need
  603. `C-c C-c' to be removed."
  604. :group 'org-sparse-trees
  605. :group 'org-time
  606. :type 'boolean)
  607. (defcustom org-occur-hook '(org-first-headline-recenter)
  608. "Hook that is run after `org-occur' has constructed a sparse tree.
  609. This can be used to recenter the window to show as much of the structure
  610. as possible."
  611. :group 'org-sparse-trees
  612. :type 'hook)
  613. (defgroup org-plain-lists nil
  614. "Options concerning plain lists in Org-mode."
  615. :tag "Org Plain lists"
  616. :group 'org-structure)
  617. (defcustom org-cycle-include-plain-lists nil
  618. "Non-nil means, include plain lists into visibility cycling.
  619. This means that during cycling, plain list items will *temporarily* be
  620. interpreted as outline headlines with a level given by 1000+i where i is the
  621. indentation of the bullet. In all other operations, plain list items are
  622. not seen as headlines. For example, you cannot assign a TODO keyword to
  623. such an item."
  624. :group 'org-plain-lists
  625. :type 'boolean)
  626. (defcustom org-plain-list-ordered-item-terminator t
  627. "The character that makes a line with leading number an ordered list item.
  628. Valid values are ?. and ?\). To get both terminators, use t. While
  629. ?. may look nicer, it creates the danger that a line with leading
  630. number may be incorrectly interpreted as an item. ?\) therefore is
  631. the safe choice."
  632. :group 'org-plain-lists
  633. :type '(choice (const :tag "dot like in \"2.\"" ?.)
  634. (const :tag "paren like in \"2)\"" ?\))
  635. (const :tab "both" t)))
  636. (defcustom org-empty-line-terminates-plain-lists nil
  637. "Non-nil means, an empty line ends all plain list levels.
  638. When nil, empty lines are part of the preceeding item."
  639. :group 'org-plain-lists
  640. :type 'boolean)
  641. (defcustom org-auto-renumber-ordered-lists t
  642. "Non-nil means, automatically renumber ordered plain lists.
  643. Renumbering happens when the sequence have been changed with
  644. \\[org-shiftmetaup] or \\[org-shiftmetadown]. After other editing commands,
  645. use \\[org-ctrl-c-ctrl-c] to trigger renumbering."
  646. :group 'org-plain-lists
  647. :type 'boolean)
  648. (defcustom org-provide-checkbox-statistics t
  649. "Non-nil means, update checkbox statistics after insert and toggle.
  650. When this is set, checkbox statistics is updated each time you either insert
  651. a new checkbox with \\[org-insert-todo-heading] or toggle a checkbox
  652. with \\[org-ctrl-c-ctrl-c\\]."
  653. :group 'org-plain-lists
  654. :type 'boolean)
  655. (defgroup org-imenu-and-speedbar nil
  656. "Options concerning imenu and speedbar in Org-mode."
  657. :tag "Org Imenu and Speedbar"
  658. :group 'org-structure)
  659. (defcustom org-imenu-depth 2
  660. "The maximum level for Imenu access to Org-mode headlines.
  661. This also applied for speedbar access."
  662. :group 'org-imenu-and-speedbar
  663. :type 'number)
  664. (defgroup org-table nil
  665. "Options concerning tables in Org-mode."
  666. :tag "Org Table"
  667. :group 'org)
  668. (defcustom org-enable-table-editor 'optimized
  669. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  670. When nil, such lines will be treated like ordinary lines.
  671. When equal to the symbol `optimized', the table editor will be optimized to
  672. do the following:
  673. - Automatic overwrite mode in front of whitespace in table fields.
  674. This makes the structure of the table stay in tact as long as the edited
  675. field does not exceed the column width.
  676. - Minimize the number of realigns. Normally, the table is aligned each time
  677. TAB or RET are pressed to move to another field. With optimization this
  678. happens only if changes to a field might have changed the column width.
  679. Optimization requires replacing the functions `self-insert-command',
  680. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  681. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  682. very good at guessing when a re-align will be necessary, but you can always
  683. force one with \\[org-ctrl-c-ctrl-c].
  684. If you would like to use the optimized version in Org-mode, but the
  685. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  686. This variable can be used to turn on and off the table editor during a session,
  687. but in order to toggle optimization, a restart is required.
  688. See also the variable `org-table-auto-blank-field'."
  689. :group 'org-table
  690. :type '(choice
  691. (const :tag "off" nil)
  692. (const :tag "on" t)
  693. (const :tag "on, optimized" optimized)))
  694. (defcustom org-table-tab-recognizes-table.el t
  695. "Non-nil means, TAB will automatically notice a table.el table.
  696. When it sees such a table, it moves point into it and - if necessary -
  697. calls `table-recognize-table'."
  698. :group 'org-table-editing
  699. :type 'boolean)
  700. (defgroup org-link nil
  701. "Options concerning links in Org-mode."
  702. :tag "Org Link"
  703. :group 'org)
  704. (defvar org-link-abbrev-alist-local nil
  705. "Buffer-local version of `org-link-abbrev-alist', which see.
  706. The value of this is taken from the #+LINK lines.")
  707. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  708. (defcustom org-link-abbrev-alist nil
  709. "Alist of link abbreviations.
  710. The car of each element is a string, to be replaced at the start of a link.
  711. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  712. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  713. [[linkkey:tag][description]]
  714. If REPLACE is a string, the tag will simply be appended to create the link.
  715. If the string contains \"%s\", the tag will be inserted there.
  716. REPLACE may also be a function that will be called with the tag as the
  717. only argument to create the link, which should be returned as a string.
  718. See the manual for examples."
  719. :group 'org-link
  720. :type 'alist)
  721. (defcustom org-descriptive-links t
  722. "Non-nil means, hide link part and only show description of bracket links.
  723. Bracket links are like [[link][descritpion]]. This variable sets the initial
  724. state in new org-mode buffers. The setting can then be toggled on a
  725. per-buffer basis from the Org->Hyperlinks menu."
  726. :group 'org-link
  727. :type 'boolean)
  728. (defcustom org-link-file-path-type 'adaptive
  729. "How the path name in file links should be stored.
  730. Valid values are:
  731. relative Relative to the current directory, i.e. the directory of the file
  732. into which the link is being inserted.
  733. absolute Absolute path, if possible with ~ for home directory.
  734. noabbrev Absolute path, no abbreviation of home directory.
  735. adaptive Use relative path for files in the current directory and sub-
  736. directories of it. For other files, use an absolute path."
  737. :group 'org-link
  738. :type '(choice
  739. (const relative)
  740. (const absolute)
  741. (const noabbrev)
  742. (const adaptive)))
  743. (defcustom org-activate-links '(bracket angle plain radio tag date)
  744. "Types of links that should be activated in Org-mode files.
  745. This is a list of symbols, each leading to the activation of a certain link
  746. type. In principle, it does not hurt to turn on most link types - there may
  747. be a small gain when turning off unused link types. The types are:
  748. bracket The recommended [[link][description]] or [[link]] links with hiding.
  749. angular Links in angular brackes that may contain whitespace like
  750. <bbdb:Carsten Dominik>.
  751. plain Plain links in normal text, no whitespace, like http://google.com.
  752. radio Text that is matched by a radio target, see manual for details.
  753. tag Tag settings in a headline (link to tag search).
  754. date Time stamps (link to calendar).
  755. Changing this variable requires a restart of Emacs to become effective."
  756. :group 'org-link
  757. :type '(set (const :tag "Double bracket links (new style)" bracket)
  758. (const :tag "Angular bracket links (old style)" angular)
  759. (const :tag "Plain text links" plain)
  760. (const :tag "Radio target matches" radio)
  761. (const :tag "Tags" tag)
  762. (const :tag "Timestamps" date)))
  763. (defcustom org-make-link-description-function nil
  764. "Function to use to generate link descriptions from links. If
  765. nil the link location will be used. This function must take two
  766. parameters; the first is the link and the second the description
  767. org-insert-link has generated, and should return the description
  768. to use."
  769. :group 'org-link
  770. :type 'function)
  771. (defgroup org-link-store nil
  772. "Options concerning storing links in Org-mode."
  773. :tag "Org Store Link"
  774. :group 'org-link)
  775. (defcustom org-email-link-description-format "Email %c: %.30s"
  776. "Format of the description part of a link to an email or usenet message.
  777. The following %-excapes will be replaced by corresponding information:
  778. %F full \"From\" field
  779. %f name, taken from \"From\" field, address if no name
  780. %T full \"To\" field
  781. %t first name in \"To\" field, address if no name
  782. %c correspondent. Unually \"from NAME\", but if you sent it yourself, it
  783. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  784. %s subject
  785. %m message-id.
  786. You may use normal field width specification between the % and the letter.
  787. This is for example useful to limit the length of the subject.
  788. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  789. :group 'org-link-store
  790. :type 'string)
  791. (defcustom org-from-is-user-regexp
  792. (let (r1 r2)
  793. (when (and user-mail-address (not (string= user-mail-address "")))
  794. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  795. (when (and user-full-name (not (string= user-full-name "")))
  796. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  797. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  798. "Regexp mached against the \"From:\" header of an email or usenet message.
  799. It should match if the message is from the user him/herself."
  800. :group 'org-link-store
  801. :type 'regexp)
  802. (defcustom org-context-in-file-links t
  803. "Non-nil means, file links from `org-store-link' contain context.
  804. A search string will be added to the file name with :: as separator and
  805. used to find the context when the link is activated by the command
  806. `org-open-at-point'.
  807. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  808. negates this setting for the duration of the command."
  809. :group 'org-link-store
  810. :type 'boolean)
  811. (defcustom org-keep-stored-link-after-insertion nil
  812. "Non-nil means, keep link in list for entire session.
  813. The command `org-store-link' adds a link pointing to the current
  814. location to an internal list. These links accumulate during a session.
  815. The command `org-insert-link' can be used to insert links into any
  816. Org-mode file (offering completion for all stored links). When this
  817. option is nil, every link which has been inserted once using \\[org-insert-link]
  818. will be removed from the list, to make completing the unused links
  819. more efficient."
  820. :group 'org-link-store
  821. :type 'boolean)
  822. (defgroup org-link-follow nil
  823. "Options concerning following links in Org-mode."
  824. :tag "Org Follow Link"
  825. :group 'org-link)
  826. (defcustom org-follow-link-hook nil
  827. "Hook that is run after a link has been followed."
  828. :group 'org-link-follow
  829. :type 'hook)
  830. (defcustom org-tab-follows-link nil
  831. "Non-nil means, on links TAB will follow the link.
  832. Needs to be set before org.el is loaded."
  833. :group 'org-link-follow
  834. :type 'boolean)
  835. (defcustom org-return-follows-link nil
  836. "Non-nil means, on links RET will follow the link.
  837. Needs to be set before org.el is loaded."
  838. :group 'org-link-follow
  839. :type 'boolean)
  840. (defcustom org-mouse-1-follows-link
  841. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  842. "Non-nil means, mouse-1 on a link will follow the link.
  843. A longer mouse click will still set point. Does not work on XEmacs.
  844. Needs to be set before org.el is loaded."
  845. :group 'org-link-follow
  846. :type 'boolean)
  847. (defcustom org-mark-ring-length 4
  848. "Number of different positions to be recorded in the ring
  849. Changing this requires a restart of Emacs to work correctly."
  850. :group 'org-link-follow
  851. :type 'interger)
  852. (defcustom org-link-frame-setup
  853. '((vm . vm-visit-folder-other-frame)
  854. (gnus . gnus-other-frame)
  855. (file . find-file-other-window))
  856. "Setup the frame configuration for following links.
  857. When following a link with Emacs, it may often be useful to display
  858. this link in another window or frame. This variable can be used to
  859. set this up for the different types of links.
  860. For VM, use any of
  861. `vm-visit-folder'
  862. `vm-visit-folder-other-frame'
  863. For Gnus, use any of
  864. `gnus'
  865. `gnus-other-frame'
  866. For FILE, use any of
  867. `find-file'
  868. `find-file-other-window'
  869. `find-file-other-frame'
  870. For the calendar, use the variable `calendar-setup'.
  871. For BBDB, it is currently only possible to display the matches in
  872. another window."
  873. :group 'org-link-follow
  874. :type '(list
  875. (cons (const vm)
  876. (choice
  877. (const vm-visit-folder)
  878. (const vm-visit-folder-other-window)
  879. (const vm-visit-folder-other-frame)))
  880. (cons (const gnus)
  881. (choice
  882. (const gnus)
  883. (const gnus-other-frame)))
  884. (cons (const file)
  885. (choice
  886. (const find-file)
  887. (const find-file-other-window)
  888. (const find-file-other-frame)))))
  889. (defcustom org-display-internal-link-with-indirect-buffer nil
  890. "Non-nil means, use indirect buffer to display infile links.
  891. Activating internal links (from one location in a file to another location
  892. in the same file) normally just jumps to the location. When the link is
  893. activated with a C-u prefix (or with mouse-3), the link is displayed in
  894. another window. When this option is set, the other window actually displays
  895. an indirect buffer clone of the current buffer, to avoid any visibility
  896. changes to the current buffer."
  897. :group 'org-link-follow
  898. :type 'boolean)
  899. (defcustom org-open-non-existing-files nil
  900. "Non-nil means, `org-open-file' will open non-existing files.
  901. When nil, an error will be generated."
  902. :group 'org-link-follow
  903. :type 'boolean)
  904. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  905. "Function and arguments to call for following mailto links.
  906. This is a list with the first element being a lisp function, and the
  907. remaining elements being arguments to the function. In string arguments,
  908. %a will be replaced by the address, and %s will be replaced by the subject
  909. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  910. :group 'org-link-follow
  911. :type '(choice
  912. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  913. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  914. (const :tag "message-mail" (message-mail "%a" "%s"))
  915. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  916. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  917. "Non-nil means, ask for confirmation before executing shell links.
  918. Shell links can be dangerous: just think about a link
  919. [[shell:rm -rf ~/*][Google Search]]
  920. This link would show up in your Org-mode document as \"Google Search\",
  921. but really it would remove your entire home directory.
  922. Therefore we advise against setting this variable to nil.
  923. Just change it to `y-or-n-p' of you want to confirm with a
  924. single keystroke rather than having to type \"yes\"."
  925. :group 'org-link-follow
  926. :type '(choice
  927. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  928. (const :tag "with y-or-n (faster)" y-or-n-p)
  929. (const :tag "no confirmation (dangerous)" nil)))
  930. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  931. "Non-nil means, ask for confirmation before executing Emacs Lisp links.
  932. Elisp links can be dangerous: just think about a link
  933. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  934. This link would show up in your Org-mode document as \"Google Search\",
  935. but really it would remove your entire home directory.
  936. Therefore we advise against setting this variable to nil.
  937. Just change it to `y-or-n-p' of you want to confirm with a
  938. single keystroke rather than having to type \"yes\"."
  939. :group 'org-link-follow
  940. :type '(choice
  941. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  942. (const :tag "with y-or-n (faster)" y-or-n-p)
  943. (const :tag "no confirmation (dangerous)" nil)))
  944. (defconst org-file-apps-defaults-gnu
  945. '((remote . emacs)
  946. (t . mailcap))
  947. "Default file applications on a UNIX or GNU/Linux system.
  948. See `org-file-apps'.")
  949. (defconst org-file-apps-defaults-macosx
  950. '((remote . emacs)
  951. (t . "open %s")
  952. ("ps" . "gv %s")
  953. ("ps.gz" . "gv %s")
  954. ("eps" . "gv %s")
  955. ("eps.gz" . "gv %s")
  956. ("dvi" . "xdvi %s")
  957. ("fig" . "xfig %s"))
  958. "Default file applications on a MacOS X system.
  959. The system \"open\" is known as a default, but we use X11 applications
  960. for some files for which the OS does not have a good default.
  961. See `org-file-apps'.")
  962. (defconst org-file-apps-defaults-windowsnt
  963. (list
  964. '(remote . emacs)
  965. (cons t
  966. (list (if (featurep 'xemacs)
  967. 'mswindows-shell-execute
  968. 'w32-shell-execute)
  969. "open" 'file)))
  970. "Default file applications on a Windows NT system.
  971. The system \"open\" is used for most files.
  972. See `org-file-apps'.")
  973. (defcustom org-file-apps
  974. '(
  975. ("txt" . emacs)
  976. ("tex" . emacs)
  977. ("ltx" . emacs)
  978. ("org" . emacs)
  979. ("el" . emacs)
  980. ("bib" . emacs)
  981. )
  982. "External applications for opening `file:path' items in a document.
  983. Org-mode uses system defaults for different file types, but
  984. you can use this variable to set the application for a given file
  985. extension. The entries in this list are cons cells where the car identifies
  986. files and the cdr the corresponding command. Possible values for the
  987. file identifier are
  988. \"ext\" A string identifying an extension
  989. `directory' Matches a directory
  990. `remote' Matches a remote file, accessible through tramp or efs.
  991. Remote files most likely should be visited through Emacs
  992. because external applications cannot handle such paths.
  993. t Default for all remaining files
  994. Possible values for the command are:
  995. `emacs' The file will be visited by the current Emacs process.
  996. `default' Use the default application for this file type.
  997. string A command to be executed by a shell; %s will be replaced
  998. by the path to the file.
  999. sexp A Lisp form which will be evaluated. The file path will
  1000. be available in the Lisp variable `file'.
  1001. For more examples, see the system specific constants
  1002. `org-file-apps-defaults-macosx'
  1003. `org-file-apps-defaults-windowsnt'
  1004. `org-file-apps-defaults-gnu'."
  1005. :group 'org-link-follow
  1006. :type '(repeat
  1007. (cons (choice :value ""
  1008. (string :tag "Extension")
  1009. (const :tag "Default for unrecognized files" t)
  1010. (const :tag "Remote file" remote)
  1011. (const :tag "Links to a directory" directory))
  1012. (choice :value ""
  1013. (const :tag "Visit with Emacs" emacs)
  1014. (const :tag "Use system default" default)
  1015. (string :tag "Command")
  1016. (sexp :tag "Lisp form")))))
  1017. (defgroup org-refile nil
  1018. "Options concerning refiling entries in Org-mode."
  1019. :tag "Org Remember"
  1020. :group 'org)
  1021. (defcustom org-directory "~/org"
  1022. "Directory with org files.
  1023. This directory will be used as default to prompt for org files.
  1024. Used by the hooks for remember.el."
  1025. :group 'org-refile
  1026. :group 'org-remember
  1027. :type 'directory)
  1028. (defcustom org-default-notes-file "~/.notes"
  1029. "Default target for storing notes.
  1030. Used by the hooks for remember.el. This can be a string, or nil to mean
  1031. the value of `remember-data-file'.
  1032. You can set this on a per-template basis with the variable
  1033. `org-remember-templates'."
  1034. :group 'org-refile
  1035. :group 'org-remember
  1036. :type '(choice
  1037. (const :tag "Default from remember-data-file" nil)
  1038. file))
  1039. (defcustom org-goto-interface 'outline
  1040. "The default interface to be used for `org-goto'.
  1041. Allowed vaues are:
  1042. outline The interface shows an outline of the relevant file
  1043. and the correct heading is found by moving through
  1044. the outline or by searching with incremental search.
  1045. outline-path-completion Headlines in the current buffer are offered via
  1046. completion."
  1047. :group 'org-refile
  1048. :type '(choice
  1049. (const :tag "Outline" outline)
  1050. (const :tag "Outline-path-completion" outline-path-completion)))
  1051. (defcustom org-reverse-note-order nil
  1052. "Non-nil means, store new notes at the beginning of a file or entry.
  1053. When nil, new notes will be filed to the end of a file or entry.
  1054. This can also be a list with cons cells of regular expressions that
  1055. are matched against file names, and values."
  1056. :group 'org-remember
  1057. :type '(choice
  1058. (const :tag "Reverse always" t)
  1059. (const :tag "Reverse never" nil)
  1060. (repeat :tag "By file name regexp"
  1061. (cons regexp boolean))))
  1062. (defcustom org-refile-targets nil
  1063. "Targets for refiling entries with \\[org-refile].
  1064. This is list of cons cells. Each cell contains:
  1065. - a specification of the files to be considered, either a list of files,
  1066. or a symbol whose function or variable value will be used to retrieve
  1067. a file name or a list of file names. Nil means, refile to a different
  1068. heading in the current buffer.
  1069. - A specification of how to find candidate refile targets. This may be
  1070. any of
  1071. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1072. This tag has to be present in all target headlines, inheritance will
  1073. not be considered.
  1074. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1075. todo keyword.
  1076. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1077. headlines that are refiling targets.
  1078. - a cons cell (:level . N). Any headline of level N is considered a target.
  1079. - a cons cell (:maxlevel . N). Any headline with level <= N is a target."
  1080. :group 'org-remember
  1081. :type '(repeat
  1082. (cons
  1083. (choice :value org-agenda-files
  1084. (const :tag "All agenda files" org-agenda-files)
  1085. (const :tag "Current buffer" nil)
  1086. (function) (variable) (file))
  1087. (choice :tag "Identify target headline by"
  1088. (cons :tag "Specific tag" (const :tag) (string))
  1089. (cons :tag "TODO keyword" (const :todo) (string))
  1090. (cons :tag "Regular expression" (const :regexp) (regexp))
  1091. (cons :tag "Level number" (const :level) (integer))
  1092. (cons :tag "Max Level number" (const :maxlevel) (integer))))))
  1093. (defcustom org-refile-use-outline-path nil
  1094. "Non-nil means, provide refile targets as paths.
  1095. So a level 3 headline will be available as level1/level2/level3.
  1096. When the value is `file', also include the file name (without directory)
  1097. into the path. When `full-file-path', include the full file path."
  1098. :group 'org-remember
  1099. :type '(choice
  1100. (const :tag "Not" nil)
  1101. (const :tag "Yes" t)
  1102. (const :tag "Start with file name" file)
  1103. (const :tag "Start with full file path" full-file-path)))
  1104. (defgroup org-todo nil
  1105. "Options concerning TODO items in Org-mode."
  1106. :tag "Org TODO"
  1107. :group 'org)
  1108. (defgroup org-progress nil
  1109. "Options concerning Progress logging in Org-mode."
  1110. :tag "Org Progress"
  1111. :group 'org-time)
  1112. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1113. "List of TODO entry keyword sequences and their interpretation.
  1114. \\<org-mode-map>This is a list of sequences.
  1115. Each sequence starts with a symbol, either `sequence' or `type',
  1116. indicating if the keywords should be interpreted as a sequence of
  1117. action steps, or as different types of TODO items. The first
  1118. keywords are states requiring action - these states will select a headline
  1119. for inclusion into the global TODO list Org-mode produces. If one of
  1120. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1121. signify that no further action is necessary. If \"|\" is not found,
  1122. the last keyword is treated as the only DONE state of the sequence.
  1123. The command \\[org-todo] cycles an entry through these states, and one
  1124. additional state where no keyword is present. For details about this
  1125. cycling, see the manual.
  1126. TODO keywords and interpretation can also be set on a per-file basis with
  1127. the special #+SEQ_TODO and #+TYP_TODO lines.
  1128. Each keyword can optionally specify a character for fast state selection
  1129. \(in combination with the variable `org-use-fast-todo-selection')
  1130. and specifiers for state change logging, using the same syntax
  1131. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1132. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1133. indicates to record a time stamp each time this state is selected.
  1134. Each keyword may also specify if a timestamp or a note should be
  1135. recorded when entering or leaving the state, by adding additional
  1136. characters in the parenthesis after the keyword. This looks like this:
  1137. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1138. record only the time of the state change. With X and Y being either
  1139. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1140. Y when leaving the state if and only if the *target* state does not
  1141. define X. You may omit any of the fast-selection key or X or /Y,
  1142. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1143. For backward compatibility, this variable may also be just a list
  1144. of keywords - in this case the interptetation (sequence or type) will be
  1145. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1146. :group 'org-todo
  1147. :group 'org-keywords
  1148. :type '(choice
  1149. (repeat :tag "Old syntax, just keywords"
  1150. (string :tag "Keyword"))
  1151. (repeat :tag "New syntax"
  1152. (cons
  1153. (choice
  1154. :tag "Interpretation"
  1155. (const :tag "Sequence (cycling hits every state)" sequence)
  1156. (const :tag "Type (cycling directly to DONE)" type))
  1157. (repeat
  1158. (string :tag "Keyword"))))))
  1159. (defvar org-todo-keywords-1 nil
  1160. "All TODO and DONE keywords active in a buffer.")
  1161. (make-variable-buffer-local 'org-todo-keywords-1)
  1162. (defvar org-todo-keywords-for-agenda nil)
  1163. (defvar org-done-keywords-for-agenda nil)
  1164. (defvar org-agenda-contributing-files nil)
  1165. (defvar org-not-done-keywords nil)
  1166. (make-variable-buffer-local 'org-not-done-keywords)
  1167. (defvar org-done-keywords nil)
  1168. (make-variable-buffer-local 'org-done-keywords)
  1169. (defvar org-todo-heads nil)
  1170. (make-variable-buffer-local 'org-todo-heads)
  1171. (defvar org-todo-sets nil)
  1172. (make-variable-buffer-local 'org-todo-sets)
  1173. (defvar org-todo-log-states nil)
  1174. (make-variable-buffer-local 'org-todo-log-states)
  1175. (defvar org-todo-kwd-alist nil)
  1176. (make-variable-buffer-local 'org-todo-kwd-alist)
  1177. (defvar org-todo-key-alist nil)
  1178. (make-variable-buffer-local 'org-todo-key-alist)
  1179. (defvar org-todo-key-trigger nil)
  1180. (make-variable-buffer-local 'org-todo-key-trigger)
  1181. (defcustom org-todo-interpretation 'sequence
  1182. "Controls how TODO keywords are interpreted.
  1183. This variable is in principle obsolete and is only used for
  1184. backward compatibility, if the interpretation of todo keywords is
  1185. not given already in `org-todo-keywords'. See that variable for
  1186. more information."
  1187. :group 'org-todo
  1188. :group 'org-keywords
  1189. :type '(choice (const sequence)
  1190. (const type)))
  1191. (defcustom org-use-fast-todo-selection 'prefix
  1192. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1193. This variable describes if and under what circumstances the cycling
  1194. mechanism for TODO keywords will be replaced by a single-key, direct
  1195. selection scheme.
  1196. When nil, fast selection is never used.
  1197. When the symbol `prefix', it will be used when `org-todo' is called with
  1198. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1199. in an agenda buffer.
  1200. When t, fast selection is used by default. In this case, the prefix
  1201. argument forces cycling instead.
  1202. In all cases, the special interface is only used if access keys have actually
  1203. been assigned by the user, i.e. if keywords in the configuration are followed
  1204. by a letter in parenthesis, like TODO(t)."
  1205. :group 'org-todo
  1206. :type '(choice
  1207. (const :tag "Never" nil)
  1208. (const :tag "By default" t)
  1209. (const :tag "Only with C-u C-c C-t" prefix)))
  1210. (defcustom org-after-todo-state-change-hook nil
  1211. "Hook which is run after the state of a TODO item was changed.
  1212. The new state (a string with a TODO keyword, or nil) is available in the
  1213. Lisp variable `state'."
  1214. :group 'org-todo
  1215. :type 'hook)
  1216. (defcustom org-log-done nil
  1217. "Non-nil means, record a CLOSED timestamp when moving an entry to DONE.
  1218. When equal to the list (done), also prompt for a closing note.
  1219. This can also be configured on a per-file basis by adding one of
  1220. the following lines anywhere in the buffer:
  1221. #+STARTUP: logdone
  1222. #+STARTUP: lognotedone
  1223. #+STARTUP: nologdone"
  1224. :group 'org-todo
  1225. :group 'org-progress
  1226. :type '(choice
  1227. (const :tag "No logging" nil)
  1228. (const :tag "Record CLOSED timestamp" time)
  1229. (const :tag "Record CLOSED timestamp with closing note." note)))
  1230. ;; Normalize old uses of org-log-done.
  1231. (cond
  1232. ((eq org-log-done t) (setq org-log-done 'time))
  1233. ((and (listp org-log-done) (memq 'done org-log-done))
  1234. (setq org-log-done 'note)))
  1235. (defcustom org-log-note-clock-out nil
  1236. "Non-nil means, recored a note when clocking out of an item.
  1237. This can also be configured on a per-file basis by adding one of
  1238. the following lines anywhere in the buffer:
  1239. #+STARTUP: lognoteclock-out
  1240. #+STARTUP: nolognoteclock-out"
  1241. :group 'org-todo
  1242. :group 'org-progress
  1243. :type 'boolean)
  1244. (defcustom org-log-done-with-time t
  1245. "Non-nil means, the CLOSED time stamp will contain date and time.
  1246. When nil, only the date will be recorded."
  1247. :group 'org-progress
  1248. :type 'boolean)
  1249. (defcustom org-log-note-headings
  1250. '((done . "CLOSING NOTE %t")
  1251. (state . "State %-12s %t")
  1252. (note . "Note taken on %t")
  1253. (clock-out . ""))
  1254. "Headings for notes added to entries.
  1255. The value is an alist, with the car being a symbol indicating the note
  1256. context, and the cdr is the heading to be used. The heading may also be the
  1257. empty string.
  1258. %t in the heading will be replaced by a time stamp.
  1259. %s will be replaced by the new TODO state, in double quotes.
  1260. %u will be replaced by the user name.
  1261. %U will be replaced by the full user name."
  1262. :group 'org-todo
  1263. :group 'org-progress
  1264. :type '(list :greedy t
  1265. (cons (const :tag "Heading when closing an item" done) string)
  1266. (cons (const :tag
  1267. "Heading when changing todo state (todo sequence only)"
  1268. state) string)
  1269. (cons (const :tag "Heading when just taking a note" note) string)
  1270. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1271. (unless (assq 'note org-log-note-headings)
  1272. (push '(note . "%t") org-log-note-headings))
  1273. (defcustom org-log-states-order-reversed t
  1274. "Non-nil means, the latest state change note will be directly after heading.
  1275. When nil, the notes will be orderer according to time."
  1276. :group 'org-todo
  1277. :group 'org-progress
  1278. :type 'boolean)
  1279. (defcustom org-log-repeat 'time
  1280. "Non-nil means, record moving through the DONE state when triggering repeat.
  1281. An auto-repeating tasks is immediately switched back to TODO when marked
  1282. done. If you are not logging state changes (by adding \"@\" or \"!\" to
  1283. the TODO keyword definition, or recording a closing note by setting
  1284. `org-log-done', there will be no record of the task moving through DONE.
  1285. This variable forces taking a note anyway. Possible values are:
  1286. nil Don't force a record
  1287. time Record a time stamp
  1288. note Record a note
  1289. This option can also be set with on a per-file-basis with
  1290. #+STARTUP: logrepeat
  1291. #+STARTUP: lognoterepeat
  1292. #+STARTUP: nologrepeat
  1293. You can have local logging settings for a subtree by setting the LOGGING
  1294. property to one or more of these keywords."
  1295. :group 'org-todo
  1296. :group 'org-progress
  1297. :type '(choice
  1298. (const :tag "Don't force a record" nil)
  1299. (const :tag "Force recording the DONE state" time)
  1300. (const :tag "Force recording a note with the DONE state" note)))
  1301. (defgroup org-priorities nil
  1302. "Priorities in Org-mode."
  1303. :tag "Org Priorities"
  1304. :group 'org-todo)
  1305. (defcustom org-highest-priority ?A
  1306. "The highest priority of TODO items. A character like ?A, ?B etc.
  1307. Must have a smaller ASCII number than `org-lowest-priority'."
  1308. :group 'org-priorities
  1309. :type 'character)
  1310. (defcustom org-lowest-priority ?C
  1311. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1312. Must have a larger ASCII number than `org-highest-priority'."
  1313. :group 'org-priorities
  1314. :type 'character)
  1315. (defcustom org-default-priority ?B
  1316. "The default priority of TODO items.
  1317. This is the priority an item get if no explicit priority is given."
  1318. :group 'org-priorities
  1319. :type 'character)
  1320. (defcustom org-priority-start-cycle-with-default t
  1321. "Non-nil means, start with default priority when starting to cycle.
  1322. When this is nil, the first step in the cycle will be (depending on the
  1323. command used) one higher or lower that the default priority."
  1324. :group 'org-priorities
  1325. :type 'boolean)
  1326. (defgroup org-time nil
  1327. "Options concerning time stamps and deadlines in Org-mode."
  1328. :tag "Org Time"
  1329. :group 'org)
  1330. (defcustom org-insert-labeled-timestamps-at-point nil
  1331. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1332. When nil, these labeled time stamps are forces into the second line of an
  1333. entry, just after the headline. When scheduling from the global TODO list,
  1334. the time stamp will always be forced into the second line."
  1335. :group 'org-time
  1336. :type 'boolean)
  1337. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1338. "Formats for `format-time-string' which are used for time stamps.
  1339. It is not recommended to change this constant.")
  1340. (defcustom org-time-stamp-rounding-minutes '(0 5)
  1341. "Number of minutes to round time stamps to.
  1342. These are two values, the first applies when first creating a time stamp.
  1343. The second applies when changing it with the commands `S-up' and `S-down'.
  1344. When changing the time stamp, this means that it will change in steps
  1345. of N minutes, as given by the second value.
  1346. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  1347. numbers should be factors of 60, so for example 5, 10, 15.
  1348. When this is larger than 1, you can still force an exact time-stamp by using
  1349. a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
  1350. and by using a prefix arg to `S-up/down' to specify the exact number
  1351. of minutes to shift."
  1352. :group 'org-time
  1353. :get '(lambda (var) ; Make sure all entries have 5 elements
  1354. (if (integerp (default-value var))
  1355. (list (default-value var) 5)
  1356. (default-value var)))
  1357. :type '(list
  1358. (integer :tag "when inserting times")
  1359. (integer :tag "when modifying times")))
  1360. ;; Normalize old customizations of this variable.
  1361. (when (integerp org-time-stamp-rounding-minutes)
  1362. (setq org-time-stamp-rounding-minutes
  1363. (list org-time-stamp-rounding-minutes
  1364. org-time-stamp-rounding-minutes)))
  1365. (defcustom org-display-custom-times nil
  1366. "Non-nil means, overlay custom formats over all time stamps.
  1367. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1368. To turn this on on a per-file basis, insert anywhere in the file:
  1369. #+STARTUP: customtime"
  1370. :group 'org-time
  1371. :set 'set-default
  1372. :type 'sexp)
  1373. (make-variable-buffer-local 'org-display-custom-times)
  1374. (defcustom org-time-stamp-custom-formats
  1375. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1376. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1377. These are overlayed over the default ISO format if the variable
  1378. `org-display-custom-times' is set. Time like %H:%M should be at the
  1379. end of the second format."
  1380. :group 'org-time
  1381. :type 'sexp)
  1382. (defun org-time-stamp-format (&optional long inactive)
  1383. "Get the right format for a time string."
  1384. (let ((f (if long (cdr org-time-stamp-formats)
  1385. (car org-time-stamp-formats))))
  1386. (if inactive
  1387. (concat "[" (substring f 1 -1) "]")
  1388. f)))
  1389. (defcustom org-deadline-warning-days 14
  1390. "No. of days before expiration during which a deadline becomes active.
  1391. This variable governs the display in sparse trees and in the agenda.
  1392. When 0 or negative, it means use this number (the absolute value of it)
  1393. even if a deadline has a different individual lead time specified."
  1394. :group 'org-time
  1395. :group 'org-agenda-daily/weekly
  1396. :type 'number)
  1397. (defcustom org-read-date-prefer-future t
  1398. "Non-nil means, assume future for incomplete date input from user.
  1399. This affects the following situations:
  1400. 1. The user gives a day, but no month.
  1401. For example, if today is the 15th, and you enter \"3\", Org-mode will
  1402. read this as the third of *next* month. However, if you enter \"17\",
  1403. it will be considered as *this* month.
  1404. 2. The user gives a month but not a year.
  1405. For example, if it is april and you enter \"feb 2\", this will be read
  1406. as feb 2, *next* year. \"May 5\", however, will be this year.
  1407. Currently this does not work for ISO week specifications.
  1408. When this option is nil, the current month and year will always be used
  1409. as defaults."
  1410. :group 'org-time
  1411. :type 'boolean)
  1412. (defcustom org-read-date-display-live t
  1413. "Non-nil means, display current interpretation of date prompt live.
  1414. This display will be in an overlay, in the minibuffer."
  1415. :group 'org-time
  1416. :type 'boolean)
  1417. (defcustom org-read-date-popup-calendar t
  1418. "Non-nil means, pop up a calendar when prompting for a date.
  1419. In the calendar, the date can be selected with mouse-1. However, the
  1420. minibuffer will also be active, and you can simply enter the date as well.
  1421. When nil, only the minibuffer will be available."
  1422. :group 'org-time
  1423. :type 'boolean)
  1424. (if (fboundp 'defvaralias)
  1425. (defvaralias 'org-popup-calendar-for-date-prompt
  1426. 'org-read-date-popup-calendar))
  1427. (defcustom org-extend-today-until 0
  1428. "The hour when your day really ends.
  1429. This has influence for the following applications:
  1430. - When switching the agenda to \"today\". It it is still earlier than
  1431. the time given here, the day recognized as TODAY is actually yesterday.
  1432. - When a date is read from the user and it is still before the time given
  1433. here, the current date and time will be assumed to be yesterday, 23:59.
  1434. FIXME:
  1435. IMPORTANT: This is still a very experimental feature, it may disappear
  1436. again or it may be extended to mean more things."
  1437. :group 'org-time
  1438. :type 'number)
  1439. (defcustom org-edit-timestamp-down-means-later nil
  1440. "Non-nil means, S-down will increase the time in a time stamp.
  1441. When nil, S-up will increase."
  1442. :group 'org-time
  1443. :type 'boolean)
  1444. (defcustom org-calendar-follow-timestamp-change t
  1445. "Non-nil means, make the calendar window follow timestamp changes.
  1446. When a timestamp is modified and the calendar window is visible, it will be
  1447. moved to the new date."
  1448. :group 'org-time
  1449. :type 'boolean)
  1450. (defgroup org-tags nil
  1451. "Options concerning tags in Org-mode."
  1452. :tag "Org Tags"
  1453. :group 'org)
  1454. (defcustom org-tag-alist nil
  1455. "List of tags allowed in Org-mode files.
  1456. When this list is nil, Org-mode will base TAG input on what is already in the
  1457. buffer.
  1458. The value of this variable is an alist, the car of each entry must be a
  1459. keyword as a string, the cdr may be a character that is used to select
  1460. that tag through the fast-tag-selection interface.
  1461. See the manual for details."
  1462. :group 'org-tags
  1463. :type '(repeat
  1464. (choice
  1465. (cons (string :tag "Tag name")
  1466. (character :tag "Access char"))
  1467. (const :tag "Start radio group" (:startgroup))
  1468. (const :tag "End radio group" (:endgroup)))))
  1469. (defcustom org-use-fast-tag-selection 'auto
  1470. "Non-nil means, use fast tag selection scheme.
  1471. This is a special interface to select and deselect tags with single keys.
  1472. When nil, fast selection is never used.
  1473. When the symbol `auto', fast selection is used if and only if selection
  1474. characters for tags have been configured, either through the variable
  1475. `org-tag-alist' or through a #+TAGS line in the buffer.
  1476. When t, fast selection is always used and selection keys are assigned
  1477. automatically if necessary."
  1478. :group 'org-tags
  1479. :type '(choice
  1480. (const :tag "Always" t)
  1481. (const :tag "Never" nil)
  1482. (const :tag "When selection characters are configured" 'auto)))
  1483. (defcustom org-fast-tag-selection-single-key nil
  1484. "Non-nil means, fast tag selection exits after first change.
  1485. When nil, you have to press RET to exit it.
  1486. During fast tag selection, you can toggle this flag with `C-c'.
  1487. This variable can also have the value `expert'. In this case, the window
  1488. displaying the tags menu is not even shown, until you press C-c again."
  1489. :group 'org-tags
  1490. :type '(choice
  1491. (const :tag "No" nil)
  1492. (const :tag "Yes" t)
  1493. (const :tag "Expert" expert)))
  1494. (defvar org-fast-tag-selection-include-todo nil
  1495. "Non-nil means, fast tags selection interface will also offer TODO states.
  1496. This is an undocumented feature, you should not rely on it.")
  1497. (defcustom org-tags-column (if (featurep 'xemacs) -79 -80)
  1498. "The column to which tags should be indented in a headline.
  1499. If this number is positive, it specifies the column. If it is negative,
  1500. it means that the tags should be flushright to that column. For example,
  1501. -80 works well for a normal 80 character screen."
  1502. :group 'org-tags
  1503. :type 'integer)
  1504. (defcustom org-auto-align-tags t
  1505. "Non-nil means, realign tags after pro/demotion of TODO state change.
  1506. These operations change the length of a headline and therefore shift
  1507. the tags around. With this options turned on, after each such operation
  1508. the tags are again aligned to `org-tags-column'."
  1509. :group 'org-tags
  1510. :type 'boolean)
  1511. (defcustom org-use-tag-inheritance t
  1512. "Non-nil means, tags in levels apply also for sublevels.
  1513. When nil, only the tags directly given in a specific line apply there.
  1514. If you turn off this option, you very likely want to turn on the
  1515. companion option `org-tags-match-list-sublevels'.
  1516. This may also be a list of tags that should be inherited, or a regexp that
  1517. matches tags that should be inherited."
  1518. :group 'org-tags
  1519. :type '(choice
  1520. (const :tag "Not" nil)
  1521. (const :tag "Always" t)
  1522. (repeat :tag "Specific tags" (string :tag "Tag"))
  1523. (regexp :tag "Tags matched by regexp")))
  1524. (defun org-tag-inherit-p (tag)
  1525. "Check if TAG is one that should be inherited."
  1526. (cond
  1527. ((eq org-use-tag-inheritance t) t)
  1528. ((not org-use-tag-inheritance) nil)
  1529. ((stringp org-use-tag-inheritance)
  1530. (string-match org-use-tag-inheritance tag))
  1531. ((listp org-use-tag-inheritance)
  1532. (member tag org-use-tag-inheritance))
  1533. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  1534. (defcustom org-tags-match-list-sublevels nil
  1535. "Non-nil means list also sublevels of headlines matching tag search.
  1536. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  1537. the sublevels of a headline matching a tag search often also match
  1538. the same search. Listing all of them can create very long lists.
  1539. Setting this variable to nil causes subtrees of a match to be skipped.
  1540. This option is off by default, because inheritance in on. If you turn
  1541. inheritance off, you very likely want to turn this option on.
  1542. As a special case, if the tag search is restricted to TODO items, the
  1543. value of this variable is ignored and sublevels are always checked, to
  1544. make sure all corresponding TODO items find their way into the list."
  1545. :group 'org-tags
  1546. :type 'boolean)
  1547. (defvar org-tags-history nil
  1548. "History of minibuffer reads for tags.")
  1549. (defvar org-last-tags-completion-table nil
  1550. "The last used completion table for tags.")
  1551. (defvar org-after-tags-change-hook nil
  1552. "Hook that is run after the tags in a line have changed.")
  1553. (defgroup org-properties nil
  1554. "Options concerning properties in Org-mode."
  1555. :tag "Org Properties"
  1556. :group 'org)
  1557. (defcustom org-property-format "%-10s %s"
  1558. "How property key/value pairs should be formatted by `indent-line'.
  1559. When `indent-line' hits a property definition, it will format the line
  1560. according to this format, mainly to make sure that the values are
  1561. lined-up with respect to each other."
  1562. :group 'org-properties
  1563. :type 'string)
  1564. (defcustom org-use-property-inheritance nil
  1565. "Non-nil means, properties apply also for sublevels.
  1566. This setting is chiefly used during property searches. Turning it on can
  1567. cause significant overhead when doing a search, which is why it is not
  1568. on by default.
  1569. When nil, only the properties directly given in the current entry count.
  1570. When t, every property is inherited. The value may also be a list of
  1571. properties that should have inheritance, or a regular expression matching
  1572. properties that should be inherited.
  1573. However, note that some special properties use inheritance under special
  1574. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  1575. and the properties ending in \"_ALL\" when they are used as descriptor
  1576. for valid values of a property.
  1577. Note for programmers:
  1578. When querying an entry with `org-entry-get', you can control if inheritance
  1579. should be used. By default, `org-entry-get' looks only at the local
  1580. properties. You can request inheritance by setting the inherit argument
  1581. to t (to force inheritance) or to `selective' (to respect the setting
  1582. in this variable)."
  1583. :group 'org-properties
  1584. :type '(choice
  1585. (const :tag "Not" nil)
  1586. (const :tag "Always" t)
  1587. (repeat :tag "Specific properties" (string :tag "Property"))
  1588. (regexp :tag "Properties matched by regexp")))
  1589. (defun org-property-inherit-p (property)
  1590. "Check if PROPERTY is one that should be inherited."
  1591. (cond
  1592. ((eq org-use-property-inheritance t) t)
  1593. ((not org-use-property-inheritance) nil)
  1594. ((stringp org-use-property-inheritance)
  1595. (string-match org-use-property-inheritance property))
  1596. ((listp org-use-property-inheritance)
  1597. (member property org-use-property-inheritance))
  1598. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  1599. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  1600. "The default column format, if no other format has been defined.
  1601. This variable can be set on the per-file basis by inserting a line
  1602. #+COLUMNS: %25ITEM ....."
  1603. :group 'org-properties
  1604. :type 'string)
  1605. (defcustom org-effort-property "Effort"
  1606. "The property that is being used to keep track of effort estimates.
  1607. Effort estimates given in this property need to have the format H:MM."
  1608. :group 'org-properties
  1609. :group 'org-progress
  1610. :type '(string :tag "Property"))
  1611. (defcustom org-global-properties nil
  1612. "List of property/value pairs that can be inherited by any entry.
  1613. You can set buffer-local values for this by adding lines like
  1614. #+PROPERTY: NAME VALUE"
  1615. :group 'org-properties
  1616. :type '(repeat
  1617. (cons (string :tag "Property")
  1618. (string :tag "Value"))))
  1619. (defvar org-local-properties nil
  1620. "List of property/value pairs that can be inherited by any entry.
  1621. Valid for the current buffer.
  1622. This variable is populated from #+PROPERTY lines.")
  1623. (defgroup org-agenda nil
  1624. "Options concerning agenda views in Org-mode."
  1625. :tag "Org Agenda"
  1626. :group 'org)
  1627. (defvar org-category nil
  1628. "Variable used by org files to set a category for agenda display.
  1629. Such files should use a file variable to set it, for example
  1630. # -*- mode: org; org-category: \"ELisp\"
  1631. or contain a special line
  1632. #+CATEGORY: ELisp
  1633. If the file does not specify a category, then file's base name
  1634. is used instead.")
  1635. (make-variable-buffer-local 'org-category)
  1636. (defcustom org-agenda-files nil
  1637. "The files to be used for agenda display.
  1638. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  1639. \\[org-remove-file]. You can also use customize to edit the list.
  1640. If an entry is a directory, all files in that directory that are matched by
  1641. `org-agenda-file-regexp' will be part of the file list.
  1642. If the value of the variable is not a list but a single file name, then
  1643. the list of agenda files is actually stored and maintained in that file, one
  1644. agenda file per line."
  1645. :group 'org-agenda
  1646. :type '(choice
  1647. (repeat :tag "List of files and directories" file)
  1648. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  1649. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  1650. "Regular expression to match files for `org-agenda-files'.
  1651. If any element in the list in that variable contains a directory instead
  1652. of a normal file, all files in that directory that are matched by this
  1653. regular expression will be included."
  1654. :group 'org-agenda
  1655. :type 'regexp)
  1656. (defcustom org-agenda-text-search-extra-files nil
  1657. "List of extra files to be searched by text search commands.
  1658. These files will be search in addition to the agenda files by the
  1659. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  1660. Note that these files will only be searched for text search commands,
  1661. not for the other agenda views like todo lists, tag searches or the weekly
  1662. agenda. This variable is intended to list notes and possibly archive files
  1663. that should also be searched by these two commands.
  1664. In fact, if the first element in the list is the symbol `agenda-archives',
  1665. than all archive files of all agenda files will be added to the search
  1666. scope."
  1667. :group 'org-agenda
  1668. :type '(set :greedy t
  1669. (const :tag "Agenda Archives" agenda-archives)
  1670. (repeat :inline t (file))))
  1671. (if (fboundp 'defvaralias)
  1672. (defvaralias 'org-agenda-multi-occur-extra-files
  1673. 'org-agenda-text-search-extra-files))
  1674. (defcustom org-agenda-skip-unavailable-files nil
  1675. "t means to just skip non-reachable files in `org-agenda-files'.
  1676. Nil means to remove them, after a query, from the list."
  1677. :group 'org-agenda
  1678. :type 'boolean)
  1679. (defcustom org-calendar-to-agenda-key [?c]
  1680. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  1681. The command `org-calendar-goto-agenda' will be bound to this key. The
  1682. default is the character `c' because then `c' can be used to switch back and
  1683. forth between agenda and calendar."
  1684. :group 'org-agenda
  1685. :type 'sexp)
  1686. (eval-after-load "calendar"
  1687. '(org-defkey calendar-mode-map org-calendar-to-agenda-key
  1688. 'org-calendar-goto-agenda))
  1689. (defgroup org-latex nil
  1690. "Options for embedding LaTeX code into Org-mode."
  1691. :tag "Org LaTeX"
  1692. :group 'org)
  1693. (defcustom org-format-latex-options
  1694. '(:foreground default :background default :scale 1.0
  1695. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  1696. :matchers ("begin" "$" "$$" "\\(" "\\["))
  1697. "Options for creating images from LaTeX fragments.
  1698. This is a property list with the following properties:
  1699. :foreground the foreground color for images embedded in emacs, e.g. \"Black\".
  1700. `default' means use the forground of the default face.
  1701. :background the background color, or \"Transparent\".
  1702. `default' means use the background of the default face.
  1703. :scale a scaling factor for the size of the images
  1704. :html-foreground, :html-background, :html-scale
  1705. The same numbers for HTML export.
  1706. :matchers a list indicating which matchers should be used to
  1707. find LaTeX fragments. Valid members of this list are:
  1708. \"begin\" find environments
  1709. \"$\" find math expressions surrounded by $...$
  1710. \"$$\" find math expressions surrounded by $$....$$
  1711. \"\\(\" find math expressions surrounded by \\(...\\)
  1712. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  1713. :group 'org-latex
  1714. :type 'plist)
  1715. (defcustom org-format-latex-header "\\documentclass{article}
  1716. \\usepackage{fullpage} % do not remove
  1717. \\usepackage{amssymb}
  1718. \\usepackage[usenames]{color}
  1719. \\usepackage{amsmath}
  1720. \\usepackage{latexsym}
  1721. \\usepackage[mathscr]{eucal}
  1722. \\pagestyle{empty} % do not remove"
  1723. "The document header used for processing LaTeX fragments."
  1724. :group 'org-latex
  1725. :type 'string)
  1726. (defgroup org-font-lock nil
  1727. "Font-lock settings for highlighting in Org-mode."
  1728. :tag "Org Font Lock"
  1729. :group 'org)
  1730. (defcustom org-level-color-stars-only nil
  1731. "Non-nil means fontify only the stars in each headline.
  1732. When nil, the entire headline is fontified.
  1733. Changing it requires restart of `font-lock-mode' to become effective
  1734. also in regions already fontified."
  1735. :group 'org-font-lock
  1736. :type 'boolean)
  1737. (defcustom org-hide-leading-stars nil
  1738. "Non-nil means, hide the first N-1 stars in a headline.
  1739. This works by using the face `org-hide' for these stars. This
  1740. face is white for a light background, and black for a dark
  1741. background. You may have to customize the face `org-hide' to
  1742. make this work.
  1743. Changing it requires restart of `font-lock-mode' to become effective
  1744. also in regions already fontified.
  1745. You may also set this on a per-file basis by adding one of the following
  1746. lines to the buffer:
  1747. #+STARTUP: hidestars
  1748. #+STARTUP: showstars"
  1749. :group 'org-font-lock
  1750. :type 'boolean)
  1751. (defcustom org-fontify-done-headline nil
  1752. "Non-nil means, change the face of a headline if it is marked DONE.
  1753. Normally, only the TODO/DONE keyword indicates the state of a headline.
  1754. When this is non-nil, the headline after the keyword is set to the
  1755. `org-headline-done' as an additional indication."
  1756. :group 'org-font-lock
  1757. :type 'boolean)
  1758. (defcustom org-fontify-emphasized-text t
  1759. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  1760. Changing this variable requires a restart of Emacs to take effect."
  1761. :group 'org-font-lock
  1762. :type 'boolean)
  1763. (defcustom org-highlight-latex-fragments-and-specials nil
  1764. "Non-nil means, fontify what is treated specially by the exporters."
  1765. :group 'org-font-lock
  1766. :type 'boolean)
  1767. (defcustom org-hide-emphasis-markers nil
  1768. "Non-nil mean font-lock should hide the emphasis marker characters."
  1769. :group 'org-font-lock
  1770. :type 'boolean)
  1771. (defvar org-emph-re nil
  1772. "Regular expression for matching emphasis.")
  1773. (defvar org-verbatim-re nil
  1774. "Regular expression for matching verbatim text.")
  1775. (defvar org-emphasis-regexp-components) ; defined just below
  1776. (defvar org-emphasis-alist) ; defined just below
  1777. (defun org-set-emph-re (var val)
  1778. "Set variable and compute the emphasis regular expression."
  1779. (set var val)
  1780. (when (and (boundp 'org-emphasis-alist)
  1781. (boundp 'org-emphasis-regexp-components)
  1782. org-emphasis-alist org-emphasis-regexp-components)
  1783. (let* ((e org-emphasis-regexp-components)
  1784. (pre (car e))
  1785. (post (nth 1 e))
  1786. (border (nth 2 e))
  1787. (body (nth 3 e))
  1788. (nl (nth 4 e))
  1789. (stacked (and nil (nth 5 e))) ; stacked is no longer allowed, forced to nil
  1790. (body1 (concat body "*?"))
  1791. (markers (mapconcat 'car org-emphasis-alist ""))
  1792. (vmarkers (mapconcat
  1793. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  1794. org-emphasis-alist "")))
  1795. ;; make sure special characters appear at the right position in the class
  1796. (if (string-match "\\^" markers)
  1797. (setq markers (concat (replace-match "" t t markers) "^")))
  1798. (if (string-match "-" markers)
  1799. (setq markers (concat (replace-match "" t t markers) "-")))
  1800. (if (string-match "\\^" vmarkers)
  1801. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  1802. (if (string-match "-" vmarkers)
  1803. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  1804. (if (> nl 0)
  1805. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  1806. (int-to-string nl) "\\}")))
  1807. ;; Make the regexp
  1808. (setq org-emph-re
  1809. (concat "\\([" pre (if (and nil stacked) markers) "]\\|^\\)"
  1810. "\\("
  1811. "\\([" markers "]\\)"
  1812. "\\("
  1813. "[^" border "]\\|"
  1814. "[^" border (if (and nil stacked) markers) "]"
  1815. body1
  1816. "[^" border (if (and nil stacked) markers) "]"
  1817. "\\)"
  1818. "\\3\\)"
  1819. "\\([" post (if (and nil stacked) markers) "]\\|$\\)"))
  1820. (setq org-verbatim-re
  1821. (concat "\\([" pre "]\\|^\\)"
  1822. "\\("
  1823. "\\([" vmarkers "]\\)"
  1824. "\\("
  1825. "[^" border "]\\|"
  1826. "[^" border "]"
  1827. body1
  1828. "[^" border "]"
  1829. "\\)"
  1830. "\\3\\)"
  1831. "\\([" post "]\\|$\\)")))))
  1832. (defcustom org-emphasis-regexp-components
  1833. '(" \t('\"" "- \t.,:?;'\")" " \t\r\n,\"'" "." 1)
  1834. "Components used to build the regular expression for emphasis.
  1835. This is a list with 6 entries. Terminology: In an emphasis string
  1836. like \" *strong word* \", we call the initial space PREMATCH, the final
  1837. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  1838. and \"trong wor\" is the body. The different components in this variable
  1839. specify what is allowed/forbidden in each part:
  1840. pre Chars allowed as prematch. Beginning of line will be allowed too.
  1841. post Chars allowed as postmatch. End of line will be allowed too.
  1842. border The chars *forbidden* as border characters.
  1843. body-regexp A regexp like \".\" to match a body character. Don't use
  1844. non-shy groups here, and don't allow newline here.
  1845. newline The maximum number of newlines allowed in an emphasis exp.
  1846. Use customize to modify this, or restart Emacs after changing it."
  1847. :group 'org-font-lock
  1848. :set 'org-set-emph-re
  1849. :type '(list
  1850. (sexp :tag "Allowed chars in pre ")
  1851. (sexp :tag "Allowed chars in post ")
  1852. (sexp :tag "Forbidden chars in border ")
  1853. (sexp :tag "Regexp for body ")
  1854. (integer :tag "number of newlines allowed")
  1855. (option (boolean :tag "Stacking (DISABLED) "))))
  1856. (defcustom org-emphasis-alist
  1857. `(("*" bold "<b>" "</b>")
  1858. ("/" italic "<i>" "</i>")
  1859. ("_" underline "<u>" "</u>")
  1860. ("=" org-code "<code>" "</code>" verbatim)
  1861. ("~" org-verbatim "" "" verbatim)
  1862. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  1863. "<del>" "</del>")
  1864. )
  1865. "Special syntax for emphasized text.
  1866. Text starting and ending with a special character will be emphasized, for
  1867. example *bold*, _underlined_ and /italic/. This variable sets the marker
  1868. characters, the face to be used by font-lock for highlighting in Org-mode
  1869. Emacs buffers, and the HTML tags to be used for this.
  1870. Use customize to modify this, or restart Emacs after changing it."
  1871. :group 'org-font-lock
  1872. :set 'org-set-emph-re
  1873. :type '(repeat
  1874. (list
  1875. (string :tag "Marker character")
  1876. (choice
  1877. (face :tag "Font-lock-face")
  1878. (plist :tag "Face property list"))
  1879. (string :tag "HTML start tag")
  1880. (string :tag "HTML end tag")
  1881. (option (const verbatim)))))
  1882. ;;; Miscellaneous options
  1883. (defgroup org-completion nil
  1884. "Completion in Org-mode."
  1885. :tag "Org Completion"
  1886. :group 'org)
  1887. (defcustom org-completion-fallback-command 'hippie-expand
  1888. "The expansion command called by \\[org-complete] in normal context.
  1889. Normal means, no org-mode-specific context."
  1890. :group 'org-completion
  1891. :type 'function)
  1892. ;;; Functions and variables from ther packages
  1893. ;; Declared here to avoid compiler warnings
  1894. ;; XEmacs only
  1895. (defvar outline-mode-menu-heading)
  1896. (defvar outline-mode-menu-show)
  1897. (defvar outline-mode-menu-hide)
  1898. (defvar zmacs-regions) ; XEmacs regions
  1899. ;; Emacs only
  1900. (defvar mark-active)
  1901. ;; Various packages
  1902. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  1903. (declare-function calendar-forward-day "cal-move" (arg))
  1904. (declare-function calendar-goto-date "cal-move" (date))
  1905. (declare-function calendar-goto-today "cal-move" ())
  1906. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  1907. (defvar calc-embedded-close-formula)
  1908. (defvar calc-embedded-open-formula)
  1909. (declare-function cdlatex-tab "ext:cdlatex" ())
  1910. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  1911. (defvar font-lock-unfontify-region-function)
  1912. (declare-function iswitchb-mode "iswitchb" (&optional arg))
  1913. (declare-function iswitchb-read-buffer (prompt &optional default require-match start matches-set))
  1914. (defvar iswitchb-temp-buflist)
  1915. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  1916. (declare-function org-agenda-skip "org-agenda" ())
  1917. (declare-function org-format-agenda-item "org-agenda"
  1918. (extra txt &optional category tags dotime noprefix remove-re))
  1919. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  1920. (declare-function org-agenda-change-all-lines "org-agenda"
  1921. (newhead hdmarker &optional fixface))
  1922. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  1923. (declare-function org-agenda-maybe-redo "org-agenda" ())
  1924. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  1925. (beg end))
  1926. (declare-function parse-time-string "parse-time" (string))
  1927. (declare-function remember "remember" (&optional initial))
  1928. (declare-function remember-buffer-desc "remember" ())
  1929. (declare-function remember-finalize "remember" ())
  1930. (defvar remember-save-after-remembering)
  1931. (defvar remember-data-file)
  1932. (defvar remember-register)
  1933. (defvar remember-buffer)
  1934. (defvar remember-handler-functions)
  1935. (defvar remember-annotation-functions)
  1936. (defvar texmathp-why)
  1937. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  1938. (declare-function table--at-cell-p "table" (position &optional object at-column))
  1939. (defvar w3m-current-url)
  1940. (defvar w3m-current-title)
  1941. (defvar org-latex-regexps)
  1942. ;;; Autoload and prepare some org modules
  1943. ;; Some table stuff that needs to be defined here, because it is used
  1944. ;; by the functions setting up org-mode or checking for table context.
  1945. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  1946. "Detects an org-type or table-type table.")
  1947. (defconst org-table-line-regexp "^[ \t]*|"
  1948. "Detects an org-type table line.")
  1949. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  1950. "Detects an org-type table line.")
  1951. (defconst org-table-hline-regexp "^[ \t]*|-"
  1952. "Detects an org-type table hline.")
  1953. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  1954. "Detects a table-type table hline.")
  1955. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  1956. "Searching from within a table (any type) this finds the first line
  1957. outside the table.")
  1958. ;; Autoload the functions in org-table.el that are needed by functions here.
  1959. (eval-and-compile
  1960. (org-autoload "org-table"
  1961. '(org-table-align org-table-begin org-table-blank-field
  1962. org-table-convert org-table-convert-region org-table-copy-down
  1963. org-table-copy-region org-table-create
  1964. org-table-create-or-convert-from-region
  1965. org-table-create-with-table.el org-table-current-dline
  1966. org-table-cut-region org-table-delete-column org-table-edit-field
  1967. org-table-edit-formulas org-table-end org-table-eval-formula
  1968. org-table-export org-table-field-info
  1969. org-table-get-stored-formulas org-table-goto-column
  1970. org-table-hline-and-move org-table-import org-table-insert-column
  1971. org-table-insert-hline org-table-insert-row org-table-iterate
  1972. org-table-justify-field-maybe org-table-kill-row
  1973. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  1974. org-table-move-column org-table-move-column-left
  1975. org-table-move-column-right org-table-move-row
  1976. org-table-move-row-down org-table-move-row-up
  1977. org-table-next-field org-table-next-row org-table-paste-rectangle
  1978. org-table-previous-field org-table-recalculate
  1979. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  1980. org-table-toggle-coordinate-overlays
  1981. org-table-toggle-formula-debugger org-table-wrap-region
  1982. orgtbl-mode turn-on-orgtbl)))
  1983. (defun org-at-table-p (&optional table-type)
  1984. "Return t if the cursor is inside an org-type table.
  1985. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  1986. (if org-enable-table-editor
  1987. (save-excursion
  1988. (beginning-of-line 1)
  1989. (looking-at (if table-type org-table-any-line-regexp
  1990. org-table-line-regexp)))
  1991. nil))
  1992. (defsubst org-table-p () (org-at-table-p))
  1993. (defun org-at-table.el-p ()
  1994. "Return t if and only if we are at a table.el table."
  1995. (and (org-at-table-p 'any)
  1996. (save-excursion
  1997. (goto-char (org-table-begin 'any))
  1998. (looking-at org-table1-hline-regexp))))
  1999. (defun org-table-recognize-table.el ()
  2000. "If there is a table.el table nearby, recognize it and move into it."
  2001. (if org-table-tab-recognizes-table.el
  2002. (if (org-at-table.el-p)
  2003. (progn
  2004. (beginning-of-line 1)
  2005. (if (looking-at org-table-dataline-regexp)
  2006. nil
  2007. (if (looking-at org-table1-hline-regexp)
  2008. (progn
  2009. (beginning-of-line 2)
  2010. (if (looking-at org-table-any-border-regexp)
  2011. (beginning-of-line -1)))))
  2012. (if (re-search-forward "|" (org-table-end t) t)
  2013. (progn
  2014. (require 'table)
  2015. (if (table--at-cell-p (point))
  2016. t
  2017. (message "recognizing table.el table...")
  2018. (table-recognize-table)
  2019. (message "recognizing table.el table...done")))
  2020. (error "This should not happen..."))
  2021. t)
  2022. nil)
  2023. nil))
  2024. (defun org-at-table-hline-p ()
  2025. "Return t if the cursor is inside a hline in a table."
  2026. (if org-enable-table-editor
  2027. (save-excursion
  2028. (beginning-of-line 1)
  2029. (looking-at org-table-hline-regexp))
  2030. nil))
  2031. (defvar org-table-clean-did-remove-column nil)
  2032. (defun org-table-map-tables (function)
  2033. "Apply FUNCTION to the start of all tables in the buffer."
  2034. (save-excursion
  2035. (save-restriction
  2036. (widen)
  2037. (goto-char (point-min))
  2038. (while (re-search-forward org-table-any-line-regexp nil t)
  2039. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  2040. (beginning-of-line 1)
  2041. (if (looking-at org-table-line-regexp)
  2042. (save-excursion (funcall function)))
  2043. (re-search-forward org-table-any-border-regexp nil 1))))
  2044. (message "Mapping tables: done"))
  2045. ;; Declare and autoload functions from org-exp.el
  2046. (declare-function org-default-export-plist "org-exp")
  2047. (declare-function org-infile-export-plist "org-exp")
  2048. (declare-function org-get-current-options "org-exp")
  2049. (eval-and-compile
  2050. (org-autoload "org-exp"
  2051. '(org-export org-export-as-ascii org-export-visible
  2052. org-insert-export-options-template org-export-as-html-and-open
  2053. org-export-as-html-batch org-export-as-html-to-buffer
  2054. org-replace-region-by-html org-export-region-as-html
  2055. org-export-as-html org-export-icalendar-this-file
  2056. org-export-icalendar-all-agenda-files
  2057. org-export-icalendar-combine-agenda-files org-export-as-xoxo)))
  2058. ;; Declare and autoload functions from org-exp.el
  2059. (eval-and-compile
  2060. (org-autoload "org-exp"
  2061. '(org-agenda org-agenda-list org-search-view
  2062. org-todo-list org-tags-view org-agenda-list-stuck-projects
  2063. org-diary org-agenda-to-appt)))
  2064. ;; Autoload org-remember
  2065. (eval-and-compile
  2066. (org-autoload "org-remember"
  2067. '(org-remember-insinuate org-remember-annotation
  2068. org-remember-apply-template org-remember org-remember-handler)))
  2069. ;; Autoload org-clock.el
  2070. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  2071. (beg end))
  2072. (defvar org-clock-marker (make-marker)
  2073. "Marker recording the last clock-in.")
  2074. (eval-and-compile
  2075. (org-autoload
  2076. "org-clock"
  2077. '(org-clock-in org-clock-out org-clock-cancel
  2078. org-clock-goto org-clock-sum org-clock-display
  2079. org-remove-clock-overlays org-clock-report
  2080. org-clocktable-shift org-dblock-write:clocktable
  2081. org-get-clocktable)))
  2082. (defun org-clock-update-time-maybe ()
  2083. "If this is a CLOCK line, update it and return t.
  2084. Otherwise, return nil."
  2085. (interactive)
  2086. (save-excursion
  2087. (beginning-of-line 1)
  2088. (skip-chars-forward " \t")
  2089. (when (looking-at org-clock-string)
  2090. (let ((re (concat "[ \t]*" org-clock-string
  2091. " *[[<]\\([^]>]+\\)[]>]-+[[<]\\([^]>]+\\)[]>]"
  2092. "\\([ \t]*=>.*\\)?"))
  2093. ts te h m s)
  2094. (if (not (looking-at re))
  2095. nil
  2096. (and (match-end 3) (delete-region (match-beginning 3) (match-end 3)))
  2097. (end-of-line 1)
  2098. (setq ts (match-string 1)
  2099. te (match-string 2))
  2100. (setq s (- (time-to-seconds
  2101. (apply 'encode-time (org-parse-time-string te)))
  2102. (time-to-seconds
  2103. (apply 'encode-time (org-parse-time-string ts))))
  2104. h (floor (/ s 3600))
  2105. s (- s (* 3600 h))
  2106. m (floor (/ s 60))
  2107. s (- s (* 60 s)))
  2108. (insert " => " (format "%2d:%02d" h m))
  2109. t)))))
  2110. (defun org-check-running-clock ()
  2111. "Check if the current buffer contains the running clock.
  2112. If yes, offer to stop it and to save the buffer with the changes."
  2113. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2114. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  2115. (buffer-name))))
  2116. (org-clock-out)
  2117. (when (y-or-n-p "Save changed buffer?")
  2118. (save-buffer))))
  2119. (defun org-clocktable-try-shift (dir n)
  2120. "Check if this line starts a clock table, if yes, shift the time block."
  2121. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  2122. (org-clocktable-shift dir n)))
  2123. ;; Autoload archiving code
  2124. ;; The stuff that is needed for cycling and tags has to be defined here.
  2125. (defgroup org-archive nil
  2126. "Options concerning archiving in Org-mode."
  2127. :tag "Org Archive"
  2128. :group 'org-structure)
  2129. (defcustom org-archive-location "%s_archive::"
  2130. "The location where subtrees should be archived.
  2131. Otherwise, the value of this variable is a string, consisting of two
  2132. parts, separated by a double-colon.
  2133. The first part is a file name - when omitted, archiving happens in the same
  2134. file. %s will be replaced by the current file name (without directory part).
  2135. Archiving to a different file is useful to keep archived entries from
  2136. contributing to the Org-mode Agenda.
  2137. The part after the double colon is a headline. The archived entries will be
  2138. filed under that headline. When omitted, the subtrees are simply filed away
  2139. at the end of the file, as top-level entries.
  2140. Here are a few examples:
  2141. \"%s_archive::\"
  2142. If the current file is Projects.org, archive in file
  2143. Projects.org_archive, as top-level trees. This is the default.
  2144. \"::* Archived Tasks\"
  2145. Archive in the current file, under the top-level headline
  2146. \"* Archived Tasks\".
  2147. \"~/org/archive.org::\"
  2148. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  2149. \"basement::** Finished Tasks\"
  2150. Archive in file ./basement (relative path), as level 3 trees
  2151. below the level 2 heading \"** Finished Tasks\".
  2152. You may set this option on a per-file basis by adding to the buffer a
  2153. line like
  2154. #+ARCHIVE: basement::** Finished Tasks
  2155. You may also define it locally for a subtree by setting an ARCHIVE property
  2156. in the entry. If such a property is found in an entry, or anywhere up
  2157. the hierarchy, it will be used."
  2158. :group 'org-archive
  2159. :type 'string)
  2160. (defcustom org-archive-tag "ARCHIVE"
  2161. "The tag that marks a subtree as archived.
  2162. An archived subtree does not open during visibility cycling, and does
  2163. not contribute to the agenda listings.
  2164. After changing this, font-lock must be restarted in the relevant buffers to
  2165. get the proper fontification."
  2166. :group 'org-archive
  2167. :group 'org-keywords
  2168. :type 'string)
  2169. (defcustom org-agenda-skip-archived-trees t
  2170. "Non-nil means, the agenda will skip any items located in archived trees.
  2171. An archived tree is a tree marked with the tag ARCHIVE."
  2172. :group 'org-archive
  2173. :group 'org-agenda-skip
  2174. :type 'boolean)
  2175. (defcustom org-cycle-open-archived-trees nil
  2176. "Non-nil means, `org-cycle' will open archived trees.
  2177. An archived tree is a tree marked with the tag ARCHIVE.
  2178. When nil, archived trees will stay folded. You can still open them with
  2179. normal outline commands like `show-all', but not with the cycling commands."
  2180. :group 'org-archive
  2181. :group 'org-cycle
  2182. :type 'boolean)
  2183. (defcustom org-sparse-tree-open-archived-trees nil
  2184. "Non-nil means sparse tree construction shows matches in archived trees.
  2185. When nil, matches in these trees are highlighted, but the trees are kept in
  2186. collapsed state."
  2187. :group 'org-archive
  2188. :group 'org-sparse-trees
  2189. :type 'boolean)
  2190. (defun org-cycle-hide-archived-subtrees (state)
  2191. "Re-hide all archived subtrees after a visibility state change."
  2192. (when (and (not org-cycle-open-archived-trees)
  2193. (not (memq state '(overview folded))))
  2194. (save-excursion
  2195. (let* ((globalp (memq state '(contents all)))
  2196. (beg (if globalp (point-min) (point)))
  2197. (end (if globalp (point-max) (org-end-of-subtree t))))
  2198. (org-hide-archived-subtrees beg end)
  2199. (goto-char beg)
  2200. (if (looking-at (concat ".*:" org-archive-tag ":"))
  2201. (message "%s" (substitute-command-keys
  2202. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  2203. (defun org-force-cycle-archived ()
  2204. "Cycle subtree even if it is archived."
  2205. (interactive)
  2206. (setq this-command 'org-cycle)
  2207. (let ((org-cycle-open-archived-trees t))
  2208. (call-interactively 'org-cycle)))
  2209. (defun org-hide-archived-subtrees (beg end)
  2210. "Re-hide all archived subtrees after a visibility state change."
  2211. (save-excursion
  2212. (let* ((re (concat ":" org-archive-tag ":")))
  2213. (goto-char beg)
  2214. (while (re-search-forward re end t)
  2215. (and (org-on-heading-p) (hide-subtree))
  2216. (org-end-of-subtree t)))))
  2217. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  2218. (eval-and-compile
  2219. (org-autoload "org-archive"
  2220. '(org-add-archive-files org-archive-subtree
  2221. org-archive-to-archive-sibling org-toggle-archive-tag)))
  2222. ;; Autoload Column View Code
  2223. (declare-function org-columns-number-to-string "org-colview")
  2224. (declare-function org-columns-get-format-and-top-level "org-colview")
  2225. (declare-function org-columns-compute "org-colview")
  2226. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  2227. '(org-columns-number-to-string org-columns-get-format-and-top-level
  2228. org-columns-compute org-agenda-columns org-columns-remove-overlays
  2229. org-columns org-insert-columns-dblock))
  2230. ;;; Variables for pre-computed regular expressions, all buffer local
  2231. (defvar org-drawer-regexp nil
  2232. "Matches first line of a hidden block.")
  2233. (make-variable-buffer-local 'org-drawer-regexp)
  2234. (defvar org-todo-regexp nil
  2235. "Matches any of the TODO state keywords.")
  2236. (make-variable-buffer-local 'org-todo-regexp)
  2237. (defvar org-not-done-regexp nil
  2238. "Matches any of the TODO state keywords except the last one.")
  2239. (make-variable-buffer-local 'org-not-done-regexp)
  2240. (defvar org-todo-line-regexp nil
  2241. "Matches a headline and puts TODO state into group 2 if present.")
  2242. (make-variable-buffer-local 'org-todo-line-regexp)
  2243. (defvar org-complex-heading-regexp nil
  2244. "Matches a headline and puts everything into groups:
  2245. group 1: the stars
  2246. group 2: The todo keyword, maybe
  2247. group 3: Priority cookie
  2248. group 4: True headline
  2249. group 5: Tags")
  2250. (make-variable-buffer-local 'org-complex-heading-regexp)
  2251. (defvar org-todo-line-tags-regexp nil
  2252. "Matches a headline and puts TODO state into group 2 if present.
  2253. Also put tags into group 4 if tags are present.")
  2254. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  2255. (defvar org-nl-done-regexp nil
  2256. "Matches newline followed by a headline with the DONE keyword.")
  2257. (make-variable-buffer-local 'org-nl-done-regexp)
  2258. (defvar org-looking-at-done-regexp nil
  2259. "Matches the DONE keyword a point.")
  2260. (make-variable-buffer-local 'org-looking-at-done-regexp)
  2261. (defvar org-ds-keyword-length 12
  2262. "Maximum length of the Deadline and SCHEDULED keywords.")
  2263. (make-variable-buffer-local 'org-ds-keyword-length)
  2264. (defvar org-deadline-regexp nil
  2265. "Matches the DEADLINE keyword.")
  2266. (make-variable-buffer-local 'org-deadline-regexp)
  2267. (defvar org-deadline-time-regexp nil
  2268. "Matches the DEADLINE keyword together with a time stamp.")
  2269. (make-variable-buffer-local 'org-deadline-time-regexp)
  2270. (defvar org-deadline-line-regexp nil
  2271. "Matches the DEADLINE keyword and the rest of the line.")
  2272. (make-variable-buffer-local 'org-deadline-line-regexp)
  2273. (defvar org-scheduled-regexp nil
  2274. "Matches the SCHEDULED keyword.")
  2275. (make-variable-buffer-local 'org-scheduled-regexp)
  2276. (defvar org-scheduled-time-regexp nil
  2277. "Matches the SCHEDULED keyword together with a time stamp.")
  2278. (make-variable-buffer-local 'org-scheduled-time-regexp)
  2279. (defvar org-closed-time-regexp nil
  2280. "Matches the CLOSED keyword together with a time stamp.")
  2281. (make-variable-buffer-local 'org-closed-time-regexp)
  2282. (defvar org-keyword-time-regexp nil
  2283. "Matches any of the 4 keywords, together with the time stamp.")
  2284. (make-variable-buffer-local 'org-keyword-time-regexp)
  2285. (defvar org-keyword-time-not-clock-regexp nil
  2286. "Matches any of the 3 keywords, together with the time stamp.")
  2287. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  2288. (defvar org-maybe-keyword-time-regexp nil
  2289. "Matches a timestamp, possibly preceeded by a keyword.")
  2290. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  2291. (defvar org-planning-or-clock-line-re nil
  2292. "Matches a line with planning or clock info.")
  2293. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  2294. (defconst org-plain-time-of-day-regexp
  2295. (concat
  2296. "\\(\\<[012]?[0-9]"
  2297. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2298. "\\(--?"
  2299. "\\(\\<[012]?[0-9]"
  2300. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2301. "\\)?")
  2302. "Regular expression to match a plain time or time range.
  2303. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2304. groups carry important information:
  2305. 0 the full match
  2306. 1 the first time, range or not
  2307. 8 the second time, if it is a range.")
  2308. (defconst org-plain-time-extension-regexp
  2309. (concat
  2310. "\\(\\<[012]?[0-9]"
  2311. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2312. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  2313. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  2314. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2315. groups carry important information:
  2316. 0 the full match
  2317. 7 hours of duration
  2318. 9 minutes of duration")
  2319. (defconst org-stamp-time-of-day-regexp
  2320. (concat
  2321. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  2322. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  2323. "\\(--?"
  2324. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  2325. "Regular expression to match a timestamp time or time range.
  2326. After a match, the following groups carry important information:
  2327. 0 the full match
  2328. 1 date plus weekday, for backreferencing to make sure both times on same day
  2329. 2 the first time, range or not
  2330. 4 the second time, if it is a range.")
  2331. (defconst org-startup-options
  2332. '(("fold" org-startup-folded t)
  2333. ("overview" org-startup-folded t)
  2334. ("nofold" org-startup-folded nil)
  2335. ("showall" org-startup-folded nil)
  2336. ("content" org-startup-folded content)
  2337. ("hidestars" org-hide-leading-stars t)
  2338. ("showstars" org-hide-leading-stars nil)
  2339. ("odd" org-odd-levels-only t)
  2340. ("oddeven" org-odd-levels-only nil)
  2341. ("align" org-startup-align-all-tables t)
  2342. ("noalign" org-startup-align-all-tables nil)
  2343. ("customtime" org-display-custom-times t)
  2344. ("logdone" org-log-done time)
  2345. ("lognotedone" org-log-done note)
  2346. ("nologdone" org-log-done nil)
  2347. ("lognoteclock-out" org-log-note-clock-out t)
  2348. ("nolognoteclock-out" org-log-note-clock-out nil)
  2349. ("logrepeat" org-log-repeat state)
  2350. ("lognoterepeat" org-log-repeat note)
  2351. ("nologrepeat" org-log-repeat nil)
  2352. ("constcgs" constants-unit-system cgs)
  2353. ("constSI" constants-unit-system SI))
  2354. "Variable associated with STARTUP options for org-mode.
  2355. Each element is a list of three items: The startup options as written
  2356. in the #+STARTUP line, the corresponding variable, and the value to
  2357. set this variable to if the option is found. An optional forth element PUSH
  2358. means to push this value onto the list in the variable.")
  2359. (defun org-set-regexps-and-options ()
  2360. "Precompute regular expressions for current buffer."
  2361. (when (org-mode-p)
  2362. (org-set-local 'org-todo-kwd-alist nil)
  2363. (org-set-local 'org-todo-key-alist nil)
  2364. (org-set-local 'org-todo-key-trigger nil)
  2365. (org-set-local 'org-todo-keywords-1 nil)
  2366. (org-set-local 'org-done-keywords nil)
  2367. (org-set-local 'org-todo-heads nil)
  2368. (org-set-local 'org-todo-sets nil)
  2369. (org-set-local 'org-todo-log-states nil)
  2370. (let ((re (org-make-options-regexp
  2371. '("CATEGORY" "SEQ_TODO" "TYP_TODO" "TODO" "COLUMNS"
  2372. "STARTUP" "ARCHIVE" "TAGS" "LINK" "PRIORITIES"
  2373. "CONSTANTS" "PROPERTY" "DRAWERS")))
  2374. (splitre "[ \t]+")
  2375. kwds kws0 kwsa key log value cat arch tags const links hw dws
  2376. tail sep kws1 prio props drawers)
  2377. (save-excursion
  2378. (save-restriction
  2379. (widen)
  2380. (goto-char (point-min))
  2381. (while (re-search-forward re nil t)
  2382. (setq key (match-string 1) value (org-match-string-no-properties 2))
  2383. (cond
  2384. ((equal key "CATEGORY")
  2385. (if (string-match "[ \t]+$" value)
  2386. (setq value (replace-match "" t t value)))
  2387. (setq cat value))
  2388. ((member key '("SEQ_TODO" "TODO"))
  2389. (push (cons 'sequence (org-split-string value splitre)) kwds))
  2390. ((equal key "TYP_TODO")
  2391. (push (cons 'type (org-split-string value splitre)) kwds))
  2392. ((equal key "TAGS")
  2393. (setq tags (append tags (org-split-string value splitre))))
  2394. ((equal key "COLUMNS")
  2395. (org-set-local 'org-columns-default-format value))
  2396. ((equal key "LINK")
  2397. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  2398. (push (cons (match-string 1 value)
  2399. (org-trim (match-string 2 value)))
  2400. links)))
  2401. ((equal key "PRIORITIES")
  2402. (setq prio (org-split-string value " +")))
  2403. ((equal key "PROPERTY")
  2404. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  2405. (push (cons (match-string 1 value) (match-string 2 value))
  2406. props)))
  2407. ((equal key "DRAWERS")
  2408. (setq drawers (org-split-string value splitre)))
  2409. ((equal key "CONSTANTS")
  2410. (setq const (append const (org-split-string value splitre))))
  2411. ((equal key "STARTUP")
  2412. (let ((opts (org-split-string value splitre))
  2413. l var val)
  2414. (while (setq l (pop opts))
  2415. (when (setq l (assoc l org-startup-options))
  2416. (setq var (nth 1 l) val (nth 2 l))
  2417. (if (not (nth 3 l))
  2418. (set (make-local-variable var) val)
  2419. (if (not (listp (symbol-value var)))
  2420. (set (make-local-variable var) nil))
  2421. (set (make-local-variable var) (symbol-value var))
  2422. (add-to-list var val))))))
  2423. ((equal key "ARCHIVE")
  2424. (string-match " *$" value)
  2425. (setq arch (replace-match "" t t value))
  2426. (remove-text-properties 0 (length arch)
  2427. '(face t fontified t) arch)))
  2428. )))
  2429. (when cat
  2430. (org-set-local 'org-category (intern cat))
  2431. (push (cons "CATEGORY" cat) props))
  2432. (when prio
  2433. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  2434. (setq prio (mapcar 'string-to-char prio))
  2435. (org-set-local 'org-highest-priority (nth 0 prio))
  2436. (org-set-local 'org-lowest-priority (nth 1 prio))
  2437. (org-set-local 'org-default-priority (nth 2 prio)))
  2438. (and props (org-set-local 'org-local-properties (nreverse props)))
  2439. (and drawers (org-set-local 'org-drawers drawers))
  2440. (and arch (org-set-local 'org-archive-location arch))
  2441. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  2442. ;; Process the TODO keywords
  2443. (unless kwds
  2444. ;; Use the global values as if they had been given locally.
  2445. (setq kwds (default-value 'org-todo-keywords))
  2446. (if (stringp (car kwds))
  2447. (setq kwds (list (cons org-todo-interpretation
  2448. (default-value 'org-todo-keywords)))))
  2449. (setq kwds (reverse kwds)))
  2450. (setq kwds (nreverse kwds))
  2451. (let (inter kws kw)
  2452. (while (setq kws (pop kwds))
  2453. (setq inter (pop kws) sep (member "|" kws)
  2454. kws0 (delete "|" (copy-sequence kws))
  2455. kwsa nil
  2456. kws1 (mapcar
  2457. (lambda (x)
  2458. ;; 1 2
  2459. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  2460. (progn
  2461. (setq kw (match-string 1 x)
  2462. key (and (match-end 2) (match-string 2 x))
  2463. log (org-extract-log-state-settings x))
  2464. (push (cons kw (and key (string-to-char key))) kwsa)
  2465. (and log (push log org-todo-log-states))
  2466. kw)
  2467. (error "Invalid TODO keyword %s" x)))
  2468. kws0)
  2469. kwsa (if kwsa (append '((:startgroup))
  2470. (nreverse kwsa)
  2471. '((:endgroup))))
  2472. hw (car kws1)
  2473. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  2474. tail (list inter hw (car dws) (org-last dws)))
  2475. (add-to-list 'org-todo-heads hw 'append)
  2476. (push kws1 org-todo-sets)
  2477. (setq org-done-keywords (append org-done-keywords dws nil))
  2478. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  2479. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  2480. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  2481. (setq org-todo-sets (nreverse org-todo-sets)
  2482. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  2483. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  2484. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  2485. ;; Process the constants
  2486. (when const
  2487. (let (e cst)
  2488. (while (setq e (pop const))
  2489. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  2490. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  2491. (setq org-table-formula-constants-local cst)))
  2492. ;; Process the tags.
  2493. (when tags
  2494. (let (e tgs)
  2495. (while (setq e (pop tags))
  2496. (cond
  2497. ((equal e "{") (push '(:startgroup) tgs))
  2498. ((equal e "}") (push '(:endgroup) tgs))
  2499. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  2500. (push (cons (match-string 1 e)
  2501. (string-to-char (match-string 2 e)))
  2502. tgs))
  2503. (t (push (list e) tgs))))
  2504. (org-set-local 'org-tag-alist nil)
  2505. (while (setq e (pop tgs))
  2506. (or (and (stringp (car e))
  2507. (assoc (car e) org-tag-alist))
  2508. (push e org-tag-alist))))))
  2509. ;; Compute the regular expressions and other local variables
  2510. (if (not org-done-keywords)
  2511. (setq org-done-keywords (list (org-last org-todo-keywords-1))))
  2512. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  2513. (length org-scheduled-string)
  2514. (length org-clock-string)
  2515. (length org-closed-string)))
  2516. org-drawer-regexp
  2517. (concat "^[ \t]*:\\("
  2518. (mapconcat 'regexp-quote org-drawers "\\|")
  2519. "\\):[ \t]*$")
  2520. org-not-done-keywords
  2521. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  2522. org-todo-regexp
  2523. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  2524. "\\|") "\\)\\>")
  2525. org-not-done-regexp
  2526. (concat "\\<\\("
  2527. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  2528. "\\)\\>")
  2529. org-todo-line-regexp
  2530. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  2531. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2532. "\\)\\>\\)?[ \t]*\\(.*\\)")
  2533. org-complex-heading-regexp
  2534. (concat "^\\(\\*+\\)\\(?:[ \t]+\\("
  2535. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2536. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  2537. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  2538. org-nl-done-regexp
  2539. (concat "\n\\*+[ \t]+"
  2540. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  2541. "\\)" "\\>")
  2542. org-todo-line-tags-regexp
  2543. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  2544. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2545. (org-re
  2546. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  2547. org-looking-at-done-regexp
  2548. (concat "^" "\\(?:"
  2549. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  2550. "\\>")
  2551. org-deadline-regexp (concat "\\<" org-deadline-string)
  2552. org-deadline-time-regexp
  2553. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  2554. org-deadline-line-regexp
  2555. (concat "\\<\\(" org-deadline-string "\\).*")
  2556. org-scheduled-regexp
  2557. (concat "\\<" org-scheduled-string)
  2558. org-scheduled-time-regexp
  2559. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  2560. org-closed-time-regexp
  2561. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  2562. org-keyword-time-regexp
  2563. (concat "\\<\\(" org-scheduled-string
  2564. "\\|" org-deadline-string
  2565. "\\|" org-closed-string
  2566. "\\|" org-clock-string "\\)"
  2567. " *[[<]\\([^]>]+\\)[]>]")
  2568. org-keyword-time-not-clock-regexp
  2569. (concat "\\<\\(" org-scheduled-string
  2570. "\\|" org-deadline-string
  2571. "\\|" org-closed-string
  2572. "\\)"
  2573. " *[[<]\\([^]>]+\\)[]>]")
  2574. org-maybe-keyword-time-regexp
  2575. (concat "\\(\\<\\(" org-scheduled-string
  2576. "\\|" org-deadline-string
  2577. "\\|" org-closed-string
  2578. "\\|" org-clock-string "\\)\\)?"
  2579. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  2580. org-planning-or-clock-line-re
  2581. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  2582. "\\|" org-deadline-string
  2583. "\\|" org-closed-string "\\|" org-clock-string
  2584. "\\)\\>\\)")
  2585. )
  2586. (org-compute-latex-and-specials-regexp)
  2587. (org-set-font-lock-defaults)))
  2588. (defun org-extract-log-state-settings (x)
  2589. "Extract the log state setting from a TODO keyword string.
  2590. This will extract info from a string like \"WAIT(w@/!)\"."
  2591. (let (kw key log1 log2)
  2592. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  2593. (setq kw (match-string 1 x)
  2594. key (and (match-end 2) (match-string 2 x))
  2595. log1 (and (match-end 3) (match-string 3 x))
  2596. log2 (and (match-end 4) (match-string 4 x)))
  2597. (and (or log1 log2)
  2598. (list kw
  2599. (and log1 (if (equal log1 "!") 'time 'note))
  2600. (and log2 (if (equal log2 "!") 'time 'note)))))))
  2601. (defun org-remove-keyword-keys (list)
  2602. "Remove a pair of parenthesis at the end of each string in LIST."
  2603. (mapcar (lambda (x)
  2604. (if (string-match "(.*)$" x)
  2605. (substring x 0 (match-beginning 0))
  2606. x))
  2607. list))
  2608. ;; FIXME: this could be done much better, using second characters etc.
  2609. (defun org-assign-fast-keys (alist)
  2610. "Assign fast keys to a keyword-key alist.
  2611. Respect keys that are already there."
  2612. (let (new e k c c1 c2 (char ?a))
  2613. (while (setq e (pop alist))
  2614. (cond
  2615. ((equal e '(:startgroup)) (push e new))
  2616. ((equal e '(:endgroup)) (push e new))
  2617. (t
  2618. (setq k (car e) c2 nil)
  2619. (if (cdr e)
  2620. (setq c (cdr e))
  2621. ;; automatically assign a character.
  2622. (setq c1 (string-to-char
  2623. (downcase (substring
  2624. k (if (= (string-to-char k) ?@) 1 0)))))
  2625. (if (or (rassoc c1 new) (rassoc c1 alist))
  2626. (while (or (rassoc char new) (rassoc char alist))
  2627. (setq char (1+ char)))
  2628. (setq c2 c1))
  2629. (setq c (or c2 char)))
  2630. (push (cons k c) new))))
  2631. (nreverse new)))
  2632. ;;; Some variables used in various places
  2633. (defvar org-window-configuration nil
  2634. "Used in various places to store a window configuration.")
  2635. (defvar org-finish-function nil
  2636. "Function to be called when `C-c C-c' is used.
  2637. This is for getting out of special buffers like remember.")
  2638. ;; FIXME: Occasionally check by commenting these, to make sure
  2639. ;; no other functions uses these, forgetting to let-bind them.
  2640. (defvar entry)
  2641. (defvar state)
  2642. (defvar last-state)
  2643. (defvar date)
  2644. (defvar description)
  2645. ;; Defined somewhere in this file, but used before definition.
  2646. (defvar org-html-entities)
  2647. (defvar org-struct-menu)
  2648. (defvar org-org-menu)
  2649. (defvar org-tbl-menu)
  2650. (defvar org-agenda-keymap)
  2651. ;;;; Define the Org-mode
  2652. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  2653. (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."))
  2654. ;; We use a before-change function to check if a table might need
  2655. ;; an update.
  2656. (defvar org-table-may-need-update t
  2657. "Indicates that a table might need an update.
  2658. This variable is set by `org-before-change-function'.
  2659. `org-table-align' sets it back to nil.")
  2660. (defun org-before-change-function (beg end)
  2661. "Every change indicates that a table might need an update."
  2662. (setq org-table-may-need-update t))
  2663. (defvar org-mode-map)
  2664. (defvar org-mode-hook nil)
  2665. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  2666. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  2667. (defvar org-table-buffer-is-an nil)
  2668. (defconst org-outline-regexp "\\*+ ")
  2669. ;;;###autoload
  2670. (define-derived-mode org-mode outline-mode "Org"
  2671. "Outline-based notes management and organizer, alias
  2672. \"Carsten's outline-mode for keeping track of everything.\"
  2673. Org-mode develops organizational tasks around a NOTES file which
  2674. contains information about projects as plain text. Org-mode is
  2675. implemented on top of outline-mode, which is ideal to keep the content
  2676. of large files well structured. It supports ToDo items, deadlines and
  2677. time stamps, which magically appear in the diary listing of the Emacs
  2678. calendar. Tables are easily created with a built-in table editor.
  2679. Plain text URL-like links connect to websites, emails (VM), Usenet
  2680. messages (Gnus), BBDB entries, and any files related to the project.
  2681. For printing and sharing of notes, an Org-mode file (or a part of it)
  2682. can be exported as a structured ASCII or HTML file.
  2683. The following commands are available:
  2684. \\{org-mode-map}"
  2685. ;; Get rid of Outline menus, they are not needed
  2686. ;; Need to do this here because define-derived-mode sets up
  2687. ;; the keymap so late. Still, it is a waste to call this each time
  2688. ;; we switch another buffer into org-mode.
  2689. (if (featurep 'xemacs)
  2690. (when (boundp 'outline-mode-menu-heading)
  2691. ;; Assume this is Greg's port, it used easymenu
  2692. (easy-menu-remove outline-mode-menu-heading)
  2693. (easy-menu-remove outline-mode-menu-show)
  2694. (easy-menu-remove outline-mode-menu-hide))
  2695. (define-key org-mode-map [menu-bar headings] 'undefined)
  2696. (define-key org-mode-map [menu-bar hide] 'undefined)
  2697. (define-key org-mode-map [menu-bar show] 'undefined))
  2698. (org-load-modules-maybe)
  2699. (easy-menu-add org-org-menu)
  2700. (easy-menu-add org-tbl-menu)
  2701. (org-install-agenda-files-menu)
  2702. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  2703. (org-add-to-invisibility-spec '(org-cwidth))
  2704. (when (featurep 'xemacs)
  2705. (org-set-local 'line-move-ignore-invisible t))
  2706. (org-set-local 'outline-regexp org-outline-regexp)
  2707. (org-set-local 'outline-level 'org-outline-level)
  2708. (when (and org-ellipsis
  2709. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  2710. (fboundp 'make-glyph-code))
  2711. (unless org-display-table
  2712. (setq org-display-table (make-display-table)))
  2713. (set-display-table-slot
  2714. org-display-table 4
  2715. (vconcat (mapcar
  2716. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  2717. org-ellipsis)))
  2718. (if (stringp org-ellipsis) org-ellipsis "..."))))
  2719. (setq buffer-display-table org-display-table))
  2720. (org-set-regexps-and-options)
  2721. ;; Calc embedded
  2722. (org-set-local 'calc-embedded-open-mode "# ")
  2723. (modify-syntax-entry ?# "<")
  2724. (modify-syntax-entry ?@ "w")
  2725. (if org-startup-truncated (setq truncate-lines t))
  2726. (org-set-local 'font-lock-unfontify-region-function
  2727. 'org-unfontify-region)
  2728. ;; Activate before-change-function
  2729. (org-set-local 'org-table-may-need-update t)
  2730. (org-add-hook 'before-change-functions 'org-before-change-function nil
  2731. 'local)
  2732. ;; Check for running clock before killing a buffer
  2733. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  2734. ;; Paragraphs and auto-filling
  2735. (org-set-autofill-regexps)
  2736. (setq indent-line-function 'org-indent-line-function)
  2737. (org-update-radio-target-regexp)
  2738. ;; Comment characters
  2739. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  2740. (org-set-local 'comment-padding " ")
  2741. ;; Align options lines
  2742. (org-set-local
  2743. 'align-mode-rules-list
  2744. '((org-in-buffer-settings
  2745. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  2746. (modes . '(org-mode)))))
  2747. ;; Imenu
  2748. (org-set-local 'imenu-create-index-function
  2749. 'org-imenu-get-tree)
  2750. ;; Make isearch reveal context
  2751. (if (or (featurep 'xemacs)
  2752. (not (boundp 'outline-isearch-open-invisible-function)))
  2753. ;; Emacs 21 and XEmacs make use of the hook
  2754. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  2755. ;; Emacs 22 deals with this through a special variable
  2756. (org-set-local 'outline-isearch-open-invisible-function
  2757. (lambda (&rest ignore) (org-show-context 'isearch))))
  2758. ;; If empty file that did not turn on org-mode automatically, make it to.
  2759. (if (and org-insert-mode-line-in-empty-file
  2760. (interactive-p)
  2761. (= (point-min) (point-max)))
  2762. (insert "# -*- mode: org -*-\n\n"))
  2763. (unless org-inhibit-startup
  2764. (when org-startup-align-all-tables
  2765. (let ((bmp (buffer-modified-p)))
  2766. (org-table-map-tables 'org-table-align)
  2767. (set-buffer-modified-p bmp)))
  2768. (org-cycle-hide-drawers 'all)
  2769. (cond
  2770. ((eq org-startup-folded t)
  2771. (org-cycle '(4)))
  2772. ((eq org-startup-folded 'content)
  2773. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  2774. (org-cycle '(4)) (org-cycle '(4)))))))
  2775. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  2776. (defun org-current-time ()
  2777. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  2778. (if (> (car org-time-stamp-rounding-minutes) 1)
  2779. (let ((r (car org-time-stamp-rounding-minutes))
  2780. (time (decode-time)))
  2781. (apply 'encode-time
  2782. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  2783. (nthcdr 2 time))))
  2784. (current-time)))
  2785. ;;;; Font-Lock stuff, including the activators
  2786. (defvar org-mouse-map (make-sparse-keymap))
  2787. (org-defkey org-mouse-map
  2788. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  2789. (org-defkey org-mouse-map
  2790. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  2791. (when org-mouse-1-follows-link
  2792. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  2793. (when org-tab-follows-link
  2794. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  2795. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  2796. (when org-return-follows-link
  2797. (org-defkey org-mouse-map [(return)] 'org-open-at-point)
  2798. (org-defkey org-mouse-map "\C-m" 'org-open-at-point))
  2799. (require 'font-lock)
  2800. (defconst org-non-link-chars "]\t\n\r<>")
  2801. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  2802. "shell" "elisp"))
  2803. (defvar org-link-types-re nil
  2804. "Matches a link that has a url-like prefix like \"http:\"")
  2805. (defvar org-link-re-with-space nil
  2806. "Matches a link with spaces, optional angular brackets around it.")
  2807. (defvar org-link-re-with-space2 nil
  2808. "Matches a link with spaces, optional angular brackets around it.")
  2809. (defvar org-angle-link-re nil
  2810. "Matches link with angular brackets, spaces are allowed.")
  2811. (defvar org-plain-link-re nil
  2812. "Matches plain link, without spaces.")
  2813. (defvar org-bracket-link-regexp nil
  2814. "Matches a link in double brackets.")
  2815. (defvar org-bracket-link-analytic-regexp nil
  2816. "Regular expression used to analyze links.
  2817. Here is what the match groups contain after a match:
  2818. 1: http:
  2819. 2: http
  2820. 3: path
  2821. 4: [desc]
  2822. 5: desc")
  2823. (defvar org-any-link-re nil
  2824. "Regular expression matching any link.")
  2825. (defun org-make-link-regexps ()
  2826. "Update the link regular expressions.
  2827. This should be called after the variable `org-link-types' has changed."
  2828. (setq org-link-types-re
  2829. (concat
  2830. "\\`\\(" (mapconcat 'identity org-link-types "\\|") "\\):")
  2831. org-link-re-with-space
  2832. (concat
  2833. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2834. "\\([^" org-non-link-chars " ]"
  2835. "[^" org-non-link-chars "]*"
  2836. "[^" org-non-link-chars " ]\\)>?")
  2837. org-link-re-with-space2
  2838. (concat
  2839. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2840. "\\([^" org-non-link-chars " ]"
  2841. "[^]\t\n\r]*"
  2842. "[^" org-non-link-chars " ]\\)>?")
  2843. org-angle-link-re
  2844. (concat
  2845. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2846. "\\([^" org-non-link-chars " ]"
  2847. "[^" org-non-link-chars "]*"
  2848. "\\)>")
  2849. org-plain-link-re
  2850. (concat
  2851. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2852. "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  2853. org-bracket-link-regexp
  2854. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  2855. org-bracket-link-analytic-regexp
  2856. (concat
  2857. "\\[\\["
  2858. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  2859. "\\([^]]+\\)"
  2860. "\\]"
  2861. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  2862. "\\]")
  2863. org-any-link-re
  2864. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  2865. org-angle-link-re "\\)\\|\\("
  2866. org-plain-link-re "\\)")))
  2867. (org-make-link-regexps)
  2868. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  2869. "Regular expression for fast time stamp matching.")
  2870. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  2871. "Regular expression for fast time stamp matching.")
  2872. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  2873. "Regular expression matching time strings for analysis.
  2874. This one does not require the space after the date, so it can be used
  2875. on a string that terminates immediately after the date.")
  2876. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  2877. "Regular expression matching time strings for analysis.")
  2878. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  2879. "Regular expression matching time stamps, with groups.")
  2880. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  2881. "Regular expression matching time stamps (also [..]), with groups.")
  2882. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  2883. "Regular expression matching a time stamp range.")
  2884. (defconst org-tr-regexp-both
  2885. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  2886. "Regular expression matching a time stamp range.")
  2887. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  2888. org-ts-regexp "\\)?")
  2889. "Regular expression matching a time stamp or time stamp range.")
  2890. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  2891. org-ts-regexp-both "\\)?")
  2892. "Regular expression matching a time stamp or time stamp range.
  2893. The time stamps may be either active or inactive.")
  2894. (defvar org-emph-face nil)
  2895. (defun org-do-emphasis-faces (limit)
  2896. "Run through the buffer and add overlays to links."
  2897. (let (rtn)
  2898. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  2899. (if (not (= (char-after (match-beginning 3))
  2900. (char-after (match-beginning 4))))
  2901. (progn
  2902. (setq rtn t)
  2903. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  2904. 'face
  2905. (nth 1 (assoc (match-string 3)
  2906. org-emphasis-alist)))
  2907. (add-text-properties (match-beginning 2) (match-end 2)
  2908. '(font-lock-multiline t))
  2909. (when org-hide-emphasis-markers
  2910. (add-text-properties (match-end 4) (match-beginning 5)
  2911. '(invisible org-link))
  2912. (add-text-properties (match-beginning 3) (match-end 3)
  2913. '(invisible org-link)))))
  2914. (backward-char 1))
  2915. rtn))
  2916. (defun org-emphasize (&optional char)
  2917. "Insert or change an emphasis, i.e. a font like bold or italic.
  2918. If there is an active region, change that region to a new emphasis.
  2919. If there is no region, just insert the marker characters and position
  2920. the cursor between them.
  2921. CHAR should be either the marker character, or the first character of the
  2922. HTML tag associated with that emphasis. If CHAR is a space, the means
  2923. to remove the emphasis of the selected region.
  2924. If char is not given (for example in an interactive call) it
  2925. will be prompted for."
  2926. (interactive)
  2927. (let ((eal org-emphasis-alist) e det
  2928. (erc org-emphasis-regexp-components)
  2929. (prompt "")
  2930. (string "") beg end move tag c s)
  2931. (if (org-region-active-p)
  2932. (setq beg (region-beginning) end (region-end)
  2933. string (buffer-substring beg end))
  2934. (setq move t))
  2935. (while (setq e (pop eal))
  2936. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  2937. c (aref tag 0))
  2938. (push (cons c (string-to-char (car e))) det)
  2939. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  2940. (substring tag 1)))))
  2941. (unless char
  2942. (message "%s" (concat "Emphasis marker or tag:" prompt))
  2943. (setq char (read-char-exclusive)))
  2944. (setq char (or (cdr (assoc char det)) char))
  2945. (if (equal char ?\ )
  2946. (setq s "" move nil)
  2947. (unless (assoc (char-to-string char) org-emphasis-alist)
  2948. (error "No such emphasis marker: \"%c\"" char))
  2949. (setq s (char-to-string char)))
  2950. (while (and (> (length string) 1)
  2951. (equal (substring string 0 1) (substring string -1))
  2952. (assoc (substring string 0 1) org-emphasis-alist))
  2953. (setq string (substring string 1 -1)))
  2954. (setq string (concat s string s))
  2955. (if beg (delete-region beg end))
  2956. (unless (or (bolp)
  2957. (string-match (concat "[" (nth 0 erc) "\n]")
  2958. (char-to-string (char-before (point)))))
  2959. (insert " "))
  2960. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  2961. (char-to-string (char-after (point))))
  2962. (insert " ") (backward-char 1))
  2963. (insert string)
  2964. (and move (backward-char 1))))
  2965. (defconst org-nonsticky-props
  2966. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  2967. (defun org-activate-plain-links (limit)
  2968. "Run through the buffer and add overlays to links."
  2969. (catch 'exit
  2970. (let (f)
  2971. (while (re-search-forward org-plain-link-re limit t)
  2972. (setq f (get-text-property (match-beginning 0) 'face))
  2973. (if (or (eq f 'org-tag)
  2974. (and (listp f) (memq 'org-tag f)))
  2975. nil
  2976. (add-text-properties (match-beginning 0) (match-end 0)
  2977. (list 'mouse-face 'highlight
  2978. 'rear-nonsticky org-nonsticky-props
  2979. 'keymap org-mouse-map
  2980. ))
  2981. (throw 'exit t))))))
  2982. (defun org-activate-code (limit)
  2983. (if (re-search-forward "^[ \t]*\\(:.*\\)" limit t)
  2984. (unless (get-text-property (match-beginning 1) 'face)
  2985. (remove-text-properties (match-beginning 0) (match-end 0)
  2986. '(display t invisible t intangible t))
  2987. t)))
  2988. (defun org-activate-angle-links (limit)
  2989. "Run through the buffer and add overlays to links."
  2990. (if (re-search-forward org-angle-link-re limit t)
  2991. (progn
  2992. (add-text-properties (match-beginning 0) (match-end 0)
  2993. (list 'mouse-face 'highlight
  2994. 'rear-nonsticky org-nonsticky-props
  2995. 'keymap org-mouse-map
  2996. ))
  2997. t)))
  2998. (defun org-activate-bracket-links (limit)
  2999. "Run through the buffer and add overlays to bracketed links."
  3000. (if (re-search-forward org-bracket-link-regexp limit t)
  3001. (let* ((help (concat "LINK: "
  3002. (org-match-string-no-properties 1)))
  3003. ;; FIXME: above we should remove the escapes.
  3004. ;; but that requires another match, protecting match data,
  3005. ;; a lot of overhead for font-lock.
  3006. (ip (org-maybe-intangible
  3007. (list 'invisible 'org-link 'rear-nonsticky org-nonsticky-props
  3008. 'keymap org-mouse-map 'mouse-face 'highlight
  3009. 'font-lock-multiline t 'help-echo help)))
  3010. (vp (list 'rear-nonsticky org-nonsticky-props
  3011. 'keymap org-mouse-map 'mouse-face 'highlight
  3012. ' font-lock-multiline t 'help-echo help)))
  3013. ;; We need to remove the invisible property here. Table narrowing
  3014. ;; may have made some of this invisible.
  3015. (remove-text-properties (match-beginning 0) (match-end 0)
  3016. '(invisible nil))
  3017. (if (match-end 3)
  3018. (progn
  3019. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  3020. (add-text-properties (match-beginning 3) (match-end 3) vp)
  3021. (add-text-properties (match-end 3) (match-end 0) ip))
  3022. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  3023. (add-text-properties (match-beginning 1) (match-end 1) vp)
  3024. (add-text-properties (match-end 1) (match-end 0) ip))
  3025. t)))
  3026. (defun org-activate-dates (limit)
  3027. "Run through the buffer and add overlays to dates."
  3028. (if (re-search-forward org-tsr-regexp-both limit t)
  3029. (progn
  3030. (add-text-properties (match-beginning 0) (match-end 0)
  3031. (list 'mouse-face 'highlight
  3032. 'rear-nonsticky org-nonsticky-props
  3033. 'keymap org-mouse-map))
  3034. (when org-display-custom-times
  3035. (if (match-end 3)
  3036. (org-display-custom-time (match-beginning 3) (match-end 3)))
  3037. (org-display-custom-time (match-beginning 1) (match-end 1)))
  3038. t)))
  3039. (defvar org-target-link-regexp nil
  3040. "Regular expression matching radio targets in plain text.")
  3041. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  3042. "Regular expression matching a link target.")
  3043. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  3044. "Regular expression matching a radio target.")
  3045. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  3046. "Regular expression matching any target.")
  3047. (defun org-activate-target-links (limit)
  3048. "Run through the buffer and add overlays to target matches."
  3049. (when org-target-link-regexp
  3050. (let ((case-fold-search t))
  3051. (if (re-search-forward org-target-link-regexp limit t)
  3052. (progn
  3053. (add-text-properties (match-beginning 0) (match-end 0)
  3054. (list 'mouse-face 'highlight
  3055. 'rear-nonsticky org-nonsticky-props
  3056. 'keymap org-mouse-map
  3057. 'help-echo "Radio target link"
  3058. 'org-linked-text t))
  3059. t)))))
  3060. (defun org-update-radio-target-regexp ()
  3061. "Find all radio targets in this file and update the regular expression."
  3062. (interactive)
  3063. (when (memq 'radio org-activate-links)
  3064. (setq org-target-link-regexp
  3065. (org-make-target-link-regexp (org-all-targets 'radio)))
  3066. (org-restart-font-lock)))
  3067. (defun org-hide-wide-columns (limit)
  3068. (let (s e)
  3069. (setq s (text-property-any (point) (or limit (point-max))
  3070. 'org-cwidth t))
  3071. (when s
  3072. (setq e (next-single-property-change s 'org-cwidth))
  3073. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  3074. (goto-char e)
  3075. t)))
  3076. (defvar org-latex-and-specials-regexp nil
  3077. "Regular expression for highlighting export special stuff.")
  3078. (defvar org-match-substring-regexp)
  3079. (defvar org-match-substring-with-braces-regexp)
  3080. (defvar org-export-html-special-string-regexps)
  3081. (defun org-compute-latex-and-specials-regexp ()
  3082. "Compute regular expression for stuff treated specially by exporters."
  3083. (if (not org-highlight-latex-fragments-and-specials)
  3084. (org-set-local 'org-latex-and-specials-regexp nil)
  3085. (require 'org-exp)
  3086. (let*
  3087. ((matchers (plist-get org-format-latex-options :matchers))
  3088. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  3089. org-latex-regexps)))
  3090. (options (org-combine-plists (org-default-export-plist)
  3091. (org-infile-export-plist)))
  3092. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  3093. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  3094. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  3095. (org-export-html-expand (plist-get options :expand-quoted-html))
  3096. (org-export-with-special-strings (plist-get options :special-strings))
  3097. (re-sub
  3098. (cond
  3099. ((equal org-export-with-sub-superscripts '{})
  3100. (list org-match-substring-with-braces-regexp))
  3101. (org-export-with-sub-superscripts
  3102. (list org-match-substring-regexp))
  3103. (t nil)))
  3104. (re-latex
  3105. (if org-export-with-LaTeX-fragments
  3106. (mapcar (lambda (x) (nth 1 x)) latexs)))
  3107. (re-macros
  3108. (if org-export-with-TeX-macros
  3109. (list (concat "\\\\"
  3110. (regexp-opt
  3111. (append (mapcar 'car org-html-entities)
  3112. (if (boundp 'org-latex-entities)
  3113. org-latex-entities nil))
  3114. 'words))) ; FIXME
  3115. ))
  3116. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  3117. (re-special (if org-export-with-special-strings
  3118. (mapcar (lambda (x) (car x))
  3119. org-export-html-special-string-regexps)))
  3120. (re-rest
  3121. (delq nil
  3122. (list
  3123. (if org-export-html-expand "@<[^>\n]+>")
  3124. ))))
  3125. (org-set-local
  3126. 'org-latex-and-specials-regexp
  3127. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  3128. re-rest) "\\|")))))
  3129. (defun org-do-latex-and-special-faces (limit)
  3130. "Run through the buffer and add overlays to links."
  3131. (when org-latex-and-specials-regexp
  3132. (let (rtn d)
  3133. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  3134. limit t))
  3135. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  3136. 'face))
  3137. '(org-code org-verbatim underline)))
  3138. (progn
  3139. (setq rtn t
  3140. d (cond ((member (char-after (1+ (match-beginning 0)))
  3141. '(?_ ?^)) 1)
  3142. (t 0)))
  3143. (font-lock-prepend-text-property
  3144. (+ d (match-beginning 0)) (match-end 0)
  3145. 'face 'org-latex-and-export-specials)
  3146. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  3147. '(font-lock-multiline t)))))
  3148. rtn)))
  3149. (defun org-restart-font-lock ()
  3150. "Restart font-lock-mode, to force refontification."
  3151. (when (and (boundp 'font-lock-mode) font-lock-mode)
  3152. (font-lock-mode -1)
  3153. (font-lock-mode 1)))
  3154. (defun org-all-targets (&optional radio)
  3155. "Return a list of all targets in this file.
  3156. With optional argument RADIO, only find radio targets."
  3157. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  3158. rtn)
  3159. (save-excursion
  3160. (goto-char (point-min))
  3161. (while (re-search-forward re nil t)
  3162. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  3163. rtn)))
  3164. (defun org-make-target-link-regexp (targets)
  3165. "Make regular expression matching all strings in TARGETS.
  3166. The regular expression finds the targets also if there is a line break
  3167. between words."
  3168. (and targets
  3169. (concat
  3170. "\\<\\("
  3171. (mapconcat
  3172. (lambda (x)
  3173. (while (string-match " +" x)
  3174. (setq x (replace-match "\\s-+" t t x)))
  3175. x)
  3176. targets
  3177. "\\|")
  3178. "\\)\\>")))
  3179. (defun org-activate-tags (limit)
  3180. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  3181. (progn
  3182. (add-text-properties (match-beginning 1) (match-end 1)
  3183. (list 'mouse-face 'highlight
  3184. 'rear-nonsticky org-nonsticky-props
  3185. 'keymap org-mouse-map))
  3186. t)))
  3187. (defun org-outline-level ()
  3188. (save-excursion
  3189. (looking-at outline-regexp)
  3190. (if (match-beginning 1)
  3191. (+ (org-get-string-indentation (match-string 1)) 1000)
  3192. (1- (- (match-end 0) (match-beginning 0))))))
  3193. (defvar org-font-lock-keywords nil)
  3194. (defconst org-property-re (org-re "^[ \t]*\\(:\\([[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  3195. "Regular expression matching a property line.")
  3196. (defvar org-font-lock-hook nil
  3197. "Functions to be called for special font loch stuff.")
  3198. (defun org-font-lock-hook (limit)
  3199. (run-hook-with-args 'org-font-lock-hook limit))
  3200. (defun org-set-font-lock-defaults ()
  3201. (let* ((em org-fontify-emphasized-text)
  3202. (lk org-activate-links)
  3203. (org-font-lock-extra-keywords
  3204. (list
  3205. ;; Call the hook
  3206. '(org-font-lock-hook)
  3207. ;; Headlines
  3208. '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
  3209. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  3210. ;; Table lines
  3211. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  3212. (1 'org-table t))
  3213. ;; Table internals
  3214. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  3215. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  3216. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  3217. ;; Drawers
  3218. (list org-drawer-regexp '(0 'org-special-keyword t))
  3219. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  3220. ;; Properties
  3221. (list org-property-re
  3222. '(1 'org-special-keyword t)
  3223. '(3 'org-property-value t))
  3224. (if org-format-transports-properties-p
  3225. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  3226. ;; Links
  3227. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  3228. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  3229. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  3230. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  3231. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  3232. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  3233. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  3234. '(org-hide-wide-columns (0 nil append))
  3235. ;; TODO lines
  3236. (list (concat "^\\*+[ \t]+" org-todo-regexp)
  3237. '(1 (org-get-todo-face 1) t))
  3238. ;; DONE
  3239. (if org-fontify-done-headline
  3240. (list (concat "^[*]+ +\\<\\("
  3241. (mapconcat 'regexp-quote org-done-keywords "\\|")
  3242. "\\)\\(.*\\)")
  3243. '(2 'org-headline-done t))
  3244. nil)
  3245. ;; Priorities
  3246. (list (concat "\\[#[A-Z0-9]\\]") '(0 'org-special-keyword t))
  3247. ;; Special keywords
  3248. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  3249. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  3250. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  3251. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  3252. ;; Emphasis
  3253. (if em
  3254. (if (featurep 'xemacs)
  3255. '(org-do-emphasis-faces (0 nil append))
  3256. '(org-do-emphasis-faces)))
  3257. ;; Checkboxes
  3258. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  3259. 2 'bold prepend)
  3260. (if org-provide-checkbox-statistics
  3261. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  3262. (0 (org-get-checkbox-statistics-face) t)))
  3263. ;; Description list items
  3264. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*?\\) ::"
  3265. 2 'bold prepend)
  3266. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  3267. '(1 'org-archived prepend))
  3268. ;; Specials
  3269. '(org-do-latex-and-special-faces)
  3270. ;; Code
  3271. '(org-activate-code (1 'org-code t))
  3272. ;; COMMENT
  3273. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  3274. "\\|" org-quote-string "\\)\\>")
  3275. '(1 'org-special-keyword t))
  3276. '("^#.*" (0 'font-lock-comment-face t))
  3277. )))
  3278. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  3279. ;; Now set the full font-lock-keywords
  3280. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  3281. (org-set-local 'font-lock-defaults
  3282. '(org-font-lock-keywords t nil nil backward-paragraph))
  3283. (kill-local-variable 'font-lock-keywords) nil))
  3284. (defvar org-m nil)
  3285. (defvar org-l nil)
  3286. (defvar org-f nil)
  3287. (defun org-get-level-face (n)
  3288. "Get the right face for match N in font-lock matching of healdines."
  3289. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  3290. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  3291. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  3292. (cond
  3293. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  3294. ((eq n 2) org-f)
  3295. (t (if org-level-color-stars-only nil org-f))))
  3296. (defun org-get-todo-face (kwd)
  3297. "Get the right face for a TODO keyword KWD.
  3298. If KWD is a number, get the corresponding match group."
  3299. (if (numberp kwd) (setq kwd (match-string kwd)))
  3300. (or (cdr (assoc kwd org-todo-keyword-faces))
  3301. (and (member kwd org-done-keywords) 'org-done)
  3302. 'org-todo))
  3303. (defun org-unfontify-region (beg end &optional maybe_loudly)
  3304. "Remove fontification and activation overlays from links."
  3305. (font-lock-default-unfontify-region beg end)
  3306. (let* ((buffer-undo-list t)
  3307. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  3308. (inhibit-modification-hooks t)
  3309. deactivate-mark buffer-file-name buffer-file-truename)
  3310. (remove-text-properties beg end
  3311. '(mouse-face t keymap t org-linked-text t
  3312. invisible t intangible t))))
  3313. ;;;; Visibility cycling, including org-goto and indirect buffer
  3314. ;;; Cycling
  3315. (defvar org-cycle-global-status nil)
  3316. (make-variable-buffer-local 'org-cycle-global-status)
  3317. (defvar org-cycle-subtree-status nil)
  3318. (make-variable-buffer-local 'org-cycle-subtree-status)
  3319. ;;;###autoload
  3320. (defun org-cycle (&optional arg)
  3321. "Visibility cycling for Org-mode.
  3322. - When this function is called with a prefix argument, rotate the entire
  3323. buffer through 3 states (global cycling)
  3324. 1. OVERVIEW: Show only top-level headlines.
  3325. 2. CONTENTS: Show all headlines of all levels, but no body text.
  3326. 3. SHOW ALL: Show everything.
  3327. - When point is at the beginning of a headline, rotate the subtree started
  3328. by this line through 3 different states (local cycling)
  3329. 1. FOLDED: Only the main headline is shown.
  3330. 2. CHILDREN: The main headline and the direct children are shown.
  3331. From this state, you can move to one of the children
  3332. and zoom in further.
  3333. 3. SUBTREE: Show the entire subtree, including body text.
  3334. - When there is a numeric prefix, go up to a heading with level ARG, do
  3335. a `show-subtree' and return to the previous cursor position. If ARG
  3336. is negative, go up that many levels.
  3337. - When point is not at the beginning of a headline, execute
  3338. `indent-relative', like TAB normally does. See the option
  3339. `org-cycle-emulate-tab' for details.
  3340. - Special case: if point is at the beginning of the buffer and there is
  3341. no headline in line 1, this function will act as if called with prefix arg.
  3342. But only if also the variable `org-cycle-global-at-bob' is t."
  3343. (interactive "P")
  3344. (org-load-modules-maybe)
  3345. (let* ((outline-regexp
  3346. (if (and (org-mode-p) org-cycle-include-plain-lists)
  3347. "\\(?:\\*+ \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"
  3348. outline-regexp))
  3349. (bob-special (and org-cycle-global-at-bob (bobp)
  3350. (not (looking-at outline-regexp))))
  3351. (org-cycle-hook
  3352. (if bob-special
  3353. (delq 'org-optimize-window-after-visibility-change
  3354. (copy-sequence org-cycle-hook))
  3355. org-cycle-hook))
  3356. (pos (point)))
  3357. (if (or bob-special (equal arg '(4)))
  3358. ;; special case: use global cycling
  3359. (setq arg t))
  3360. (cond
  3361. ((org-at-table-p 'any)
  3362. ;; Enter the table or move to the next field in the table
  3363. (or (org-table-recognize-table.el)
  3364. (progn
  3365. (if arg (org-table-edit-field t)
  3366. (org-table-justify-field-maybe)
  3367. (call-interactively 'org-table-next-field)))))
  3368. ((eq arg t) ;; Global cycling
  3369. (cond
  3370. ((and (eq last-command this-command)
  3371. (eq org-cycle-global-status 'overview))
  3372. ;; We just created the overview - now do table of contents
  3373. ;; This can be slow in very large buffers, so indicate action
  3374. (message "CONTENTS...")
  3375. (org-content)
  3376. (message "CONTENTS...done")
  3377. (setq org-cycle-global-status 'contents)
  3378. (run-hook-with-args 'org-cycle-hook 'contents))
  3379. ((and (eq last-command this-command)
  3380. (eq org-cycle-global-status 'contents))
  3381. ;; We just showed the table of contents - now show everything
  3382. (show-all)
  3383. (message "SHOW ALL")
  3384. (setq org-cycle-global-status 'all)
  3385. (run-hook-with-args 'org-cycle-hook 'all))
  3386. (t
  3387. ;; Default action: go to overview
  3388. (org-overview)
  3389. (message "OVERVIEW")
  3390. (setq org-cycle-global-status 'overview)
  3391. (run-hook-with-args 'org-cycle-hook 'overview))))
  3392. ((and org-drawers org-drawer-regexp
  3393. (save-excursion
  3394. (beginning-of-line 1)
  3395. (looking-at org-drawer-regexp)))
  3396. ;; Toggle block visibility
  3397. (org-flag-drawer
  3398. (not (get-char-property (match-end 0) 'invisible))))
  3399. ((integerp arg)
  3400. ;; Show-subtree, ARG levels up from here.
  3401. (save-excursion
  3402. (org-back-to-heading)
  3403. (outline-up-heading (if (< arg 0) (- arg)
  3404. (- (funcall outline-level) arg)))
  3405. (org-show-subtree)))
  3406. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  3407. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  3408. ;; At a heading: rotate between three different views
  3409. (org-back-to-heading)
  3410. (let ((goal-column 0) eoh eol eos)
  3411. ;; First, some boundaries
  3412. (save-excursion
  3413. (org-back-to-heading)
  3414. (save-excursion
  3415. (beginning-of-line 2)
  3416. (while (and (not (eobp)) ;; this is like `next-line'
  3417. (get-char-property (1- (point)) 'invisible))
  3418. (beginning-of-line 2)) (setq eol (point)))
  3419. (outline-end-of-heading) (setq eoh (point))
  3420. (org-end-of-subtree t)
  3421. (unless (eobp)
  3422. (skip-chars-forward " \t\n")
  3423. (beginning-of-line 1) ; in case this is an item
  3424. )
  3425. (setq eos (1- (point))))
  3426. ;; Find out what to do next and set `this-command'
  3427. (cond
  3428. ((= eos eoh)
  3429. ;; Nothing is hidden behind this heading
  3430. (message "EMPTY ENTRY")
  3431. (setq org-cycle-subtree-status nil)
  3432. (save-excursion
  3433. (goto-char eos)
  3434. (outline-next-heading)
  3435. (if (org-invisible-p) (org-flag-heading nil))))
  3436. ((or (>= eol eos)
  3437. (not (string-match "\\S-" (buffer-substring eol eos))))
  3438. ;; Entire subtree is hidden in one line: open it
  3439. (org-show-entry)
  3440. (show-children)
  3441. (message "CHILDREN")
  3442. (save-excursion
  3443. (goto-char eos)
  3444. (outline-next-heading)
  3445. (if (org-invisible-p) (org-flag-heading nil)))
  3446. (setq org-cycle-subtree-status 'children)
  3447. (run-hook-with-args 'org-cycle-hook 'children))
  3448. ((and (eq last-command this-command)
  3449. (eq org-cycle-subtree-status 'children))
  3450. ;; We just showed the children, now show everything.
  3451. (org-show-subtree)
  3452. (message "SUBTREE")
  3453. (setq org-cycle-subtree-status 'subtree)
  3454. (run-hook-with-args 'org-cycle-hook 'subtree))
  3455. (t
  3456. ;; Default action: hide the subtree.
  3457. (hide-subtree)
  3458. (message "FOLDED")
  3459. (setq org-cycle-subtree-status 'folded)
  3460. (run-hook-with-args 'org-cycle-hook 'folded)))))
  3461. ;; TAB emulation
  3462. (buffer-read-only (org-back-to-heading))
  3463. ((org-try-cdlatex-tab))
  3464. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  3465. (or (not (bolp))
  3466. (not (looking-at outline-regexp))))
  3467. (call-interactively (global-key-binding "\t")))
  3468. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  3469. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  3470. (or (and (eq org-cycle-emulate-tab 'white)
  3471. (= (match-end 0) (point-at-eol)))
  3472. (and (eq org-cycle-emulate-tab 'whitestart)
  3473. (>= (match-end 0) pos))))
  3474. t
  3475. (eq org-cycle-emulate-tab t))
  3476. (call-interactively (global-key-binding "\t")))
  3477. (t (save-excursion
  3478. (org-back-to-heading)
  3479. (org-cycle))))))
  3480. ;;;###autoload
  3481. (defun org-global-cycle (&optional arg)
  3482. "Cycle the global visibility. For details see `org-cycle'."
  3483. (interactive "P")
  3484. (let ((org-cycle-include-plain-lists
  3485. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  3486. (if (integerp arg)
  3487. (progn
  3488. (show-all)
  3489. (hide-sublevels arg)
  3490. (setq org-cycle-global-status 'contents))
  3491. (org-cycle '(4)))))
  3492. (defun org-overview ()
  3493. "Switch to overview mode, shoing only top-level headlines.
  3494. Really, this shows all headlines with level equal or greater than the level
  3495. of the first headline in the buffer. This is important, because if the
  3496. first headline is not level one, then (hide-sublevels 1) gives confusing
  3497. results."
  3498. (interactive)
  3499. (let ((level (save-excursion
  3500. (goto-char (point-min))
  3501. (if (re-search-forward (concat "^" outline-regexp) nil t)
  3502. (progn
  3503. (goto-char (match-beginning 0))
  3504. (funcall outline-level))))))
  3505. (and level (hide-sublevels level))))
  3506. (defun org-content (&optional arg)
  3507. "Show all headlines in the buffer, like a table of contents.
  3508. With numerical argument N, show content up to level N."
  3509. (interactive "P")
  3510. (save-excursion
  3511. ;; Visit all headings and show their offspring
  3512. (and (integerp arg) (org-overview))
  3513. (goto-char (point-max))
  3514. (catch 'exit
  3515. (while (and (progn (condition-case nil
  3516. (outline-previous-visible-heading 1)
  3517. (error (goto-char (point-min))))
  3518. t)
  3519. (looking-at outline-regexp))
  3520. (if (integerp arg)
  3521. (show-children (1- arg))
  3522. (show-branches))
  3523. (if (bobp) (throw 'exit nil))))))
  3524. (defun org-optimize-window-after-visibility-change (state)
  3525. "Adjust the window after a change in outline visibility.
  3526. This function is the default value of the hook `org-cycle-hook'."
  3527. (when (get-buffer-window (current-buffer))
  3528. (cond
  3529. ; ((eq state 'overview) (org-first-headline-recenter 1))
  3530. ; ((eq state 'overview) (org-beginning-of-line))
  3531. ((eq state 'content) nil)
  3532. ((eq state 'all) nil)
  3533. ((eq state 'folded) nil)
  3534. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  3535. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  3536. (defun org-compact-display-after-subtree-move ()
  3537. (let (beg end)
  3538. (save-excursion
  3539. (if (org-up-heading-safe)
  3540. (progn
  3541. (hide-subtree)
  3542. (show-entry)
  3543. (show-children)
  3544. (org-cycle-show-empty-lines 'children)
  3545. (org-cycle-hide-drawers 'children))
  3546. (org-overview)))))
  3547. (defun org-cycle-show-empty-lines (state)
  3548. "Show empty lines above all visible headlines.
  3549. The region to be covered depends on STATE when called through
  3550. `org-cycle-hook'. Lisp program can use t for STATE to get the
  3551. entire buffer covered. Note that an empty line is only shown if there
  3552. are at least `org-cycle-separator-lines' empty lines before the headeline."
  3553. (when (> org-cycle-separator-lines 0)
  3554. (save-excursion
  3555. (let* ((n org-cycle-separator-lines)
  3556. (re (cond
  3557. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  3558. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  3559. (t (let ((ns (number-to-string (- n 2))))
  3560. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  3561. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  3562. beg end)
  3563. (cond
  3564. ((memq state '(overview contents t))
  3565. (setq beg (point-min) end (point-max)))
  3566. ((memq state '(children folded))
  3567. (setq beg (point) end (progn (org-end-of-subtree t t)
  3568. (beginning-of-line 2)
  3569. (point)))))
  3570. (when beg
  3571. (goto-char beg)
  3572. (while (re-search-forward re end t)
  3573. (if (not (get-char-property (match-end 1) 'invisible))
  3574. (outline-flag-region
  3575. (match-beginning 1) (match-end 1) nil)))))))
  3576. ;; Never hide empty lines at the end of the file.
  3577. (save-excursion
  3578. (goto-char (point-max))
  3579. (outline-previous-heading)
  3580. (outline-end-of-heading)
  3581. (if (and (looking-at "[ \t\n]+")
  3582. (= (match-end 0) (point-max)))
  3583. (outline-flag-region (point) (match-end 0) nil))))
  3584. (defun org-cycle-hide-drawers (state)
  3585. "Re-hide all drawers after a visibility state change."
  3586. (when (and (org-mode-p)
  3587. (not (memq state '(overview folded))))
  3588. (save-excursion
  3589. (let* ((globalp (memq state '(contents all)))
  3590. (beg (if globalp (point-min) (point)))
  3591. (end (if globalp (point-max) (org-end-of-subtree t))))
  3592. (goto-char beg)
  3593. (while (re-search-forward org-drawer-regexp end t)
  3594. (org-flag-drawer t))))))
  3595. (defun org-flag-drawer (flag)
  3596. (save-excursion
  3597. (beginning-of-line 1)
  3598. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  3599. (let ((b (match-end 0))
  3600. (outline-regexp org-outline-regexp))
  3601. (if (re-search-forward
  3602. "^[ \t]*:END:"
  3603. (save-excursion (outline-next-heading) (point)) t)
  3604. (outline-flag-region b (point-at-eol) flag)
  3605. (error ":END: line missing"))))))
  3606. (defun org-subtree-end-visible-p ()
  3607. "Is the end of the current subtree visible?"
  3608. (pos-visible-in-window-p
  3609. (save-excursion (org-end-of-subtree t) (point))))
  3610. (defun org-first-headline-recenter (&optional N)
  3611. "Move cursor to the first headline and recenter the headline.
  3612. Optional argument N means, put the headline into the Nth line of the window."
  3613. (goto-char (point-min))
  3614. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  3615. (beginning-of-line)
  3616. (recenter (prefix-numeric-value N))))
  3617. ;;; Org-goto
  3618. (defvar org-goto-window-configuration nil)
  3619. (defvar org-goto-marker nil)
  3620. (defvar org-goto-map
  3621. (let ((map (make-sparse-keymap)))
  3622. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  3623. (while (setq cmd (pop cmds))
  3624. (substitute-key-definition cmd cmd map global-map)))
  3625. (suppress-keymap map)
  3626. (org-defkey map "\C-m" 'org-goto-ret)
  3627. (org-defkey map [(return)] 'org-goto-ret)
  3628. (org-defkey map [(left)] 'org-goto-left)
  3629. (org-defkey map [(right)] 'org-goto-right)
  3630. (org-defkey map [(control ?g)] 'org-goto-quit)
  3631. (org-defkey map "\C-i" 'org-cycle)
  3632. (org-defkey map [(tab)] 'org-cycle)
  3633. (org-defkey map [(down)] 'outline-next-visible-heading)
  3634. (org-defkey map [(up)] 'outline-previous-visible-heading)
  3635. (if org-goto-auto-isearch
  3636. (if (fboundp 'define-key-after)
  3637. (define-key-after map [t] 'org-goto-local-auto-isearch)
  3638. nil)
  3639. (org-defkey map "q" 'org-goto-quit)
  3640. (org-defkey map "n" 'outline-next-visible-heading)
  3641. (org-defkey map "p" 'outline-previous-visible-heading)
  3642. (org-defkey map "f" 'outline-forward-same-level)
  3643. (org-defkey map "b" 'outline-backward-same-level)
  3644. (org-defkey map "u" 'outline-up-heading))
  3645. (org-defkey map "/" 'org-occur)
  3646. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  3647. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  3648. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  3649. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  3650. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  3651. map))
  3652. (defconst org-goto-help
  3653. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  3654. RET=jump to location [Q]uit and return to previous location
  3655. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  3656. (defvar org-goto-start-pos) ; dynamically scoped parameter
  3657. (defun org-goto (&optional alternative-interface)
  3658. "Look up a different location in the current file, keeping current visibility.
  3659. When you want look-up or go to a different location in a document, the
  3660. fastest way is often to fold the entire buffer and then dive into the tree.
  3661. This method has the disadvantage, that the previous location will be folded,
  3662. which may not be what you want.
  3663. This command works around this by showing a copy of the current buffer
  3664. in an indirect buffer, in overview mode. You can dive into the tree in
  3665. that copy, use org-occur and incremental search to find a location.
  3666. When pressing RET or `Q', the command returns to the original buffer in
  3667. which the visibility is still unchanged. After RET is will also jump to
  3668. the location selected in the indirect buffer and expose the
  3669. the headline hierarchy above."
  3670. (interactive "P")
  3671. (let* ((org-refile-targets '((nil . (:maxlevel . 10))))
  3672. (org-refile-use-outline-path t)
  3673. (interface
  3674. (if (not alternative-interface)
  3675. org-goto-interface
  3676. (if (eq org-goto-interface 'outline)
  3677. 'outline-path-completion
  3678. 'outline)))
  3679. (org-goto-start-pos (point))
  3680. (selected-point
  3681. (if (eq interface 'outline)
  3682. (car (org-get-location (current-buffer) org-goto-help))
  3683. (nth 3 (org-refile-get-location "Goto: ")))))
  3684. (if selected-point
  3685. (progn
  3686. (org-mark-ring-push org-goto-start-pos)
  3687. (goto-char selected-point)
  3688. (if (or (org-invisible-p) (org-invisible-p2))
  3689. (org-show-context 'org-goto)))
  3690. (message "Quit"))))
  3691. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  3692. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  3693. (defvar org-goto-local-auto-isearch-map) ; defined below
  3694. (defun org-get-location (buf help)
  3695. "Let the user select a location in the Org-mode buffer BUF.
  3696. This function uses a recursive edit. It returns the selected position
  3697. or nil."
  3698. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  3699. (isearch-hide-immediately nil)
  3700. (isearch-search-fun-function
  3701. (lambda () 'org-goto-local-search-forward-headings))
  3702. (org-goto-selected-point org-goto-exit-command))
  3703. (save-excursion
  3704. (save-window-excursion
  3705. (delete-other-windows)
  3706. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  3707. (switch-to-buffer
  3708. (condition-case nil
  3709. (make-indirect-buffer (current-buffer) "*org-goto*")
  3710. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  3711. (with-output-to-temp-buffer "*Help*"
  3712. (princ help))
  3713. (shrink-window-if-larger-than-buffer (get-buffer-window "*Help*"))
  3714. (setq buffer-read-only nil)
  3715. (let ((org-startup-truncated t)
  3716. (org-startup-folded nil)
  3717. (org-startup-align-all-tables nil))
  3718. (org-mode)
  3719. (org-overview))
  3720. (setq buffer-read-only t)
  3721. (if (and (boundp 'org-goto-start-pos)
  3722. (integer-or-marker-p org-goto-start-pos))
  3723. (let ((org-show-hierarchy-above t)
  3724. (org-show-siblings t)
  3725. (org-show-following-heading t))
  3726. (goto-char org-goto-start-pos)
  3727. (and (org-invisible-p) (org-show-context)))
  3728. (goto-char (point-min)))
  3729. (org-beginning-of-line)
  3730. (message "Select location and press RET")
  3731. (use-local-map org-goto-map)
  3732. (recursive-edit)
  3733. ))
  3734. (kill-buffer "*org-goto*")
  3735. (cons org-goto-selected-point org-goto-exit-command)))
  3736. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  3737. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  3738. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  3739. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  3740. (defun org-goto-local-search-forward-headings (string bound noerror)
  3741. "Search and make sure that anu matches are in headlines."
  3742. (catch 'return
  3743. (while (search-forward string bound noerror)
  3744. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  3745. (and (member :headline context)
  3746. (not (member :tags context))))
  3747. (throw 'return (point))))))
  3748. (defun org-goto-local-auto-isearch ()
  3749. "Start isearch."
  3750. (interactive)
  3751. (goto-char (point-min))
  3752. (let ((keys (this-command-keys)))
  3753. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  3754. (isearch-mode t)
  3755. (isearch-process-search-char (string-to-char keys)))))
  3756. (defun org-goto-ret (&optional arg)
  3757. "Finish `org-goto' by going to the new location."
  3758. (interactive "P")
  3759. (setq org-goto-selected-point (point)
  3760. org-goto-exit-command 'return)
  3761. (throw 'exit nil))
  3762. (defun org-goto-left ()
  3763. "Finish `org-goto' by going to the new location."
  3764. (interactive)
  3765. (if (org-on-heading-p)
  3766. (progn
  3767. (beginning-of-line 1)
  3768. (setq org-goto-selected-point (point)
  3769. org-goto-exit-command 'left)
  3770. (throw 'exit nil))
  3771. (error "Not on a heading")))
  3772. (defun org-goto-right ()
  3773. "Finish `org-goto' by going to the new location."
  3774. (interactive)
  3775. (if (org-on-heading-p)
  3776. (progn
  3777. (setq org-goto-selected-point (point)
  3778. org-goto-exit-command 'right)
  3779. (throw 'exit nil))
  3780. (error "Not on a heading")))
  3781. (defun org-goto-quit ()
  3782. "Finish `org-goto' without cursor motion."
  3783. (interactive)
  3784. (setq org-goto-selected-point nil)
  3785. (setq org-goto-exit-command 'quit)
  3786. (throw 'exit nil))
  3787. ;;; Indirect buffer display of subtrees
  3788. (defvar org-indirect-dedicated-frame nil
  3789. "This is the frame being used for indirect tree display.")
  3790. (defvar org-last-indirect-buffer nil)
  3791. (defun org-tree-to-indirect-buffer (&optional arg)
  3792. "Create indirect buffer and narrow it to current subtree.
  3793. With numerical prefix ARG, go up to this level and then take that tree.
  3794. If ARG is negative, go up that many levels.
  3795. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  3796. indirect buffer previously made with this command, to avoid proliferation of
  3797. indirect buffers. However, when you call the command with a `C-u' prefix, or
  3798. when `org-indirect-buffer-display' is `new-frame', the last buffer
  3799. is kept so that you can work with several indirect buffers at the same time.
  3800. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  3801. requests that a new frame be made for the new buffer, so that the dedicated
  3802. frame is not changed."
  3803. (interactive "P")
  3804. (let ((cbuf (current-buffer))
  3805. (cwin (selected-window))
  3806. (pos (point))
  3807. beg end level heading ibuf)
  3808. (save-excursion
  3809. (org-back-to-heading t)
  3810. (when (numberp arg)
  3811. (setq level (org-outline-level))
  3812. (if (< arg 0) (setq arg (+ level arg)))
  3813. (while (> (setq level (org-outline-level)) arg)
  3814. (outline-up-heading 1 t)))
  3815. (setq beg (point)
  3816. heading (org-get-heading))
  3817. (org-end-of-subtree t) (setq end (point)))
  3818. (if (and (buffer-live-p org-last-indirect-buffer)
  3819. (not (eq org-indirect-buffer-display 'new-frame))
  3820. (not arg))
  3821. (kill-buffer org-last-indirect-buffer))
  3822. (setq ibuf (org-get-indirect-buffer cbuf)
  3823. org-last-indirect-buffer ibuf)
  3824. (cond
  3825. ((or (eq org-indirect-buffer-display 'new-frame)
  3826. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  3827. (select-frame (make-frame))
  3828. (delete-other-windows)
  3829. (switch-to-buffer ibuf)
  3830. (org-set-frame-title heading))
  3831. ((eq org-indirect-buffer-display 'dedicated-frame)
  3832. (raise-frame
  3833. (select-frame (or (and org-indirect-dedicated-frame
  3834. (frame-live-p org-indirect-dedicated-frame)
  3835. org-indirect-dedicated-frame)
  3836. (setq org-indirect-dedicated-frame (make-frame)))))
  3837. (delete-other-windows)
  3838. (switch-to-buffer ibuf)
  3839. (org-set-frame-title (concat "Indirect: " heading)))
  3840. ((eq org-indirect-buffer-display 'current-window)
  3841. (switch-to-buffer ibuf))
  3842. ((eq org-indirect-buffer-display 'other-window)
  3843. (pop-to-buffer ibuf))
  3844. (t (error "Invalid value.")))
  3845. (if (featurep 'xemacs)
  3846. (save-excursion (org-mode) (turn-on-font-lock)))
  3847. (narrow-to-region beg end)
  3848. (show-all)
  3849. (goto-char pos)
  3850. (and (window-live-p cwin) (select-window cwin))))
  3851. (defun org-get-indirect-buffer (&optional buffer)
  3852. (setq buffer (or buffer (current-buffer)))
  3853. (let ((n 1) (base (buffer-name buffer)) bname)
  3854. (while (buffer-live-p
  3855. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  3856. (setq n (1+ n)))
  3857. (condition-case nil
  3858. (make-indirect-buffer buffer bname 'clone)
  3859. (error (make-indirect-buffer buffer bname)))))
  3860. (defun org-set-frame-title (title)
  3861. "Set the title of the current frame to the string TITLE."
  3862. ;; FIXME: how to name a single frame in XEmacs???
  3863. (unless (featurep 'xemacs)
  3864. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  3865. ;;;; Structure editing
  3866. ;;; Inserting headlines
  3867. (defun org-insert-heading (&optional force-heading)
  3868. "Insert a new heading or item with same depth at point.
  3869. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  3870. If point is at the beginning of a headline, insert a sibling before the
  3871. current headline. If point is not at the beginning, do not split the line,
  3872. but create the new hedline after the current line."
  3873. (interactive "P")
  3874. (if (= (buffer-size) 0)
  3875. (insert "\n* ")
  3876. (when (or force-heading (not (org-insert-item)))
  3877. (let* ((head (save-excursion
  3878. (condition-case nil
  3879. (progn
  3880. (org-back-to-heading)
  3881. (match-string 0))
  3882. (error "*"))))
  3883. (blank (cdr (assq 'heading org-blank-before-new-entry)))
  3884. pos)
  3885. (cond
  3886. ((and (org-on-heading-p) (bolp)
  3887. (or (bobp)
  3888. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  3889. ;; insert before the current line
  3890. (open-line (if blank 2 1)))
  3891. ((and (bolp)
  3892. (or (bobp)
  3893. (save-excursion
  3894. (backward-char 1) (not (org-invisible-p)))))
  3895. ;; insert right here
  3896. nil)
  3897. (t
  3898. ;; in the middle of the line
  3899. (org-show-entry)
  3900. (let ((split
  3901. (org-get-alist-option org-M-RET-may-split-line 'headline))
  3902. tags pos)
  3903. (if (org-on-heading-p)
  3904. (progn
  3905. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  3906. (setq tags (and (match-end 2) (match-string 2)))
  3907. (and (match-end 1)
  3908. (delete-region (match-beginning 1) (match-end 1)))
  3909. (setq pos (point-at-bol))
  3910. (or split (end-of-line 1))
  3911. (delete-horizontal-space)
  3912. (newline (if blank 2 1))
  3913. (when tags
  3914. (save-excursion
  3915. (goto-char pos)
  3916. (end-of-line 1)
  3917. (insert " " tags)
  3918. (org-set-tags nil 'align))))
  3919. (or split (end-of-line 1))
  3920. (newline (if blank 2 1))))))
  3921. (insert head) (just-one-space)
  3922. (setq pos (point))
  3923. (end-of-line 1)
  3924. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  3925. (run-hooks 'org-insert-heading-hook)))))
  3926. (defun org-get-heading (&optional no-tags)
  3927. "Return the heading of the current entry, without the stars."
  3928. (save-excursion
  3929. (org-back-to-heading t)
  3930. (if (looking-at
  3931. (if no-tags
  3932. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  3933. "\\*+[ \t]+\\([^\r\n]*\\)"))
  3934. (match-string 1) "")))
  3935. (defun org-insert-heading-after-current ()
  3936. "Insert a new heading with same level as current, after current subtree."
  3937. (interactive)
  3938. (org-back-to-heading)
  3939. (org-insert-heading)
  3940. (org-move-subtree-down)
  3941. (end-of-line 1))
  3942. (defun org-insert-todo-heading (arg)
  3943. "Insert a new heading with the same level and TODO state as current heading.
  3944. If the heading has no TODO state, or if the state is DONE, use the first
  3945. state (TODO by default). Also with prefix arg, force first state."
  3946. (interactive "P")
  3947. (when (not (org-insert-item 'checkbox))
  3948. (org-insert-heading)
  3949. (save-excursion
  3950. (org-back-to-heading)
  3951. (outline-previous-heading)
  3952. (looking-at org-todo-line-regexp))
  3953. (if (or arg
  3954. (not (match-beginning 2))
  3955. (member (match-string 2) org-done-keywords))
  3956. (insert (car org-todo-keywords-1) " ")
  3957. (insert (match-string 2) " "))))
  3958. (defun org-insert-subheading (arg)
  3959. "Insert a new subheading and demote it.
  3960. Works for outline headings and for plain lists alike."
  3961. (interactive "P")
  3962. (org-insert-heading arg)
  3963. (cond
  3964. ((org-on-heading-p) (org-do-demote))
  3965. ((org-at-item-p) (org-indent-item 1))))
  3966. (defun org-insert-todo-subheading (arg)
  3967. "Insert a new subheading with TODO keyword or checkbox and demote it.
  3968. Works for outline headings and for plain lists alike."
  3969. (interactive "P")
  3970. (org-insert-todo-heading arg)
  3971. (cond
  3972. ((org-on-heading-p) (org-do-demote))
  3973. ((org-at-item-p) (org-indent-item 1))))
  3974. ;;; Promotion and Demotion
  3975. (defun org-promote-subtree ()
  3976. "Promote the entire subtree.
  3977. See also `org-promote'."
  3978. (interactive)
  3979. (save-excursion
  3980. (org-map-tree 'org-promote))
  3981. (org-fix-position-after-promote))
  3982. (defun org-demote-subtree ()
  3983. "Demote the entire subtree. See `org-demote'.
  3984. See also `org-promote'."
  3985. (interactive)
  3986. (save-excursion
  3987. (org-map-tree 'org-demote))
  3988. (org-fix-position-after-promote))
  3989. (defun org-do-promote ()
  3990. "Promote the current heading higher up the tree.
  3991. If the region is active in `transient-mark-mode', promote all headings
  3992. in the region."
  3993. (interactive)
  3994. (save-excursion
  3995. (if (org-region-active-p)
  3996. (org-map-region 'org-promote (region-beginning) (region-end))
  3997. (org-promote)))
  3998. (org-fix-position-after-promote))
  3999. (defun org-do-demote ()
  4000. "Demote the current heading lower down the tree.
  4001. If the region is active in `transient-mark-mode', demote all headings
  4002. in the region."
  4003. (interactive)
  4004. (save-excursion
  4005. (if (org-region-active-p)
  4006. (org-map-region 'org-demote (region-beginning) (region-end))
  4007. (org-demote)))
  4008. (org-fix-position-after-promote))
  4009. (defun org-fix-position-after-promote ()
  4010. "Make sure that after pro/demotion cursor position is right."
  4011. (let ((pos (point)))
  4012. (when (save-excursion
  4013. (beginning-of-line 1)
  4014. (looking-at org-todo-line-regexp)
  4015. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  4016. (cond ((eobp) (insert " "))
  4017. ((eolp) (insert " "))
  4018. ((equal (char-after) ?\ ) (forward-char 1))))))
  4019. (defun org-reduced-level (l)
  4020. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  4021. (defun org-get-valid-level (level &optional change)
  4022. "Rectify a level change under the influence of `org-odd-levels-only'
  4023. LEVEL is a current level, CHANGE is by how much the level should be
  4024. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  4025. even level numbers will become the next higher odd number."
  4026. (if org-odd-levels-only
  4027. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  4028. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  4029. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  4030. (max 1 (+ level change))))
  4031. (if (boundp 'define-obsolete-function-alias)
  4032. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  4033. (define-obsolete-function-alias 'org-get-legal-level
  4034. 'org-get-valid-level)
  4035. (define-obsolete-function-alias 'org-get-legal-level
  4036. 'org-get-valid-level "23.1")))
  4037. (defun org-promote ()
  4038. "Promote the current heading higher up the tree.
  4039. If the region is active in `transient-mark-mode', promote all headings
  4040. in the region."
  4041. (org-back-to-heading t)
  4042. (let* ((level (save-match-data (funcall outline-level)))
  4043. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  4044. (diff (abs (- level (length up-head) -1))))
  4045. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  4046. (replace-match up-head nil t)
  4047. ;; Fixup tag positioning
  4048. (and org-auto-align-tags (org-set-tags nil t))
  4049. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  4050. (defun org-demote ()
  4051. "Demote the current heading lower down the tree.
  4052. If the region is active in `transient-mark-mode', demote all headings
  4053. in the region."
  4054. (org-back-to-heading t)
  4055. (let* ((level (save-match-data (funcall outline-level)))
  4056. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  4057. (diff (abs (- level (length down-head) -1))))
  4058. (replace-match down-head nil t)
  4059. ;; Fixup tag positioning
  4060. (and org-auto-align-tags (org-set-tags nil t))
  4061. (if org-adapt-indentation (org-fixup-indentation diff))))
  4062. (defun org-map-tree (fun)
  4063. "Call FUN for every heading underneath the current one."
  4064. (org-back-to-heading)
  4065. (let ((level (funcall outline-level)))
  4066. (save-excursion
  4067. (funcall fun)
  4068. (while (and (progn
  4069. (outline-next-heading)
  4070. (> (funcall outline-level) level))
  4071. (not (eobp)))
  4072. (funcall fun)))))
  4073. (defun org-map-region (fun beg end)
  4074. "Call FUN for every heading between BEG and END."
  4075. (let ((org-ignore-region t))
  4076. (save-excursion
  4077. (setq end (copy-marker end))
  4078. (goto-char beg)
  4079. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  4080. (< (point) end))
  4081. (funcall fun))
  4082. (while (and (progn
  4083. (outline-next-heading)
  4084. (< (point) end))
  4085. (not (eobp)))
  4086. (funcall fun)))))
  4087. (defun org-fixup-indentation (diff)
  4088. "Change the indentation in the current entry by DIFF
  4089. However, if any line in the current entry has no indentation, or if it
  4090. would end up with no indentation after the change, nothing at all is done."
  4091. (save-excursion
  4092. (let ((end (save-excursion (outline-next-heading)
  4093. (point-marker)))
  4094. (prohibit (if (> diff 0)
  4095. "^\\S-"
  4096. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  4097. col)
  4098. (unless (save-excursion (end-of-line 1)
  4099. (re-search-forward prohibit end t))
  4100. (while (and (< (point) end)
  4101. (re-search-forward "^[ \t]+" end t))
  4102. (goto-char (match-end 0))
  4103. (setq col (current-column))
  4104. (if (< diff 0) (replace-match ""))
  4105. (indent-to (+ diff col))))
  4106. (move-marker end nil))))
  4107. (defun org-convert-to-odd-levels ()
  4108. "Convert an org-mode file with all levels allowed to one with odd levels.
  4109. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  4110. level 5 etc."
  4111. (interactive)
  4112. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  4113. (let ((org-odd-levels-only nil) n)
  4114. (save-excursion
  4115. (goto-char (point-min))
  4116. (while (re-search-forward "^\\*\\*+ " nil t)
  4117. (setq n (- (length (match-string 0)) 2))
  4118. (while (>= (setq n (1- n)) 0)
  4119. (org-demote))
  4120. (end-of-line 1))))))
  4121. (defun org-convert-to-oddeven-levels ()
  4122. "Convert an org-mode file with only odd levels to one with odd and even levels.
  4123. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  4124. section with an even level, conversion would destroy the structure of the file. An error
  4125. is signaled in this case."
  4126. (interactive)
  4127. (goto-char (point-min))
  4128. ;; First check if there are no even levels
  4129. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  4130. (org-show-context t)
  4131. (error "Not all levels are odd in this file. Conversion not possible."))
  4132. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  4133. (let ((org-odd-levels-only nil) n)
  4134. (save-excursion
  4135. (goto-char (point-min))
  4136. (while (re-search-forward "^\\*\\*+ " nil t)
  4137. (setq n (/ (1- (length (match-string 0))) 2))
  4138. (while (>= (setq n (1- n)) 0)
  4139. (org-promote))
  4140. (end-of-line 1))))))
  4141. (defun org-tr-level (n)
  4142. "Make N odd if required."
  4143. (if org-odd-levels-only (1+ (/ n 2)) n))
  4144. ;;; Vertical tree motion, cutting and pasting of subtrees
  4145. (defun org-move-subtree-up (&optional arg)
  4146. "Move the current subtree up past ARG headlines of the same level."
  4147. (interactive "p")
  4148. (org-move-subtree-down (- (prefix-numeric-value arg))))
  4149. (defun org-move-subtree-down (&optional arg)
  4150. "Move the current subtree down past ARG headlines of the same level."
  4151. (interactive "p")
  4152. (setq arg (prefix-numeric-value arg))
  4153. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  4154. 'outline-get-last-sibling))
  4155. (ins-point (make-marker))
  4156. (cnt (abs arg))
  4157. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  4158. ;; Select the tree
  4159. (org-back-to-heading)
  4160. (setq beg0 (point))
  4161. (save-excursion
  4162. (setq ne-beg (org-back-over-empty-lines))
  4163. (setq beg (point)))
  4164. (save-match-data
  4165. (save-excursion (outline-end-of-heading)
  4166. (setq folded (org-invisible-p)))
  4167. (outline-end-of-subtree))
  4168. (outline-next-heading)
  4169. (setq ne-end (org-back-over-empty-lines))
  4170. (setq end (point))
  4171. (goto-char beg0)
  4172. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  4173. ;; include less whitespace
  4174. (save-excursion
  4175. (goto-char beg)
  4176. (forward-line (- ne-beg ne-end))
  4177. (setq beg (point))))
  4178. ;; Find insertion point, with error handling
  4179. (while (> cnt 0)
  4180. (or (and (funcall movfunc) (looking-at outline-regexp))
  4181. (progn (goto-char beg0)
  4182. (error "Cannot move past superior level or buffer limit")))
  4183. (setq cnt (1- cnt)))
  4184. (if (> arg 0)
  4185. ;; Moving forward - still need to move over subtree
  4186. (progn (org-end-of-subtree t t)
  4187. (save-excursion
  4188. (org-back-over-empty-lines)
  4189. (or (bolp) (newline)))))
  4190. (setq ne-ins (org-back-over-empty-lines))
  4191. (move-marker ins-point (point))
  4192. (setq txt (buffer-substring beg end))
  4193. (org-save-markers-in-region beg end)
  4194. (delete-region beg end)
  4195. (outline-flag-region (1- beg) beg nil)
  4196. (outline-flag-region (1- (point)) (point) nil)
  4197. (let ((bbb (point)))
  4198. (insert-before-markers txt)
  4199. (org-reinstall-markers-in-region bbb)
  4200. (move-marker ins-point bbb))
  4201. (or (bolp) (insert "\n"))
  4202. (setq ins-end (point))
  4203. (goto-char ins-point)
  4204. (org-skip-whitespace)
  4205. (when (and (< arg 0)
  4206. (org-first-sibling-p)
  4207. (> ne-ins ne-beg))
  4208. ;; Move whitespace back to beginning
  4209. (save-excursion
  4210. (goto-char ins-end)
  4211. (let ((kill-whole-line t))
  4212. (kill-line (- ne-ins ne-beg)) (point)))
  4213. (insert (make-string (- ne-ins ne-beg) ?\n)))
  4214. (move-marker ins-point nil)
  4215. (org-compact-display-after-subtree-move)
  4216. (unless folded
  4217. (org-show-entry)
  4218. (show-children)
  4219. (org-cycle-hide-drawers 'children))))
  4220. (defvar org-subtree-clip ""
  4221. "Clipboard for cut and paste of subtrees.
  4222. This is actually only a copy of the kill, because we use the normal kill
  4223. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  4224. (defvar org-subtree-clip-folded nil
  4225. "Was the last copied subtree folded?
  4226. This is used to fold the tree back after pasting.")
  4227. (defun org-cut-subtree (&optional n)
  4228. "Cut the current subtree into the clipboard.
  4229. With prefix arg N, cut this many sequential subtrees.
  4230. This is a short-hand for marking the subtree and then cutting it."
  4231. (interactive "p")
  4232. (org-copy-subtree n 'cut))
  4233. (defun org-copy-subtree (&optional n cut force-store-markers)
  4234. "Cut the current subtree into the clipboard.
  4235. With prefix arg N, cut this many sequential subtrees.
  4236. This is a short-hand for marking the subtree and then copying it.
  4237. If CUT is non-nil, actually cut the subtree.
  4238. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  4239. of some markers in the region, even if CUT is non-nil. This is
  4240. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  4241. (interactive "p")
  4242. (let (beg end folded (beg0 (point)))
  4243. (if (interactive-p)
  4244. (org-back-to-heading nil) ; take what looks like a subtree
  4245. (org-back-to-heading t)) ; take what is really there
  4246. (org-back-over-empty-lines)
  4247. (setq beg (point))
  4248. (skip-chars-forward " \t\r\n")
  4249. (save-match-data
  4250. (save-excursion (outline-end-of-heading)
  4251. (setq folded (org-invisible-p)))
  4252. (condition-case nil
  4253. (outline-forward-same-level (1- n))
  4254. (error nil))
  4255. (org-end-of-subtree t t))
  4256. (org-back-over-empty-lines)
  4257. (setq end (point))
  4258. (goto-char beg0)
  4259. (when (> end beg)
  4260. (setq org-subtree-clip-folded folded)
  4261. (when (or cut force-store-markers)
  4262. (org-save-markers-in-region beg end))
  4263. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  4264. (setq org-subtree-clip (current-kill 0))
  4265. (message "%s: Subtree(s) with %d characters"
  4266. (if cut "Cut" "Copied")
  4267. (length org-subtree-clip)))))
  4268. (defun org-paste-subtree (&optional level tree)
  4269. "Paste the clipboard as a subtree, with modification of headline level.
  4270. The entire subtree is promoted or demoted in order to match a new headline
  4271. level. By default, the new level is derived from the visible headings
  4272. before and after the insertion point, and taken to be the inferior headline
  4273. level of the two. So if the previous visible heading is level 3 and the
  4274. next is level 4 (or vice versa), level 4 will be used for insertion.
  4275. This makes sure that the subtree remains an independent subtree and does
  4276. not swallow low level entries.
  4277. You can also force a different level, either by using a numeric prefix
  4278. argument, or by inserting the heading marker by hand. For example, if the
  4279. cursor is after \"*****\", then the tree will be shifted to level 5.
  4280. If you want to insert the tree as is, just use \\[yank].
  4281. If optional TREE is given, use this text instead of the kill ring."
  4282. (interactive "P")
  4283. (unless (org-kill-is-subtree-p tree)
  4284. (error "%s"
  4285. (substitute-command-keys
  4286. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  4287. (let* ((txt (or tree (and kill-ring (current-kill 0))))
  4288. (^re (concat "^\\(" outline-regexp "\\)"))
  4289. (re (concat "\\(" outline-regexp "\\)"))
  4290. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  4291. (old-level (if (string-match ^re txt)
  4292. (- (match-end 0) (match-beginning 0) 1)
  4293. -1))
  4294. (force-level (cond (level (prefix-numeric-value level))
  4295. ((string-match
  4296. ^re_ (buffer-substring (point-at-bol) (point)))
  4297. (- (match-end 1) (match-beginning 1)))
  4298. (t nil)))
  4299. (previous-level (save-excursion
  4300. (condition-case nil
  4301. (progn
  4302. (outline-previous-visible-heading 1)
  4303. (if (looking-at re)
  4304. (- (match-end 0) (match-beginning 0) 1)
  4305. 1))
  4306. (error 1))))
  4307. (next-level (save-excursion
  4308. (condition-case nil
  4309. (progn
  4310. (or (looking-at outline-regexp)
  4311. (outline-next-visible-heading 1))
  4312. (if (looking-at re)
  4313. (- (match-end 0) (match-beginning 0) 1)
  4314. 1))
  4315. (error 1))))
  4316. (new-level (or force-level (max previous-level next-level)))
  4317. (shift (if (or (= old-level -1)
  4318. (= new-level -1)
  4319. (= old-level new-level))
  4320. 0
  4321. (- new-level old-level)))
  4322. (delta (if (> shift 0) -1 1))
  4323. (func (if (> shift 0) 'org-demote 'org-promote))
  4324. (org-odd-levels-only nil)
  4325. beg end)
  4326. ;; Remove the forced level indicator
  4327. (if force-level
  4328. (delete-region (point-at-bol) (point)))
  4329. ;; Paste
  4330. (beginning-of-line 1)
  4331. (org-back-over-empty-lines)
  4332. (setq beg (point))
  4333. (insert-before-markers txt)
  4334. (unless (string-match "\n\\'" txt) (insert "\n"))
  4335. (org-reinstall-markers-in-region beg)
  4336. (setq end (point))
  4337. (goto-char beg)
  4338. (skip-chars-forward " \t\n\r")
  4339. (setq beg (point))
  4340. ;; Shift if necessary
  4341. (unless (= shift 0)
  4342. (save-restriction
  4343. (narrow-to-region beg end)
  4344. (while (not (= shift 0))
  4345. (org-map-region func (point-min) (point-max))
  4346. (setq shift (+ delta shift)))
  4347. (goto-char (point-min))))
  4348. (when (interactive-p)
  4349. (message "Clipboard pasted as level %d subtree" new-level))
  4350. (if (and kill-ring
  4351. (eq org-subtree-clip (current-kill 0))
  4352. org-subtree-clip-folded)
  4353. ;; The tree was folded before it was killed/copied
  4354. (hide-subtree))))
  4355. (defun org-kill-is-subtree-p (&optional txt)
  4356. "Check if the current kill is an outline subtree, or a set of trees.
  4357. Returns nil if kill does not start with a headline, or if the first
  4358. headline level is not the largest headline level in the tree.
  4359. So this will actually accept several entries of equal levels as well,
  4360. which is OK for `org-paste-subtree'.
  4361. If optional TXT is given, check this string instead of the current kill."
  4362. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  4363. (start-level (and kill
  4364. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  4365. org-outline-regexp "\\)")
  4366. kill)
  4367. (- (match-end 2) (match-beginning 2) 1)))
  4368. (re (concat "^" org-outline-regexp))
  4369. (start (1+ (match-beginning 2))))
  4370. (if (not start-level)
  4371. (progn
  4372. nil) ;; does not even start with a heading
  4373. (catch 'exit
  4374. (while (setq start (string-match re kill (1+ start)))
  4375. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  4376. (throw 'exit nil)))
  4377. t))))
  4378. (defvar org-markers-to-move nil
  4379. "Markers that should be moved with a cut-and-paste operation.
  4380. Those markers are stored together with their positions relative to
  4381. the start of the region.")
  4382. (defun org-save-markers-in-region (beg end)
  4383. "Check markers in region.
  4384. If these markers are between BEG and END, record their position relative
  4385. to BEG, so that after moving the block of text, we can put the markers back
  4386. into place.
  4387. This function gets called just before an entry or tree gets cut from the
  4388. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  4389. called immediately, to move the markers with the entries."
  4390. (setq org-markers-to-move nil)
  4391. (when (featurep 'org-clock)
  4392. (org-clock-save-markers-for-cut-and-paste beg end))
  4393. (when (featurep 'org-agenda)
  4394. (org-agenda-save-markers-for-cut-and-paste beg end)))
  4395. (defun org-check-and-save-marker (marker beg end)
  4396. "Check if MARKER is between BEG and END.
  4397. If yes, remember the marker and the distance to BEG."
  4398. (when (and (marker-buffer marker)
  4399. (equal (marker-buffer marker) (current-buffer)))
  4400. (if (and (>= marker beg) (< marker end))
  4401. (push (cons marker (- marker beg)) org-markers-to-move))))
  4402. (defun org-reinstall-markers-in-region (beg)
  4403. "Move all remembered markers to their position relative to BEG."
  4404. (mapc (lambda (x)
  4405. (move-marker (car x) (+ beg (cdr x))))
  4406. org-markers-to-move)
  4407. (setq org-markers-to-move nil))
  4408. (defun org-narrow-to-subtree ()
  4409. "Narrow buffer to the current subtree."
  4410. (interactive)
  4411. (save-excursion
  4412. (save-match-data
  4413. (narrow-to-region
  4414. (progn (org-back-to-heading) (point))
  4415. (progn (org-end-of-subtree t t) (point))))))
  4416. ;;; Outline Sorting
  4417. (defun org-sort (with-case)
  4418. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  4419. Optional argument WITH-CASE means sort case-sensitively."
  4420. (interactive "P")
  4421. (if (org-at-table-p)
  4422. (org-call-with-arg 'org-table-sort-lines with-case)
  4423. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  4424. (defun org-sort-remove-invisible (s)
  4425. (remove-text-properties 0 (length s) org-rm-props s)
  4426. (while (string-match org-bracket-link-regexp s)
  4427. (setq s (replace-match (if (match-end 2)
  4428. (match-string 3 s)
  4429. (match-string 1 s)) t t s)))
  4430. s)
  4431. (defvar org-priority-regexp) ; defined later in the file
  4432. (defun org-sort-entries-or-items (&optional with-case sorting-type getkey-func property)
  4433. "Sort entries on a certain level of an outline tree.
  4434. If there is an active region, the entries in the region are sorted.
  4435. Else, if the cursor is before the first entry, sort the top-level items.
  4436. Else, the children of the entry at point are sorted.
  4437. Sorting can be alphabetically, numerically, and by date/time as given by
  4438. the first time stamp in the entry. The command prompts for the sorting
  4439. type unless it has been given to the function through the SORTING-TYPE
  4440. argument, which needs to a character, any of (?n ?N ?a ?A ?t ?T ?p ?P ?f ?F).
  4441. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  4442. called with point at the beginning of the record. It must return either
  4443. a string or a number that should serve as the sorting key for that record.
  4444. Comparing entries ignores case by default. However, with an optional argument
  4445. WITH-CASE, the sorting considers case as well."
  4446. (interactive "P")
  4447. (let ((case-func (if with-case 'identity 'downcase))
  4448. start beg end stars re re2
  4449. txt what tmp plain-list-p)
  4450. ;; Find beginning and end of region to sort
  4451. (cond
  4452. ((org-region-active-p)
  4453. ;; we will sort the region
  4454. (setq end (region-end)
  4455. what "region")
  4456. (goto-char (region-beginning))
  4457. (if (not (org-on-heading-p)) (outline-next-heading))
  4458. (setq start (point)))
  4459. ((org-at-item-p)
  4460. ;; we will sort this plain list
  4461. (org-beginning-of-item-list) (setq start (point))
  4462. (org-end-of-item-list) (setq end (point))
  4463. (goto-char start)
  4464. (setq plain-list-p t
  4465. what "plain list"))
  4466. ((or (org-on-heading-p)
  4467. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  4468. ;; we will sort the children of the current headline
  4469. (org-back-to-heading)
  4470. (setq start (point)
  4471. end (progn (org-end-of-subtree t t)
  4472. (org-back-over-empty-lines)
  4473. (point))
  4474. what "children")
  4475. (goto-char start)
  4476. (show-subtree)
  4477. (outline-next-heading))
  4478. (t
  4479. ;; we will sort the top-level entries in this file
  4480. (goto-char (point-min))
  4481. (or (org-on-heading-p) (outline-next-heading))
  4482. (setq start (point) end (point-max) what "top-level")
  4483. (goto-char start)
  4484. (show-all)))
  4485. (setq beg (point))
  4486. (if (>= beg end) (error "Nothing to sort"))
  4487. (unless plain-list-p
  4488. (looking-at "\\(\\*+\\)")
  4489. (setq stars (match-string 1)
  4490. re (concat "^" (regexp-quote stars) " +")
  4491. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  4492. txt (buffer-substring beg end))
  4493. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  4494. (if (and (not (equal stars "*")) (string-match re2 txt))
  4495. (error "Region to sort contains a level above the first entry")))
  4496. (unless sorting-type
  4497. (message
  4498. (if plain-list-p
  4499. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  4500. "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:")
  4501. what)
  4502. (setq sorting-type (read-char-exclusive))
  4503. (and (= (downcase sorting-type) ?f)
  4504. (setq getkey-func
  4505. (completing-read "Sort using function: "
  4506. obarray 'fboundp t nil nil))
  4507. (setq getkey-func (intern getkey-func)))
  4508. (and (= (downcase sorting-type) ?r)
  4509. (setq property
  4510. (completing-read "Property: "
  4511. (mapcar 'list (org-buffer-property-keys t))
  4512. nil t))))
  4513. (message "Sorting entries...")
  4514. (save-restriction
  4515. (narrow-to-region start end)
  4516. (let ((dcst (downcase sorting-type))
  4517. (now (current-time)))
  4518. (sort-subr
  4519. (/= dcst sorting-type)
  4520. ;; This function moves to the beginning character of the "record" to
  4521. ;; be sorted.
  4522. (if plain-list-p
  4523. (lambda nil
  4524. (if (org-at-item-p) t (goto-char (point-max))))
  4525. (lambda nil
  4526. (if (re-search-forward re nil t)
  4527. (goto-char (match-beginning 0))
  4528. (goto-char (point-max)))))
  4529. ;; This function moves to the last character of the "record" being
  4530. ;; sorted.
  4531. (if plain-list-p
  4532. 'org-end-of-item
  4533. (lambda nil
  4534. (save-match-data
  4535. (condition-case nil
  4536. (outline-forward-same-level 1)
  4537. (error
  4538. (goto-char (point-max)))))))
  4539. ;; This function returns the value that gets sorted against.
  4540. (if plain-list-p
  4541. (lambda nil
  4542. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  4543. (cond
  4544. ((= dcst ?n)
  4545. (string-to-number (buffer-substring (match-end 0)
  4546. (point-at-eol))))
  4547. ((= dcst ?a)
  4548. (buffer-substring (match-end 0) (point-at-eol)))
  4549. ((= dcst ?t)
  4550. (if (re-search-forward org-ts-regexp
  4551. (point-at-eol) t)
  4552. (org-time-string-to-time (match-string 0))
  4553. now))
  4554. ((= dcst ?f)
  4555. (if getkey-func
  4556. (progn
  4557. (setq tmp (funcall getkey-func))
  4558. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  4559. tmp)
  4560. (error "Invalid key function `%s'" getkey-func)))
  4561. (t (error "Invalid sorting type `%c'" sorting-type)))))
  4562. (lambda nil
  4563. (cond
  4564. ((= dcst ?n)
  4565. (if (looking-at outline-regexp)
  4566. (string-to-number (buffer-substring (match-end 0)
  4567. (point-at-eol)))
  4568. nil))
  4569. ((= dcst ?a)
  4570. (funcall case-func (buffer-substring (point-at-bol)
  4571. (point-at-eol))))
  4572. ((= dcst ?t)
  4573. (if (re-search-forward org-ts-regexp
  4574. (save-excursion
  4575. (forward-line 2)
  4576. (point)) t)
  4577. (org-time-string-to-time (match-string 0))
  4578. now))
  4579. ((= dcst ?p)
  4580. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  4581. (string-to-char (match-string 2))
  4582. org-default-priority))
  4583. ((= dcst ?r)
  4584. (or (org-entry-get nil property) ""))
  4585. ((= dcst ?o)
  4586. (if (looking-at org-complex-heading-regexp)
  4587. (- 9999 (length (member (match-string 2)
  4588. org-todo-keywords-1)))))
  4589. ((= dcst ?f)
  4590. (if getkey-func
  4591. (progn
  4592. (setq tmp (funcall getkey-func))
  4593. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  4594. tmp)
  4595. (error "Invalid key function `%s'" getkey-func)))
  4596. (t (error "Invalid sorting type `%c'" sorting-type)))))
  4597. nil
  4598. (cond
  4599. ((= dcst ?a) 'string<)
  4600. ((= dcst ?t) 'time-less-p)
  4601. (t nil)))))
  4602. (message "Sorting entries...done")))
  4603. (defun org-do-sort (table what &optional with-case sorting-type)
  4604. "Sort TABLE of WHAT according to SORTING-TYPE.
  4605. The user will be prompted for the SORTING-TYPE if the call to this
  4606. function does not specify it. WHAT is only for the prompt, to indicate
  4607. what is being sorted. The sorting key will be extracted from
  4608. the car of the elements of the table.
  4609. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  4610. (unless sorting-type
  4611. (message
  4612. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  4613. what)
  4614. (setq sorting-type (read-char-exclusive)))
  4615. (let ((dcst (downcase sorting-type))
  4616. extractfun comparefun)
  4617. ;; Define the appropriate functions
  4618. (cond
  4619. ((= dcst ?n)
  4620. (setq extractfun 'string-to-number
  4621. comparefun (if (= dcst sorting-type) '< '>)))
  4622. ((= dcst ?a)
  4623. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  4624. (lambda(x) (downcase (org-sort-remove-invisible x))))
  4625. comparefun (if (= dcst sorting-type)
  4626. 'string<
  4627. (lambda (a b) (and (not (string< a b))
  4628. (not (string= a b)))))))
  4629. ((= dcst ?t)
  4630. (setq extractfun
  4631. (lambda (x)
  4632. (if (string-match org-ts-regexp x)
  4633. (time-to-seconds
  4634. (org-time-string-to-time (match-string 0 x)))
  4635. 0))
  4636. comparefun (if (= dcst sorting-type) '< '>)))
  4637. (t (error "Invalid sorting type `%c'" sorting-type)))
  4638. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  4639. table)
  4640. (lambda (a b) (funcall comparefun (car a) (car b))))))
  4641. ;;;; Plain list items, including checkboxes
  4642. ;;; Plain list items
  4643. (defun org-at-item-p ()
  4644. "Is point in a line starting a hand-formatted item?"
  4645. (let ((llt org-plain-list-ordered-item-terminator))
  4646. (save-excursion
  4647. (goto-char (point-at-bol))
  4648. (looking-at
  4649. (cond
  4650. ((eq llt t) "\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  4651. ((= llt ?.) "\\([ \t]*\\([-+]\\|\\([0-9]+\\.\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  4652. ((= llt ?\)) "\\([ \t]*\\([-+]\\|\\([0-9]+))\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  4653. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))))))
  4654. (defun org-in-item-p ()
  4655. "It the cursor inside a plain list item.
  4656. Does not have to be the first line."
  4657. (save-excursion
  4658. (condition-case nil
  4659. (progn
  4660. (org-beginning-of-item)
  4661. (org-at-item-p)
  4662. t)
  4663. (error nil))))
  4664. (defun org-insert-item (&optional checkbox)
  4665. "Insert a new item at the current level.
  4666. Return t when things worked, nil when we are not in an item."
  4667. (when (save-excursion
  4668. (condition-case nil
  4669. (progn
  4670. (org-beginning-of-item)
  4671. (org-at-item-p)
  4672. (if (org-invisible-p) (error "Invisible item"))
  4673. t)
  4674. (error nil)))
  4675. (let* ((bul (match-string 0))
  4676. (eow (save-excursion (beginning-of-line 1) (looking-at "[ \t]*")
  4677. (match-end 0)))
  4678. (blank (cdr (assq 'plain-list-item org-blank-before-new-entry)))
  4679. pos)
  4680. (cond
  4681. ((and (org-at-item-p) (<= (point) eow))
  4682. ;; before the bullet
  4683. (beginning-of-line 1)
  4684. (open-line (if blank 2 1)))
  4685. ((<= (point) eow)
  4686. (beginning-of-line 1))
  4687. (t
  4688. (unless (org-get-alist-option org-M-RET-may-split-line 'item)
  4689. (end-of-line 1)
  4690. (delete-horizontal-space))
  4691. (newline (if blank 2 1))))
  4692. (insert bul (if checkbox "[ ]" ""))
  4693. (just-one-space)
  4694. (setq pos (point))
  4695. (end-of-line 1)
  4696. (unless (= (point) pos) (just-one-space) (backward-delete-char 1)))
  4697. (org-maybe-renumber-ordered-list)
  4698. (and checkbox (org-update-checkbox-count-maybe))
  4699. t))
  4700. ;;; Checkboxes
  4701. (defun org-at-item-checkbox-p ()
  4702. "Is point at a line starting a plain-list item with a checklet?"
  4703. (and (org-at-item-p)
  4704. (save-excursion
  4705. (goto-char (match-end 0))
  4706. (skip-chars-forward " \t")
  4707. (looking-at "\\[[- X]\\]"))))
  4708. (defun org-toggle-checkbox (&optional arg)
  4709. "Toggle the checkbox in the current line."
  4710. (interactive "P")
  4711. (catch 'exit
  4712. (let (beg end status (firstnew 'unknown))
  4713. (cond
  4714. ((org-region-active-p)
  4715. (setq beg (region-beginning) end (region-end)))
  4716. ((org-on-heading-p)
  4717. (setq beg (point) end (save-excursion (outline-next-heading) (point))))
  4718. ((org-at-item-checkbox-p)
  4719. (let ((pos (point)))
  4720. (replace-match
  4721. (cond (arg "[-]")
  4722. ((member (match-string 0) '("[ ]" "[-]")) "[X]")
  4723. (t "[ ]"))
  4724. t t)
  4725. (goto-char pos))
  4726. (throw 'exit t))
  4727. (t (error "Not at a checkbox or heading, and no active region")))
  4728. (save-excursion
  4729. (goto-char beg)
  4730. (while (< (point) end)
  4731. (when (org-at-item-checkbox-p)
  4732. (setq status (equal (match-string 0) "[X]"))
  4733. (when (eq firstnew 'unknown)
  4734. (setq firstnew (not status)))
  4735. (replace-match
  4736. (if (if arg (not status) firstnew) "[X]" "[ ]") t t))
  4737. (beginning-of-line 2)))))
  4738. (org-update-checkbox-count-maybe))
  4739. (defun org-update-checkbox-count-maybe ()
  4740. "Update checkbox statistics unless turned off by user."
  4741. (when org-provide-checkbox-statistics
  4742. (org-update-checkbox-count)))
  4743. (defun org-update-checkbox-count (&optional all)
  4744. "Update the checkbox statistics in the current section.
  4745. This will find all statistic cookies like [57%] and [6/12] and update them
  4746. with the current numbers. With optional prefix argument ALL, do this for
  4747. the whole buffer."
  4748. (interactive "P")
  4749. (save-excursion
  4750. (let* ((buffer-invisibility-spec (org-inhibit-invisibility)) ; Emacs 21
  4751. (beg (condition-case nil
  4752. (progn (outline-back-to-heading) (point))
  4753. (error (point-min))))
  4754. (end (move-marker (make-marker)
  4755. (progn (outline-next-heading) (point))))
  4756. (re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  4757. (re-box "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)")
  4758. (re-find (concat re "\\|" re-box))
  4759. beg-cookie end-cookie is-percent c-on c-off lim
  4760. eline curr-ind next-ind continue-from startsearch
  4761. (cstat 0)
  4762. )
  4763. (when all
  4764. (goto-char (point-min))
  4765. (outline-next-heading)
  4766. (setq beg (point) end (point-max)))
  4767. (goto-char end)
  4768. ;; find each statistic cookie
  4769. (while (re-search-backward re-find beg t)
  4770. (setq beg-cookie (match-beginning 1)
  4771. end-cookie (match-end 1)
  4772. cstat (+ cstat (if end-cookie 1 0))
  4773. startsearch (point-at-eol)
  4774. continue-from (point-at-bol)
  4775. is-percent (match-beginning 2)
  4776. lim (cond
  4777. ((org-on-heading-p) (outline-next-heading) (point))
  4778. ((org-at-item-p) (org-end-of-item) (point))
  4779. (t nil))
  4780. c-on 0
  4781. c-off 0)
  4782. (when lim
  4783. ;; find first checkbox for this cookie and gather
  4784. ;; statistics from all that are at this indentation level
  4785. (goto-char startsearch)
  4786. (if (re-search-forward re-box lim t)
  4787. (progn
  4788. (org-beginning-of-item)
  4789. (setq curr-ind (org-get-indentation))
  4790. (setq next-ind curr-ind)
  4791. (while (and (bolp) (org-at-item-p) (= curr-ind next-ind))
  4792. (save-excursion (end-of-line) (setq eline (point)))
  4793. (if (re-search-forward re-box eline t)
  4794. (if (member (match-string 2) '("[ ]" "[-]"))
  4795. (setq c-off (1+ c-off))
  4796. (setq c-on (1+ c-on))
  4797. )
  4798. )
  4799. (org-end-of-item)
  4800. (setq next-ind (org-get-indentation))
  4801. )))
  4802. (goto-char continue-from)
  4803. ;; update cookie
  4804. (when end-cookie
  4805. (delete-region beg-cookie end-cookie)
  4806. (goto-char beg-cookie)
  4807. (insert
  4808. (if is-percent
  4809. (format "[%d%%]" (/ (* 100 c-on) (max 1 (+ c-on c-off))))
  4810. (format "[%d/%d]" c-on (+ c-on c-off)))))
  4811. ;; update items checkbox if it has one
  4812. (when (org-at-item-p)
  4813. (org-beginning-of-item)
  4814. (when (and (> (+ c-on c-off) 0)
  4815. (re-search-forward re-box (point-at-eol) t))
  4816. (setq beg-cookie (match-beginning 2)
  4817. end-cookie (match-end 2))
  4818. (delete-region beg-cookie end-cookie)
  4819. (goto-char beg-cookie)
  4820. (cond ((= c-off 0) (insert "[X]"))
  4821. ((= c-on 0) (insert "[ ]"))
  4822. (t (insert "[-]")))
  4823. )))
  4824. (goto-char continue-from))
  4825. (when (interactive-p)
  4826. (message "Checkbox satistics updated %s (%d places)"
  4827. (if all "in entire file" "in current outline entry") cstat)))))
  4828. (defun org-get-checkbox-statistics-face ()
  4829. "Select the face for checkbox statistics.
  4830. The face will be `org-done' when all relevant boxes are checked. Otherwise
  4831. it will be `org-todo'."
  4832. (if (match-end 1)
  4833. (if (equal (match-string 1) "100%") 'org-done 'org-todo)
  4834. (if (and (> (match-end 2) (match-beginning 2))
  4835. (equal (match-string 2) (match-string 3)))
  4836. 'org-done
  4837. 'org-todo)))
  4838. (defun org-get-indentation (&optional line)
  4839. "Get the indentation of the current line, interpreting tabs.
  4840. When LINE is given, assume it represents a line and compute its indentation."
  4841. (if line
  4842. (if (string-match "^ *" (org-remove-tabs line))
  4843. (match-end 0))
  4844. (save-excursion
  4845. (beginning-of-line 1)
  4846. (skip-chars-forward " \t")
  4847. (current-column))))
  4848. (defun org-remove-tabs (s &optional width)
  4849. "Replace tabulators in S with spaces.
  4850. Assumes that s is a single line, starting in column 0."
  4851. (setq width (or width tab-width))
  4852. (while (string-match "\t" s)
  4853. (setq s (replace-match
  4854. (make-string
  4855. (- (* width (/ (+ (match-beginning 0) width) width))
  4856. (match-beginning 0)) ?\ )
  4857. t t s)))
  4858. s)
  4859. (defun org-fix-indentation (line ind)
  4860. "Fix indentation in LINE.
  4861. IND is a cons cell with target and minimum indentation.
  4862. If the current indenation in LINE is smaller than the minimum,
  4863. leave it alone. If it is larger than ind, set it to the target."
  4864. (let* ((l (org-remove-tabs line))
  4865. (i (org-get-indentation l))
  4866. (i1 (car ind)) (i2 (cdr ind)))
  4867. (if (>= i i2) (setq l (substring line i2)))
  4868. (if (> i1 0)
  4869. (concat (make-string i1 ?\ ) l)
  4870. l)))
  4871. (defun org-beginning-of-item ()
  4872. "Go to the beginning of the current hand-formatted item.
  4873. If the cursor is not in an item, throw an error."
  4874. (interactive)
  4875. (let ((pos (point))
  4876. (limit (save-excursion
  4877. (condition-case nil
  4878. (progn
  4879. (org-back-to-heading)
  4880. (beginning-of-line 2) (point))
  4881. (error (point-min)))))
  4882. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  4883. ind ind1)
  4884. (if (org-at-item-p)
  4885. (beginning-of-line 1)
  4886. (beginning-of-line 1)
  4887. (skip-chars-forward " \t")
  4888. (setq ind (current-column))
  4889. (if (catch 'exit
  4890. (while t
  4891. (beginning-of-line 0)
  4892. (if (or (bobp) (< (point) limit)) (throw 'exit nil))
  4893. (if (looking-at "[ \t]*$")
  4894. (setq ind1 ind-empty)
  4895. (skip-chars-forward " \t")
  4896. (setq ind1 (current-column)))
  4897. (if (< ind1 ind)
  4898. (progn (beginning-of-line 1) (throw 'exit (org-at-item-p))))))
  4899. nil
  4900. (goto-char pos)
  4901. (error "Not in an item")))))
  4902. (defun org-end-of-item ()
  4903. "Go to the end of the current hand-formatted item.
  4904. If the cursor is not in an item, throw an error."
  4905. (interactive)
  4906. (let* ((pos (point))
  4907. ind1
  4908. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  4909. (limit (save-excursion (outline-next-heading) (point)))
  4910. (ind (save-excursion
  4911. (org-beginning-of-item)
  4912. (skip-chars-forward " \t")
  4913. (current-column)))
  4914. (end (catch 'exit
  4915. (while t
  4916. (beginning-of-line 2)
  4917. (if (eobp) (throw 'exit (point)))
  4918. (if (>= (point) limit) (throw 'exit (point-at-bol)))
  4919. (if (looking-at "[ \t]*$")
  4920. (setq ind1 ind-empty)
  4921. (skip-chars-forward " \t")
  4922. (setq ind1 (current-column)))
  4923. (if (<= ind1 ind)
  4924. (throw 'exit (point-at-bol)))))))
  4925. (if end
  4926. (goto-char end)
  4927. (goto-char pos)
  4928. (error "Not in an item"))))
  4929. (defun org-next-item ()
  4930. "Move to the beginning of the next item in the current plain list.
  4931. Error if not at a plain list, or if this is the last item in the list."
  4932. (interactive)
  4933. (let (ind ind1 (pos (point)))
  4934. (org-beginning-of-item)
  4935. (setq ind (org-get-indentation))
  4936. (org-end-of-item)
  4937. (setq ind1 (org-get-indentation))
  4938. (unless (and (org-at-item-p) (= ind ind1))
  4939. (goto-char pos)
  4940. (error "On last item"))))
  4941. (defun org-previous-item ()
  4942. "Move to the beginning of the previous item in the current plain list.
  4943. Error if not at a plain list, or if this is the first item in the list."
  4944. (interactive)
  4945. (let (beg ind ind1 (pos (point)))
  4946. (org-beginning-of-item)
  4947. (setq beg (point))
  4948. (setq ind (org-get-indentation))
  4949. (goto-char beg)
  4950. (catch 'exit
  4951. (while t
  4952. (beginning-of-line 0)
  4953. (if (looking-at "[ \t]*$")
  4954. nil
  4955. (if (<= (setq ind1 (org-get-indentation)) ind)
  4956. (throw 'exit t)))))
  4957. (condition-case nil
  4958. (if (or (not (org-at-item-p))
  4959. (< ind1 (1- ind)))
  4960. (error "")
  4961. (org-beginning-of-item))
  4962. (error (goto-char pos)
  4963. (error "On first item")))))
  4964. (defun org-first-list-item-p ()
  4965. "Is this heading the item in a plain list?"
  4966. (unless (org-at-item-p)
  4967. (error "Not at a plain list item"))
  4968. (org-beginning-of-item)
  4969. (= (point) (save-excursion (org-beginning-of-item-list))))
  4970. (defun org-move-item-down ()
  4971. "Move the plain list item at point down, i.e. swap with following item.
  4972. Subitems (items with larger indentation) are considered part of the item,
  4973. so this really moves item trees."
  4974. (interactive)
  4975. (let (beg beg0 end end0 ind ind1 (pos (point)) txt ne-end ne-beg)
  4976. (org-beginning-of-item)
  4977. (setq beg0 (point))
  4978. (save-excursion
  4979. (setq ne-beg (org-back-over-empty-lines))
  4980. (setq beg (point)))
  4981. (goto-char beg0)
  4982. (setq ind (org-get-indentation))
  4983. (org-end-of-item)
  4984. (setq end0 (point))
  4985. (setq ind1 (org-get-indentation))
  4986. (setq ne-end (org-back-over-empty-lines))
  4987. (setq end (point))
  4988. (goto-char beg0)
  4989. (when (and (org-first-list-item-p) (< ne-end ne-beg))
  4990. ;; include less whitespace
  4991. (save-excursion
  4992. (goto-char beg)
  4993. (forward-line (- ne-beg ne-end))
  4994. (setq beg (point))))
  4995. (goto-char end0)
  4996. (if (and (org-at-item-p) (= ind ind1))
  4997. (progn
  4998. (org-end-of-item)
  4999. (org-back-over-empty-lines)
  5000. (setq txt (buffer-substring beg end))
  5001. (save-excursion
  5002. (delete-region beg end))
  5003. (setq pos (point))
  5004. (insert txt)
  5005. (goto-char pos) (org-skip-whitespace)
  5006. (org-maybe-renumber-ordered-list))
  5007. (goto-char pos)
  5008. (error "Cannot move this item further down"))))
  5009. (defun org-move-item-up (arg)
  5010. "Move the plain list item at point up, i.e. swap with previous item.
  5011. Subitems (items with larger indentation) are considered part of the item,
  5012. so this really moves item trees."
  5013. (interactive "p")
  5014. (let (beg beg0 end ind ind1 (pos (point)) txt
  5015. ne-beg ne-ins ins-end)
  5016. (org-beginning-of-item)
  5017. (setq beg0 (point))
  5018. (setq ind (org-get-indentation))
  5019. (save-excursion
  5020. (setq ne-beg (org-back-over-empty-lines))
  5021. (setq beg (point)))
  5022. (goto-char beg0)
  5023. (org-end-of-item)
  5024. (setq end (point))
  5025. (goto-char beg0)
  5026. (catch 'exit
  5027. (while t
  5028. (beginning-of-line 0)
  5029. (if (looking-at "[ \t]*$")
  5030. (if org-empty-line-terminates-plain-lists
  5031. (progn
  5032. (goto-char pos)
  5033. (error "Cannot move this item further up"))
  5034. nil)
  5035. (if (<= (setq ind1 (org-get-indentation)) ind)
  5036. (throw 'exit t)))))
  5037. (condition-case nil
  5038. (org-beginning-of-item)
  5039. (error (goto-char beg)
  5040. (error "Cannot move this item further up")))
  5041. (setq ind1 (org-get-indentation))
  5042. (if (and (org-at-item-p) (= ind ind1))
  5043. (progn
  5044. (setq ne-ins (org-back-over-empty-lines))
  5045. (setq txt (buffer-substring beg end))
  5046. (save-excursion
  5047. (delete-region beg end))
  5048. (setq pos (point))
  5049. (insert txt)
  5050. (setq ins-end (point))
  5051. (goto-char pos) (org-skip-whitespace)
  5052. (when (and (org-first-list-item-p) (> ne-ins ne-beg))
  5053. ;; Move whitespace back to beginning
  5054. (save-excursion
  5055. (goto-char ins-end)
  5056. (let ((kill-whole-line t))
  5057. (kill-line (- ne-ins ne-beg)) (point)))
  5058. (insert (make-string (- ne-ins ne-beg) ?\n)))
  5059. (org-maybe-renumber-ordered-list))
  5060. (goto-char pos)
  5061. (error "Cannot move this item further up"))))
  5062. (defun org-maybe-renumber-ordered-list ()
  5063. "Renumber the ordered list at point if setup allows it.
  5064. This tests the user option `org-auto-renumber-ordered-lists' before
  5065. doing the renumbering."
  5066. (interactive)
  5067. (when (and org-auto-renumber-ordered-lists
  5068. (org-at-item-p))
  5069. (if (match-beginning 3)
  5070. (org-renumber-ordered-list 1)
  5071. (org-fix-bullet-type))))
  5072. (defun org-maybe-renumber-ordered-list-safe ()
  5073. (condition-case nil
  5074. (save-excursion
  5075. (org-maybe-renumber-ordered-list))
  5076. (error nil)))
  5077. (defun org-cycle-list-bullet (&optional which)
  5078. "Cycle through the different itemize/enumerate bullets.
  5079. This cycle the entire list level through the sequence:
  5080. `-' -> `+' -> `*' -> `1.' -> `1)'
  5081. If WHICH is a string, use that as the new bullet. If WHICH is an integer,
  5082. 0 meand `-', 1 means `+' etc."
  5083. (interactive "P")
  5084. (org-preserve-lc
  5085. (org-beginning-of-item-list)
  5086. (org-at-item-p)
  5087. (beginning-of-line 1)
  5088. (let ((current (match-string 0))
  5089. (prevp (eq which 'previous))
  5090. new)
  5091. (setq new (cond
  5092. ((and (numberp which)
  5093. (nth (1- which) '("-" "+" "*" "1." "1)"))))
  5094. ((string-match "-" current) (if prevp "1)" "+"))
  5095. ((string-match "\\+" current)
  5096. (if prevp "-" (if (looking-at "\\S-") "1." "*")))
  5097. ((string-match "\\*" current) (if prevp "+" "1."))
  5098. ((string-match "\\." current) (if prevp "*" "1)"))
  5099. ((string-match ")" current) (if prevp "1." "-"))
  5100. (t (error "This should not happen"))))
  5101. (and (looking-at "\\([ \t]*\\)\\S-+") (replace-match (concat "\\1" new)))
  5102. (org-fix-bullet-type)
  5103. (org-maybe-renumber-ordered-list))))
  5104. (defun org-get-string-indentation (s)
  5105. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  5106. (let ((n -1) (i 0) (w tab-width) c)
  5107. (catch 'exit
  5108. (while (< (setq n (1+ n)) (length s))
  5109. (setq c (aref s n))
  5110. (cond ((= c ?\ ) (setq i (1+ i)))
  5111. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  5112. (t (throw 'exit t)))))
  5113. i))
  5114. (defun org-renumber-ordered-list (arg)
  5115. "Renumber an ordered plain list.
  5116. Cursor needs to be in the first line of an item, the line that starts
  5117. with something like \"1.\" or \"2)\"."
  5118. (interactive "p")
  5119. (unless (and (org-at-item-p)
  5120. (match-beginning 3))
  5121. (error "This is not an ordered list"))
  5122. (let ((line (org-current-line))
  5123. (col (current-column))
  5124. (ind (org-get-string-indentation
  5125. (buffer-substring (point-at-bol) (match-beginning 3))))
  5126. ;; (term (substring (match-string 3) -1))
  5127. ind1 (n (1- arg))
  5128. fmt)
  5129. ;; find where this list begins
  5130. (org-beginning-of-item-list)
  5131. (looking-at "[ \t]*[0-9]+\\([.)]\\)")
  5132. (setq fmt (concat "%d" (match-string 1)))
  5133. (beginning-of-line 0)
  5134. ;; walk forward and replace these numbers
  5135. (catch 'exit
  5136. (while t
  5137. (catch 'next
  5138. (beginning-of-line 2)
  5139. (if (eobp) (throw 'exit nil))
  5140. (if (looking-at "[ \t]*$") (throw 'next nil))
  5141. (skip-chars-forward " \t") (setq ind1 (current-column))
  5142. (if (> ind1 ind) (throw 'next t))
  5143. (if (< ind1 ind) (throw 'exit t))
  5144. (if (not (org-at-item-p)) (throw 'exit nil))
  5145. (delete-region (match-beginning 2) (match-end 2))
  5146. (goto-char (match-beginning 2))
  5147. (insert (format fmt (setq n (1+ n)))))))
  5148. (goto-line line)
  5149. (org-move-to-column col)))
  5150. (defun org-fix-bullet-type ()
  5151. "Make sure all items in this list have the same bullet as the firsst item."
  5152. (interactive)
  5153. (unless (org-at-item-p) (error "This is not a list"))
  5154. (let ((line (org-current-line))
  5155. (col (current-column))
  5156. (ind (current-indentation))
  5157. ind1 bullet)
  5158. ;; find where this list begins
  5159. (org-beginning-of-item-list)
  5160. (beginning-of-line 1)
  5161. ;; find out what the bullet type is
  5162. (looking-at "[ \t]*\\(\\S-+\\)")
  5163. (setq bullet (match-string 1))
  5164. ;; walk forward and replace these numbers
  5165. (beginning-of-line 0)
  5166. (catch 'exit
  5167. (while t
  5168. (catch 'next
  5169. (beginning-of-line 2)
  5170. (if (eobp) (throw 'exit nil))
  5171. (if (looking-at "[ \t]*$") (throw 'next nil))
  5172. (skip-chars-forward " \t") (setq ind1 (current-column))
  5173. (if (> ind1 ind) (throw 'next t))
  5174. (if (< ind1 ind) (throw 'exit t))
  5175. (if (not (org-at-item-p)) (throw 'exit nil))
  5176. (skip-chars-forward " \t")
  5177. (looking-at "\\S-+")
  5178. (replace-match bullet))))
  5179. (goto-line line)
  5180. (org-move-to-column col)
  5181. (if (string-match "[0-9]" bullet)
  5182. (org-renumber-ordered-list 1))))
  5183. (defun org-beginning-of-item-list ()
  5184. "Go to the beginning of the current item list.
  5185. I.e. to the first item in this list."
  5186. (interactive)
  5187. (org-beginning-of-item)
  5188. (let ((pos (point-at-bol))
  5189. (ind (org-get-indentation))
  5190. ind1)
  5191. ;; find where this list begins
  5192. (catch 'exit
  5193. (while t
  5194. (catch 'next
  5195. (beginning-of-line 0)
  5196. (if (looking-at "[ \t]*$")
  5197. (throw (if (bobp) 'exit 'next) t))
  5198. (skip-chars-forward " \t") (setq ind1 (current-column))
  5199. (if (or (< ind1 ind)
  5200. (and (= ind1 ind)
  5201. (not (org-at-item-p)))
  5202. (bobp))
  5203. (throw 'exit t)
  5204. (when (org-at-item-p) (setq pos (point-at-bol)))))))
  5205. (goto-char pos)))
  5206. (defun org-end-of-item-list ()
  5207. "Go to the end of the current item list.
  5208. I.e. to the text after the last item."
  5209. (interactive)
  5210. (org-beginning-of-item)
  5211. (let ((pos (point-at-bol))
  5212. (ind (org-get-indentation))
  5213. ind1)
  5214. ;; find where this list begins
  5215. (catch 'exit
  5216. (while t
  5217. (catch 'next
  5218. (beginning-of-line 2)
  5219. (if (looking-at "[ \t]*$")
  5220. (throw (if (eobp) 'exit 'next) t))
  5221. (skip-chars-forward " \t") (setq ind1 (current-column))
  5222. (if (or (< ind1 ind)
  5223. (and (= ind1 ind)
  5224. (not (org-at-item-p)))
  5225. (eobp))
  5226. (progn
  5227. (setq pos (point-at-bol))
  5228. (throw 'exit t))))))
  5229. (goto-char pos)))
  5230. (defvar org-last-indent-begin-marker (make-marker))
  5231. (defvar org-last-indent-end-marker (make-marker))
  5232. (defun org-outdent-item (arg)
  5233. "Outdent a local list item."
  5234. (interactive "p")
  5235. (org-indent-item (- arg)))
  5236. (defun org-indent-item (arg)
  5237. "Indent a local list item."
  5238. (interactive "p")
  5239. (unless (org-at-item-p)
  5240. (error "Not on an item"))
  5241. (save-excursion
  5242. (let (beg end ind ind1 tmp delta ind-down ind-up)
  5243. (if (memq last-command '(org-shiftmetaright org-shiftmetaleft))
  5244. (setq beg org-last-indent-begin-marker
  5245. end org-last-indent-end-marker)
  5246. (org-beginning-of-item)
  5247. (setq beg (move-marker org-last-indent-begin-marker (point)))
  5248. (org-end-of-item)
  5249. (setq end (move-marker org-last-indent-end-marker (point))))
  5250. (goto-char beg)
  5251. (setq tmp (org-item-indent-positions)
  5252. ind (car tmp)
  5253. ind-down (nth 2 tmp)
  5254. ind-up (nth 1 tmp)
  5255. delta (if (> arg 0)
  5256. (if ind-down (- ind-down ind) 2)
  5257. (if ind-up (- ind-up ind) -2)))
  5258. (if (< (+ delta ind) 0) (error "Cannot outdent beyond margin"))
  5259. (while (< (point) end)
  5260. (beginning-of-line 1)
  5261. (skip-chars-forward " \t") (setq ind1 (current-column))
  5262. (delete-region (point-at-bol) (point))
  5263. (or (eolp) (org-indent-to-column (+ ind1 delta)))
  5264. (beginning-of-line 2))))
  5265. (org-fix-bullet-type)
  5266. (org-maybe-renumber-ordered-list-safe)
  5267. (save-excursion
  5268. (beginning-of-line 0)
  5269. (condition-case nil (org-beginning-of-item) (error nil))
  5270. (org-maybe-renumber-ordered-list-safe)))
  5271. (defun org-item-indent-positions ()
  5272. "Return indentation for plain list items.
  5273. This returns a list with three values: The current indentation, the
  5274. parent indentation and the indentation a child should habe.
  5275. Assumes cursor in item line."
  5276. (let* ((bolpos (point-at-bol))
  5277. (ind (org-get-indentation))
  5278. ind-down ind-up pos)
  5279. (save-excursion
  5280. (org-beginning-of-item-list)
  5281. (skip-chars-backward "\n\r \t")
  5282. (when (org-in-item-p)
  5283. (org-beginning-of-item)
  5284. (setq ind-up (org-get-indentation))))
  5285. (setq pos (point))
  5286. (save-excursion
  5287. (cond
  5288. ((and (condition-case nil (progn (org-previous-item) t)
  5289. (error nil))
  5290. (or (forward-char 1) t)
  5291. (re-search-forward "^\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)" bolpos t))
  5292. (setq ind-down (org-get-indentation)))
  5293. ((and (goto-char pos)
  5294. (org-at-item-p))
  5295. (goto-char (match-end 0))
  5296. (skip-chars-forward " \t")
  5297. (setq ind-down (current-column)))))
  5298. (list ind ind-up ind-down)))
  5299. ;;; The orgstruct minor mode
  5300. ;; Define a minor mode which can be used in other modes in order to
  5301. ;; integrate the org-mode structure editing commands.
  5302. ;; This is really a hack, because the org-mode structure commands use
  5303. ;; keys which normally belong to the major mode. Here is how it
  5304. ;; works: The minor mode defines all the keys necessary to operate the
  5305. ;; structure commands, but wraps the commands into a function which
  5306. ;; tests if the cursor is currently at a headline or a plain list
  5307. ;; item. If that is the case, the structure command is used,
  5308. ;; temporarily setting many Org-mode variables like regular
  5309. ;; expressions for filling etc. However, when any of those keys is
  5310. ;; used at a different location, function uses `key-binding' to look
  5311. ;; up if the key has an associated command in another currently active
  5312. ;; keymap (minor modes, major mode, global), and executes that
  5313. ;; command. There might be problems if any of the keys is otherwise
  5314. ;; used as a prefix key.
  5315. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  5316. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  5317. ;; addresses this by checking explicitly for both bindings.
  5318. (defvar orgstruct-mode-map (make-sparse-keymap)
  5319. "Keymap for the minor `orgstruct-mode'.")
  5320. (defvar org-local-vars nil
  5321. "List of local variables, for use by `orgstruct-mode'")
  5322. ;;;###autoload
  5323. (define-minor-mode orgstruct-mode
  5324. "Toggle the minor more `orgstruct-mode'.
  5325. This mode is for using Org-mode structure commands in other modes.
  5326. The following key behave as if Org-mode was active, if the cursor
  5327. is on a headline, or on a plain list item (both in the definition
  5328. of Org-mode).
  5329. M-up Move entry/item up
  5330. M-down Move entry/item down
  5331. M-left Promote
  5332. M-right Demote
  5333. M-S-up Move entry/item up
  5334. M-S-down Move entry/item down
  5335. M-S-left Promote subtree
  5336. M-S-right Demote subtree
  5337. M-q Fill paragraph and items like in Org-mode
  5338. C-c ^ Sort entries
  5339. C-c - Cycle list bullet
  5340. TAB Cycle item visibility
  5341. M-RET Insert new heading/item
  5342. S-M-RET Insert new TODO heading / Chekbox item
  5343. C-c C-c Set tags / toggle checkbox"
  5344. nil " OrgStruct" nil
  5345. (org-load-modules-maybe)
  5346. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  5347. ;;;###autoload
  5348. (defun turn-on-orgstruct ()
  5349. "Unconditionally turn on `orgstruct-mode'."
  5350. (orgstruct-mode 1))
  5351. ;;;###autoload
  5352. (defun turn-on-orgstruct++ ()
  5353. "Unconditionally turn on `orgstruct-mode', and force org-mode indentations.
  5354. In addition to setting orgstruct-mode, this also exports all indentation and
  5355. autofilling variables from org-mode into the buffer. Note that turning
  5356. off orgstruct-mode will *not* remove these additional settings."
  5357. (orgstruct-mode 1)
  5358. (let (var val)
  5359. (mapc
  5360. (lambda (x)
  5361. (when (string-match
  5362. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5363. (symbol-name (car x)))
  5364. (setq var (car x) val (nth 1 x))
  5365. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  5366. org-local-vars)))
  5367. (defun orgstruct-error ()
  5368. "Error when there is no default binding for a structure key."
  5369. (interactive)
  5370. (error "This key has no function outside structure elements"))
  5371. (defun orgstruct-setup ()
  5372. "Setup orgstruct keymaps."
  5373. (let ((nfunc 0)
  5374. (bindings
  5375. (list
  5376. '([(meta up)] org-metaup)
  5377. '([(meta down)] org-metadown)
  5378. '([(meta left)] org-metaleft)
  5379. '([(meta right)] org-metaright)
  5380. '([(meta shift up)] org-shiftmetaup)
  5381. '([(meta shift down)] org-shiftmetadown)
  5382. '([(meta shift left)] org-shiftmetaleft)
  5383. '([(meta shift right)] org-shiftmetaright)
  5384. '([(shift up)] org-shiftup)
  5385. '([(shift down)] org-shiftdown)
  5386. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  5387. '("\M-q" fill-paragraph)
  5388. '("\C-c^" org-sort)
  5389. '("\C-c-" org-cycle-list-bullet)))
  5390. elt key fun cmd)
  5391. (while (setq elt (pop bindings))
  5392. (setq nfunc (1+ nfunc))
  5393. (setq key (org-key (car elt))
  5394. fun (nth 1 elt)
  5395. cmd (orgstruct-make-binding fun nfunc key))
  5396. (org-defkey orgstruct-mode-map key cmd))
  5397. ;; Special treatment needed for TAB and RET
  5398. (org-defkey orgstruct-mode-map [(tab)]
  5399. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  5400. (org-defkey orgstruct-mode-map "\C-i"
  5401. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  5402. (org-defkey orgstruct-mode-map "\M-\C-m"
  5403. (orgstruct-make-binding 'org-insert-heading 105
  5404. "\M-\C-m" [(meta return)]))
  5405. (org-defkey orgstruct-mode-map [(meta return)]
  5406. (orgstruct-make-binding 'org-insert-heading 106
  5407. [(meta return)] "\M-\C-m"))
  5408. (org-defkey orgstruct-mode-map [(shift meta return)]
  5409. (orgstruct-make-binding 'org-insert-todo-heading 107
  5410. [(meta return)] "\M-\C-m"))
  5411. (unless org-local-vars
  5412. (setq org-local-vars (org-get-local-variables)))
  5413. t))
  5414. (defun orgstruct-make-binding (fun n &rest keys)
  5415. "Create a function for binding in the structure minor mode.
  5416. FUN is the command to call inside a table. N is used to create a unique
  5417. command name. KEYS are keys that should be checked in for a command
  5418. to execute outside of tables."
  5419. (eval
  5420. (list 'defun
  5421. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  5422. '(arg)
  5423. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  5424. "Outside of structure, run the binding of `"
  5425. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  5426. "'.")
  5427. '(interactive "p")
  5428. (list 'if
  5429. '(org-context-p 'headline 'item)
  5430. (list 'org-run-like-in-org-mode (list 'quote fun))
  5431. (list 'let '(orgstruct-mode)
  5432. (list 'call-interactively
  5433. (append '(or)
  5434. (mapcar (lambda (k)
  5435. (list 'key-binding k))
  5436. keys)
  5437. '('orgstruct-error))))))))
  5438. (defun org-context-p (&rest contexts)
  5439. "Check if local context is and of CONTEXTS.
  5440. Possible values in the list of contexts are `table', `headline', and `item'."
  5441. (let ((pos (point)))
  5442. (goto-char (point-at-bol))
  5443. (prog1 (or (and (memq 'table contexts)
  5444. (looking-at "[ \t]*|"))
  5445. (and (memq 'headline contexts)
  5446. (looking-at "\\*+"))
  5447. (and (memq 'item contexts)
  5448. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)")))
  5449. (goto-char pos))))
  5450. (defun org-get-local-variables ()
  5451. "Return a list of all local variables in an org-mode buffer."
  5452. (let (varlist)
  5453. (with-current-buffer (get-buffer-create "*Org tmp*")
  5454. (erase-buffer)
  5455. (org-mode)
  5456. (setq varlist (buffer-local-variables)))
  5457. (kill-buffer "*Org tmp*")
  5458. (delq nil
  5459. (mapcar
  5460. (lambda (x)
  5461. (setq x
  5462. (if (symbolp x)
  5463. (list x)
  5464. (list (car x) (list 'quote (cdr x)))))
  5465. (if (string-match
  5466. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5467. (symbol-name (car x)))
  5468. x nil))
  5469. varlist))))
  5470. ;;;###autoload
  5471. (defun org-run-like-in-org-mode (cmd)
  5472. (org-load-modules-maybe)
  5473. (unless org-local-vars
  5474. (setq org-local-vars (org-get-local-variables)))
  5475. (eval (list 'let org-local-vars
  5476. (list 'call-interactively (list 'quote cmd)))))
  5477. ;;;; Archiving
  5478. (defun org-get-category (&optional pos)
  5479. "Get the category applying to position POS."
  5480. (get-text-property (or pos (point)) 'org-category))
  5481. (defun org-refresh-category-properties ()
  5482. "Refresh category text properties in the buffer."
  5483. (let ((def-cat (cond
  5484. ((null org-category)
  5485. (if buffer-file-name
  5486. (file-name-sans-extension
  5487. (file-name-nondirectory buffer-file-name))
  5488. "???"))
  5489. ((symbolp org-category) (symbol-name org-category))
  5490. (t org-category)))
  5491. beg end cat pos optionp)
  5492. (org-unmodified
  5493. (save-excursion
  5494. (save-restriction
  5495. (widen)
  5496. (goto-char (point-min))
  5497. (put-text-property (point) (point-max) 'org-category def-cat)
  5498. (while (re-search-forward
  5499. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  5500. (setq pos (match-end 0)
  5501. optionp (equal (char-after (match-beginning 0)) ?#)
  5502. cat (org-trim (match-string 2)))
  5503. (if optionp
  5504. (setq beg (point-at-bol) end (point-max))
  5505. (org-back-to-heading t)
  5506. (setq beg (point) end (org-end-of-subtree t t)))
  5507. (put-text-property beg end 'org-category cat)
  5508. (goto-char pos)))))))
  5509. ;;;; Link Stuff
  5510. ;;; Link abbreviations
  5511. (defun org-link-expand-abbrev (link)
  5512. "Apply replacements as defined in `org-link-abbrev-alist."
  5513. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  5514. (let* ((key (match-string 1 link))
  5515. (as (or (assoc key org-link-abbrev-alist-local)
  5516. (assoc key org-link-abbrev-alist)))
  5517. (tag (and (match-end 2) (match-string 3 link)))
  5518. rpl)
  5519. (if (not as)
  5520. link
  5521. (setq rpl (cdr as))
  5522. (cond
  5523. ((symbolp rpl) (funcall rpl tag))
  5524. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  5525. (t (concat rpl tag)))))
  5526. link))
  5527. ;;; Storing and inserting links
  5528. (defvar org-insert-link-history nil
  5529. "Minibuffer history for links inserted with `org-insert-link'.")
  5530. (defvar org-stored-links nil
  5531. "Contains the links stored with `org-store-link'.")
  5532. (defvar org-store-link-plist nil
  5533. "Plist with info about the most recently link created with `org-store-link'.")
  5534. (defvar org-link-protocols nil
  5535. "Link protocols added to Org-mode using `org-add-link-type'.")
  5536. (defvar org-store-link-functions nil
  5537. "List of functions that are called to create and store a link.
  5538. Each function will be called in turn until one returns a non-nil
  5539. value. Each function should check if it is responsible for creating
  5540. this link (for example by looking at the major mode).
  5541. If not, it must exit and return nil.
  5542. If yes, it should return a non-nil value after a calling
  5543. `org-store-link-props' with a list of properties and values.
  5544. Special properties are:
  5545. :type The link prefix. like \"http\". This must be given.
  5546. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  5547. This is obligatory as well.
  5548. :description Optional default description for the second pair
  5549. of brackets in an Org-mode link. The user can still change
  5550. this when inserting this link into an Org-mode buffer.
  5551. In addition to these, any additional properties can be specified
  5552. and then used in remember templates.")
  5553. (defun org-add-link-type (type &optional follow export)
  5554. "Add TYPE to the list of `org-link-types'.
  5555. Re-compute all regular expressions depending on `org-link-types'
  5556. FOLLOW and EXPORT are two functions.
  5557. FOLLOW should take the link path as the single argument and do whatever
  5558. is necessary to follow the link, for example find a file or display
  5559. a mail message.
  5560. EXPORT should format the link path for export to one of the export formats.
  5561. It should be a function accepting three arguments:
  5562. path the path of the link, the text after the prefix (like \"http:\")
  5563. desc the description of the link, if any, nil if there was no descripton
  5564. format the export format, a symbol like `html' or `latex'.
  5565. The function may use the FORMAT information to return different values
  5566. depending on the format. The return value will be put literally into
  5567. the exported file.
  5568. Org-mode has a built-in default for exporting links. If you are happy with
  5569. this default, there is no need to define an export function for the link
  5570. type. For a simple example of an export function, see `org-bbdb.el'."
  5571. (add-to-list 'org-link-types type t)
  5572. (org-make-link-regexps)
  5573. (if (assoc type org-link-protocols)
  5574. (setcdr (assoc type org-link-protocols) (list follow export))
  5575. (push (list type follow export) org-link-protocols)))
  5576. ;;;###autoload
  5577. (defun org-store-link (arg)
  5578. "\\<org-mode-map>Store an org-link to the current location.
  5579. This link is added to `org-stored-links' and can later be inserted
  5580. into an org-buffer with \\[org-insert-link].
  5581. For some link types, a prefix arg is interpreted:
  5582. For links to usenet articles, arg negates `org-usenet-links-prefer-google'.
  5583. For file links, arg negates `org-context-in-file-links'."
  5584. (interactive "P")
  5585. (org-load-modules-maybe)
  5586. (setq org-store-link-plist nil) ; reset
  5587. (let (link cpltxt desc description search txt)
  5588. (cond
  5589. ((run-hook-with-args-until-success 'org-store-link-functions)
  5590. (setq link (plist-get org-store-link-plist :link)
  5591. desc (or (plist-get org-store-link-plist :description) link)))
  5592. ((eq major-mode 'calendar-mode)
  5593. (let ((cd (calendar-cursor-to-date)))
  5594. (setq link
  5595. (format-time-string
  5596. (car org-time-stamp-formats)
  5597. (apply 'encode-time
  5598. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  5599. nil nil nil))))
  5600. (org-store-link-props :type "calendar" :date cd)))
  5601. ((eq major-mode 'w3-mode)
  5602. (setq cpltxt (url-view-url t)
  5603. link (org-make-link cpltxt))
  5604. (org-store-link-props :type "w3" :url (url-view-url t)))
  5605. ((eq major-mode 'w3m-mode)
  5606. (setq cpltxt (or w3m-current-title w3m-current-url)
  5607. link (org-make-link w3m-current-url))
  5608. (org-store-link-props :type "w3m" :url (url-view-url t)))
  5609. ((setq search (run-hook-with-args-until-success
  5610. 'org-create-file-search-functions))
  5611. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  5612. "::" search))
  5613. (setq cpltxt (or description link)))
  5614. ((eq major-mode 'image-mode)
  5615. (setq cpltxt (concat "file:"
  5616. (abbreviate-file-name buffer-file-name))
  5617. link (org-make-link cpltxt))
  5618. (org-store-link-props :type "image" :file buffer-file-name))
  5619. ((eq major-mode 'dired-mode)
  5620. ;; link to the file in the current line
  5621. (setq cpltxt (concat "file:"
  5622. (abbreviate-file-name
  5623. (expand-file-name
  5624. (dired-get-filename nil t))))
  5625. link (org-make-link cpltxt)))
  5626. ((and buffer-file-name (org-mode-p))
  5627. ;; Just link to current headline
  5628. (setq cpltxt (concat "file:"
  5629. (abbreviate-file-name buffer-file-name)))
  5630. ;; Add a context search string
  5631. (when (org-xor org-context-in-file-links arg)
  5632. ;; Check if we are on a target
  5633. (if (org-in-regexp "<<\\(.*?\\)>>")
  5634. (setq cpltxt (concat cpltxt "::" (match-string 1)))
  5635. (setq txt (cond
  5636. ((org-on-heading-p) nil)
  5637. ((org-region-active-p)
  5638. (buffer-substring (region-beginning) (region-end)))
  5639. (t nil)))
  5640. (when (or (null txt) (string-match "\\S-" txt))
  5641. (setq cpltxt
  5642. (concat cpltxt "::"
  5643. (condition-case nil
  5644. (org-make-org-heading-search-string txt)
  5645. (error "")))
  5646. desc "NONE"))))
  5647. (if (string-match "::\\'" cpltxt)
  5648. (setq cpltxt (substring cpltxt 0 -2)))
  5649. (setq link (org-make-link cpltxt)))
  5650. ((buffer-file-name (buffer-base-buffer))
  5651. ;; Just link to this file here.
  5652. (setq cpltxt (concat "file:"
  5653. (abbreviate-file-name
  5654. (buffer-file-name (buffer-base-buffer)))))
  5655. ;; Add a context string
  5656. (when (org-xor org-context-in-file-links arg)
  5657. (setq txt (if (org-region-active-p)
  5658. (buffer-substring (region-beginning) (region-end))
  5659. (buffer-substring (point-at-bol) (point-at-eol))))
  5660. ;; Only use search option if there is some text.
  5661. (when (string-match "\\S-" txt)
  5662. (setq cpltxt
  5663. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  5664. desc "NONE")))
  5665. (setq link (org-make-link cpltxt)))
  5666. ((interactive-p)
  5667. (error "Cannot link to a buffer which is not visiting a file"))
  5668. (t (setq link nil)))
  5669. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  5670. (setq link (or link cpltxt)
  5671. desc (or desc cpltxt))
  5672. (if (equal desc "NONE") (setq desc nil))
  5673. (if (and (interactive-p) link)
  5674. (progn
  5675. (setq org-stored-links
  5676. (cons (list link desc) org-stored-links))
  5677. (message "Stored: %s" (or desc link)))
  5678. (and link (org-make-link-string link desc)))))
  5679. (defun org-store-link-props (&rest plist)
  5680. "Store link properties, extract names and addresses."
  5681. (let (x adr)
  5682. (when (setq x (plist-get plist :from))
  5683. (setq adr (mail-extract-address-components x))
  5684. (plist-put plist :fromname (car adr))
  5685. (plist-put plist :fromaddress (nth 1 adr)))
  5686. (when (setq x (plist-get plist :to))
  5687. (setq adr (mail-extract-address-components x))
  5688. (plist-put plist :toname (car adr))
  5689. (plist-put plist :toaddress (nth 1 adr))))
  5690. (let ((from (plist-get plist :from))
  5691. (to (plist-get plist :to)))
  5692. (when (and from to org-from-is-user-regexp)
  5693. (plist-put plist :fromto
  5694. (if (string-match org-from-is-user-regexp from)
  5695. (concat "to %t")
  5696. (concat "from %f")))))
  5697. (setq org-store-link-plist plist))
  5698. (defun org-add-link-props (&rest plist)
  5699. "Add these properties to the link property list."
  5700. (let (key value)
  5701. (while plist
  5702. (setq key (pop plist) value (pop plist))
  5703. (setq org-store-link-plist
  5704. (plist-put org-store-link-plist key value)))))
  5705. (defun org-email-link-description (&optional fmt)
  5706. "Return the description part of an email link.
  5707. This takes information from `org-store-link-plist' and formats it
  5708. according to FMT (default from `org-email-link-description-format')."
  5709. (setq fmt (or fmt org-email-link-description-format))
  5710. (let* ((p org-store-link-plist)
  5711. (to (plist-get p :toaddress))
  5712. (from (plist-get p :fromaddress))
  5713. (table
  5714. (list
  5715. (cons "%c" (plist-get p :fromto))
  5716. (cons "%F" (plist-get p :from))
  5717. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  5718. (cons "%T" (plist-get p :to))
  5719. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  5720. (cons "%s" (plist-get p :subject))
  5721. (cons "%m" (plist-get p :message-id)))))
  5722. (when (string-match "%c" fmt)
  5723. ;; Check if the user wrote this message
  5724. (if (and org-from-is-user-regexp from to
  5725. (save-match-data (string-match org-from-is-user-regexp from)))
  5726. (setq fmt (replace-match "to %t" t t fmt))
  5727. (setq fmt (replace-match "from %f" t t fmt))))
  5728. (org-replace-escapes fmt table)))
  5729. (defun org-make-org-heading-search-string (&optional string heading)
  5730. "Make search string for STRING or current headline."
  5731. (interactive)
  5732. (let ((s (or string (org-get-heading))))
  5733. (unless (and string (not heading))
  5734. ;; We are using a headline, clean up garbage in there.
  5735. (if (string-match org-todo-regexp s)
  5736. (setq s (replace-match "" t t s)))
  5737. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  5738. (setq s (replace-match "" t t s)))
  5739. (setq s (org-trim s))
  5740. (if (string-match (concat "^\\(" org-quote-string "\\|"
  5741. org-comment-string "\\)") s)
  5742. (setq s (replace-match "" t t s)))
  5743. (while (string-match org-ts-regexp s)
  5744. (setq s (replace-match "" t t s))))
  5745. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  5746. (setq s (replace-match " " t t s)))
  5747. (or string (setq s (concat "*" s))) ; Add * for headlines
  5748. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  5749. (defun org-make-link (&rest strings)
  5750. "Concatenate STRINGS."
  5751. (apply 'concat strings))
  5752. (defun org-make-link-string (link &optional description)
  5753. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  5754. (unless (string-match "\\S-" link)
  5755. (error "Empty link"))
  5756. (when (stringp description)
  5757. ;; Remove brackets from the description, they are fatal.
  5758. (while (string-match "\\[" description)
  5759. (setq description (replace-match "{" t t description)))
  5760. (while (string-match "\\]" description)
  5761. (setq description (replace-match "}" t t description))))
  5762. (when (equal (org-link-escape link) description)
  5763. ;; No description needed, it is identical
  5764. (setq description nil))
  5765. (when (and (not description)
  5766. (not (equal link (org-link-escape link))))
  5767. (setq description link))
  5768. (concat "[[" (org-link-escape link) "]"
  5769. (if description (concat "[" description "]") "")
  5770. "]"))
  5771. (defconst org-link-escape-chars
  5772. '((?\ . "%20")
  5773. (?\[ . "%5B")
  5774. (?\] . "%5D")
  5775. (?\340 . "%E0") ; `a
  5776. (?\342 . "%E2") ; ^a
  5777. (?\347 . "%E7") ; ,c
  5778. (?\350 . "%E8") ; `e
  5779. (?\351 . "%E9") ; 'e
  5780. (?\352 . "%EA") ; ^e
  5781. (?\356 . "%EE") ; ^i
  5782. (?\364 . "%F4") ; ^o
  5783. (?\371 . "%F9") ; `u
  5784. (?\373 . "%FB") ; ^u
  5785. (?\; . "%3B")
  5786. (?? . "%3F")
  5787. (?= . "%3D")
  5788. (?+ . "%2B")
  5789. )
  5790. "Association list of escapes for some characters problematic in links.
  5791. This is the list that is used for internal purposes.")
  5792. (defconst org-link-escape-chars-browser
  5793. '((?\ . "%20")) ; 32 for the SPC char
  5794. "Association list of escapes for some characters problematic in links.
  5795. This is the list that is used before handing over to the browser.")
  5796. (defun org-link-escape (text &optional table)
  5797. "Escape charaters in TEXT that are problematic for links."
  5798. (setq table (or table org-link-escape-chars))
  5799. (when text
  5800. (let ((re (mapconcat (lambda (x) (regexp-quote
  5801. (char-to-string (car x))))
  5802. table "\\|")))
  5803. (while (string-match re text)
  5804. (setq text
  5805. (replace-match
  5806. (cdr (assoc (string-to-char (match-string 0 text))
  5807. table))
  5808. t t text)))
  5809. text)))
  5810. (defun org-link-unescape (text &optional table)
  5811. "Reverse the action of `org-link-escape'."
  5812. (setq table (or table org-link-escape-chars))
  5813. (when text
  5814. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  5815. table "\\|")))
  5816. (while (string-match re text)
  5817. (setq text
  5818. (replace-match
  5819. (char-to-string (car (rassoc (match-string 0 text) table)))
  5820. t t text)))
  5821. text)))
  5822. (defun org-xor (a b)
  5823. "Exclusive or."
  5824. (if a (not b) b))
  5825. (defun org-get-header (header)
  5826. "Find a header field in the current buffer."
  5827. (save-excursion
  5828. (goto-char (point-min))
  5829. (let ((case-fold-search t) s)
  5830. (cond
  5831. ((eq header 'from)
  5832. (if (re-search-forward "^From:\\s-+\\(.*\\)" nil t)
  5833. (setq s (match-string 1)))
  5834. (while (string-match "\"" s)
  5835. (setq s (replace-match "" t t s)))
  5836. (if (string-match "[<(].*" s)
  5837. (setq s (replace-match "" t t s))))
  5838. ((eq header 'message-id)
  5839. (if (re-search-forward "^message-id:\\s-+\\(.*\\)" nil t)
  5840. (setq s (match-string 1))))
  5841. ((eq header 'subject)
  5842. (if (re-search-forward "^subject:\\s-+\\(.*\\)" nil t)
  5843. (setq s (match-string 1)))))
  5844. (if (string-match "\\`[ \t\]+" s) (setq s (replace-match "" t t s)))
  5845. (if (string-match "[ \t\]+\\'" s) (setq s (replace-match "" t t s)))
  5846. s)))
  5847. (defun org-fixup-message-id-for-http (s)
  5848. "Replace special characters in a message id, so it can be used in an http query."
  5849. (while (string-match "<" s)
  5850. (setq s (replace-match "%3C" t t s)))
  5851. (while (string-match ">" s)
  5852. (setq s (replace-match "%3E" t t s)))
  5853. (while (string-match "@" s)
  5854. (setq s (replace-match "%40" t t s)))
  5855. s)
  5856. ;;;###autoload
  5857. (defun org-insert-link-global ()
  5858. "Insert a link like Org-mode does.
  5859. This command can be called in any mode to insert a link in Org-mode syntax."
  5860. (interactive)
  5861. (org-load-modules-maybe)
  5862. (org-run-like-in-org-mode 'org-insert-link))
  5863. (defun org-insert-link (&optional complete-file link-location)
  5864. "Insert a link. At the prompt, enter the link.
  5865. Completion can be used to select a link previously stored with
  5866. `org-store-link'. When the empty string is entered (i.e. if you just
  5867. press RET at the prompt), the link defaults to the most recently
  5868. stored link. As SPC triggers completion in the minibuffer, you need to
  5869. use M-SPC or C-q SPC to force the insertion of a space character.
  5870. You will also be prompted for a description, and if one is given, it will
  5871. be displayed in the buffer instead of the link.
  5872. If there is already a link at point, this command will allow you to edit link
  5873. and description parts.
  5874. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  5875. be selected using completion. The path to the file will be relative to the
  5876. current directory if the file is in the current directory or a subdirectory.
  5877. Otherwise, the link will be the absolute path as completed in the minibuffer
  5878. \(i.e. normally ~/path/to/file).
  5879. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  5880. the current directory or below. With three \\[universal-argument] prefixes, negate the meaning
  5881. of `org-keep-stored-link-after-insertion'.
  5882. If `org-make-link-description-function' is non-nil, this function will be
  5883. called with the link target, and the result will be the default
  5884. link description.
  5885. If the LINK-LOCATION parameter is non-nil, this value will be
  5886. used as the link location instead of reading one interactively."
  5887. (interactive "P")
  5888. (let* ((wcf (current-window-configuration))
  5889. (region (if (org-region-active-p)
  5890. (buffer-substring (region-beginning) (region-end))))
  5891. (remove (and region (list (region-beginning) (region-end))))
  5892. (desc region)
  5893. tmphist ; byte-compile incorrectly complains about this
  5894. (link link-location)
  5895. entry file)
  5896. (cond
  5897. (link-location) ; specified by arg, just use it.
  5898. ((org-in-regexp org-bracket-link-regexp 1)
  5899. ;; We do have a link at point, and we are going to edit it.
  5900. (setq remove (list (match-beginning 0) (match-end 0)))
  5901. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  5902. (setq link (read-string "Link: "
  5903. (org-link-unescape
  5904. (org-match-string-no-properties 1)))))
  5905. ((or (org-in-regexp org-angle-link-re)
  5906. (org-in-regexp org-plain-link-re))
  5907. ;; Convert to bracket link
  5908. (setq remove (list (match-beginning 0) (match-end 0))
  5909. link (read-string "Link: "
  5910. (org-remove-angle-brackets (match-string 0)))))
  5911. ((equal complete-file '(4))
  5912. ;; Completing read for file names.
  5913. (setq file (read-file-name "File: "))
  5914. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  5915. (pwd1 (file-name-as-directory (abbreviate-file-name
  5916. (expand-file-name ".")))))
  5917. (cond
  5918. ((equal complete-file '(16))
  5919. (setq link (org-make-link
  5920. "file:"
  5921. (abbreviate-file-name (expand-file-name file)))))
  5922. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  5923. (setq link (org-make-link "file:" (match-string 1 file))))
  5924. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  5925. (expand-file-name file))
  5926. (setq link (org-make-link
  5927. "file:" (match-string 1 (expand-file-name file)))))
  5928. (t (setq link (org-make-link "file:" file))))))
  5929. (t
  5930. ;; Read link, with completion for stored links.
  5931. (with-output-to-temp-buffer "*Org Links*"
  5932. (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
  5933. (when org-stored-links
  5934. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  5935. (princ (mapconcat
  5936. (lambda (x)
  5937. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  5938. (reverse org-stored-links) "\n"))))
  5939. (let ((cw (selected-window)))
  5940. (select-window (get-buffer-window "*Org Links*"))
  5941. (shrink-window-if-larger-than-buffer)
  5942. (setq truncate-lines t)
  5943. (select-window cw))
  5944. ;; Fake a link history, containing the stored links.
  5945. (setq tmphist (append (mapcar 'car org-stored-links)
  5946. org-insert-link-history))
  5947. (unwind-protect
  5948. (setq link (org-completing-read
  5949. "Link: "
  5950. (append
  5951. (mapcar (lambda (x) (list (concat (car x) ":")))
  5952. (append org-link-abbrev-alist-local org-link-abbrev-alist))
  5953. (mapcar (lambda (x) (list (concat x ":")))
  5954. org-link-types))
  5955. nil nil nil
  5956. 'tmphist
  5957. (or (car (car org-stored-links)))))
  5958. (set-window-configuration wcf)
  5959. (kill-buffer "*Org Links*"))
  5960. (setq entry (assoc link org-stored-links))
  5961. (or entry (push link org-insert-link-history))
  5962. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  5963. (not org-keep-stored-link-after-insertion))
  5964. (setq org-stored-links (delq (assoc link org-stored-links)
  5965. org-stored-links)))
  5966. (setq desc (or desc (nth 1 entry)))))
  5967. (if (string-match org-plain-link-re link)
  5968. ;; URL-like link, normalize the use of angular brackets.
  5969. (setq link (org-make-link (org-remove-angle-brackets link))))
  5970. ;; Check if we are linking to the current file with a search option
  5971. ;; If yes, simplify the link by using only the search option.
  5972. (when (and buffer-file-name
  5973. (string-match "\\<file:\\(.+?\\)::\\([^>]+\\)" link))
  5974. (let* ((path (match-string 1 link))
  5975. (case-fold-search nil)
  5976. (search (match-string 2 link)))
  5977. (save-match-data
  5978. (if (equal (file-truename buffer-file-name) (file-truename path))
  5979. ;; We are linking to this same file, with a search option
  5980. (setq link search)))))
  5981. ;; Check if we can/should use a relative path. If yes, simplify the link
  5982. (when (string-match "\\<file:\\(.*\\)" link)
  5983. (let* ((path (match-string 1 link))
  5984. (origpath path)
  5985. (case-fold-search nil))
  5986. (cond
  5987. ((eq org-link-file-path-type 'absolute)
  5988. (setq path (abbreviate-file-name (expand-file-name path))))
  5989. ((eq org-link-file-path-type 'noabbrev)
  5990. (setq path (expand-file-name path)))
  5991. ((eq org-link-file-path-type 'relative)
  5992. (setq path (file-relative-name path)))
  5993. (t
  5994. (save-match-data
  5995. (if (string-match (concat "^" (regexp-quote
  5996. (file-name-as-directory
  5997. (expand-file-name "."))))
  5998. (expand-file-name path))
  5999. ;; We are linking a file with relative path name.
  6000. (setq path (substring (expand-file-name path)
  6001. (match-end 0)))))))
  6002. (setq link (concat "file:" path))
  6003. (if (equal desc origpath)
  6004. (setq desc path))))
  6005. (if org-make-link-description-function
  6006. (setq desc (funcall org-make-link-description-function link desc)))
  6007. (setq desc (read-string "Description: " desc))
  6008. (unless (string-match "\\S-" desc) (setq desc nil))
  6009. (if remove (apply 'delete-region remove))
  6010. (insert (org-make-link-string link desc))))
  6011. (defun org-completing-read (&rest args)
  6012. (let ((minibuffer-local-completion-map
  6013. (copy-keymap minibuffer-local-completion-map)))
  6014. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  6015. (apply 'completing-read args)))
  6016. ;;; Opening/following a link
  6017. (defvar org-link-search-failed nil)
  6018. (defun org-next-link ()
  6019. "Move forward to the next link.
  6020. If the link is in hidden text, expose it."
  6021. (interactive)
  6022. (when (and org-link-search-failed (eq this-command last-command))
  6023. (goto-char (point-min))
  6024. (message "Link search wrapped back to beginning of buffer"))
  6025. (setq org-link-search-failed nil)
  6026. (let* ((pos (point))
  6027. (ct (org-context))
  6028. (a (assoc :link ct)))
  6029. (if a (goto-char (nth 2 a)))
  6030. (if (re-search-forward org-any-link-re nil t)
  6031. (progn
  6032. (goto-char (match-beginning 0))
  6033. (if (org-invisible-p) (org-show-context)))
  6034. (goto-char pos)
  6035. (setq org-link-search-failed t)
  6036. (error "No further link found"))))
  6037. (defun org-previous-link ()
  6038. "Move backward to the previous link.
  6039. If the link is in hidden text, expose it."
  6040. (interactive)
  6041. (when (and org-link-search-failed (eq this-command last-command))
  6042. (goto-char (point-max))
  6043. (message "Link search wrapped back to end of buffer"))
  6044. (setq org-link-search-failed nil)
  6045. (let* ((pos (point))
  6046. (ct (org-context))
  6047. (a (assoc :link ct)))
  6048. (if a (goto-char (nth 1 a)))
  6049. (if (re-search-backward org-any-link-re nil t)
  6050. (progn
  6051. (goto-char (match-beginning 0))
  6052. (if (org-invisible-p) (org-show-context)))
  6053. (goto-char pos)
  6054. (setq org-link-search-failed t)
  6055. (error "No further link found"))))
  6056. (defun org-find-file-at-mouse (ev)
  6057. "Open file link or URL at mouse."
  6058. (interactive "e")
  6059. (mouse-set-point ev)
  6060. (org-open-at-point 'in-emacs))
  6061. (defun org-open-at-mouse (ev)
  6062. "Open file link or URL at mouse."
  6063. (interactive "e")
  6064. (mouse-set-point ev)
  6065. (org-open-at-point))
  6066. (defvar org-window-config-before-follow-link nil
  6067. "The window configuration before following a link.
  6068. This is saved in case the need arises to restore it.")
  6069. (defvar org-open-link-marker (make-marker)
  6070. "Marker pointing to the location where `org-open-at-point; was called.")
  6071. ;;;###autoload
  6072. (defun org-open-at-point-global ()
  6073. "Follow a link like Org-mode does.
  6074. This command can be called in any mode to follow a link that has
  6075. Org-mode syntax."
  6076. (interactive)
  6077. (org-run-like-in-org-mode 'org-open-at-point))
  6078. ;;;###autoload
  6079. (defun org-open-link-from-string (s &optional arg)
  6080. "Open a link in the string S, as if it was in Org-mode."
  6081. (interactive "sLink: \nP")
  6082. (with-temp-buffer
  6083. (let ((org-inhibit-startup t))
  6084. (org-mode)
  6085. (insert s)
  6086. (goto-char (point-min))
  6087. (org-open-at-point arg))))
  6088. (defun org-open-at-point (&optional in-emacs)
  6089. "Open link at or after point.
  6090. If there is no link at point, this function will search forward up to
  6091. the end of the current subtree.
  6092. Normally, files will be opened by an appropriate application. If the
  6093. optional argument IN-EMACS is non-nil, Emacs will visit the file."
  6094. (interactive "P")
  6095. (org-load-modules-maybe)
  6096. (move-marker org-open-link-marker (point))
  6097. (setq org-window-config-before-follow-link (current-window-configuration))
  6098. (org-remove-occur-highlights nil nil t)
  6099. (if (org-at-timestamp-p t)
  6100. (org-follow-timestamp-link)
  6101. (let (type path link line search (pos (point)))
  6102. (catch 'match
  6103. (save-excursion
  6104. (skip-chars-forward "^]\n\r")
  6105. (when (org-in-regexp org-bracket-link-regexp)
  6106. (setq link (org-link-unescape (org-match-string-no-properties 1)))
  6107. (while (string-match " *\n *" link)
  6108. (setq link (replace-match " " t t link)))
  6109. (setq link (org-link-expand-abbrev link))
  6110. (if (string-match org-link-re-with-space2 link)
  6111. (setq type (match-string 1 link) path (match-string 2 link))
  6112. (setq type "thisfile" path link))
  6113. (throw 'match t)))
  6114. (when (get-text-property (point) 'org-linked-text)
  6115. (setq type "thisfile"
  6116. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  6117. (1+ (point)) (point))
  6118. path (buffer-substring
  6119. (previous-single-property-change pos 'org-linked-text)
  6120. (next-single-property-change pos 'org-linked-text)))
  6121. (throw 'match t))
  6122. (save-excursion
  6123. (when (or (org-in-regexp org-angle-link-re)
  6124. (org-in-regexp org-plain-link-re))
  6125. (setq type (match-string 1) path (match-string 2))
  6126. (throw 'match t)))
  6127. (when (org-in-regexp "\\<\\([^><\n]+\\)\\>")
  6128. (setq type "tree-match"
  6129. path (match-string 1))
  6130. (throw 'match t))
  6131. (save-excursion
  6132. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  6133. (setq type "tags"
  6134. path (match-string 1))
  6135. (while (string-match ":" path)
  6136. (setq path (replace-match "+" t t path)))
  6137. (throw 'match t))))
  6138. (unless path
  6139. (error "No link found"))
  6140. ;; Remove any trailing spaces in path
  6141. (if (string-match " +\\'" path)
  6142. (setq path (replace-match "" t t path)))
  6143. (cond
  6144. ((assoc type org-link-protocols)
  6145. (funcall (nth 1 (assoc type org-link-protocols)) path))
  6146. ((equal type "mailto")
  6147. (let ((cmd (car org-link-mailto-program))
  6148. (args (cdr org-link-mailto-program)) args1
  6149. (address path) (subject "") a)
  6150. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  6151. (setq address (match-string 1 path)
  6152. subject (org-link-escape (match-string 2 path))))
  6153. (while args
  6154. (cond
  6155. ((not (stringp (car args))) (push (pop args) args1))
  6156. (t (setq a (pop args))
  6157. (if (string-match "%a" a)
  6158. (setq a (replace-match address t t a)))
  6159. (if (string-match "%s" a)
  6160. (setq a (replace-match subject t t a)))
  6161. (push a args1))))
  6162. (apply cmd (nreverse args1))))
  6163. ((member type '("http" "https" "ftp" "news"))
  6164. (browse-url (concat type ":" (org-link-escape
  6165. path org-link-escape-chars-browser))))
  6166. ((member type '("message"))
  6167. (browse-url (concat type ":" path)))
  6168. ((string= type "tags")
  6169. (org-tags-view in-emacs path))
  6170. ((string= type "thisfile")
  6171. (if in-emacs
  6172. (switch-to-buffer-other-window
  6173. (org-get-buffer-for-internal-link (current-buffer)))
  6174. (org-mark-ring-push))
  6175. (let ((cmd `(org-link-search
  6176. ,path
  6177. ,(cond ((equal in-emacs '(4)) 'occur)
  6178. ((equal in-emacs '(16)) 'org-occur)
  6179. (t nil))
  6180. ,pos)))
  6181. (condition-case nil (eval cmd)
  6182. (error (progn (widen) (eval cmd))))))
  6183. ((string= type "tree-match")
  6184. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  6185. ((string= type "file")
  6186. (if (string-match "::\\([0-9]+\\)\\'" path)
  6187. (setq line (string-to-number (match-string 1 path))
  6188. path (substring path 0 (match-beginning 0)))
  6189. (if (string-match "::\\(.+\\)\\'" path)
  6190. (setq search (match-string 1 path)
  6191. path (substring path 0 (match-beginning 0)))))
  6192. (if (string-match "[*?{]" (file-name-nondirectory path))
  6193. (dired path)
  6194. (org-open-file path in-emacs line search)))
  6195. ((string= type "news")
  6196. (require 'org-gnus)
  6197. (org-gnus-follow-link path))
  6198. ((string= type "shell")
  6199. (let ((cmd path))
  6200. (if (or (not org-confirm-shell-link-function)
  6201. (funcall org-confirm-shell-link-function
  6202. (format "Execute \"%s\" in shell? "
  6203. (org-add-props cmd nil
  6204. 'face 'org-warning))))
  6205. (progn
  6206. (message "Executing %s" cmd)
  6207. (shell-command cmd))
  6208. (error "Abort"))))
  6209. ((string= type "elisp")
  6210. (let ((cmd path))
  6211. (if (or (not org-confirm-elisp-link-function)
  6212. (funcall org-confirm-elisp-link-function
  6213. (format "Execute \"%s\" as elisp? "
  6214. (org-add-props cmd nil
  6215. 'face 'org-warning))))
  6216. (message "%s => %s" cmd (eval (read cmd)))
  6217. (error "Abort"))))
  6218. (t
  6219. (browse-url-at-point)))))
  6220. (move-marker org-open-link-marker nil)
  6221. (run-hook-with-args 'org-follow-link-hook))
  6222. ;;;; Time estimates
  6223. (defun org-get-effort (&optional pom)
  6224. "Get the effort estimate for the current entry."
  6225. (org-entry-get pom org-effort-property))
  6226. ;;; File search
  6227. (defvar org-create-file-search-functions nil
  6228. "List of functions to construct the right search string for a file link.
  6229. These functions are called in turn with point at the location to
  6230. which the link should point.
  6231. A function in the hook should first test if it would like to
  6232. handle this file type, for example by checking the major-mode or
  6233. the file extension. If it decides not to handle this file, it
  6234. should just return nil to give other functions a chance. If it
  6235. does handle the file, it must return the search string to be used
  6236. when following the link. The search string will be part of the
  6237. file link, given after a double colon, and `org-open-at-point'
  6238. will automatically search for it. If special measures must be
  6239. taken to make the search successful, another function should be
  6240. added to the companion hook `org-execute-file-search-functions',
  6241. which see.
  6242. A function in this hook may also use `setq' to set the variable
  6243. `description' to provide a suggestion for the descriptive text to
  6244. be used for this link when it gets inserted into an Org-mode
  6245. buffer with \\[org-insert-link].")
  6246. (defvar org-execute-file-search-functions nil
  6247. "List of functions to execute a file search triggered by a link.
  6248. Functions added to this hook must accept a single argument, the
  6249. search string that was part of the file link, the part after the
  6250. double colon. The function must first check if it would like to
  6251. handle this search, for example by checking the major-mode or the
  6252. file extension. If it decides not to handle this search, it
  6253. should just return nil to give other functions a chance. If it
  6254. does handle the search, it must return a non-nil value to keep
  6255. other functions from trying.
  6256. Each function can access the current prefix argument through the
  6257. variable `current-prefix-argument'. Note that a single prefix is
  6258. used to force opening a link in Emacs, so it may be good to only
  6259. use a numeric or double prefix to guide the search function.
  6260. In case this is needed, a function in this hook can also restore
  6261. the window configuration before `org-open-at-point' was called using:
  6262. (set-window-configuration org-window-config-before-follow-link)")
  6263. (defun org-link-search (s &optional type avoid-pos)
  6264. "Search for a link search option.
  6265. If S is surrounded by forward slashes, it is interpreted as a
  6266. regular expression. In org-mode files, this will create an `org-occur'
  6267. sparse tree. In ordinary files, `occur' will be used to list matches.
  6268. If the current buffer is in `dired-mode', grep will be used to search
  6269. in all files. If AVOID-POS is given, ignore matches near that position."
  6270. (let ((case-fold-search t)
  6271. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  6272. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  6273. (append '(("") (" ") ("\t") ("\n"))
  6274. org-emphasis-alist)
  6275. "\\|") "\\)"))
  6276. (pos (point))
  6277. (pre nil) (post nil)
  6278. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  6279. (cond
  6280. ;; First check if there are any special
  6281. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  6282. ;; Now try the builtin stuff
  6283. ((save-excursion
  6284. (goto-char (point-min))
  6285. (and
  6286. (re-search-forward
  6287. (concat "<<" (regexp-quote s0) ">>") nil t)
  6288. (setq type 'dedicated
  6289. pos (match-beginning 0))))
  6290. ;; There is an exact target for this
  6291. (goto-char pos))
  6292. ((string-match "^/\\(.*\\)/$" s)
  6293. ;; A regular expression
  6294. (cond
  6295. ((org-mode-p)
  6296. (org-occur (match-string 1 s)))
  6297. ;;((eq major-mode 'dired-mode)
  6298. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  6299. (t (org-do-occur (match-string 1 s)))))
  6300. (t
  6301. ;; A normal search strings
  6302. (when (equal (string-to-char s) ?*)
  6303. ;; Anchor on headlines, post may include tags.
  6304. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  6305. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  6306. s (substring s 1)))
  6307. (remove-text-properties
  6308. 0 (length s)
  6309. '(face nil mouse-face nil keymap nil fontified nil) s)
  6310. ;; Make a series of regular expressions to find a match
  6311. (setq words (org-split-string s "[ \n\r\t]+")
  6312. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  6313. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  6314. "\\)" markers)
  6315. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  6316. re2a (concat "[ \t\r\n]" re2a_)
  6317. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  6318. re4 (concat "[^a-zA-Z_]" re4_)
  6319. re1 (concat pre re2 post)
  6320. re3 (concat pre (if pre re4_ re4) post)
  6321. re5 (concat pre ".*" re4)
  6322. re2 (concat pre re2)
  6323. re2a (concat pre (if pre re2a_ re2a))
  6324. re4 (concat pre (if pre re4_ re4))
  6325. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  6326. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  6327. re5 "\\)"
  6328. ))
  6329. (cond
  6330. ((eq type 'org-occur) (org-occur reall))
  6331. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  6332. (t (goto-char (point-min))
  6333. (setq type 'fuzzy)
  6334. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  6335. (org-search-not-self 1 re1 nil t)
  6336. (org-search-not-self 1 re2 nil t)
  6337. (org-search-not-self 1 re2a nil t)
  6338. (org-search-not-self 1 re3 nil t)
  6339. (org-search-not-self 1 re4 nil t)
  6340. (org-search-not-self 1 re5 nil t)
  6341. )
  6342. (goto-char (match-beginning 1))
  6343. (goto-char pos)
  6344. (error "No match")))))
  6345. (t
  6346. ;; Normal string-search
  6347. (goto-char (point-min))
  6348. (if (search-forward s nil t)
  6349. (goto-char (match-beginning 0))
  6350. (error "No match"))))
  6351. (and (org-mode-p) (org-show-context 'link-search))
  6352. type))
  6353. (defun org-search-not-self (group &rest args)
  6354. "Execute `re-search-forward', but only accept matches that do not
  6355. enclose the position of `org-open-link-marker'."
  6356. (let ((m org-open-link-marker))
  6357. (catch 'exit
  6358. (while (apply 're-search-forward args)
  6359. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  6360. (goto-char (match-end group))
  6361. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  6362. (> (match-beginning 0) (marker-position m))
  6363. (< (match-end 0) (marker-position m)))
  6364. (save-match-data
  6365. (or (not (org-in-regexp
  6366. org-bracket-link-analytic-regexp 1))
  6367. (not (match-end 4)) ; no description
  6368. (and (<= (match-beginning 4) (point))
  6369. (>= (match-end 4) (point))))))
  6370. (throw 'exit (point))))))))
  6371. (defun org-get-buffer-for-internal-link (buffer)
  6372. "Return a buffer to be used for displaying the link target of internal links."
  6373. (cond
  6374. ((not org-display-internal-link-with-indirect-buffer)
  6375. buffer)
  6376. ((string-match "(Clone)$" (buffer-name buffer))
  6377. (message "Buffer is already a clone, not making another one")
  6378. ;; we also do not modify visibility in this case
  6379. buffer)
  6380. (t ; make a new indirect buffer for displaying the link
  6381. (let* ((bn (buffer-name buffer))
  6382. (ibn (concat bn "(Clone)"))
  6383. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  6384. (with-current-buffer ib (org-overview))
  6385. ib))))
  6386. (defun org-do-occur (regexp &optional cleanup)
  6387. "Call the Emacs command `occur'.
  6388. If CLEANUP is non-nil, remove the printout of the regular expression
  6389. in the *Occur* buffer. This is useful if the regex is long and not useful
  6390. to read."
  6391. (occur regexp)
  6392. (when cleanup
  6393. (let ((cwin (selected-window)) win beg end)
  6394. (when (setq win (get-buffer-window "*Occur*"))
  6395. (select-window win))
  6396. (goto-char (point-min))
  6397. (when (re-search-forward "match[a-z]+" nil t)
  6398. (setq beg (match-end 0))
  6399. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  6400. (setq end (1- (match-beginning 0)))))
  6401. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  6402. (goto-char (point-min))
  6403. (select-window cwin))))
  6404. ;;; The mark ring for links jumps
  6405. (defvar org-mark-ring nil
  6406. "Mark ring for positions before jumps in Org-mode.")
  6407. (defvar org-mark-ring-last-goto nil
  6408. "Last position in the mark ring used to go back.")
  6409. ;; Fill and close the ring
  6410. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  6411. (loop for i from 1 to org-mark-ring-length do
  6412. (push (make-marker) org-mark-ring))
  6413. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  6414. org-mark-ring)
  6415. (defun org-mark-ring-push (&optional pos buffer)
  6416. "Put the current position or POS into the mark ring and rotate it."
  6417. (interactive)
  6418. (setq pos (or pos (point)))
  6419. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  6420. (move-marker (car org-mark-ring)
  6421. (or pos (point))
  6422. (or buffer (current-buffer)))
  6423. (message "%s"
  6424. (substitute-command-keys
  6425. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  6426. (defun org-mark-ring-goto (&optional n)
  6427. "Jump to the previous position in the mark ring.
  6428. With prefix arg N, jump back that many stored positions. When
  6429. called several times in succession, walk through the entire ring.
  6430. Org-mode commands jumping to a different position in the current file,
  6431. or to another Org-mode file, automatically push the old position
  6432. onto the ring."
  6433. (interactive "p")
  6434. (let (p m)
  6435. (if (eq last-command this-command)
  6436. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  6437. (setq p org-mark-ring))
  6438. (setq org-mark-ring-last-goto p)
  6439. (setq m (car p))
  6440. (switch-to-buffer (marker-buffer m))
  6441. (goto-char m)
  6442. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  6443. (defun org-remove-angle-brackets (s)
  6444. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  6445. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  6446. s)
  6447. (defun org-add-angle-brackets (s)
  6448. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  6449. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  6450. s)
  6451. ;;; Following specific links
  6452. (defun org-follow-timestamp-link ()
  6453. (cond
  6454. ((org-at-date-range-p t)
  6455. (let ((org-agenda-start-on-weekday)
  6456. (t1 (match-string 1))
  6457. (t2 (match-string 2)))
  6458. (setq t1 (time-to-days (org-time-string-to-time t1))
  6459. t2 (time-to-days (org-time-string-to-time t2)))
  6460. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  6461. ((org-at-timestamp-p t)
  6462. (org-agenda-list nil (time-to-days (org-time-string-to-time
  6463. (substring (match-string 1) 0 10)))
  6464. 1))
  6465. (t (error "This should not happen"))))
  6466. ;;; Following file links
  6467. (defvar org-wait nil)
  6468. (defun org-open-file (path &optional in-emacs line search)
  6469. "Open the file at PATH.
  6470. First, this expands any special file name abbreviations. Then the
  6471. configuration variable `org-file-apps' is checked if it contains an
  6472. entry for this file type, and if yes, the corresponding command is launched.
  6473. If no application is found, Emacs simply visits the file.
  6474. With optional argument IN-EMACS, Emacs will visit the file.
  6475. Optional LINE specifies a line to go to, optional SEARCH a string to
  6476. search for. If LINE or SEARCH is given, the file will always be
  6477. opened in Emacs.
  6478. If the file does not exist, an error is thrown."
  6479. (setq in-emacs (or in-emacs line search))
  6480. (let* ((file (if (equal path "")
  6481. buffer-file-name
  6482. (substitute-in-file-name (expand-file-name path))))
  6483. (apps (append org-file-apps (org-default-apps)))
  6484. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  6485. (dirp (if remp nil (file-directory-p file)))
  6486. (dfile (downcase file))
  6487. (old-buffer (current-buffer))
  6488. (old-pos (point))
  6489. (old-mode major-mode)
  6490. ext cmd)
  6491. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  6492. (setq ext (match-string 1 dfile))
  6493. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  6494. (setq ext (match-string 1 dfile))))
  6495. (if in-emacs
  6496. (setq cmd 'emacs)
  6497. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  6498. (and dirp (cdr (assoc 'directory apps)))
  6499. (cdr (assoc ext apps))
  6500. (cdr (assoc t apps)))))
  6501. (when (eq cmd 'mailcap)
  6502. (require 'mailcap)
  6503. (mailcap-parse-mailcaps)
  6504. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  6505. (command (mailcap-mime-info mime-type)))
  6506. (if (stringp command)
  6507. (setq cmd command)
  6508. (setq cmd 'emacs))))
  6509. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  6510. (not (file-exists-p file))
  6511. (not org-open-non-existing-files))
  6512. (error "No such file: %s" file))
  6513. (cond
  6514. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  6515. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  6516. (while (string-match "['\"]%s['\"]" cmd)
  6517. (setq cmd (replace-match "%s" t t cmd)))
  6518. (while (string-match "%s" cmd)
  6519. (setq cmd (replace-match
  6520. (save-match-data (shell-quote-argument file))
  6521. t t cmd)))
  6522. (save-window-excursion
  6523. (start-process-shell-command cmd nil cmd)
  6524. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  6525. ))
  6526. ((or (stringp cmd)
  6527. (eq cmd 'emacs))
  6528. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  6529. (widen)
  6530. (if line (goto-line line)
  6531. (if search (org-link-search search))))
  6532. ((consp cmd)
  6533. (eval cmd))
  6534. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  6535. (and (org-mode-p) (eq old-mode 'org-mode)
  6536. (or (not (equal old-buffer (current-buffer)))
  6537. (not (equal old-pos (point))))
  6538. (org-mark-ring-push old-pos old-buffer))))
  6539. (defun org-default-apps ()
  6540. "Return the default applications for this operating system."
  6541. (cond
  6542. ((eq system-type 'darwin)
  6543. org-file-apps-defaults-macosx)
  6544. ((eq system-type 'windows-nt)
  6545. org-file-apps-defaults-windowsnt)
  6546. (t org-file-apps-defaults-gnu)))
  6547. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  6548. (defun org-file-remote-p (file)
  6549. "Test whether FILE specifies a location on a remote system.
  6550. Return non-nil if the location is indeed remote.
  6551. For example, the filename \"/user@host:/foo\" specifies a location
  6552. on the system \"/user@host:\"."
  6553. (cond ((fboundp 'file-remote-p)
  6554. (file-remote-p file))
  6555. ((fboundp 'tramp-handle-file-remote-p)
  6556. (tramp-handle-file-remote-p file))
  6557. ((and (boundp 'ange-ftp-name-format)
  6558. (string-match (car ange-ftp-name-format) file))
  6559. t)
  6560. (t nil)))
  6561. ;;;; Refiling
  6562. (defun org-get-org-file ()
  6563. "Read a filename, with default directory `org-directory'."
  6564. (let ((default (or org-default-notes-file remember-data-file)))
  6565. (read-file-name (format "File name [%s]: " default)
  6566. (file-name-as-directory org-directory)
  6567. default)))
  6568. (defun org-notes-order-reversed-p ()
  6569. "Check if the current file should receive notes in reversed order."
  6570. (cond
  6571. ((not org-reverse-note-order) nil)
  6572. ((eq t org-reverse-note-order) t)
  6573. ((not (listp org-reverse-note-order)) nil)
  6574. (t (catch 'exit
  6575. (let ((all org-reverse-note-order)
  6576. entry)
  6577. (while (setq entry (pop all))
  6578. (if (string-match (car entry) buffer-file-name)
  6579. (throw 'exit (cdr entry))))
  6580. nil)))))
  6581. (defvar org-refile-target-table nil
  6582. "The list of refile targets, created by `org-refile'.")
  6583. (defvar org-agenda-new-buffers nil
  6584. "Buffers created to visit agenda files.")
  6585. (defun org-get-refile-targets (&optional default-buffer)
  6586. "Produce a table with refile targets."
  6587. (let ((entries (or org-refile-targets '((nil . (:level . 1)))))
  6588. targets txt re files f desc descre)
  6589. (with-current-buffer (or default-buffer (current-buffer))
  6590. (while (setq entry (pop entries))
  6591. (setq files (car entry) desc (cdr entry))
  6592. (cond
  6593. ((null files) (setq files (list (current-buffer))))
  6594. ((eq files 'org-agenda-files)
  6595. (setq files (org-agenda-files 'unrestricted)))
  6596. ((and (symbolp files) (fboundp files))
  6597. (setq files (funcall files)))
  6598. ((and (symbolp files) (boundp files))
  6599. (setq files (symbol-value files))))
  6600. (if (stringp files) (setq files (list files)))
  6601. (cond
  6602. ((eq (car desc) :tag)
  6603. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  6604. ((eq (car desc) :todo)
  6605. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  6606. ((eq (car desc) :regexp)
  6607. (setq descre (cdr desc)))
  6608. ((eq (car desc) :level)
  6609. (setq descre (concat "^\\*\\{" (number-to-string
  6610. (if org-odd-levels-only
  6611. (1- (* 2 (cdr desc)))
  6612. (cdr desc)))
  6613. "\\}[ \t]")))
  6614. ((eq (car desc) :maxlevel)
  6615. (setq descre (concat "^\\*\\{1," (number-to-string
  6616. (if org-odd-levels-only
  6617. (1- (* 2 (cdr desc)))
  6618. (cdr desc)))
  6619. "\\}[ \t]")))
  6620. (t (error "Bad refiling target description %s" desc)))
  6621. (while (setq f (pop files))
  6622. (save-excursion
  6623. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  6624. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  6625. (save-excursion
  6626. (save-restriction
  6627. (widen)
  6628. (goto-char (point-min))
  6629. (while (re-search-forward descre nil t)
  6630. (goto-char (point-at-bol))
  6631. (when (looking-at org-complex-heading-regexp)
  6632. (setq txt (match-string 4)
  6633. re (concat "^" (regexp-quote
  6634. (buffer-substring (match-beginning 1)
  6635. (match-end 4)))))
  6636. (if (match-end 5) (setq re (concat re "[ \t]+"
  6637. (regexp-quote
  6638. (match-string 5)))))
  6639. (setq re (concat re "[ \t]*$"))
  6640. (when org-refile-use-outline-path
  6641. (setq txt (mapconcat 'identity
  6642. (append
  6643. (if (eq org-refile-use-outline-path 'file)
  6644. (list (file-name-nondirectory
  6645. (buffer-file-name (buffer-base-buffer))))
  6646. (if (eq org-refile-use-outline-path 'full-file-path)
  6647. (list (buffer-file-name (buffer-base-buffer)))))
  6648. (org-get-outline-path)
  6649. (list txt))
  6650. "/")))
  6651. (push (list txt f re (point)) targets))
  6652. (goto-char (point-at-eol))))))))
  6653. (nreverse targets))))
  6654. (defun org-get-outline-path ()
  6655. "Return the outline path to the current entry, as a list."
  6656. (let (rtn)
  6657. (save-excursion
  6658. (while (org-up-heading-safe)
  6659. (when (looking-at org-complex-heading-regexp)
  6660. (push (org-match-string-no-properties 4) rtn)))
  6661. rtn)))
  6662. (defvar org-refile-history nil
  6663. "History for refiling operations.")
  6664. (defun org-refile (&optional goto default-buffer)
  6665. "Move the entry at point to another heading.
  6666. The list of target headings is compiled using the information in
  6667. `org-refile-targets', which see. This list is created before each use
  6668. and will therefore always be up-to-date.
  6669. At the target location, the entry is filed as a subitem of the target heading.
  6670. Depending on `org-reverse-note-order', the new subitem will either be the
  6671. first of the last subitem.
  6672. With prefix arg GOTO, the command will only visit the target location,
  6673. not actually move anything.
  6674. With a double prefix `C-c C-c', go to the location where the last refiling
  6675. operation has put the subtree."
  6676. (interactive "P")
  6677. (let* ((cbuf (current-buffer))
  6678. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  6679. pos it nbuf file re level reversed)
  6680. (if (equal goto '(16))
  6681. (org-refile-goto-last-stored)
  6682. (when (setq it (org-refile-get-location
  6683. (if goto "Goto: " "Refile to: ") default-buffer))
  6684. (setq file (nth 1 it)
  6685. re (nth 2 it)
  6686. pos (nth 3 it))
  6687. (setq nbuf (or (find-buffer-visiting file)
  6688. (find-file-noselect file)))
  6689. (if goto
  6690. (progn
  6691. (switch-to-buffer nbuf)
  6692. (goto-char pos)
  6693. (org-show-context 'org-goto))
  6694. (org-copy-subtree 1 nil t)
  6695. (save-excursion
  6696. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  6697. (find-file-noselect file))))
  6698. (setq reversed (org-notes-order-reversed-p))
  6699. (save-excursion
  6700. (save-restriction
  6701. (widen)
  6702. (goto-char pos)
  6703. (looking-at outline-regexp)
  6704. (setq level (org-get-valid-level (funcall outline-level) 1))
  6705. (goto-char
  6706. (if reversed
  6707. (outline-next-heading)
  6708. (or (save-excursion (outline-get-next-sibling))
  6709. (org-end-of-subtree t t)
  6710. (point-max))))
  6711. (bookmark-set "org-refile-last-stored")
  6712. (org-paste-subtree level))))
  6713. (org-cut-subtree)
  6714. (setq org-markers-to-move nil)
  6715. (message "Entry refiled to \"%s\"" (car it)))))))
  6716. (defun org-refile-goto-last-stored ()
  6717. "Go to the location where the last refile was stored."
  6718. (interactive)
  6719. (bookmark-jump "org-refile-last-stored")
  6720. (message "This is the location of the last refile"))
  6721. (defun org-refile-get-location (&optional prompt default-buffer)
  6722. "Prompt the user for a refile location, using PROMPT."
  6723. (let ((org-refile-targets org-refile-targets)
  6724. (org-refile-use-outline-path org-refile-use-outline-path))
  6725. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  6726. (unless org-refile-target-table
  6727. (error "No refile targets"))
  6728. (let* ((cbuf (current-buffer))
  6729. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  6730. (fname (and filename (file-truename filename)))
  6731. (tbl (mapcar
  6732. (lambda (x)
  6733. (if (not (equal fname (file-truename (nth 1 x))))
  6734. (cons (concat (car x) " (" (file-name-nondirectory
  6735. (nth 1 x)) ")")
  6736. (cdr x))
  6737. x))
  6738. org-refile-target-table))
  6739. (completion-ignore-case t))
  6740. (assoc (completing-read prompt tbl nil t nil 'org-refile-history)
  6741. tbl)))
  6742. ;;;; Dynamic blocks
  6743. (defun org-find-dblock (name)
  6744. "Find the first dynamic block with name NAME in the buffer.
  6745. If not found, stay at current position and return nil."
  6746. (let (pos)
  6747. (save-excursion
  6748. (goto-char (point-min))
  6749. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  6750. nil t)
  6751. (match-beginning 0))))
  6752. (if pos (goto-char pos))
  6753. pos))
  6754. (defconst org-dblock-start-re
  6755. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  6756. "Matches the startline of a dynamic block, with parameters.")
  6757. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  6758. "Matches the end of a dyhamic block.")
  6759. (defun org-create-dblock (plist)
  6760. "Create a dynamic block section, with parameters taken from PLIST.
  6761. PLIST must containe a :name entry which is used as name of the block."
  6762. (unless (bolp) (newline))
  6763. (let ((name (plist-get plist :name)))
  6764. (insert "#+BEGIN: " name)
  6765. (while plist
  6766. (if (eq (car plist) :name)
  6767. (setq plist (cddr plist))
  6768. (insert " " (prin1-to-string (pop plist)))))
  6769. (insert "\n\n#+END:\n")
  6770. (beginning-of-line -2)))
  6771. (defun org-prepare-dblock ()
  6772. "Prepare dynamic block for refresh.
  6773. This empties the block, puts the cursor at the insert position and returns
  6774. the property list including an extra property :name with the block name."
  6775. (unless (looking-at org-dblock-start-re)
  6776. (error "Not at a dynamic block"))
  6777. (let* ((begdel (1+ (match-end 0)))
  6778. (name (org-no-properties (match-string 1)))
  6779. (params (append (list :name name)
  6780. (read (concat "(" (match-string 3) ")")))))
  6781. (unless (re-search-forward org-dblock-end-re nil t)
  6782. (error "Dynamic block not terminated"))
  6783. (setq params
  6784. (append params
  6785. (list :content (buffer-substring
  6786. begdel (match-beginning 0)))))
  6787. (delete-region begdel (match-beginning 0))
  6788. (goto-char begdel)
  6789. (open-line 1)
  6790. params))
  6791. (defun org-map-dblocks (&optional command)
  6792. "Apply COMMAND to all dynamic blocks in the current buffer.
  6793. If COMMAND is not given, use `org-update-dblock'."
  6794. (let ((cmd (or command 'org-update-dblock))
  6795. pos)
  6796. (save-excursion
  6797. (goto-char (point-min))
  6798. (while (re-search-forward org-dblock-start-re nil t)
  6799. (goto-char (setq pos (match-beginning 0)))
  6800. (condition-case nil
  6801. (funcall cmd)
  6802. (error (message "Error during update of dynamic block")))
  6803. (goto-char pos)
  6804. (unless (re-search-forward org-dblock-end-re nil t)
  6805. (error "Dynamic block not terminated"))))))
  6806. (defun org-dblock-update (&optional arg)
  6807. "User command for updating dynamic blocks.
  6808. Update the dynamic block at point. With prefix ARG, update all dynamic
  6809. blocks in the buffer."
  6810. (interactive "P")
  6811. (if arg
  6812. (org-update-all-dblocks)
  6813. (or (looking-at org-dblock-start-re)
  6814. (org-beginning-of-dblock))
  6815. (org-update-dblock)))
  6816. (defun org-update-dblock ()
  6817. "Update the dynamic block at point
  6818. This means to empty the block, parse for parameters and then call
  6819. the correct writing function."
  6820. (save-window-excursion
  6821. (let* ((pos (point))
  6822. (line (org-current-line))
  6823. (params (org-prepare-dblock))
  6824. (name (plist-get params :name))
  6825. (cmd (intern (concat "org-dblock-write:" name))))
  6826. (message "Updating dynamic block `%s' at line %d..." name line)
  6827. (funcall cmd params)
  6828. (message "Updating dynamic block `%s' at line %d...done" name line)
  6829. (goto-char pos))))
  6830. (defun org-beginning-of-dblock ()
  6831. "Find the beginning of the dynamic block at point.
  6832. Error if there is no scuh block at point."
  6833. (let ((pos (point))
  6834. beg)
  6835. (end-of-line 1)
  6836. (if (and (re-search-backward org-dblock-start-re nil t)
  6837. (setq beg (match-beginning 0))
  6838. (re-search-forward org-dblock-end-re nil t)
  6839. (> (match-end 0) pos))
  6840. (goto-char beg)
  6841. (goto-char pos)
  6842. (error "Not in a dynamic block"))))
  6843. (defun org-update-all-dblocks ()
  6844. "Update all dynamic blocks in the buffer.
  6845. This function can be used in a hook."
  6846. (when (org-mode-p)
  6847. (org-map-dblocks 'org-update-dblock)))
  6848. ;;;; Completion
  6849. (defconst org-additional-option-like-keywords
  6850. '("BEGIN_HTML" "BEGIN_LaTeX" "END_HTML" "END_LaTeX"
  6851. "ORGTBL" "HTML:" "LaTeX:" "BEGIN:" "END:" "TBLFM"
  6852. "BEGIN_EXAMPLE" "END_EXAMPLE"))
  6853. (defun org-complete (&optional arg)
  6854. "Perform completion on word at point.
  6855. At the beginning of a headline, this completes TODO keywords as given in
  6856. `org-todo-keywords'.
  6857. If the current word is preceded by a backslash, completes the TeX symbols
  6858. that are supported for HTML support.
  6859. If the current word is preceded by \"#+\", completes special words for
  6860. setting file options.
  6861. In the line after \"#+STARTUP:, complete valid keywords.\"
  6862. At all other locations, this simply calls the value of
  6863. `org-completion-fallback-command'."
  6864. (interactive "P")
  6865. (org-without-partial-completion
  6866. (catch 'exit
  6867. (let* ((end (point))
  6868. (beg1 (save-excursion
  6869. (skip-chars-backward (org-re "[:alnum:]_@"))
  6870. (point)))
  6871. (beg (save-excursion
  6872. (skip-chars-backward "a-zA-Z0-9_:$")
  6873. (point)))
  6874. (confirm (lambda (x) (stringp (car x))))
  6875. (searchhead (equal (char-before beg) ?*))
  6876. (tag (and (equal (char-before beg1) ?:)
  6877. (equal (char-after (point-at-bol)) ?*)))
  6878. (prop (and (equal (char-before beg1) ?:)
  6879. (not (equal (char-after (point-at-bol)) ?*))))
  6880. (texp (equal (char-before beg) ?\\))
  6881. (link (equal (char-before beg) ?\[))
  6882. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  6883. beg)
  6884. "#+"))
  6885. (startup (string-match "^#\\+STARTUP:.*"
  6886. (buffer-substring (point-at-bol) (point))))
  6887. (completion-ignore-case opt)
  6888. (type nil)
  6889. (tbl nil)
  6890. (table (cond
  6891. (opt
  6892. (setq type :opt)
  6893. (require 'org-exp)
  6894. (append
  6895. (mapcar
  6896. (lambda (x)
  6897. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  6898. (cons (match-string 2 x) (match-string 1 x)))
  6899. (org-split-string (org-get-current-options) "\n"))
  6900. (mapcar 'list org-additional-option-like-keywords)))
  6901. (startup
  6902. (setq type :startup)
  6903. org-startup-options)
  6904. (link (append org-link-abbrev-alist-local
  6905. org-link-abbrev-alist))
  6906. (texp
  6907. (setq type :tex)
  6908. org-html-entities)
  6909. ((string-match "\\`\\*+[ \t]+\\'"
  6910. (buffer-substring (point-at-bol) beg))
  6911. (setq type :todo)
  6912. (mapcar 'list org-todo-keywords-1))
  6913. (searchhead
  6914. (setq type :searchhead)
  6915. (save-excursion
  6916. (goto-char (point-min))
  6917. (while (re-search-forward org-todo-line-regexp nil t)
  6918. (push (list
  6919. (org-make-org-heading-search-string
  6920. (match-string 3) t))
  6921. tbl)))
  6922. tbl)
  6923. (tag (setq type :tag beg beg1)
  6924. (or org-tag-alist (org-get-buffer-tags)))
  6925. (prop (setq type :prop beg beg1)
  6926. (mapcar 'list (org-buffer-property-keys nil t t)))
  6927. (t (progn
  6928. (call-interactively org-completion-fallback-command)
  6929. (throw 'exit nil)))))
  6930. (pattern (buffer-substring-no-properties beg end))
  6931. (completion (try-completion pattern table confirm)))
  6932. (cond ((eq completion t)
  6933. (if (not (assoc (upcase pattern) table))
  6934. (message "Already complete")
  6935. (if (and (equal type :opt)
  6936. (not (member (car (assoc (upcase pattern) table))
  6937. org-additional-option-like-keywords)))
  6938. (insert (substring (cdr (assoc (upcase pattern) table))
  6939. (length pattern)))
  6940. (if (memq type '(:tag :prop)) (insert ":")))))
  6941. ((null completion)
  6942. (message "Can't find completion for \"%s\"" pattern)
  6943. (ding))
  6944. ((not (string= pattern completion))
  6945. (delete-region beg end)
  6946. (if (string-match " +$" completion)
  6947. (setq completion (replace-match "" t t completion)))
  6948. (insert completion)
  6949. (if (get-buffer-window "*Completions*")
  6950. (delete-window (get-buffer-window "*Completions*")))
  6951. (if (assoc completion table)
  6952. (if (eq type :todo) (insert " ")
  6953. (if (memq type '(:tag :prop)) (insert ":"))))
  6954. (if (and (equal type :opt) (assoc completion table))
  6955. (message "%s" (substitute-command-keys
  6956. "Press \\[org-complete] again to insert example settings"))))
  6957. (t
  6958. (message "Making completion list...")
  6959. (let ((list (sort (all-completions pattern table confirm)
  6960. 'string<)))
  6961. (with-output-to-temp-buffer "*Completions*"
  6962. (condition-case nil
  6963. ;; Protection needed for XEmacs and emacs 21
  6964. (display-completion-list list pattern)
  6965. (error (display-completion-list list)))))
  6966. (message "Making completion list...%s" "done")))))))
  6967. ;;;; TODO, DEADLINE, Comments
  6968. (defun org-toggle-comment ()
  6969. "Change the COMMENT state of an entry."
  6970. (interactive)
  6971. (save-excursion
  6972. (org-back-to-heading)
  6973. (let (case-fold-search)
  6974. (if (looking-at (concat outline-regexp
  6975. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  6976. (replace-match "" t t nil 1)
  6977. (if (looking-at outline-regexp)
  6978. (progn
  6979. (goto-char (match-end 0))
  6980. (insert org-comment-string " ")))))))
  6981. (defvar org-last-todo-state-is-todo nil
  6982. "This is non-nil when the last TODO state change led to a TODO state.
  6983. If the last change removed the TODO tag or switched to DONE, then
  6984. this is nil.")
  6985. (defvar org-setting-tags nil) ; dynamically skiped
  6986. (defun org-parse-local-options (string var)
  6987. "Parse STRING for startup setting relevant for variable VAR."
  6988. (let ((rtn (symbol-value var))
  6989. e opts)
  6990. (save-match-data
  6991. (if (or (not string) (not (string-match "\\S-" string)))
  6992. rtn
  6993. (setq opts (delq nil (mapcar (lambda (x)
  6994. (setq e (assoc x org-startup-options))
  6995. (if (eq (nth 1 e) var) e nil))
  6996. (org-split-string string "[ \t]+"))))
  6997. (if (not opts)
  6998. rtn
  6999. (setq rtn nil)
  7000. (while (setq e (pop opts))
  7001. (if (not (nth 3 e))
  7002. (setq rtn (nth 2 e))
  7003. (if (not (listp rtn)) (setq rtn nil))
  7004. (push (nth 2 e) rtn)))
  7005. rtn)))))
  7006. (defvar org-blocker-hook nil
  7007. "Hook for functions that are allowed to block a state change.
  7008. Each function gets as its single argument a property list, see
  7009. `org-trigger-hook' for more information about this list.
  7010. If any of the functions in this hook returns nil, the state change
  7011. is blocked.")
  7012. (defvar org-trigger-hook nil
  7013. "Hook for functions that are triggered by a state change.
  7014. Each function gets as its single argument a property list with at least
  7015. the following elements:
  7016. (:type type-of-change :position pos-at-entry-start
  7017. :from old-state :to new-state)
  7018. Depending on the type, more properties may be present.
  7019. This mechanism is currently implemented for:
  7020. TODO state changes
  7021. ------------------
  7022. :type todo-state-change
  7023. :from previous state (keyword as a string), or nil
  7024. :to new state (keyword as a string), or nil")
  7025. (defun org-todo (&optional arg)
  7026. "Change the TODO state of an item.
  7027. The state of an item is given by a keyword at the start of the heading,
  7028. like
  7029. *** TODO Write paper
  7030. *** DONE Call mom
  7031. The different keywords are specified in the variable `org-todo-keywords'.
  7032. By default the available states are \"TODO\" and \"DONE\".
  7033. So for this example: when the item starts with TODO, it is changed to DONE.
  7034. When it starts with DONE, the DONE is removed. And when neither TODO nor
  7035. DONE are present, add TODO at the beginning of the heading.
  7036. With C-u prefix arg, use completion to determine the new state.
  7037. With numeric prefix arg, switch to that state.
  7038. For calling through lisp, arg is also interpreted in the following way:
  7039. 'none -> empty state
  7040. \"\"(empty string) -> switch to empty state
  7041. 'done -> switch to DONE
  7042. 'nextset -> switch to the next set of keywords
  7043. 'previousset -> switch to the previous set of keywords
  7044. \"WAITING\" -> switch to the specified keyword, but only if it
  7045. really is a member of `org-todo-keywords'."
  7046. (interactive "P")
  7047. (save-excursion
  7048. (catch 'exit
  7049. (org-back-to-heading)
  7050. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  7051. (or (looking-at (concat " +" org-todo-regexp " *"))
  7052. (looking-at " *"))
  7053. (let* ((match-data (match-data))
  7054. (startpos (point-at-bol))
  7055. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  7056. (org-log-done org-log-done)
  7057. (org-log-repeat org-log-repeat)
  7058. (org-todo-log-states org-todo-log-states)
  7059. (this (match-string 1))
  7060. (hl-pos (match-beginning 0))
  7061. (head (org-get-todo-sequence-head this))
  7062. (ass (assoc head org-todo-kwd-alist))
  7063. (interpret (nth 1 ass))
  7064. (done-word (nth 3 ass))
  7065. (final-done-word (nth 4 ass))
  7066. (last-state (or this ""))
  7067. (completion-ignore-case t)
  7068. (member (member this org-todo-keywords-1))
  7069. (tail (cdr member))
  7070. (state (cond
  7071. ((and org-todo-key-trigger
  7072. (or (and (equal arg '(4)) (eq org-use-fast-todo-selection 'prefix))
  7073. (and (not arg) org-use-fast-todo-selection
  7074. (not (eq org-use-fast-todo-selection 'prefix)))))
  7075. ;; Use fast selection
  7076. (org-fast-todo-selection))
  7077. ((and (equal arg '(4))
  7078. (or (not org-use-fast-todo-selection)
  7079. (not org-todo-key-trigger)))
  7080. ;; Read a state with completion
  7081. (completing-read "State: " (mapcar (lambda(x) (list x))
  7082. org-todo-keywords-1)
  7083. nil t))
  7084. ((eq arg 'right)
  7085. (if this
  7086. (if tail (car tail) nil)
  7087. (car org-todo-keywords-1)))
  7088. ((eq arg 'left)
  7089. (if (equal member org-todo-keywords-1)
  7090. nil
  7091. (if this
  7092. (nth (- (length org-todo-keywords-1) (length tail) 2)
  7093. org-todo-keywords-1)
  7094. (org-last org-todo-keywords-1))))
  7095. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  7096. (setq arg nil))) ; hack to fall back to cycling
  7097. (arg
  7098. ;; user or caller requests a specific state
  7099. (cond
  7100. ((equal arg "") nil)
  7101. ((eq arg 'none) nil)
  7102. ((eq arg 'done) (or done-word (car org-done-keywords)))
  7103. ((eq arg 'nextset)
  7104. (or (car (cdr (member head org-todo-heads)))
  7105. (car org-todo-heads)))
  7106. ((eq arg 'previousset)
  7107. (let ((org-todo-heads (reverse org-todo-heads)))
  7108. (or (car (cdr (member head org-todo-heads)))
  7109. (car org-todo-heads))))
  7110. ((car (member arg org-todo-keywords-1)))
  7111. ((nth (1- (prefix-numeric-value arg))
  7112. org-todo-keywords-1))))
  7113. ((null member) (or head (car org-todo-keywords-1)))
  7114. ((equal this final-done-word) nil) ;; -> make empty
  7115. ((null tail) nil) ;; -> first entry
  7116. ((eq interpret 'sequence)
  7117. (car tail))
  7118. ((memq interpret '(type priority))
  7119. (if (eq this-command last-command)
  7120. (car tail)
  7121. (if (> (length tail) 0)
  7122. (or done-word (car org-done-keywords))
  7123. nil)))
  7124. (t nil)))
  7125. (next (if state (concat " " state " ") " "))
  7126. (change-plist (list :type 'todo-state-change :from this :to state
  7127. :position startpos))
  7128. dolog now-done-p)
  7129. (when org-blocker-hook
  7130. (unless (save-excursion
  7131. (save-match-data
  7132. (run-hook-with-args-until-failure
  7133. 'org-blocker-hook change-plist)))
  7134. (if (interactive-p)
  7135. (error "TODO state change from %s to %s blocked" this state)
  7136. ;; fail silently
  7137. (message "TODO state change from %s to %s blocked" this state)
  7138. (throw 'exit nil))))
  7139. (store-match-data match-data)
  7140. (replace-match next t t)
  7141. (unless (pos-visible-in-window-p hl-pos)
  7142. (message "TODO state changed to %s" (org-trim next)))
  7143. (unless head
  7144. (setq head (org-get-todo-sequence-head state)
  7145. ass (assoc head org-todo-kwd-alist)
  7146. interpret (nth 1 ass)
  7147. done-word (nth 3 ass)
  7148. final-done-word (nth 4 ass)))
  7149. (when (memq arg '(nextset previousset))
  7150. (message "Keyword-Set %d/%d: %s"
  7151. (- (length org-todo-sets) -1
  7152. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  7153. (length org-todo-sets)
  7154. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  7155. (setq org-last-todo-state-is-todo
  7156. (not (member state org-done-keywords)))
  7157. (setq now-done-p (and (member state org-done-keywords)
  7158. (not (member this org-done-keywords))))
  7159. (and logging (org-local-logging logging))
  7160. (when (and (or org-todo-log-states org-log-done)
  7161. (not (memq arg '(nextset previousset))))
  7162. ;; we need to look at recording a time and note
  7163. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  7164. (nth 2 (assoc this org-todo-log-states))))
  7165. (when (and state
  7166. (member state org-not-done-keywords)
  7167. (not (member this org-not-done-keywords)))
  7168. ;; This is now a todo state and was not one before
  7169. ;; If there was a CLOSED time stamp, get rid of it.
  7170. (org-add-planning-info nil nil 'closed))
  7171. (when (and now-done-p org-log-done)
  7172. ;; It is now done, and it was not done before
  7173. (org-add-planning-info 'closed (org-current-time))
  7174. (if (and (not dolog) (eq 'note org-log-done))
  7175. (org-add-log-setup 'done state 'findpos 'note)))
  7176. (when (and state dolog)
  7177. ;; This is a non-nil state, and we need to log it
  7178. (org-add-log-setup 'state state 'findpos dolog)))
  7179. ;; Fixup tag positioning
  7180. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  7181. (run-hooks 'org-after-todo-state-change-hook)
  7182. (if (and arg (not (member state org-done-keywords)))
  7183. (setq head (org-get-todo-sequence-head state)))
  7184. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  7185. ;; Do we need to trigger a repeat?
  7186. (when now-done-p (org-auto-repeat-maybe state))
  7187. ;; Fixup cursor location if close to the keyword
  7188. (if (and (outline-on-heading-p)
  7189. (not (bolp))
  7190. (save-excursion (beginning-of-line 1)
  7191. (looking-at org-todo-line-regexp))
  7192. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  7193. (progn
  7194. (goto-char (or (match-end 2) (match-end 1)))
  7195. (just-one-space)))
  7196. (when org-trigger-hook
  7197. (save-excursion
  7198. (run-hook-with-args 'org-trigger-hook change-plist)))))))
  7199. (defun org-local-logging (value)
  7200. "Get logging settings from a property VALUE."
  7201. (let* (words w a)
  7202. ;; directly set the variables, they are already local.
  7203. (setq org-log-done nil
  7204. org-log-repeat nil
  7205. org-todo-log-states nil)
  7206. (setq words (org-split-string value))
  7207. (while (setq w (pop words))
  7208. (cond
  7209. ((setq a (assoc w org-startup-options))
  7210. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  7211. (set (nth 1 a) (nth 2 a))))
  7212. ((setq a (org-extract-log-state-settings w))
  7213. (and (member (car a) org-todo-keywords-1)
  7214. (push a org-todo-log-states)))))))
  7215. (defun org-get-todo-sequence-head (kwd)
  7216. "Return the head of the TODO sequence to which KWD belongs.
  7217. If KWD is not set, check if there is a text property remembering the
  7218. right sequence."
  7219. (let (p)
  7220. (cond
  7221. ((not kwd)
  7222. (or (get-text-property (point-at-bol) 'org-todo-head)
  7223. (progn
  7224. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  7225. nil (point-at-eol)))
  7226. (get-text-property p 'org-todo-head))))
  7227. ((not (member kwd org-todo-keywords-1))
  7228. (car org-todo-keywords-1))
  7229. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  7230. (defun org-fast-todo-selection ()
  7231. "Fast TODO keyword selection with single keys.
  7232. Returns the new TODO keyword, or nil if no state change should occur."
  7233. (let* ((fulltable org-todo-key-alist)
  7234. (done-keywords org-done-keywords) ;; needed for the faces.
  7235. (maxlen (apply 'max (mapcar
  7236. (lambda (x)
  7237. (if (stringp (car x)) (string-width (car x)) 0))
  7238. fulltable)))
  7239. (expert nil)
  7240. (fwidth (+ maxlen 3 1 3))
  7241. (ncol (/ (- (window-width) 4) fwidth))
  7242. tg cnt e c tbl
  7243. groups ingroup)
  7244. (save-window-excursion
  7245. (if expert
  7246. (set-buffer (get-buffer-create " *Org todo*"))
  7247. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  7248. (erase-buffer)
  7249. (org-set-local 'org-done-keywords done-keywords)
  7250. (setq tbl fulltable cnt 0)
  7251. (while (setq e (pop tbl))
  7252. (cond
  7253. ((equal e '(:startgroup))
  7254. (push '() groups) (setq ingroup t)
  7255. (when (not (= cnt 0))
  7256. (setq cnt 0)
  7257. (insert "\n"))
  7258. (insert "{ "))
  7259. ((equal e '(:endgroup))
  7260. (setq ingroup nil cnt 0)
  7261. (insert "}\n"))
  7262. (t
  7263. (setq tg (car e) c (cdr e))
  7264. (if ingroup (push tg (car groups)))
  7265. (setq tg (org-add-props tg nil 'face
  7266. (org-get-todo-face tg)))
  7267. (if (and (= cnt 0) (not ingroup)) (insert " "))
  7268. (insert "[" c "] " tg (make-string
  7269. (- fwidth 4 (length tg)) ?\ ))
  7270. (when (= (setq cnt (1+ cnt)) ncol)
  7271. (insert "\n")
  7272. (if ingroup (insert " "))
  7273. (setq cnt 0)))))
  7274. (insert "\n")
  7275. (goto-char (point-min))
  7276. (if (and (not expert) (fboundp 'fit-window-to-buffer))
  7277. (fit-window-to-buffer))
  7278. (message "[a-z..]:Set [SPC]:clear")
  7279. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  7280. (cond
  7281. ((or (= c ?\C-g)
  7282. (and (= c ?q) (not (rassoc c fulltable))))
  7283. (setq quit-flag t))
  7284. ((= c ?\ ) nil)
  7285. ((setq e (rassoc c fulltable) tg (car e))
  7286. tg)
  7287. (t (setq quit-flag t))))))
  7288. (defun org-entry-is-todo-p ()
  7289. (member (org-get-todo-state) org-not-done-keywords))
  7290. (defun org-entry-is-done-p ()
  7291. (member (org-get-todo-state) org-done-keywords))
  7292. (defun org-get-todo-state ()
  7293. (save-excursion
  7294. (org-back-to-heading t)
  7295. (and (looking-at org-todo-line-regexp)
  7296. (match-end 2)
  7297. (match-string 2))))
  7298. (defun org-at-date-range-p (&optional inactive-ok)
  7299. "Is the cursor inside a date range?"
  7300. (interactive)
  7301. (save-excursion
  7302. (catch 'exit
  7303. (let ((pos (point)))
  7304. (skip-chars-backward "^[<\r\n")
  7305. (skip-chars-backward "<[")
  7306. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  7307. (>= (match-end 0) pos)
  7308. (throw 'exit t))
  7309. (skip-chars-backward "^<[\r\n")
  7310. (skip-chars-backward "<[")
  7311. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  7312. (>= (match-end 0) pos)
  7313. (throw 'exit t)))
  7314. nil)))
  7315. (defun org-get-repeat ()
  7316. "Check if tere is a deadline/schedule with repeater in this entry."
  7317. (save-match-data
  7318. (save-excursion
  7319. (org-back-to-heading t)
  7320. (if (re-search-forward
  7321. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  7322. (match-string 1)))))
  7323. (defvar org-last-changed-timestamp)
  7324. (defvar org-log-post-message)
  7325. (defvar org-log-note-purpose)
  7326. (defvar org-log-note-how)
  7327. (defun org-auto-repeat-maybe (done-word)
  7328. "Check if the current headline contains a repeated deadline/schedule.
  7329. If yes, set TODO state back to what it was and change the base date
  7330. of repeating deadline/scheduled time stamps to new date.
  7331. This function is run automatically after each state change to a DONE state."
  7332. ;; last-state is dynamically scoped into this function
  7333. (let* ((repeat (org-get-repeat))
  7334. (aa (assoc last-state org-todo-kwd-alist))
  7335. (interpret (nth 1 aa))
  7336. (head (nth 2 aa))
  7337. (whata '(("d" . day) ("m" . month) ("y" . year)))
  7338. (msg "Entry repeats: ")
  7339. (org-log-done nil)
  7340. (org-todo-log-states nil)
  7341. (nshiftmax 10) (nshift 0)
  7342. re type n what ts mb0 time)
  7343. (when repeat
  7344. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  7345. (org-todo (if (eq interpret 'type) last-state head))
  7346. (when org-log-repeat
  7347. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  7348. (memq 'org-add-log-note post-command-hook))
  7349. ;; OK, we are already setup for some record
  7350. (if (eq org-log-repeat 'note)
  7351. ;; make sure we take a note, not only a time stamp
  7352. (setq org-log-note-how 'note))
  7353. ;; Set up for taking a record
  7354. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  7355. 'findpos org-log-repeat)))
  7356. (org-back-to-heading t)
  7357. (org-add-planning-info nil nil 'closed)
  7358. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  7359. org-deadline-time-regexp "\\)\\|\\("
  7360. org-ts-regexp "\\)"))
  7361. (while (re-search-forward
  7362. re (save-excursion (outline-next-heading) (point)) t)
  7363. (setq type (if (match-end 1) org-scheduled-string
  7364. (if (match-end 3) org-deadline-string "Plain:"))
  7365. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0)))
  7366. mb0 (match-beginning 0))
  7367. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  7368. (setq n (string-to-number (match-string 2 ts))
  7369. what (match-string 3 ts))
  7370. (if (equal what "w") (setq n (* n 7) what "d"))
  7371. ;; Preparation, see if we need to modify the start date for the change
  7372. (when (match-end 1)
  7373. (setq time (save-match-data (org-time-string-to-time ts)))
  7374. (cond
  7375. ((equal (match-string 1 ts) ".")
  7376. ;; Shift starting date to today
  7377. (org-timestamp-change
  7378. (- (time-to-days (current-time)) (time-to-days time))
  7379. 'day))
  7380. ((equal (match-string 1 ts) "+")
  7381. (while (or (= nshift 0)
  7382. (<= (time-to-days time) (time-to-days (current-time))))
  7383. (when (= (incf nshift) nshiftmax)
  7384. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  7385. (error "Abort")))
  7386. (org-timestamp-change n (cdr (assoc what whata)))
  7387. (org-at-timestamp-p t)
  7388. (setq ts (match-string 1))
  7389. (setq time (save-match-data (org-time-string-to-time ts))))
  7390. (org-timestamp-change (- n) (cdr (assoc what whata)))
  7391. ;; rematch, so that we have everything in place for the real shift
  7392. (org-at-timestamp-p t)
  7393. (setq ts (match-string 1))
  7394. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  7395. (org-timestamp-change n (cdr (assoc what whata)))
  7396. (setq msg (concat msg type org-last-changed-timestamp " "))))
  7397. (setq org-log-post-message msg)
  7398. (message "%s" msg))))
  7399. (defun org-show-todo-tree (arg)
  7400. "Make a compact tree which shows all headlines marked with TODO.
  7401. The tree will show the lines where the regexp matches, and all higher
  7402. headlines above the match.
  7403. With a \\[universal-argument] prefix, also show the DONE entries.
  7404. With a numeric prefix N, construct a sparse tree for the Nth element
  7405. of `org-todo-keywords-1'."
  7406. (interactive "P")
  7407. (let ((case-fold-search nil)
  7408. (kwd-re
  7409. (cond ((null arg) org-not-done-regexp)
  7410. ((equal arg '(4))
  7411. (let ((kwd (completing-read "Keyword (or KWD1|KWD2|...): "
  7412. (mapcar 'list org-todo-keywords-1))))
  7413. (concat "\\("
  7414. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  7415. "\\)\\>")))
  7416. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  7417. (regexp-quote (nth (1- (prefix-numeric-value arg))
  7418. org-todo-keywords-1)))
  7419. (t (error "Invalid prefix argument: %s" arg)))))
  7420. (message "%d TODO entries found"
  7421. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  7422. (defun org-deadline (&optional remove)
  7423. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  7424. With argument REMOVE, remove any deadline from the item."
  7425. (interactive "P")
  7426. (if remove
  7427. (progn
  7428. (org-remove-timestamp-with-keyword org-deadline-string)
  7429. (message "Item no longer has a deadline."))
  7430. (org-add-planning-info 'deadline nil 'closed)))
  7431. (defun org-schedule (&optional remove)
  7432. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  7433. With argument REMOVE, remove any scheduling date from the item."
  7434. (interactive "P")
  7435. (if remove
  7436. (progn
  7437. (org-remove-timestamp-with-keyword org-scheduled-string)
  7438. (message "Item is no longer scheduled."))
  7439. (org-add-planning-info 'scheduled nil 'closed)))
  7440. (defun org-remove-timestamp-with-keyword (keyword)
  7441. "Remove all time stamps with KEYWORD in the current entry."
  7442. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  7443. beg)
  7444. (save-excursion
  7445. (org-back-to-heading t)
  7446. (setq beg (point))
  7447. (org-end-of-subtree t t)
  7448. (while (re-search-backward re beg t)
  7449. (replace-match "")
  7450. (unless (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  7451. (delete-region (point-at-bol) (min (1+ (point)) (point-max))))))))
  7452. (defun org-add-planning-info (what &optional time &rest remove)
  7453. "Insert new timestamp with keyword in the line directly after the headline.
  7454. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  7455. If non is given, the user is prompted for a date.
  7456. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  7457. be removed."
  7458. (interactive)
  7459. (let (org-time-was-given org-end-time-was-given ts
  7460. end default-time default-input)
  7461. (when (and (not time) (memq what '(scheduled deadline)))
  7462. ;; Try to get a default date/time from existing timestamp
  7463. (save-excursion
  7464. (org-back-to-heading t)
  7465. (setq end (save-excursion (outline-next-heading) (point)))
  7466. (when (re-search-forward (if (eq what 'scheduled)
  7467. org-scheduled-time-regexp
  7468. org-deadline-time-regexp)
  7469. end t)
  7470. (setq ts (match-string 1)
  7471. default-time
  7472. (apply 'encode-time (org-parse-time-string ts))
  7473. default-input (and ts (org-get-compact-tod ts))))))
  7474. (when what
  7475. ;; If necessary, get the time from the user
  7476. (setq time (or time (org-read-date nil 'to-time nil nil
  7477. default-time default-input))))
  7478. (when (and org-insert-labeled-timestamps-at-point
  7479. (member what '(scheduled deadline)))
  7480. (insert
  7481. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  7482. (org-insert-time-stamp time org-time-was-given
  7483. nil nil nil (list org-end-time-was-given))
  7484. (setq what nil))
  7485. (save-excursion
  7486. (save-restriction
  7487. (let (col list elt ts buffer-invisibility-spec)
  7488. (org-back-to-heading t)
  7489. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  7490. (goto-char (match-end 1))
  7491. (setq col (current-column))
  7492. (goto-char (match-end 0))
  7493. (if (eobp) (insert "\n") (forward-char 1))
  7494. (if (and (not (looking-at outline-regexp))
  7495. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  7496. "[^\r\n]*"))
  7497. (not (equal (match-string 1) org-clock-string)))
  7498. (narrow-to-region (match-beginning 0) (match-end 0))
  7499. (insert-before-markers "\n")
  7500. (backward-char 1)
  7501. (narrow-to-region (point) (point))
  7502. (org-indent-to-column col))
  7503. ;; Check if we have to remove something.
  7504. (setq list (cons what remove))
  7505. (while list
  7506. (setq elt (pop list))
  7507. (goto-char (point-min))
  7508. (when (or (and (eq elt 'scheduled)
  7509. (re-search-forward org-scheduled-time-regexp nil t))
  7510. (and (eq elt 'deadline)
  7511. (re-search-forward org-deadline-time-regexp nil t))
  7512. (and (eq elt 'closed)
  7513. (re-search-forward org-closed-time-regexp nil t)))
  7514. (replace-match "")
  7515. (if (looking-at "--+<[^>]+>") (replace-match ""))
  7516. (if (looking-at " +") (replace-match ""))))
  7517. (goto-char (point-max))
  7518. (when what
  7519. (insert
  7520. (if (not (equal (char-before) ?\ )) " " "")
  7521. (cond ((eq what 'scheduled) org-scheduled-string)
  7522. ((eq what 'deadline) org-deadline-string)
  7523. ((eq what 'closed) org-closed-string))
  7524. " ")
  7525. (setq ts (org-insert-time-stamp
  7526. time
  7527. (or org-time-was-given
  7528. (and (eq what 'closed) org-log-done-with-time))
  7529. (eq what 'closed)
  7530. nil nil (list org-end-time-was-given)))
  7531. (end-of-line 1))
  7532. (goto-char (point-min))
  7533. (widen)
  7534. (if (and (looking-at "[ \t]+\n")
  7535. (equal (char-before) ?\n))
  7536. (backward-delete-char 1))
  7537. ts)))))
  7538. (defvar org-log-note-marker (make-marker))
  7539. (defvar org-log-note-purpose nil)
  7540. (defvar org-log-note-state nil)
  7541. (defvar org-log-note-how nil)
  7542. (defvar org-log-note-window-configuration nil)
  7543. (defvar org-log-note-return-to (make-marker))
  7544. (defvar org-log-post-message nil
  7545. "Message to be displayed after a log note has been stored.
  7546. The auto-repeater uses this.")
  7547. (defun org-add-note ()
  7548. "Add a note to the current entry.
  7549. This is done in the same way as adding a state change note."
  7550. (interactive)
  7551. (org-add-log-setup 'note nil t nil))
  7552. (defun org-add-log-setup (&optional purpose state findpos how)
  7553. "Set up the post command hook to take a note.
  7554. If this is about to TODO state change, the new state is expected in STATE.
  7555. When FINDPOS is non-nil, find the correct position for the note in
  7556. the current entry. If not, assume that it can be inserted at point."
  7557. (save-excursion
  7558. (when findpos
  7559. (org-back-to-heading t)
  7560. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  7561. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  7562. "[^\r\n]*\\)?"))
  7563. (goto-char (match-end 0))
  7564. (unless org-log-states-order-reversed
  7565. (and (= (char-after) ?\n) (forward-char 1))
  7566. (org-skip-over-state-notes)
  7567. (skip-chars-backward " \t\n\r")))
  7568. (move-marker org-log-note-marker (point))
  7569. (setq org-log-note-purpose purpose
  7570. org-log-note-state state
  7571. org-log-note-how how)
  7572. (add-hook 'post-command-hook 'org-add-log-note 'append)))
  7573. (defun org-skip-over-state-notes ()
  7574. "Skip past the list of State notes in an entry."
  7575. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  7576. (while (looking-at "[ \t]*- State")
  7577. (condition-case nil
  7578. (org-next-item)
  7579. (error (org-end-of-item)))))
  7580. (defun org-add-log-note (&optional purpose)
  7581. "Pop up a window for taking a note, and add this note later at point."
  7582. (remove-hook 'post-command-hook 'org-add-log-note)
  7583. (setq org-log-note-window-configuration (current-window-configuration))
  7584. (delete-other-windows)
  7585. (move-marker org-log-note-return-to (point))
  7586. (switch-to-buffer (marker-buffer org-log-note-marker))
  7587. (goto-char org-log-note-marker)
  7588. (org-switch-to-buffer-other-window "*Org Note*")
  7589. (erase-buffer)
  7590. (if (memq org-log-note-how '(time state))
  7591. (org-store-log-note)
  7592. (let ((org-inhibit-startup t)) (org-mode))
  7593. (insert (format "# Insert note for %s.
  7594. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  7595. (cond
  7596. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  7597. ((eq org-log-note-purpose 'done) "closed todo item")
  7598. ((eq org-log-note-purpose 'state)
  7599. (format "state change to \"%s\"" org-log-note-state))
  7600. ((eq org-log-note-purpose 'note)
  7601. "this entry")
  7602. (t (error "This should not happen")))))
  7603. (org-set-local 'org-finish-function 'org-store-log-note)))
  7604. (defvar org-note-abort nil) ; dynamically scoped
  7605. (defun org-store-log-note ()
  7606. "Finish taking a log note, and insert it to where it belongs."
  7607. (let ((txt (buffer-string))
  7608. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  7609. lines ind)
  7610. (kill-buffer (current-buffer))
  7611. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  7612. (setq txt (replace-match "" t t txt)))
  7613. (if (string-match "\\s-+\\'" txt)
  7614. (setq txt (replace-match "" t t txt)))
  7615. (setq lines (org-split-string txt "\n"))
  7616. (when (and note (string-match "\\S-" note))
  7617. (setq note
  7618. (org-replace-escapes
  7619. note
  7620. (list (cons "%u" (user-login-name))
  7621. (cons "%U" user-full-name)
  7622. (cons "%t" (format-time-string
  7623. (org-time-stamp-format 'long 'inactive)
  7624. (current-time)))
  7625. (cons "%s" (if org-log-note-state
  7626. (concat "\"" org-log-note-state "\"")
  7627. "")))))
  7628. (if lines (setq note (concat note " \\\\")))
  7629. (push note lines))
  7630. (when (or current-prefix-arg org-note-abort) (setq lines nil))
  7631. (when lines
  7632. (save-excursion
  7633. (set-buffer (marker-buffer org-log-note-marker))
  7634. (save-excursion
  7635. (goto-char org-log-note-marker)
  7636. (move-marker org-log-note-marker nil)
  7637. (end-of-line 1)
  7638. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  7639. (indent-relative nil)
  7640. (insert "- " (pop lines))
  7641. (org-indent-line-function)
  7642. (beginning-of-line 1)
  7643. (looking-at "[ \t]*")
  7644. (setq ind (concat (match-string 0) " "))
  7645. (end-of-line 1)
  7646. (while lines (insert "\n" ind (pop lines)))))))
  7647. (set-window-configuration org-log-note-window-configuration)
  7648. (with-current-buffer (marker-buffer org-log-note-return-to)
  7649. (goto-char org-log-note-return-to))
  7650. (move-marker org-log-note-return-to nil)
  7651. (and org-log-post-message (message "%s" org-log-post-message)))
  7652. (defun org-sparse-tree (&optional arg)
  7653. "Create a sparse tree, prompt for the details.
  7654. This command can create sparse trees. You first need to select the type
  7655. of match used to create the tree:
  7656. t Show entries with a specific TODO keyword.
  7657. T Show entries selected by a tags match.
  7658. p Enter a property name and its value (both with completion on existing
  7659. names/values) and show entries with that property.
  7660. r Show entries matching a regular expression
  7661. d Show deadlines due within `org-deadline-warning-days'."
  7662. (interactive "P")
  7663. (let (ans kwd value)
  7664. (message "Sparse tree: [/]regexp [t]odo-kwd [T]ag [p]roperty [d]eadlines [b]efore-date")
  7665. (setq ans (read-char-exclusive))
  7666. (cond
  7667. ((equal ans ?d)
  7668. (call-interactively 'org-check-deadlines))
  7669. ((equal ans ?b)
  7670. (call-interactively 'org-check-before-date))
  7671. ((equal ans ?t)
  7672. (org-show-todo-tree '(4)))
  7673. ((equal ans ?T)
  7674. (call-interactively 'org-tags-sparse-tree))
  7675. ((member ans '(?p ?P))
  7676. (setq kwd (completing-read "Property: "
  7677. (mapcar 'list (org-buffer-property-keys))))
  7678. (setq value (completing-read "Value: "
  7679. (mapcar 'list (org-property-values kwd))))
  7680. (unless (string-match "\\`{.*}\\'" value)
  7681. (setq value (concat "\"" value "\"")))
  7682. (org-tags-sparse-tree arg (concat kwd "=" value)))
  7683. ((member ans '(?r ?R ?/))
  7684. (call-interactively 'org-occur))
  7685. (t (error "No such sparse tree command \"%c\"" ans)))))
  7686. (defvar org-occur-highlights nil
  7687. "List of overlays used for occur matches.")
  7688. (make-variable-buffer-local 'org-occur-highlights)
  7689. (defvar org-occur-parameters nil
  7690. "Parameters of the active org-occur calls.
  7691. This is a list, each call to org-occur pushes as cons cell,
  7692. containing the regular expression and the callback, onto the list.
  7693. The list can contain several entries if `org-occur' has been called
  7694. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  7695. will only contain one set of parameters. When the highlights are
  7696. removed (for example with `C-c C-c', or with the next edit (depending
  7697. on `org-remove-highlights-with-change'), this variable is emptied
  7698. as well.")
  7699. (make-variable-buffer-local 'org-occur-parameters)
  7700. (defun org-occur (regexp &optional keep-previous callback)
  7701. "Make a compact tree which shows all matches of REGEXP.
  7702. The tree will show the lines where the regexp matches, and all higher
  7703. headlines above the match. It will also show the heading after the match,
  7704. to make sure editing the matching entry is easy.
  7705. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  7706. call to `org-occur' will be kept, to allow stacking of calls to this
  7707. command.
  7708. If CALLBACK is non-nil, it is a function which is called to confirm
  7709. that the match should indeed be shown."
  7710. (interactive "sRegexp: \nP")
  7711. (unless keep-previous
  7712. (org-remove-occur-highlights nil nil t))
  7713. (push (cons regexp callback) org-occur-parameters)
  7714. (let ((cnt 0))
  7715. (save-excursion
  7716. (goto-char (point-min))
  7717. (if (or (not keep-previous) ; do not want to keep
  7718. (not org-occur-highlights)) ; no previous matches
  7719. ;; hide everything
  7720. (org-overview))
  7721. (while (re-search-forward regexp nil t)
  7722. (when (or (not callback)
  7723. (save-match-data (funcall callback)))
  7724. (setq cnt (1+ cnt))
  7725. (when org-highlight-sparse-tree-matches
  7726. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  7727. (org-show-context 'occur-tree))))
  7728. (when org-remove-highlights-with-change
  7729. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  7730. nil 'local))
  7731. (unless org-sparse-tree-open-archived-trees
  7732. (org-hide-archived-subtrees (point-min) (point-max)))
  7733. (run-hooks 'org-occur-hook)
  7734. (if (interactive-p)
  7735. (message "%d match(es) for regexp %s" cnt regexp))
  7736. cnt))
  7737. (defun org-show-context (&optional key)
  7738. "Make sure point and context and visible.
  7739. How much context is shown depends upon the variables
  7740. `org-show-hierarchy-above', `org-show-following-heading'. and
  7741. `org-show-siblings'."
  7742. (let ((heading-p (org-on-heading-p t))
  7743. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  7744. (following-p (org-get-alist-option org-show-following-heading key))
  7745. (entry-p (org-get-alist-option org-show-entry-below key))
  7746. (siblings-p (org-get-alist-option org-show-siblings key)))
  7747. (catch 'exit
  7748. ;; Show heading or entry text
  7749. (if (and heading-p (not entry-p))
  7750. (org-flag-heading nil) ; only show the heading
  7751. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  7752. (org-show-hidden-entry))) ; show entire entry
  7753. (when following-p
  7754. ;; Show next sibling, or heading below text
  7755. (save-excursion
  7756. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  7757. (org-flag-heading nil))))
  7758. (when siblings-p (org-show-siblings))
  7759. (when hierarchy-p
  7760. ;; show all higher headings, possibly with siblings
  7761. (save-excursion
  7762. (while (and (condition-case nil
  7763. (progn (org-up-heading-all 1) t)
  7764. (error nil))
  7765. (not (bobp)))
  7766. (org-flag-heading nil)
  7767. (when siblings-p (org-show-siblings))))))))
  7768. (defun org-reveal (&optional siblings)
  7769. "Show current entry, hierarchy above it, and the following headline.
  7770. This can be used to show a consistent set of context around locations
  7771. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  7772. not t for the search context.
  7773. With optional argument SIBLINGS, on each level of the hierarchy all
  7774. siblings are shown. This repairs the tree structure to what it would
  7775. look like when opened with hierarchical calls to `org-cycle'."
  7776. (interactive "P")
  7777. (let ((org-show-hierarchy-above t)
  7778. (org-show-following-heading t)
  7779. (org-show-siblings (if siblings t org-show-siblings)))
  7780. (org-show-context nil)))
  7781. (defun org-highlight-new-match (beg end)
  7782. "Highlight from BEG to END and mark the highlight is an occur headline."
  7783. (let ((ov (org-make-overlay beg end)))
  7784. (org-overlay-put ov 'face 'secondary-selection)
  7785. (push ov org-occur-highlights)))
  7786. (defun org-remove-occur-highlights (&optional beg end noremove)
  7787. "Remove the occur highlights from the buffer.
  7788. BEG and END are ignored. If NOREMOVE is nil, remove this function
  7789. from the `before-change-functions' in the current buffer."
  7790. (interactive)
  7791. (unless org-inhibit-highlight-removal
  7792. (mapc 'org-delete-overlay org-occur-highlights)
  7793. (setq org-occur-highlights nil)
  7794. (setq org-occur-parameters nil)
  7795. (unless noremove
  7796. (remove-hook 'before-change-functions
  7797. 'org-remove-occur-highlights 'local))))
  7798. ;;;; Priorities
  7799. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  7800. "Regular expression matching the priority indicator.")
  7801. (defvar org-remove-priority-next-time nil)
  7802. (defun org-priority-up ()
  7803. "Increase the priority of the current item."
  7804. (interactive)
  7805. (org-priority 'up))
  7806. (defun org-priority-down ()
  7807. "Decrease the priority of the current item."
  7808. (interactive)
  7809. (org-priority 'down))
  7810. (defun org-priority (&optional action)
  7811. "Change the priority of an item by ARG.
  7812. ACTION can be `set', `up', `down', or a character."
  7813. (interactive)
  7814. (setq action (or action 'set))
  7815. (let (current new news have remove)
  7816. (save-excursion
  7817. (org-back-to-heading)
  7818. (if (looking-at org-priority-regexp)
  7819. (setq current (string-to-char (match-string 2))
  7820. have t)
  7821. (setq current org-default-priority))
  7822. (cond
  7823. ((or (eq action 'set)
  7824. (if (featurep 'xemacs) (characterp action) (integerp action)))
  7825. (if (not (eq action 'set))
  7826. (setq new action)
  7827. (message "Priority %c-%c, SPC to remove: "
  7828. org-highest-priority org-lowest-priority)
  7829. (setq new (read-char-exclusive)))
  7830. (if (and (= (upcase org-highest-priority) org-highest-priority)
  7831. (= (upcase org-lowest-priority) org-lowest-priority))
  7832. (setq new (upcase new)))
  7833. (cond ((equal new ?\ ) (setq remove t))
  7834. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  7835. (error "Priority must be between `%c' and `%c'"
  7836. org-highest-priority org-lowest-priority))))
  7837. ((eq action 'up)
  7838. (if (and (not have) (eq last-command this-command))
  7839. (setq new org-lowest-priority)
  7840. (setq new (if (and org-priority-start-cycle-with-default (not have))
  7841. org-default-priority (1- current)))))
  7842. ((eq action 'down)
  7843. (if (and (not have) (eq last-command this-command))
  7844. (setq new org-highest-priority)
  7845. (setq new (if (and org-priority-start-cycle-with-default (not have))
  7846. org-default-priority (1+ current)))))
  7847. (t (error "Invalid action")))
  7848. (if (or (< (upcase new) org-highest-priority)
  7849. (> (upcase new) org-lowest-priority))
  7850. (setq remove t))
  7851. (setq news (format "%c" new))
  7852. (if have
  7853. (if remove
  7854. (replace-match "" t t nil 1)
  7855. (replace-match news t t nil 2))
  7856. (if remove
  7857. (error "No priority cookie found in line")
  7858. (looking-at org-todo-line-regexp)
  7859. (if (match-end 2)
  7860. (progn
  7861. (goto-char (match-end 2))
  7862. (insert " [#" news "]"))
  7863. (goto-char (match-beginning 3))
  7864. (insert "[#" news "] ")))))
  7865. (org-preserve-lc (org-set-tags nil 'align))
  7866. (if remove
  7867. (message "Priority removed")
  7868. (message "Priority of current item set to %s" news))))
  7869. (defun org-get-priority (s)
  7870. "Find priority cookie and return priority."
  7871. (save-match-data
  7872. (if (not (string-match org-priority-regexp s))
  7873. (* 1000 (- org-lowest-priority org-default-priority))
  7874. (* 1000 (- org-lowest-priority
  7875. (string-to-char (match-string 2 s)))))))
  7876. ;;;; Tags
  7877. (defun org-scan-tags (action matcher &optional todo-only)
  7878. "Scan headline tags with inheritance and produce output ACTION.
  7879. ACTION can be `sparse-tree' or `agenda'. MATCHER is a Lisp form to be
  7880. evaluated, testing if a given set of tags qualifies a headline for
  7881. inclusion. When TODO-ONLY is non-nil, only lines with a TODO keyword
  7882. are included in the output."
  7883. (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
  7884. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  7885. (org-re
  7886. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  7887. (props (list 'face nil
  7888. 'done-face 'org-done
  7889. 'undone-face nil
  7890. 'mouse-face 'highlight
  7891. 'org-not-done-regexp org-not-done-regexp
  7892. 'org-todo-regexp org-todo-regexp
  7893. 'keymap org-agenda-keymap
  7894. 'help-echo
  7895. (format "mouse-2 or RET jump to org file %s"
  7896. (abbreviate-file-name
  7897. (or (buffer-file-name (buffer-base-buffer))
  7898. (buffer-name (buffer-base-buffer)))))))
  7899. (case-fold-search nil)
  7900. lspos
  7901. tags tags-list tags-alist (llast 0) rtn level category i txt
  7902. todo marker entry priority)
  7903. (save-excursion
  7904. (goto-char (point-min))
  7905. (when (eq action 'sparse-tree)
  7906. (org-overview)
  7907. (org-remove-occur-highlights))
  7908. (while (re-search-forward re nil t)
  7909. (catch :skip
  7910. (setq todo (if (match-end 1) (match-string 2))
  7911. tags (if (match-end 4) (match-string 4)))
  7912. (goto-char (setq lspos (1+ (match-beginning 0))))
  7913. (setq level (org-reduced-level (funcall outline-level))
  7914. category (org-get-category))
  7915. (setq i llast llast level)
  7916. ;; remove tag lists from same and sublevels
  7917. (while (>= i level)
  7918. (when (setq entry (assoc i tags-alist))
  7919. (setq tags-alist (delete entry tags-alist)))
  7920. (setq i (1- i)))
  7921. ;; add the next tags
  7922. (when tags
  7923. (setq tags (mapcar 'downcase (org-split-string tags ":"))
  7924. tags-alist
  7925. (cons (cons level tags) tags-alist)))
  7926. ;; compile tags for current headline
  7927. (setq tags-list
  7928. (if org-use-tag-inheritance
  7929. (apply 'append (mapcar 'cdr tags-alist))
  7930. tags))
  7931. (when (and tags org-use-tag-inheritance
  7932. (not (eq t org-use-tag-inheritance)))
  7933. ;; selective inheritance, remove uninherited ones
  7934. (setcdr (car tags-alist)
  7935. (org-remove-uniherited-tags (cdar tags-alist))))
  7936. (when (and (or (not todo-only) (member todo org-not-done-keywords))
  7937. (eval matcher)
  7938. (or (not org-agenda-skip-archived-trees)
  7939. (not (member org-archive-tag tags-list))))
  7940. (and (eq action 'agenda) (org-agenda-skip))
  7941. ;; list this headline
  7942. (if (eq action 'sparse-tree)
  7943. (progn
  7944. (and org-highlight-sparse-tree-matches
  7945. (org-get-heading) (match-end 0)
  7946. (org-highlight-new-match
  7947. (match-beginning 0) (match-beginning 1)))
  7948. (org-show-context 'tags-tree))
  7949. (setq txt (org-format-agenda-item
  7950. ""
  7951. (concat
  7952. (if org-tags-match-list-sublevels
  7953. (make-string (1- level) ?.) "")
  7954. (org-get-heading))
  7955. category tags-list)
  7956. priority (org-get-priority txt))
  7957. (goto-char lspos)
  7958. (setq marker (org-agenda-new-marker))
  7959. (org-add-props txt props
  7960. 'org-marker marker 'org-hd-marker marker 'org-category category
  7961. 'priority priority 'type "tagsmatch")
  7962. (push txt rtn))
  7963. ;; if we are to skip sublevels, jump to end of subtree
  7964. (or org-tags-match-list-sublevels (org-end-of-subtree t))))))
  7965. (when (and (eq action 'sparse-tree)
  7966. (not org-sparse-tree-open-archived-trees))
  7967. (org-hide-archived-subtrees (point-min) (point-max)))
  7968. (nreverse rtn)))
  7969. (defun org-remove-uniherited-tags (tags)
  7970. "Remove all tags that are not inherited from the list TAGS."
  7971. (cond
  7972. ((eq org-use-tag-inheritance t) tags)
  7973. ((not org-use-tag-inheritance) nil)
  7974. ((stringp org-use-tag-inheritance)
  7975. (delq nil (mapcar
  7976. (lambda (x) (if (string-match org-use-tag-inheritance x) x nil))
  7977. tags)))
  7978. ((listp org-use-tag-inheritance)
  7979. (org-delete-all org-use-tag-inheritance tags))))
  7980. (defvar todo-only) ;; dynamically scoped
  7981. (defun org-tags-sparse-tree (&optional todo-only match)
  7982. "Create a sparse tree according to tags string MATCH.
  7983. MATCH can contain positive and negative selection of tags, like
  7984. \"+WORK+URGENT-WITHBOSS\".
  7985. If optional argument TODO_ONLY is non-nil, only select lines that are
  7986. also TODO lines."
  7987. (interactive "P")
  7988. (org-prepare-agenda-buffers (list (current-buffer)))
  7989. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  7990. (defvar org-cached-props nil)
  7991. (defun org-cached-entry-get (pom property)
  7992. (if (or (eq t org-use-property-inheritance)
  7993. (and (stringp org-use-property-inheritance)
  7994. (string-match org-use-property-inheritance property))
  7995. (and (listp org-use-property-inheritance)
  7996. (member property org-use-property-inheritance)))
  7997. ;; Caching is not possible, check it directly
  7998. (org-entry-get pom property 'inherit)
  7999. ;; Get all properties, so that we can do complicated checks easily
  8000. (cdr (assoc property (or org-cached-props
  8001. (setq org-cached-props
  8002. (org-entry-properties pom)))))))
  8003. (defun org-global-tags-completion-table (&optional files)
  8004. "Return the list of all tags in all agenda buffer/files."
  8005. (save-excursion
  8006. (org-uniquify
  8007. (delq nil
  8008. (apply 'append
  8009. (mapcar
  8010. (lambda (file)
  8011. (set-buffer (find-file-noselect file))
  8012. (append (org-get-buffer-tags)
  8013. (mapcar (lambda (x) (if (stringp (car-safe x))
  8014. (list (car-safe x)) nil))
  8015. org-tag-alist)))
  8016. (if (and files (car files))
  8017. files
  8018. (org-agenda-files))))))))
  8019. (defun org-make-tags-matcher (match)
  8020. "Create the TAGS//TODO matcher form for the selection string MATCH."
  8021. ;; todo-only is scoped dynamically into this function, and the function
  8022. ;; may change it it the matcher asksk for it.
  8023. (unless match
  8024. ;; Get a new match request, with completion
  8025. (let ((org-last-tags-completion-table
  8026. (org-global-tags-completion-table)))
  8027. (setq match (completing-read
  8028. "Match: " 'org-tags-completion-function nil nil nil
  8029. 'org-tags-history))))
  8030. ;; Parse the string and create a lisp form
  8031. (let ((match0 match)
  8032. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
  8033. minus tag mm
  8034. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  8035. orterms term orlist re-p str-p level-p level-op
  8036. prop-p pn pv po cat-p gv)
  8037. (if (string-match "/+" match)
  8038. ;; match contains also a todo-matching request
  8039. (progn
  8040. (setq tagsmatch (substring match 0 (match-beginning 0))
  8041. todomatch (substring match (match-end 0)))
  8042. (if (string-match "^!" todomatch)
  8043. (setq todo-only t todomatch (substring todomatch 1)))
  8044. (if (string-match "^\\s-*$" todomatch)
  8045. (setq todomatch nil)))
  8046. ;; only matching tags
  8047. (setq tagsmatch match todomatch nil))
  8048. ;; Make the tags matcher
  8049. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  8050. (setq tagsmatcher t)
  8051. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  8052. (while (setq term (pop orterms))
  8053. (while (and (equal (substring term -1) "\\") orterms)
  8054. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  8055. (while (string-match re term)
  8056. (setq minus (and (match-end 1)
  8057. (equal (match-string 1 term) "-"))
  8058. tag (match-string 2 term)
  8059. re-p (equal (string-to-char tag) ?{)
  8060. level-p (match-end 4)
  8061. prop-p (match-end 5)
  8062. mm (cond
  8063. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  8064. (level-p
  8065. (setq level-op (org-op-to-function (match-string 3 term)))
  8066. `(,level-op level ,(string-to-number
  8067. (match-string 4 term))))
  8068. (prop-p
  8069. (setq pn (match-string 5 term)
  8070. po (match-string 6 term)
  8071. pv (match-string 7 term)
  8072. cat-p (equal pn "CATEGORY")
  8073. re-p (equal (string-to-char pv) ?{)
  8074. str-p (equal (string-to-char pv) ?\")
  8075. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  8076. (setq po (org-op-to-function po str-p))
  8077. (if (equal pn "CATEGORY")
  8078. (setq gv '(get-text-property (point) 'org-category))
  8079. (setq gv `(org-cached-entry-get nil ,pn)))
  8080. (if re-p
  8081. (if (eq po 'org<>)
  8082. `(not (string-match ,pv (or ,gv "")))
  8083. `(string-match ,pv (or ,gv "")))
  8084. (if str-p
  8085. `(,po (or ,gv "") ,pv)
  8086. `(,po (string-to-number (or ,gv ""))
  8087. ,(string-to-number pv) ))))
  8088. (t `(member ,(downcase tag) tags-list)))
  8089. mm (if minus (list 'not mm) mm)
  8090. term (substring term (match-end 0)))
  8091. (push mm tagsmatcher))
  8092. (push (if (> (length tagsmatcher) 1)
  8093. (cons 'and tagsmatcher)
  8094. (car tagsmatcher))
  8095. orlist)
  8096. (setq tagsmatcher nil))
  8097. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  8098. (setq tagsmatcher
  8099. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  8100. ;; Make the todo matcher
  8101. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  8102. (setq todomatcher t)
  8103. (setq orterms (org-split-string todomatch "|") orlist nil)
  8104. (while (setq term (pop orterms))
  8105. (while (string-match re term)
  8106. (setq minus (and (match-end 1)
  8107. (equal (match-string 1 term) "-"))
  8108. kwd (match-string 2 term)
  8109. re-p (equal (string-to-char kwd) ?{)
  8110. term (substring term (match-end 0))
  8111. mm (if re-p
  8112. `(string-match ,(substring kwd 1 -1) todo)
  8113. (list 'equal 'todo kwd))
  8114. mm (if minus (list 'not mm) mm))
  8115. (push mm todomatcher))
  8116. (push (if (> (length todomatcher) 1)
  8117. (cons 'and todomatcher)
  8118. (car todomatcher))
  8119. orlist)
  8120. (setq todomatcher nil))
  8121. (setq todomatcher (if (> (length orlist) 1)
  8122. (cons 'or orlist) (car orlist))))
  8123. ;; Return the string and lisp forms of the matcher
  8124. (setq matcher (if todomatcher
  8125. (list 'and tagsmatcher todomatcher)
  8126. tagsmatcher))
  8127. (cons match0 matcher)))
  8128. (defun org-op-to-function (op &optional stringp)
  8129. (setq op
  8130. (cond
  8131. ((equal op "<" ) '(< string< ))
  8132. ((equal op ">" ) '(> org-string> ))
  8133. ((member op '("<=" "=<")) '(<= org-string<= ))
  8134. ((member op '(">=" "=>")) '(>= org-string>= ))
  8135. ((member op '("=" "==")) '(= string= ))
  8136. ((member op '("<>" "!=")) '(org<> org-string<> ))))
  8137. (nth (if stringp 1 0) op))
  8138. (defun org<> (a b) (not (= a b)))
  8139. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  8140. (defun org-string>= (a b) (not (string< a b)))
  8141. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  8142. (defun org-string<> (a b) (not (string= a b)))
  8143. (defun org-match-any-p (re list)
  8144. "Does re match any element of list?"
  8145. (setq list (mapcar (lambda (x) (string-match re x)) list))
  8146. (delq nil list))
  8147. (defvar org-add-colon-after-tag-completion nil) ;; dynamically skoped param
  8148. (defvar org-tags-overlay (org-make-overlay 1 1))
  8149. (org-detach-overlay org-tags-overlay)
  8150. (defun org-get-tags-at (&optional pos)
  8151. "Get a list of all headline tags applicable at POS.
  8152. POS defaults to point. If tags are inherited, the list contains
  8153. the targets in the same sequence as the headlines appear, i.e.
  8154. sthe tags of the current headline come last."
  8155. (interactive)
  8156. (let (tags ltags lastpos parent)
  8157. (save-excursion
  8158. (save-restriction
  8159. (widen)
  8160. (goto-char (or pos (point)))
  8161. (save-match-data
  8162. (condition-case nil
  8163. (progn
  8164. (org-back-to-heading t)
  8165. (while (not (equal lastpos (point)))
  8166. (setq lastpos (point))
  8167. (when (looking-at (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  8168. (setq ltags (org-split-string
  8169. (org-match-string-no-properties 1) ":"))
  8170. (setq tags (append (org-remove-uniherited-tags ltags)
  8171. tags)))
  8172. (or org-use-tag-inheritance (error ""))
  8173. (org-up-heading-all 1)
  8174. (setq parent t)))
  8175. (error nil))))
  8176. tags)))
  8177. (defun org-toggle-tag (tag &optional onoff)
  8178. "Toggle the tag TAG for the current line.
  8179. If ONOFF is `on' or `off', don't toggle but set to this state."
  8180. (unless (org-on-heading-p t) (error "Not on headling"))
  8181. (let (res current)
  8182. (save-excursion
  8183. (beginning-of-line)
  8184. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  8185. (point-at-eol) t)
  8186. (progn
  8187. (setq current (match-string 1))
  8188. (replace-match ""))
  8189. (setq current ""))
  8190. (setq current (nreverse (org-split-string current ":")))
  8191. (cond
  8192. ((eq onoff 'on)
  8193. (setq res t)
  8194. (or (member tag current) (push tag current)))
  8195. ((eq onoff 'off)
  8196. (or (not (member tag current)) (setq current (delete tag current))))
  8197. (t (if (member tag current)
  8198. (setq current (delete tag current))
  8199. (setq res t)
  8200. (push tag current))))
  8201. (end-of-line 1)
  8202. (if current
  8203. (progn
  8204. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  8205. (org-set-tags nil t))
  8206. (delete-horizontal-space))
  8207. (run-hooks 'org-after-tags-change-hook))
  8208. res))
  8209. (defun org-align-tags-here (to-col)
  8210. ;; Assumes that this is a headline
  8211. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  8212. (beginning-of-line 1)
  8213. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  8214. (< pos (match-beginning 2)))
  8215. (progn
  8216. (setq tags-l (- (match-end 2) (match-beginning 2)))
  8217. (goto-char (match-beginning 1))
  8218. (insert " ")
  8219. (delete-region (point) (1+ (match-beginning 2)))
  8220. (setq ncol (max (1+ (current-column))
  8221. (1+ col)
  8222. (if (> to-col 0)
  8223. to-col
  8224. (- (abs to-col) tags-l))))
  8225. (setq p (point))
  8226. (insert (make-string (- ncol (current-column)) ?\ ))
  8227. (setq ncol (current-column))
  8228. (tabify p (point-at-eol))
  8229. (org-move-to-column (min ncol col) t))
  8230. (goto-char pos))))
  8231. (defun org-set-tags (&optional arg just-align)
  8232. "Set the tags for the current headline.
  8233. With prefix ARG, realign all tags in headings in the current buffer."
  8234. (interactive "P")
  8235. (let* ((re (concat "^" outline-regexp))
  8236. (current (org-get-tags-string))
  8237. (col (current-column))
  8238. (org-setting-tags t)
  8239. table current-tags inherited-tags ; computed below when needed
  8240. tags p0 c0 c1 rpl)
  8241. (if arg
  8242. (save-excursion
  8243. (goto-char (point-min))
  8244. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  8245. (while (re-search-forward re nil t)
  8246. (org-set-tags nil t)
  8247. (end-of-line 1)))
  8248. (message "All tags realigned to column %d" org-tags-column))
  8249. (if just-align
  8250. (setq tags current)
  8251. ;; Get a new set of tags from the user
  8252. (save-excursion
  8253. (setq table (or org-tag-alist (org-get-buffer-tags))
  8254. org-last-tags-completion-table table
  8255. current-tags (org-split-string current ":")
  8256. inherited-tags (nreverse
  8257. (nthcdr (length current-tags)
  8258. (nreverse (org-get-tags-at))))
  8259. tags
  8260. (if (or (eq t org-use-fast-tag-selection)
  8261. (and org-use-fast-tag-selection
  8262. (delq nil (mapcar 'cdr table))))
  8263. (org-fast-tag-selection
  8264. current-tags inherited-tags table
  8265. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  8266. (let ((org-add-colon-after-tag-completion t))
  8267. (org-trim
  8268. (org-without-partial-completion
  8269. (completing-read "Tags: " 'org-tags-completion-function
  8270. nil nil current 'org-tags-history)))))))
  8271. (while (string-match "[-+&]+" tags)
  8272. ;; No boolean logic, just a list
  8273. (setq tags (replace-match ":" t t tags))))
  8274. (if (string-match "\\`[\t ]*\\'" tags)
  8275. (setq tags "")
  8276. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  8277. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  8278. ;; Insert new tags at the correct column
  8279. (beginning-of-line 1)
  8280. (cond
  8281. ((and (equal current "") (equal tags "")))
  8282. ((re-search-forward
  8283. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  8284. (point-at-eol) t)
  8285. (if (equal tags "")
  8286. (setq rpl "")
  8287. (goto-char (match-beginning 0))
  8288. (setq c0 (current-column) p0 (point)
  8289. c1 (max (1+ c0) (if (> org-tags-column 0)
  8290. org-tags-column
  8291. (- (- org-tags-column) (length tags))))
  8292. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  8293. (replace-match rpl t t)
  8294. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  8295. tags)
  8296. (t (error "Tags alignment failed")))
  8297. (org-move-to-column col)
  8298. (unless just-align
  8299. (run-hooks 'org-after-tags-change-hook)))))
  8300. (defun org-change-tag-in-region (beg end tag off)
  8301. "Add or remove TAG for each entry in the region.
  8302. This works in the agenda, and also in an org-mode buffer."
  8303. (interactive
  8304. (list (region-beginning) (region-end)
  8305. (let ((org-last-tags-completion-table
  8306. (if (org-mode-p)
  8307. (org-get-buffer-tags)
  8308. (org-global-tags-completion-table))))
  8309. (completing-read
  8310. "Tag: " 'org-tags-completion-function nil nil nil
  8311. 'org-tags-history))
  8312. (progn
  8313. (message "[s]et or [r]emove? ")
  8314. (equal (read-char-exclusive) ?r))))
  8315. (if (fboundp 'deactivate-mark) (deactivate-mark))
  8316. (let ((agendap (equal major-mode 'org-agenda-mode))
  8317. l1 l2 m buf pos newhead (cnt 0))
  8318. (goto-char end)
  8319. (setq l2 (1- (org-current-line)))
  8320. (goto-char beg)
  8321. (setq l1 (org-current-line))
  8322. (loop for l from l1 to l2 do
  8323. (goto-line l)
  8324. (setq m (get-text-property (point) 'org-hd-marker))
  8325. (when (or (and (org-mode-p) (org-on-heading-p))
  8326. (and agendap m))
  8327. (setq buf (if agendap (marker-buffer m) (current-buffer))
  8328. pos (if agendap m (point)))
  8329. (with-current-buffer buf
  8330. (save-excursion
  8331. (save-restriction
  8332. (goto-char pos)
  8333. (setq cnt (1+ cnt))
  8334. (org-toggle-tag tag (if off 'off 'on))
  8335. (setq newhead (org-get-heading)))))
  8336. (and agendap (org-agenda-change-all-lines newhead m))))
  8337. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  8338. (defun org-tags-completion-function (string predicate &optional flag)
  8339. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  8340. (confirm (lambda (x) (stringp (car x)))))
  8341. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  8342. (setq s1 (match-string 1 string)
  8343. s2 (match-string 2 string))
  8344. (setq s1 "" s2 string))
  8345. (cond
  8346. ((eq flag nil)
  8347. ;; try completion
  8348. (setq rtn (try-completion s2 ctable confirm))
  8349. (if (stringp rtn)
  8350. (setq rtn
  8351. (concat s1 s2 (substring rtn (length s2))
  8352. (if (and org-add-colon-after-tag-completion
  8353. (assoc rtn ctable))
  8354. ":" ""))))
  8355. rtn)
  8356. ((eq flag t)
  8357. ;; all-completions
  8358. (all-completions s2 ctable confirm)
  8359. )
  8360. ((eq flag 'lambda)
  8361. ;; exact match?
  8362. (assoc s2 ctable)))
  8363. ))
  8364. (defun org-fast-tag-insert (kwd tags face &optional end)
  8365. "Insert KDW, and the TAGS, the latter with face FACE. Also inser END."
  8366. (insert (format "%-12s" (concat kwd ":"))
  8367. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  8368. (or end "")))
  8369. (defun org-fast-tag-show-exit (flag)
  8370. (save-excursion
  8371. (goto-line 3)
  8372. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  8373. (replace-match ""))
  8374. (when flag
  8375. (end-of-line 1)
  8376. (org-move-to-column (- (window-width) 19) t)
  8377. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  8378. (defun org-set-current-tags-overlay (current prefix)
  8379. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  8380. (if (featurep 'xemacs)
  8381. (org-overlay-display org-tags-overlay (concat prefix s)
  8382. 'secondary-selection)
  8383. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  8384. (org-overlay-display org-tags-overlay (concat prefix s)))))
  8385. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  8386. "Fast tag selection with single keys.
  8387. CURRENT is the current list of tags in the headline, INHERITED is the
  8388. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  8389. possibly with grouping information. TODO-TABLE is a similar table with
  8390. TODO keywords, should these have keys assigned to them.
  8391. If the keys are nil, a-z are automatically assigned.
  8392. Returns the new tags string, or nil to not change the current settings."
  8393. (let* ((fulltable (append table todo-table))
  8394. (maxlen (apply 'max (mapcar
  8395. (lambda (x)
  8396. (if (stringp (car x)) (string-width (car x)) 0))
  8397. fulltable)))
  8398. (buf (current-buffer))
  8399. (expert (eq org-fast-tag-selection-single-key 'expert))
  8400. (buffer-tags nil)
  8401. (fwidth (+ maxlen 3 1 3))
  8402. (ncol (/ (- (window-width) 4) fwidth))
  8403. (i-face 'org-done)
  8404. (c-face 'org-todo)
  8405. tg cnt e c char c1 c2 ntable tbl rtn
  8406. ov-start ov-end ov-prefix
  8407. (exit-after-next org-fast-tag-selection-single-key)
  8408. (done-keywords org-done-keywords)
  8409. groups ingroup)
  8410. (save-excursion
  8411. (beginning-of-line 1)
  8412. (if (looking-at
  8413. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  8414. (setq ov-start (match-beginning 1)
  8415. ov-end (match-end 1)
  8416. ov-prefix "")
  8417. (setq ov-start (1- (point-at-eol))
  8418. ov-end (1+ ov-start))
  8419. (skip-chars-forward "^\n\r")
  8420. (setq ov-prefix
  8421. (concat
  8422. (buffer-substring (1- (point)) (point))
  8423. (if (> (current-column) org-tags-column)
  8424. " "
  8425. (make-string (- org-tags-column (current-column)) ?\ ))))))
  8426. (org-move-overlay org-tags-overlay ov-start ov-end)
  8427. (save-window-excursion
  8428. (if expert
  8429. (set-buffer (get-buffer-create " *Org tags*"))
  8430. (delete-other-windows)
  8431. (split-window-vertically)
  8432. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  8433. (erase-buffer)
  8434. (org-set-local 'org-done-keywords done-keywords)
  8435. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  8436. (org-fast-tag-insert "Current" current c-face "\n\n")
  8437. (org-fast-tag-show-exit exit-after-next)
  8438. (org-set-current-tags-overlay current ov-prefix)
  8439. (setq tbl fulltable char ?a cnt 0)
  8440. (while (setq e (pop tbl))
  8441. (cond
  8442. ((equal e '(:startgroup))
  8443. (push '() groups) (setq ingroup t)
  8444. (when (not (= cnt 0))
  8445. (setq cnt 0)
  8446. (insert "\n"))
  8447. (insert "{ "))
  8448. ((equal e '(:endgroup))
  8449. (setq ingroup nil cnt 0)
  8450. (insert "}\n"))
  8451. (t
  8452. (setq tg (car e) c2 nil)
  8453. (if (cdr e)
  8454. (setq c (cdr e))
  8455. ;; automatically assign a character.
  8456. (setq c1 (string-to-char
  8457. (downcase (substring
  8458. tg (if (= (string-to-char tg) ?@) 1 0)))))
  8459. (if (or (rassoc c1 ntable) (rassoc c1 table))
  8460. (while (or (rassoc char ntable) (rassoc char table))
  8461. (setq char (1+ char)))
  8462. (setq c2 c1))
  8463. (setq c (or c2 char)))
  8464. (if ingroup (push tg (car groups)))
  8465. (setq tg (org-add-props tg nil 'face
  8466. (cond
  8467. ((not (assoc tg table))
  8468. (org-get-todo-face tg))
  8469. ((member tg current) c-face)
  8470. ((member tg inherited) i-face)
  8471. (t nil))))
  8472. (if (and (= cnt 0) (not ingroup)) (insert " "))
  8473. (insert "[" c "] " tg (make-string
  8474. (- fwidth 4 (length tg)) ?\ ))
  8475. (push (cons tg c) ntable)
  8476. (when (= (setq cnt (1+ cnt)) ncol)
  8477. (insert "\n")
  8478. (if ingroup (insert " "))
  8479. (setq cnt 0)))))
  8480. (setq ntable (nreverse ntable))
  8481. (insert "\n")
  8482. (goto-char (point-min))
  8483. (if (and (not expert) (fboundp 'fit-window-to-buffer))
  8484. (fit-window-to-buffer))
  8485. (setq rtn
  8486. (catch 'exit
  8487. (while t
  8488. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  8489. (if groups " [!] no groups" " [!]groups")
  8490. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  8491. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  8492. (cond
  8493. ((= c ?\r) (throw 'exit t))
  8494. ((= c ?!)
  8495. (setq groups (not groups))
  8496. (goto-char (point-min))
  8497. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  8498. ((= c ?\C-c)
  8499. (if (not expert)
  8500. (org-fast-tag-show-exit
  8501. (setq exit-after-next (not exit-after-next)))
  8502. (setq expert nil)
  8503. (delete-other-windows)
  8504. (split-window-vertically)
  8505. (org-switch-to-buffer-other-window " *Org tags*")
  8506. (and (fboundp 'fit-window-to-buffer)
  8507. (fit-window-to-buffer))))
  8508. ((or (= c ?\C-g)
  8509. (and (= c ?q) (not (rassoc c ntable))))
  8510. (org-detach-overlay org-tags-overlay)
  8511. (setq quit-flag t))
  8512. ((= c ?\ )
  8513. (setq current nil)
  8514. (if exit-after-next (setq exit-after-next 'now)))
  8515. ((= c ?\t)
  8516. (condition-case nil
  8517. (setq tg (completing-read
  8518. "Tag: "
  8519. (or buffer-tags
  8520. (with-current-buffer buf
  8521. (org-get-buffer-tags)))))
  8522. (quit (setq tg "")))
  8523. (when (string-match "\\S-" tg)
  8524. (add-to-list 'buffer-tags (list tg))
  8525. (if (member tg current)
  8526. (setq current (delete tg current))
  8527. (push tg current)))
  8528. (if exit-after-next (setq exit-after-next 'now)))
  8529. ((setq e (rassoc c todo-table) tg (car e))
  8530. (with-current-buffer buf
  8531. (save-excursion (org-todo tg)))
  8532. (if exit-after-next (setq exit-after-next 'now)))
  8533. ((setq e (rassoc c ntable) tg (car e))
  8534. (if (member tg current)
  8535. (setq current (delete tg current))
  8536. (loop for g in groups do
  8537. (if (member tg g)
  8538. (mapc (lambda (x)
  8539. (setq current (delete x current)))
  8540. g)))
  8541. (push tg current))
  8542. (if exit-after-next (setq exit-after-next 'now))))
  8543. ;; Create a sorted list
  8544. (setq current
  8545. (sort current
  8546. (lambda (a b)
  8547. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  8548. (if (eq exit-after-next 'now) (throw 'exit t))
  8549. (goto-char (point-min))
  8550. (beginning-of-line 2)
  8551. (delete-region (point) (point-at-eol))
  8552. (org-fast-tag-insert "Current" current c-face)
  8553. (org-set-current-tags-overlay current ov-prefix)
  8554. (while (re-search-forward
  8555. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  8556. (setq tg (match-string 1))
  8557. (add-text-properties
  8558. (match-beginning 1) (match-end 1)
  8559. (list 'face
  8560. (cond
  8561. ((member tg current) c-face)
  8562. ((member tg inherited) i-face)
  8563. (t (get-text-property (match-beginning 1) 'face))))))
  8564. (goto-char (point-min)))))
  8565. (org-detach-overlay org-tags-overlay)
  8566. (if rtn
  8567. (mapconcat 'identity current ":")
  8568. nil))))
  8569. (defun org-get-tags-string ()
  8570. "Get the TAGS string in the current headline."
  8571. (unless (org-on-heading-p t)
  8572. (error "Not on a heading"))
  8573. (save-excursion
  8574. (beginning-of-line 1)
  8575. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  8576. (org-match-string-no-properties 1)
  8577. "")))
  8578. (defun org-get-tags ()
  8579. "Get the list of tags specified in the current headline."
  8580. (org-split-string (org-get-tags-string) ":"))
  8581. (defun org-get-buffer-tags ()
  8582. "Get a table of all tags used in the buffer, for completion."
  8583. (let (tags)
  8584. (save-excursion
  8585. (goto-char (point-min))
  8586. (while (re-search-forward
  8587. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  8588. (when (equal (char-after (point-at-bol 0)) ?*)
  8589. (mapc (lambda (x) (add-to-list 'tags x))
  8590. (org-split-string (org-match-string-no-properties 1) ":")))))
  8591. (mapcar 'list tags)))
  8592. ;;;; Properties
  8593. ;;; Setting and retrieving properties
  8594. (defconst org-special-properties
  8595. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "PRIORITY"
  8596. "TIMESTAMP" "TIMESTAMP_IA")
  8597. "The special properties valid in Org-mode.
  8598. These are properties that are not defined in the property drawer,
  8599. but in some other way.")
  8600. (defconst org-default-properties
  8601. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION"
  8602. "LOCATION" "LOGGING" "COLUMNS")
  8603. "Some properties that are used by Org-mode for various purposes.
  8604. Being in this list makes sure that they are offered for completion.")
  8605. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  8606. "Regular expression matching the first line of a property drawer.")
  8607. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  8608. "Regular expression matching the first line of a property drawer.")
  8609. (defun org-property-action ()
  8610. "Do an action on properties."
  8611. (interactive)
  8612. (let (c)
  8613. (org-at-property-p)
  8614. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  8615. (setq c (read-char-exclusive))
  8616. (cond
  8617. ((equal c ?s)
  8618. (call-interactively 'org-set-property))
  8619. ((equal c ?d)
  8620. (call-interactively 'org-delete-property))
  8621. ((equal c ?D)
  8622. (call-interactively 'org-delete-property-globally))
  8623. ((equal c ?c)
  8624. (call-interactively 'org-compute-property-at-point))
  8625. (t (error "No such property action %c" c)))))
  8626. (defun org-at-property-p ()
  8627. "Is the cursor in a property line?"
  8628. ;; FIXME: Does not check if we are actually in the drawer.
  8629. ;; FIXME: also returns true on any drawers.....
  8630. ;; This is used by C-c C-c for property action.
  8631. (save-excursion
  8632. (beginning-of-line 1)
  8633. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  8634. (defun org-get-property-block (&optional beg end force)
  8635. "Return the (beg . end) range of the body of the property drawer.
  8636. BEG and END can be beginning and end of subtree, if not given
  8637. they will be found.
  8638. If the drawer does not exist and FORCE is non-nil, create the drawer."
  8639. (catch 'exit
  8640. (save-excursion
  8641. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  8642. (end (or end (progn (outline-next-heading) (point)))))
  8643. (goto-char beg)
  8644. (if (re-search-forward org-property-start-re end t)
  8645. (setq beg (1+ (match-end 0)))
  8646. (if force
  8647. (save-excursion
  8648. (org-insert-property-drawer)
  8649. (setq end (progn (outline-next-heading) (point))))
  8650. (throw 'exit nil))
  8651. (goto-char beg)
  8652. (if (re-search-forward org-property-start-re end t)
  8653. (setq beg (1+ (match-end 0)))))
  8654. (if (re-search-forward org-property-end-re end t)
  8655. (setq end (match-beginning 0))
  8656. (or force (throw 'exit nil))
  8657. (goto-char beg)
  8658. (setq end beg)
  8659. (org-indent-line-function)
  8660. (insert ":END:\n"))
  8661. (cons beg end)))))
  8662. (defun org-entry-properties (&optional pom which)
  8663. "Get all properties of the entry at point-or-marker POM.
  8664. This includes the TODO keyword, the tags, time strings for deadline,
  8665. scheduled, and clocking, and any additional properties defined in the
  8666. entry. The return value is an alist, keys may occur multiple times
  8667. if the property key was used several times.
  8668. POM may also be nil, in which case the current entry is used.
  8669. If WHICH is nil or `all', get all properties. If WHICH is
  8670. `special' or `standard', only get that subclass."
  8671. (setq which (or which 'all))
  8672. (org-with-point-at pom
  8673. (let ((clockstr (substring org-clock-string 0 -1))
  8674. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  8675. beg end range props sum-props key value string clocksum)
  8676. (save-excursion
  8677. (when (condition-case nil (org-back-to-heading t) (error nil))
  8678. (setq beg (point))
  8679. (setq sum-props (get-text-property (point) 'org-summaries))
  8680. (setq clocksum (get-text-property (point) :org-clock-minutes))
  8681. (outline-next-heading)
  8682. (setq end (point))
  8683. (when (memq which '(all special))
  8684. ;; Get the special properties, like TODO and tags
  8685. (goto-char beg)
  8686. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  8687. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  8688. (when (looking-at org-priority-regexp)
  8689. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  8690. (when (and (setq value (org-get-tags-string))
  8691. (string-match "\\S-" value))
  8692. (push (cons "TAGS" value) props))
  8693. (when (setq value (org-get-tags-at))
  8694. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  8695. props))
  8696. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  8697. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  8698. string (if (equal key clockstr)
  8699. (org-no-properties
  8700. (org-trim
  8701. (buffer-substring
  8702. (match-beginning 3) (goto-char (point-at-eol)))))
  8703. (substring (org-match-string-no-properties 3) 1 -1)))
  8704. (unless key
  8705. (if (= (char-after (match-beginning 3)) ?\[)
  8706. (setq key "TIMESTAMP_IA")
  8707. (setq key "TIMESTAMP")))
  8708. (when (or (equal key clockstr) (not (assoc key props)))
  8709. (push (cons key string) props)))
  8710. )
  8711. (when (memq which '(all standard))
  8712. ;; Get the standard properties, like :PORP: ...
  8713. (setq range (org-get-property-block beg end))
  8714. (when range
  8715. (goto-char (car range))
  8716. (while (re-search-forward
  8717. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  8718. (cdr range) t)
  8719. (setq key (org-match-string-no-properties 1)
  8720. value (org-trim (or (org-match-string-no-properties 2) "")))
  8721. (unless (member key excluded)
  8722. (push (cons key (or value "")) props)))))
  8723. (if clocksum
  8724. (push (cons "CLOCKSUM"
  8725. (org-columns-number-to-string (/ (float clocksum) 60.)
  8726. 'add_times))
  8727. props))
  8728. (append sum-props (nreverse props)))))))
  8729. (defun org-entry-get (pom property &optional inherit)
  8730. "Get value of PROPERTY for entry at point-or-marker POM.
  8731. If INHERIT is non-nil and the entry does not have the property,
  8732. then also check higher levels of the hierarchy.
  8733. If INHERIT is the symbol `selective', use inheritance only if the setting
  8734. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  8735. If the property is present but empty, the return value is the empty string.
  8736. If the property is not present at all, nil is returned."
  8737. (org-with-point-at pom
  8738. (if (and inherit (if (eq inherit 'selective)
  8739. (org-property-inherit-p property)
  8740. t))
  8741. (org-entry-get-with-inheritance property)
  8742. (if (member property org-special-properties)
  8743. ;; We need a special property. Use brute force, get all properties.
  8744. (cdr (assoc property (org-entry-properties nil 'special)))
  8745. (let ((range (org-get-property-block)))
  8746. (if (and range
  8747. (goto-char (car range))
  8748. (re-search-forward
  8749. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)?")
  8750. (cdr range) t))
  8751. ;; Found the property, return it.
  8752. (if (match-end 1)
  8753. (org-match-string-no-properties 1)
  8754. "")))))))
  8755. (defun org-property-or-variable-value (var &optional inherit)
  8756. "Check if there is a property fixing the value of VAR.
  8757. If yes, return this value. If not, return the current value of the variable."
  8758. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  8759. (if (and prop (stringp prop) (string-match "\\S-" prop))
  8760. (read prop)
  8761. (symbol-value var))))
  8762. (defun org-entry-delete (pom property)
  8763. "Delete the property PROPERTY from entry at point-or-marker POM."
  8764. (org-with-point-at pom
  8765. (if (member property org-special-properties)
  8766. nil ; cannot delete these properties.
  8767. (let ((range (org-get-property-block)))
  8768. (if (and range
  8769. (goto-char (car range))
  8770. (re-search-forward
  8771. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)")
  8772. (cdr range) t))
  8773. (progn
  8774. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  8775. t)
  8776. nil)))))
  8777. ;; Multi-values properties are properties that contain multiple values
  8778. ;; These values are assumed to be single words, separated by whitespace.
  8779. (defun org-entry-add-to-multivalued-property (pom property value)
  8780. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  8781. (let* ((old (org-entry-get pom property))
  8782. (values (and old (org-split-string old "[ \t]"))))
  8783. (unless (member value values)
  8784. (setq values (cons value values))
  8785. (org-entry-put pom property
  8786. (mapconcat 'identity values " ")))))
  8787. (defun org-entry-remove-from-multivalued-property (pom property value)
  8788. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  8789. (let* ((old (org-entry-get pom property))
  8790. (values (and old (org-split-string old "[ \t]"))))
  8791. (when (member value values)
  8792. (setq values (delete value values))
  8793. (org-entry-put pom property
  8794. (mapconcat 'identity values " ")))))
  8795. (defun org-entry-member-in-multivalued-property (pom property value)
  8796. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  8797. (let* ((old (org-entry-get pom property))
  8798. (values (and old (org-split-string old "[ \t]"))))
  8799. (member value values)))
  8800. (defvar org-entry-property-inherited-from (make-marker))
  8801. (defun org-entry-get-with-inheritance (property)
  8802. "Get entry property, and search higher levels if not present."
  8803. (let (tmp)
  8804. (save-excursion
  8805. (save-restriction
  8806. (widen)
  8807. (catch 'ex
  8808. (while t
  8809. (when (setq tmp (org-entry-get nil property))
  8810. (org-back-to-heading t)
  8811. (move-marker org-entry-property-inherited-from (point))
  8812. (throw 'ex tmp))
  8813. (or (org-up-heading-safe) (throw 'ex nil)))))
  8814. (or tmp (cdr (assoc property org-local-properties))
  8815. (cdr (assoc property org-global-properties))))))
  8816. (defun org-entry-put (pom property value)
  8817. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  8818. (org-with-point-at pom
  8819. (org-back-to-heading t)
  8820. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  8821. range)
  8822. (cond
  8823. ((equal property "TODO")
  8824. (when (and (stringp value) (string-match "\\S-" value)
  8825. (not (member value org-todo-keywords-1)))
  8826. (error "\"%s\" is not a valid TODO state" value))
  8827. (if (or (not value)
  8828. (not (string-match "\\S-" value)))
  8829. (setq value 'none))
  8830. (org-todo value)
  8831. (org-set-tags nil 'align))
  8832. ((equal property "PRIORITY")
  8833. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  8834. (string-to-char value) ?\ ))
  8835. (org-set-tags nil 'align))
  8836. ((equal property "SCHEDULED")
  8837. (if (re-search-forward org-scheduled-time-regexp end t)
  8838. (cond
  8839. ((eq value 'earlier) (org-timestamp-change -1 'day))
  8840. ((eq value 'later) (org-timestamp-change 1 'day))
  8841. (t (call-interactively 'org-schedule)))
  8842. (call-interactively 'org-schedule)))
  8843. ((equal property "DEADLINE")
  8844. (if (re-search-forward org-deadline-time-regexp end t)
  8845. (cond
  8846. ((eq value 'earlier) (org-timestamp-change -1 'day))
  8847. ((eq value 'later) (org-timestamp-change 1 'day))
  8848. (t (call-interactively 'org-deadline)))
  8849. (call-interactively 'org-deadline)))
  8850. ((member property org-special-properties)
  8851. (error "The %s property can not yet be set with `org-entry-put'"
  8852. property))
  8853. (t ; a non-special property
  8854. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  8855. (setq range (org-get-property-block beg end 'force))
  8856. (goto-char (car range))
  8857. (if (re-search-forward
  8858. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  8859. (progn
  8860. (delete-region (match-beginning 1) (match-end 1))
  8861. (goto-char (match-beginning 1)))
  8862. (goto-char (cdr range))
  8863. (insert "\n")
  8864. (backward-char 1)
  8865. (org-indent-line-function)
  8866. (insert ":" property ":"))
  8867. (and value (insert " " value))
  8868. (org-indent-line-function)))))))
  8869. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  8870. "Get all property keys in the current buffer.
  8871. With INCLUDE-SPECIALS, also list the special properties that relect things
  8872. like tags and TODO state.
  8873. With INCLUDE-DEFAULTS, also include properties that has special meaning
  8874. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  8875. With INCLUDE-COLUMNS, also include property names given in COLUMN
  8876. formats in the current buffer."
  8877. (let (rtn range cfmt cols s p)
  8878. (save-excursion
  8879. (save-restriction
  8880. (widen)
  8881. (goto-char (point-min))
  8882. (while (re-search-forward org-property-start-re nil t)
  8883. (setq range (org-get-property-block))
  8884. (goto-char (car range))
  8885. (while (re-search-forward
  8886. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  8887. (cdr range) t)
  8888. (add-to-list 'rtn (org-match-string-no-properties 1)))
  8889. (outline-next-heading))))
  8890. (when include-specials
  8891. (setq rtn (append org-special-properties rtn)))
  8892. (when include-defaults
  8893. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties))
  8894. (when include-columns
  8895. (save-excursion
  8896. (save-restriction
  8897. (widen)
  8898. (goto-char (point-min))
  8899. (while (re-search-forward
  8900. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  8901. nil t)
  8902. (setq cfmt (match-string 2) s 0)
  8903. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  8904. cfmt s)
  8905. (setq s (match-end 0)
  8906. p (match-string 1 cfmt))
  8907. (unless (or (equal p "ITEM")
  8908. (member p org-special-properties))
  8909. (add-to-list 'rtn (match-string 1 cfmt))))))))
  8910. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  8911. (defun org-property-values (key)
  8912. "Return a list of all values of property KEY."
  8913. (save-excursion
  8914. (save-restriction
  8915. (widen)
  8916. (goto-char (point-min))
  8917. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  8918. values)
  8919. (while (re-search-forward re nil t)
  8920. (add-to-list 'values (org-trim (match-string 1))))
  8921. (delete "" values)))))
  8922. (defun org-insert-property-drawer ()
  8923. "Insert a property drawer into the current entry."
  8924. (interactive)
  8925. (org-back-to-heading t)
  8926. (looking-at outline-regexp)
  8927. (let ((indent (- (match-end 0)(match-beginning 0)))
  8928. (beg (point))
  8929. (re (concat "^[ \t]*" org-keyword-time-regexp))
  8930. end hiddenp)
  8931. (outline-next-heading)
  8932. (setq end (point))
  8933. (goto-char beg)
  8934. (while (re-search-forward re end t))
  8935. (setq hiddenp (org-invisible-p))
  8936. (end-of-line 1)
  8937. (and (equal (char-after) ?\n) (forward-char 1))
  8938. (while (looking-at "^[ \t]*\\(:CLOCK:\\|CLOCK\\|:END:\\)")
  8939. (beginning-of-line 2))
  8940. (org-skip-over-state-notes)
  8941. (skip-chars-backward " \t\n\r")
  8942. (if (eq (char-before) ?*) (forward-char 1))
  8943. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  8944. (beginning-of-line 0)
  8945. (org-indent-to-column indent)
  8946. (beginning-of-line 2)
  8947. (org-indent-to-column indent)
  8948. (beginning-of-line 0)
  8949. (if hiddenp
  8950. (save-excursion
  8951. (org-back-to-heading t)
  8952. (hide-entry))
  8953. (org-flag-drawer t))))
  8954. (defun org-set-property (property value)
  8955. "In the current entry, set PROPERTY to VALUE.
  8956. When called interactively, this will prompt for a property name, offering
  8957. completion on existing and default properties. And then it will prompt
  8958. for a value, offering competion either on allowed values (via an inherited
  8959. xxx_ALL property) or on existing values in other instances of this property
  8960. in the current file."
  8961. (interactive
  8962. (let* ((completion-ignore-case t)
  8963. (keys (org-buffer-property-keys nil t t))
  8964. (prop0 (completing-read "Property: " (mapcar 'list keys)))
  8965. (prop (if (member prop0 keys)
  8966. prop0
  8967. (or (cdr (assoc (downcase prop0)
  8968. (mapcar (lambda (x) (cons (downcase x) x))
  8969. keys)))
  8970. prop0)))
  8971. (cur (org-entry-get nil prop))
  8972. (allowed (org-property-get-allowed-values nil prop 'table))
  8973. (existing (mapcar 'list (org-property-values prop)))
  8974. (val (if allowed
  8975. (org-completing-read "Value: " allowed nil 'req-match)
  8976. (org-completing-read
  8977. (concat "Value" (if (and cur (string-match "\\S-" cur))
  8978. (concat "[" cur "]") "")
  8979. ": ")
  8980. existing nil nil "" nil cur))))
  8981. (list prop (if (equal val "") cur val))))
  8982. (unless (equal (org-entry-get nil property) value)
  8983. (org-entry-put nil property value)))
  8984. (defun org-delete-property (property)
  8985. "In the current entry, delete PROPERTY."
  8986. (interactive
  8987. (let* ((completion-ignore-case t)
  8988. (prop (completing-read
  8989. "Property: " (org-entry-properties nil 'standard))))
  8990. (list prop)))
  8991. (message "Property %s %s" property
  8992. (if (org-entry-delete nil property)
  8993. "deleted"
  8994. "was not present in the entry")))
  8995. (defun org-delete-property-globally (property)
  8996. "Remove PROPERTY globally, from all entries."
  8997. (interactive
  8998. (let* ((completion-ignore-case t)
  8999. (prop (completing-read
  9000. "Globally remove property: "
  9001. (mapcar 'list (org-buffer-property-keys)))))
  9002. (list prop)))
  9003. (save-excursion
  9004. (save-restriction
  9005. (widen)
  9006. (goto-char (point-min))
  9007. (let ((cnt 0))
  9008. (while (re-search-forward
  9009. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  9010. nil t)
  9011. (setq cnt (1+ cnt))
  9012. (replace-match ""))
  9013. (message "Property \"%s\" removed from %d entries" property cnt)))))
  9014. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  9015. (defun org-compute-property-at-point ()
  9016. "Compute the property at point.
  9017. This looks for an enclosing column format, extracts the operator and
  9018. then applies it to the proerty in the column format's scope."
  9019. (interactive)
  9020. (unless (org-at-property-p)
  9021. (error "Not at a property"))
  9022. (let ((prop (org-match-string-no-properties 2)))
  9023. (org-columns-get-format-and-top-level)
  9024. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  9025. (error "No operator defined for property %s" prop))
  9026. (org-columns-compute prop)))
  9027. (defun org-property-get-allowed-values (pom property &optional table)
  9028. "Get allowed values for the property PROPERTY.
  9029. When TABLE is non-nil, return an alist that can directly be used for
  9030. completion."
  9031. (let (vals)
  9032. (cond
  9033. ((equal property "TODO")
  9034. (setq vals (org-with-point-at pom
  9035. (append org-todo-keywords-1 '("")))))
  9036. ((equal property "PRIORITY")
  9037. (let ((n org-lowest-priority))
  9038. (while (>= n org-highest-priority)
  9039. (push (char-to-string n) vals)
  9040. (setq n (1- n)))))
  9041. ((member property org-special-properties))
  9042. (t
  9043. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  9044. (when (and vals (string-match "\\S-" vals))
  9045. (setq vals (car (read-from-string (concat "(" vals ")"))))
  9046. (setq vals (mapcar (lambda (x)
  9047. (cond ((stringp x) x)
  9048. ((numberp x) (number-to-string x))
  9049. ((symbolp x) (symbol-name x))
  9050. (t "???")))
  9051. vals)))))
  9052. (if table (mapcar 'list vals) vals)))
  9053. (defun org-property-previous-allowed-value (&optional previous)
  9054. "Switch to the next allowed value for this property."
  9055. (interactive)
  9056. (org-property-next-allowed-value t))
  9057. (defun org-property-next-allowed-value (&optional previous)
  9058. "Switch to the next allowed value for this property."
  9059. (interactive)
  9060. (unless (org-at-property-p)
  9061. (error "Not at a property"))
  9062. (let* ((key (match-string 2))
  9063. (value (match-string 3))
  9064. (allowed (or (org-property-get-allowed-values (point) key)
  9065. (and (member value '("[ ]" "[-]" "[X]"))
  9066. '("[ ]" "[X]"))))
  9067. nval)
  9068. (unless allowed
  9069. (error "Allowed values for this property have not been defined"))
  9070. (if previous (setq allowed (reverse allowed)))
  9071. (if (member value allowed)
  9072. (setq nval (car (cdr (member value allowed)))))
  9073. (setq nval (or nval (car allowed)))
  9074. (if (equal nval value)
  9075. (error "Only one allowed value for this property"))
  9076. (org-at-property-p)
  9077. (replace-match (concat " :" key ": " nval) t t)
  9078. (org-indent-line-function)
  9079. (beginning-of-line 1)
  9080. (skip-chars-forward " \t")))
  9081. (defun org-find-entry-with-id (ident)
  9082. "Locate the entry that contains the ID property with exact value IDENT.
  9083. IDENT can be a string, a symbol or a number, this function will search for
  9084. the string representation of it.
  9085. Return the position where this entry starts, or nil if there is no such entry."
  9086. (let ((id (cond
  9087. ((stringp ident) ident)
  9088. ((symbol-name ident) (symbol-name ident))
  9089. ((numberp ident) (number-to-string ident))
  9090. (t (error "IDENT %s must be a string, symbol or number" ident))))
  9091. (case-fold-search nil))
  9092. (save-excursion
  9093. (save-restriction
  9094. (widen)
  9095. (goto-char (point-min))
  9096. (when (re-search-forward
  9097. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  9098. nil t)
  9099. (org-back-to-heading)
  9100. (point))))))
  9101. ;;;; Timestamps
  9102. (defvar org-last-changed-timestamp nil)
  9103. (defvar org-time-was-given) ; dynamically scoped parameter
  9104. (defvar org-end-time-was-given) ; dynamically scoped parameter
  9105. (defvar org-ts-what) ; dynamically scoped parameter
  9106. (defun org-time-stamp (arg)
  9107. "Prompt for a date/time and insert a time stamp.
  9108. If the user specifies a time like HH:MM, or if this command is called
  9109. with a prefix argument, the time stamp will contain date and time.
  9110. Otherwise, only the date will be included. All parts of a date not
  9111. specified by the user will be filled in from the current date/time.
  9112. So if you press just return without typing anything, the time stamp
  9113. will represent the current date/time. If there is already a timestamp
  9114. at the cursor, it will be modified."
  9115. (interactive "P")
  9116. (let* ((ts nil)
  9117. (default-time
  9118. ;; Default time is either today, or, when entering a range,
  9119. ;; the range start.
  9120. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  9121. (save-excursion
  9122. (re-search-backward
  9123. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  9124. (- (point) 20) t)))
  9125. (apply 'encode-time (org-parse-time-string (match-string 1)))
  9126. (current-time)))
  9127. (default-input (and ts (org-get-compact-tod ts)))
  9128. org-time-was-given org-end-time-was-given time)
  9129. (cond
  9130. ((and (org-at-timestamp-p)
  9131. (eq last-command 'org-time-stamp)
  9132. (eq this-command 'org-time-stamp))
  9133. (insert "--")
  9134. (setq time (let ((this-command this-command))
  9135. (org-read-date arg 'totime nil nil default-time default-input)))
  9136. (org-insert-time-stamp time (or org-time-was-given arg)))
  9137. ((org-at-timestamp-p)
  9138. (setq time (let ((this-command this-command))
  9139. (org-read-date arg 'totime nil nil default-time default-input)))
  9140. (when (org-at-timestamp-p) ; just to get the match data
  9141. (replace-match "")
  9142. (setq org-last-changed-timestamp
  9143. (org-insert-time-stamp
  9144. time (or org-time-was-given arg)
  9145. nil nil nil (list org-end-time-was-given))))
  9146. (message "Timestamp updated"))
  9147. (t
  9148. (setq time (let ((this-command this-command))
  9149. (org-read-date arg 'totime nil nil default-time default-input)))
  9150. (org-insert-time-stamp time (or org-time-was-given arg)
  9151. nil nil nil (list org-end-time-was-given))))))
  9152. ;; FIXME: can we use this for something else, like computing time differences?
  9153. (defun org-get-compact-tod (s)
  9154. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  9155. (let* ((t1 (match-string 1 s))
  9156. (h1 (string-to-number (match-string 2 s)))
  9157. (m1 (string-to-number (match-string 3 s)))
  9158. (t2 (and (match-end 4) (match-string 5 s)))
  9159. (h2 (and t2 (string-to-number (match-string 6 s))))
  9160. (m2 (and t2 (string-to-number (match-string 7 s))))
  9161. dh dm)
  9162. (if (not t2)
  9163. t1
  9164. (setq dh (- h2 h1) dm (- m2 m1))
  9165. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  9166. (concat t1 "+" (number-to-string dh)
  9167. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  9168. (defun org-time-stamp-inactive (&optional arg)
  9169. "Insert an inactive time stamp.
  9170. An inactive time stamp is enclosed in square brackets instead of angle
  9171. brackets. It is inactive in the sense that it does not trigger agenda entries,
  9172. does not link to the calendar and cannot be changed with the S-cursor keys.
  9173. So these are more for recording a certain time/date."
  9174. (interactive "P")
  9175. (let (org-time-was-given org-end-time-was-given time)
  9176. (setq time (org-read-date arg 'totime))
  9177. (org-insert-time-stamp time (or org-time-was-given arg) 'inactive
  9178. nil nil (list org-end-time-was-given))))
  9179. (defvar org-date-ovl (org-make-overlay 1 1))
  9180. (org-overlay-put org-date-ovl 'face 'org-warning)
  9181. (org-detach-overlay org-date-ovl)
  9182. (defvar org-ans1) ; dynamically scoped parameter
  9183. (defvar org-ans2) ; dynamically scoped parameter
  9184. (defvar org-plain-time-of-day-regexp) ; defined below
  9185. (defvar org-read-date-overlay nil)
  9186. (defvar org-dcst nil) ; dynamically scoped
  9187. (defun org-read-date (&optional with-time to-time from-string prompt
  9188. default-time default-input)
  9189. "Read a date, possibly a time, and make things smooth for the user.
  9190. The prompt will suggest to enter an ISO date, but you can also enter anything
  9191. which will at least partially be understood by `parse-time-string'.
  9192. Unrecognized parts of the date will default to the current day, month, year,
  9193. hour and minute. If this command is called to replace a timestamp at point,
  9194. of to enter the second timestamp of a range, the default time is taken from the
  9195. existing stamp. For example,
  9196. 3-2-5 --> 2003-02-05
  9197. feb 15 --> currentyear-02-15
  9198. sep 12 9 --> 2009-09-12
  9199. 12:45 --> today 12:45
  9200. 22 sept 0:34 --> currentyear-09-22 0:34
  9201. 12 --> currentyear-currentmonth-12
  9202. Fri --> nearest Friday (today or later)
  9203. etc.
  9204. Furthermore you can specify a relative date by giving, as the *first* thing
  9205. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  9206. change in days weeks, months, years.
  9207. With a single plus or minus, the date is relative to today. With a double
  9208. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  9209. +4d --> four days from today
  9210. +4 --> same as above
  9211. +2w --> two weeks from today
  9212. ++5 --> five days from default date
  9213. The function understands only English month and weekday abbreviations,
  9214. but this can be configured with the variables `parse-time-months' and
  9215. `parse-time-weekdays'.
  9216. While prompting, a calendar is popped up - you can also select the
  9217. date with the mouse (button 1). The calendar shows a period of three
  9218. months. To scroll it to other months, use the keys `>' and `<'.
  9219. If you don't like the calendar, turn it off with
  9220. \(setq org-read-date-popup-calendar nil)
  9221. With optional argument TO-TIME, the date will immediately be converted
  9222. to an internal time.
  9223. With an optional argument WITH-TIME, the prompt will suggest to also
  9224. insert a time. Note that when WITH-TIME is not set, you can still
  9225. enter a time, and this function will inform the calling routine about
  9226. this change. The calling routine may then choose to change the format
  9227. used to insert the time stamp into the buffer to include the time.
  9228. With optional argument FROM-STRING, read from this string instead from
  9229. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  9230. the time/date that is used for everything that is not specified by the
  9231. user."
  9232. (require 'parse-time)
  9233. (let* ((org-time-stamp-rounding-minutes
  9234. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  9235. (org-dcst org-display-custom-times)
  9236. (ct (org-current-time))
  9237. (def (or default-time ct))
  9238. (defdecode (decode-time def))
  9239. (dummy (progn
  9240. (when (< (nth 2 defdecode) org-extend-today-until)
  9241. (setcar (nthcdr 2 defdecode) -1)
  9242. (setcar (nthcdr 1 defdecode) 59)
  9243. (setq def (apply 'encode-time defdecode)
  9244. defdecode (decode-time def)))))
  9245. (calendar-move-hook nil)
  9246. (calendar-view-diary-initially-flag nil)
  9247. (view-diary-entries-initially nil)
  9248. (calendar-view-holidays-initially-flag nil)
  9249. (view-calendar-holidays-initially nil)
  9250. (timestr (format-time-string
  9251. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  9252. (prompt (concat (if prompt (concat prompt " ") "")
  9253. (format "Date+time [%s]: " timestr)))
  9254. ans (org-ans0 "") org-ans1 org-ans2 final)
  9255. (cond
  9256. (from-string (setq ans from-string))
  9257. (org-read-date-popup-calendar
  9258. (save-excursion
  9259. (save-window-excursion
  9260. (calendar)
  9261. (calendar-forward-day (- (time-to-days def)
  9262. (calendar-absolute-from-gregorian
  9263. (calendar-current-date))))
  9264. (org-eval-in-calendar nil t)
  9265. (let* ((old-map (current-local-map))
  9266. (map (copy-keymap calendar-mode-map))
  9267. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  9268. (org-defkey map (kbd "RET") 'org-calendar-select)
  9269. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  9270. 'org-calendar-select-mouse)
  9271. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  9272. 'org-calendar-select-mouse)
  9273. (org-defkey minibuffer-local-map [(meta shift left)]
  9274. (lambda () (interactive)
  9275. (org-eval-in-calendar '(calendar-backward-month 1))))
  9276. (org-defkey minibuffer-local-map [(meta shift right)]
  9277. (lambda () (interactive)
  9278. (org-eval-in-calendar '(calendar-forward-month 1))))
  9279. (org-defkey minibuffer-local-map [(meta shift up)]
  9280. (lambda () (interactive)
  9281. (org-eval-in-calendar '(calendar-backward-year 1))))
  9282. (org-defkey minibuffer-local-map [(meta shift down)]
  9283. (lambda () (interactive)
  9284. (org-eval-in-calendar '(calendar-forward-year 1))))
  9285. (org-defkey minibuffer-local-map [(shift up)]
  9286. (lambda () (interactive)
  9287. (org-eval-in-calendar '(calendar-backward-week 1))))
  9288. (org-defkey minibuffer-local-map [(shift down)]
  9289. (lambda () (interactive)
  9290. (org-eval-in-calendar '(calendar-forward-week 1))))
  9291. (org-defkey minibuffer-local-map [(shift left)]
  9292. (lambda () (interactive)
  9293. (org-eval-in-calendar '(calendar-backward-day 1))))
  9294. (org-defkey minibuffer-local-map [(shift right)]
  9295. (lambda () (interactive)
  9296. (org-eval-in-calendar '(calendar-forward-day 1))))
  9297. (org-defkey minibuffer-local-map ">"
  9298. (lambda () (interactive)
  9299. (org-eval-in-calendar '(scroll-calendar-left 1))))
  9300. (org-defkey minibuffer-local-map "<"
  9301. (lambda () (interactive)
  9302. (org-eval-in-calendar '(scroll-calendar-right 1))))
  9303. (unwind-protect
  9304. (progn
  9305. (use-local-map map)
  9306. (add-hook 'post-command-hook 'org-read-date-display)
  9307. (setq org-ans0 (read-string prompt default-input nil nil))
  9308. ;; org-ans0: from prompt
  9309. ;; org-ans1: from mouse click
  9310. ;; org-ans2: from calendar motion
  9311. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  9312. (remove-hook 'post-command-hook 'org-read-date-display)
  9313. (use-local-map old-map)
  9314. (when org-read-date-overlay
  9315. (org-delete-overlay org-read-date-overlay)
  9316. (setq org-read-date-overlay nil)))))))
  9317. (t ; Naked prompt only
  9318. (unwind-protect
  9319. (setq ans (read-string prompt default-input nil timestr))
  9320. (when org-read-date-overlay
  9321. (org-delete-overlay org-read-date-overlay)
  9322. (setq org-read-date-overlay nil)))))
  9323. (setq final (org-read-date-analyze ans def defdecode))
  9324. (if to-time
  9325. (apply 'encode-time final)
  9326. (if (and (boundp 'org-time-was-given) org-time-was-given)
  9327. (format "%04d-%02d-%02d %02d:%02d"
  9328. (nth 5 final) (nth 4 final) (nth 3 final)
  9329. (nth 2 final) (nth 1 final))
  9330. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  9331. (defvar def)
  9332. (defvar defdecode)
  9333. (defvar with-time)
  9334. (defun org-read-date-display ()
  9335. "Display the currrent date prompt interpretation in the minibuffer."
  9336. (when org-read-date-display-live
  9337. (when org-read-date-overlay
  9338. (org-delete-overlay org-read-date-overlay))
  9339. (let ((p (point)))
  9340. (end-of-line 1)
  9341. (while (not (equal (buffer-substring
  9342. (max (point-min) (- (point) 4)) (point))
  9343. " "))
  9344. (insert " "))
  9345. (goto-char p))
  9346. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  9347. " " (or org-ans1 org-ans2)))
  9348. (org-end-time-was-given nil)
  9349. (f (org-read-date-analyze ans def defdecode))
  9350. (fmts (if org-dcst
  9351. org-time-stamp-custom-formats
  9352. org-time-stamp-formats))
  9353. (fmt (if (or with-time
  9354. (and (boundp 'org-time-was-given) org-time-was-given))
  9355. (cdr fmts)
  9356. (car fmts)))
  9357. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  9358. (when (and org-end-time-was-given
  9359. (string-match org-plain-time-of-day-regexp txt))
  9360. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  9361. org-end-time-was-given
  9362. (substring txt (match-end 0)))))
  9363. (setq org-read-date-overlay
  9364. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  9365. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  9366. (defun org-read-date-analyze (ans def defdecode)
  9367. "Analyze the combined answer of the date prompt."
  9368. ;; FIXME: cleanup and comment
  9369. (let (delta deltan deltaw deltadef year month day
  9370. hour minute second wday pm h2 m2 tl wday1
  9371. iso-year iso-weekday iso-week iso-year iso-date)
  9372. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  9373. (setq ans "+0"))
  9374. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  9375. (setq ans (replace-match "" t t ans)
  9376. deltan (car delta)
  9377. deltaw (nth 1 delta)
  9378. deltadef (nth 2 delta)))
  9379. ;; Check if there is an iso week date in there
  9380. ;; If yes, sore the info and ostpone interpreting it until the rest
  9381. ;; of the parsing is done
  9382. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  9383. (setq iso-year (if (match-end 1) (org-small-year-to-year (string-to-number (match-string 1 ans))))
  9384. iso-weekday (if (match-end 3) (string-to-number (match-string 3 ans)))
  9385. iso-week (string-to-number (match-string 2 ans)))
  9386. (setq ans (replace-match "" t t ans)))
  9387. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  9388. (when (string-match
  9389. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  9390. (setq year (if (match-end 2)
  9391. (string-to-number (match-string 2 ans))
  9392. (string-to-number (format-time-string "%Y")))
  9393. month (string-to-number (match-string 3 ans))
  9394. day (string-to-number (match-string 4 ans)))
  9395. (if (< year 100) (setq year (+ 2000 year)))
  9396. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  9397. t nil ans)))
  9398. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  9399. ;; If there is a time with am/pm, and *no* time without it, we convert
  9400. ;; so that matching will be successful.
  9401. (loop for i from 1 to 2 do ; twice, for end time as well
  9402. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  9403. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  9404. (setq hour (string-to-number (match-string 1 ans))
  9405. minute (if (match-end 3)
  9406. (string-to-number (match-string 3 ans))
  9407. 0)
  9408. pm (equal ?p
  9409. (string-to-char (downcase (match-string 4 ans)))))
  9410. (if (and (= hour 12) (not pm))
  9411. (setq hour 0)
  9412. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  9413. (setq ans (replace-match (format "%02d:%02d" hour minute)
  9414. t t ans))))
  9415. ;; Check if a time range is given as a duration
  9416. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  9417. (setq hour (string-to-number (match-string 1 ans))
  9418. h2 (+ hour (string-to-number (match-string 3 ans)))
  9419. minute (string-to-number (match-string 2 ans))
  9420. m2 (+ minute (if (match-end 5) (string-to-number
  9421. (match-string 5 ans))0)))
  9422. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  9423. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  9424. t t ans)))
  9425. ;; Check if there is a time range
  9426. (when (boundp 'org-end-time-was-given)
  9427. (setq org-time-was-given nil)
  9428. (when (and (string-match org-plain-time-of-day-regexp ans)
  9429. (match-end 8))
  9430. (setq org-end-time-was-given (match-string 8 ans))
  9431. (setq ans (concat (substring ans 0 (match-beginning 7))
  9432. (substring ans (match-end 7))))))
  9433. (setq tl (parse-time-string ans)
  9434. day (or (nth 3 tl) (nth 3 defdecode))
  9435. month (or (nth 4 tl)
  9436. (if (and org-read-date-prefer-future
  9437. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  9438. (1+ (nth 4 defdecode))
  9439. (nth 4 defdecode)))
  9440. year (or (nth 5 tl)
  9441. (if (and org-read-date-prefer-future
  9442. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  9443. (1+ (nth 5 defdecode))
  9444. (nth 5 defdecode)))
  9445. hour (or (nth 2 tl) (nth 2 defdecode))
  9446. minute (or (nth 1 tl) (nth 1 defdecode))
  9447. second (or (nth 0 tl) 0)
  9448. wday (nth 6 tl))
  9449. ;; Special date definitions below
  9450. (cond
  9451. (iso-week
  9452. ;; There was an iso week
  9453. (setq year (or iso-year year)
  9454. day (or iso-weekday wday 1)
  9455. wday nil ; to make sure that the trigger below does not match
  9456. iso-date (calendar-gregorian-from-absolute
  9457. (calendar-absolute-from-iso
  9458. (list iso-week day year))))
  9459. ; FIXME: Should we also push ISO weeks into the future?
  9460. ; (when (and org-read-date-prefer-future
  9461. ; (not iso-year)
  9462. ; (< (calendar-absolute-from-gregorian iso-date)
  9463. ; (time-to-days (current-time))))
  9464. ; (setq year (1+ year)
  9465. ; iso-date (calendar-gregorian-from-absolute
  9466. ; (calendar-absolute-from-iso
  9467. ; (list iso-week day year)))))
  9468. (setq month (car iso-date)
  9469. year (nth 2 iso-date)
  9470. day (nth 1 iso-date)))
  9471. (deltan
  9472. (unless deltadef
  9473. (let ((now (decode-time (current-time))))
  9474. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  9475. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  9476. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  9477. ((equal deltaw "m") (setq month (+ month deltan)))
  9478. ((equal deltaw "y") (setq year (+ year deltan)))))
  9479. ((and wday (not (nth 3 tl)))
  9480. ;; Weekday was given, but no day, so pick that day in the week
  9481. ;; on or after the derived date.
  9482. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  9483. (unless (equal wday wday1)
  9484. (setq day (+ day (% (- wday wday1 -7) 7))))))
  9485. (if (and (boundp 'org-time-was-given)
  9486. (nth 2 tl))
  9487. (setq org-time-was-given t))
  9488. (if (< year 100) (setq year (+ 2000 year)))
  9489. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  9490. (list second minute hour day month year)))
  9491. (defvar parse-time-weekdays)
  9492. (defun org-read-date-get-relative (s today default)
  9493. "Check string S for special relative date string.
  9494. TODAY and DEFAULT are internal times, for today and for a default.
  9495. Return shift list (N what def-flag)
  9496. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  9497. N is the number of WHATs to shift.
  9498. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  9499. the DEFAULT date rather than TODAY."
  9500. (when (and
  9501. (string-match
  9502. (concat
  9503. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  9504. "\\([0-9]+\\)?"
  9505. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  9506. "\\([ \t]\\|$\\)") s)
  9507. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  9508. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  9509. (string-to-char (substring (match-string 1 s) -1))
  9510. ?+))
  9511. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  9512. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  9513. (what (if (match-end 3) (match-string 3 s) "d"))
  9514. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  9515. (date (if rel default today))
  9516. (wday (nth 6 (decode-time date)))
  9517. delta)
  9518. (if wday1
  9519. (progn
  9520. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  9521. (if (= dir ?-) (setq delta (- delta 7)))
  9522. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  9523. (list delta "d" rel))
  9524. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  9525. (defun org-eval-in-calendar (form &optional keepdate)
  9526. "Eval FORM in the calendar window and return to current window.
  9527. Also, store the cursor date in variable org-ans2."
  9528. (let ((sw (selected-window)))
  9529. (select-window (get-buffer-window "*Calendar*"))
  9530. (eval form)
  9531. (when (and (not keepdate) (calendar-cursor-to-date))
  9532. (let* ((date (calendar-cursor-to-date))
  9533. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  9534. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  9535. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  9536. (select-window sw)))
  9537. ; ;; Update the prompt to show new default date
  9538. ; (save-excursion
  9539. ; (goto-char (point-min))
  9540. ; (when (and org-ans2
  9541. ; (re-search-forward "\\[[-0-9]+\\]" nil t)
  9542. ; (get-text-property (match-end 0) 'field))
  9543. ; (let ((inhibit-read-only t))
  9544. ; (replace-match (concat "[" org-ans2 "]") t t)
  9545. ; (add-text-properties (point-min) (1+ (match-end 0))
  9546. ; (text-properties-at (1+ (point-min)))))))))
  9547. (defun org-calendar-select ()
  9548. "Return to `org-read-date' with the date currently selected.
  9549. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  9550. (interactive)
  9551. (when (calendar-cursor-to-date)
  9552. (let* ((date (calendar-cursor-to-date))
  9553. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  9554. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  9555. (if (active-minibuffer-window) (exit-minibuffer))))
  9556. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  9557. "Insert a date stamp for the date given by the internal TIME.
  9558. WITH-HM means, use the stamp format that includes the time of the day.
  9559. INACTIVE means use square brackets instead of angular ones, so that the
  9560. stamp will not contribute to the agenda.
  9561. PRE and POST are optional strings to be inserted before and after the
  9562. stamp.
  9563. The command returns the inserted time stamp."
  9564. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  9565. stamp)
  9566. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  9567. (insert-before-markers (or pre ""))
  9568. (insert-before-markers (setq stamp (format-time-string fmt time)))
  9569. (when (listp extra)
  9570. (setq extra (car extra))
  9571. (if (and (stringp extra)
  9572. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  9573. (setq extra (format "-%02d:%02d"
  9574. (string-to-number (match-string 1 extra))
  9575. (string-to-number (match-string 2 extra))))
  9576. (setq extra nil)))
  9577. (when extra
  9578. (backward-char 1)
  9579. (insert-before-markers extra)
  9580. (forward-char 1))
  9581. (insert-before-markers (or post ""))
  9582. stamp))
  9583. (defun org-toggle-time-stamp-overlays ()
  9584. "Toggle the use of custom time stamp formats."
  9585. (interactive)
  9586. (setq org-display-custom-times (not org-display-custom-times))
  9587. (unless org-display-custom-times
  9588. (let ((p (point-min)) (bmp (buffer-modified-p)))
  9589. (while (setq p (next-single-property-change p 'display))
  9590. (if (and (get-text-property p 'display)
  9591. (eq (get-text-property p 'face) 'org-date))
  9592. (remove-text-properties
  9593. p (setq p (next-single-property-change p 'display))
  9594. '(display t))))
  9595. (set-buffer-modified-p bmp)))
  9596. (if (featurep 'xemacs)
  9597. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  9598. (org-restart-font-lock)
  9599. (setq org-table-may-need-update t)
  9600. (if org-display-custom-times
  9601. (message "Time stamps are overlayed with custom format")
  9602. (message "Time stamp overlays removed")))
  9603. (defun org-display-custom-time (beg end)
  9604. "Overlay modified time stamp format over timestamp between BEG and END."
  9605. (let* ((ts (buffer-substring beg end))
  9606. t1 w1 with-hm tf time str w2 (off 0))
  9607. (save-match-data
  9608. (setq t1 (org-parse-time-string ts t))
  9609. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\)?\\'" ts)
  9610. (setq off (- (match-end 0) (match-beginning 0)))))
  9611. (setq end (- end off))
  9612. (setq w1 (- end beg)
  9613. with-hm (and (nth 1 t1) (nth 2 t1))
  9614. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  9615. time (org-fix-decoded-time t1)
  9616. str (org-add-props
  9617. (format-time-string
  9618. (substring tf 1 -1) (apply 'encode-time time))
  9619. nil 'mouse-face 'highlight)
  9620. w2 (length str))
  9621. (if (not (= w2 w1))
  9622. (add-text-properties (1+ beg) (+ 2 beg)
  9623. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  9624. (if (featurep 'xemacs)
  9625. (progn
  9626. (put-text-property beg end 'invisible t)
  9627. (put-text-property beg end 'end-glyph (make-glyph str)))
  9628. (put-text-property beg end 'display str))))
  9629. (defun org-translate-time (string)
  9630. "Translate all timestamps in STRING to custom format.
  9631. But do this only if the variable `org-display-custom-times' is set."
  9632. (when org-display-custom-times
  9633. (save-match-data
  9634. (let* ((start 0)
  9635. (re org-ts-regexp-both)
  9636. t1 with-hm inactive tf time str beg end)
  9637. (while (setq start (string-match re string start))
  9638. (setq beg (match-beginning 0)
  9639. end (match-end 0)
  9640. t1 (save-match-data
  9641. (org-parse-time-string (substring string beg end) t))
  9642. with-hm (and (nth 1 t1) (nth 2 t1))
  9643. inactive (equal (substring string beg (1+ beg)) "[")
  9644. tf (funcall (if with-hm 'cdr 'car)
  9645. org-time-stamp-custom-formats)
  9646. time (org-fix-decoded-time t1)
  9647. str (format-time-string
  9648. (concat
  9649. (if inactive "[" "<") (substring tf 1 -1)
  9650. (if inactive "]" ">"))
  9651. (apply 'encode-time time))
  9652. string (replace-match str t t string)
  9653. start (+ start (length str)))))))
  9654. string)
  9655. (defun org-fix-decoded-time (time)
  9656. "Set 0 instead of nil for the first 6 elements of time.
  9657. Don't touch the rest."
  9658. (let ((n 0))
  9659. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  9660. (defun org-days-to-time (timestamp-string)
  9661. "Difference between TIMESTAMP-STRING and now in days."
  9662. (- (time-to-days (org-time-string-to-time timestamp-string))
  9663. (time-to-days (current-time))))
  9664. (defun org-deadline-close (timestamp-string &optional ndays)
  9665. "Is the time in TIMESTAMP-STRING close to the current date?"
  9666. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  9667. (and (< (org-days-to-time timestamp-string) ndays)
  9668. (not (org-entry-is-done-p))))
  9669. (defun org-get-wdays (ts)
  9670. "Get the deadline lead time appropriate for timestring TS."
  9671. (cond
  9672. ((<= org-deadline-warning-days 0)
  9673. ;; 0 or negative, enforce this value no matter what
  9674. (- org-deadline-warning-days))
  9675. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\)" ts)
  9676. ;; lead time is specified.
  9677. (floor (* (string-to-number (match-string 1 ts))
  9678. (cdr (assoc (match-string 2 ts)
  9679. '(("d" . 1) ("w" . 7)
  9680. ("m" . 30.4) ("y" . 365.25)))))))
  9681. ;; go for the default.
  9682. (t org-deadline-warning-days)))
  9683. (defun org-calendar-select-mouse (ev)
  9684. "Return to `org-read-date' with the date currently selected.
  9685. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  9686. (interactive "e")
  9687. (mouse-set-point ev)
  9688. (when (calendar-cursor-to-date)
  9689. (let* ((date (calendar-cursor-to-date))
  9690. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  9691. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  9692. (if (active-minibuffer-window) (exit-minibuffer))))
  9693. (defun org-check-deadlines (ndays)
  9694. "Check if there are any deadlines due or past due.
  9695. A deadline is considered due if it happens within `org-deadline-warning-days'
  9696. days from today's date. If the deadline appears in an entry marked DONE,
  9697. it is not shown. The prefix arg NDAYS can be used to test that many
  9698. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  9699. (interactive "P")
  9700. (let* ((org-warn-days
  9701. (cond
  9702. ((equal ndays '(4)) 100000)
  9703. (ndays (prefix-numeric-value ndays))
  9704. (t (abs org-deadline-warning-days))))
  9705. (case-fold-search nil)
  9706. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  9707. (callback
  9708. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  9709. (message "%d deadlines past-due or due within %d days"
  9710. (org-occur regexp nil callback)
  9711. org-warn-days)))
  9712. (defun org-check-before-date (date)
  9713. "Check if there are deadlines or scheduled entries before DATE."
  9714. (interactive (list (org-read-date)))
  9715. (let ((case-fold-search nil)
  9716. (regexp (concat "\\<\\(" org-deadline-string
  9717. "\\|" org-scheduled-string
  9718. "\\) *<\\([^>]+\\)>"))
  9719. (callback
  9720. (lambda () (time-less-p
  9721. (org-time-string-to-time (match-string 2))
  9722. (org-time-string-to-time date)))))
  9723. (message "%d entries before %s"
  9724. (org-occur regexp nil callback) date)))
  9725. (defun org-evaluate-time-range (&optional to-buffer)
  9726. "Evaluate a time range by computing the difference between start and end.
  9727. Normally the result is just printed in the echo area, but with prefix arg
  9728. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  9729. If the time range is actually in a table, the result is inserted into the
  9730. next column.
  9731. For time difference computation, a year is assumed to be exactly 365
  9732. days in order to avoid rounding problems."
  9733. (interactive "P")
  9734. (or
  9735. (org-clock-update-time-maybe)
  9736. (save-excursion
  9737. (unless (org-at-date-range-p t)
  9738. (goto-char (point-at-bol))
  9739. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  9740. (if (not (org-at-date-range-p t))
  9741. (error "Not at a time-stamp range, and none found in current line")))
  9742. (let* ((ts1 (match-string 1))
  9743. (ts2 (match-string 2))
  9744. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  9745. (match-end (match-end 0))
  9746. (time1 (org-time-string-to-time ts1))
  9747. (time2 (org-time-string-to-time ts2))
  9748. (t1 (time-to-seconds time1))
  9749. (t2 (time-to-seconds time2))
  9750. (diff (abs (- t2 t1)))
  9751. (negative (< (- t2 t1) 0))
  9752. ;; (ys (floor (* 365 24 60 60)))
  9753. (ds (* 24 60 60))
  9754. (hs (* 60 60))
  9755. (fy "%dy %dd %02d:%02d")
  9756. (fy1 "%dy %dd")
  9757. (fd "%dd %02d:%02d")
  9758. (fd1 "%dd")
  9759. (fh "%02d:%02d")
  9760. y d h m align)
  9761. (if havetime
  9762. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  9763. y 0
  9764. d (floor (/ diff ds)) diff (mod diff ds)
  9765. h (floor (/ diff hs)) diff (mod diff hs)
  9766. m (floor (/ diff 60)))
  9767. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  9768. y 0
  9769. d (floor (+ (/ diff ds) 0.5))
  9770. h 0 m 0))
  9771. (if (not to-buffer)
  9772. (message "%s" (org-make-tdiff-string y d h m))
  9773. (if (org-at-table-p)
  9774. (progn
  9775. (goto-char match-end)
  9776. (setq align t)
  9777. (and (looking-at " *|") (goto-char (match-end 0))))
  9778. (goto-char match-end))
  9779. (if (looking-at
  9780. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  9781. (replace-match ""))
  9782. (if negative (insert " -"))
  9783. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  9784. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  9785. (insert " " (format fh h m))))
  9786. (if align (org-table-align))
  9787. (message "Time difference inserted")))))
  9788. (defun org-make-tdiff-string (y d h m)
  9789. (let ((fmt "")
  9790. (l nil))
  9791. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  9792. l (push y l)))
  9793. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  9794. l (push d l)))
  9795. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  9796. l (push h l)))
  9797. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  9798. l (push m l)))
  9799. (apply 'format fmt (nreverse l))))
  9800. (defun org-time-string-to-time (s)
  9801. (apply 'encode-time (org-parse-time-string s)))
  9802. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  9803. "Convert a time stamp to an absolute day number.
  9804. If there is a specifyer for a cyclic time stamp, get the closest date to
  9805. DAYNR.
  9806. PREFER and SHOW_ALL are passed through to `org-closest-date'."
  9807. (cond
  9808. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  9809. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  9810. daynr
  9811. (+ daynr 1000)))
  9812. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  9813. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  9814. (time-to-days (current-time))) (match-string 0 s)
  9815. prefer show-all))
  9816. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  9817. (defun org-days-to-iso-week (days)
  9818. "Return the iso week number."
  9819. (require 'cal-iso)
  9820. (car (calendar-iso-from-absolute days)))
  9821. (defun org-small-year-to-year (year)
  9822. "Convert 2-digit years into 4-digit years.
  9823. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  9824. The year 2000 cannot be abbreviated. Any year lager than 99
  9825. is retrned unchanged."
  9826. (if (< year 38)
  9827. (setq year (+ 2000 year))
  9828. (if (< year 100)
  9829. (setq year (+ 1900 year))))
  9830. year)
  9831. (defun org-time-from-absolute (d)
  9832. "Return the time corresponding to date D.
  9833. D may be an absolute day number, or a calendar-type list (month day year)."
  9834. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  9835. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  9836. (defun org-calendar-holiday ()
  9837. "List of holidays, for Diary display in Org-mode."
  9838. (require 'holidays)
  9839. (let ((hl (funcall
  9840. (if (fboundp 'calendar-check-holidays)
  9841. 'calendar-check-holidays 'check-calendar-holidays) date)))
  9842. (if hl (mapconcat 'identity hl "; "))))
  9843. (defun org-diary-sexp-entry (sexp entry date)
  9844. "Process a SEXP diary ENTRY for DATE."
  9845. (require 'diary-lib)
  9846. (let ((result (if calendar-debug-sexp
  9847. (let ((stack-trace-on-error t))
  9848. (eval (car (read-from-string sexp))))
  9849. (condition-case nil
  9850. (eval (car (read-from-string sexp)))
  9851. (error
  9852. (beep)
  9853. (message "Bad sexp at line %d in %s: %s"
  9854. (org-current-line)
  9855. (buffer-file-name) sexp)
  9856. (sleep-for 2))))))
  9857. (cond ((stringp result) result)
  9858. ((and (consp result)
  9859. (stringp (cdr result))) (cdr result))
  9860. (result entry)
  9861. (t nil))))
  9862. (defun org-diary-to-ical-string (frombuf)
  9863. "Get iCalendar entries from diary entries in buffer FROMBUF.
  9864. This uses the icalendar.el library."
  9865. (let* ((tmpdir (if (featurep 'xemacs)
  9866. (temp-directory)
  9867. temporary-file-directory))
  9868. (tmpfile (make-temp-name
  9869. (expand-file-name "orgics" tmpdir)))
  9870. buf rtn b e)
  9871. (save-excursion
  9872. (set-buffer frombuf)
  9873. (icalendar-export-region (point-min) (point-max) tmpfile)
  9874. (setq buf (find-buffer-visiting tmpfile))
  9875. (set-buffer buf)
  9876. (goto-char (point-min))
  9877. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  9878. (setq b (match-beginning 0)))
  9879. (goto-char (point-max))
  9880. (if (re-search-backward "^END:VEVENT" nil t)
  9881. (setq e (match-end 0)))
  9882. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  9883. (kill-buffer buf)
  9884. (kill-buffer frombuf)
  9885. (delete-file tmpfile)
  9886. rtn))
  9887. (defun org-closest-date (start current change prefer show-all)
  9888. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  9889. When PREFER is `past' return a date that is either CURRENT or past.
  9890. When PREFER is `future', return a date that is either CURRENT or future.
  9891. When SHOW-ALL is nil, only return the current occurence of a time stamp."
  9892. ;; Make the proper lists from the dates
  9893. (catch 'exit
  9894. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  9895. dn dw sday cday n1 n2
  9896. d m y y1 y2 date1 date2 nmonths nm ny m2)
  9897. (setq start (org-date-to-gregorian start)
  9898. current (org-date-to-gregorian
  9899. (if show-all
  9900. current
  9901. (time-to-days (current-time))))
  9902. sday (calendar-absolute-from-gregorian start)
  9903. cday (calendar-absolute-from-gregorian current))
  9904. (if (<= cday sday) (throw 'exit sday))
  9905. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  9906. (setq dn (string-to-number (match-string 1 change))
  9907. dw (cdr (assoc (match-string 2 change) a1)))
  9908. (error "Invalid change specifyer: %s" change))
  9909. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  9910. (cond
  9911. ((eq dw 'day)
  9912. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  9913. n2 (+ n1 dn)))
  9914. ((eq dw 'year)
  9915. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  9916. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  9917. (setq date1 (list m d y1)
  9918. n1 (calendar-absolute-from-gregorian date1)
  9919. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  9920. n2 (calendar-absolute-from-gregorian date2)))
  9921. ((eq dw 'month)
  9922. ;; approx number of month between the tow dates
  9923. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  9924. ;; How often does dn fit in there?
  9925. (setq d (nth 1 start) m (car start) y (nth 2 start)
  9926. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  9927. m (+ m nm)
  9928. ny (floor (/ m 12))
  9929. y (+ y ny)
  9930. m (- m (* ny 12)))
  9931. (while (> m 12) (setq m (- m 12) y (1+ y)))
  9932. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  9933. (setq m2 (+ m dn) y2 y)
  9934. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  9935. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  9936. (while (< n2 cday)
  9937. (setq n1 n2 m m2 y y2)
  9938. (setq m2 (+ m dn) y2 y)
  9939. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  9940. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  9941. (if show-all
  9942. (cond
  9943. ((eq prefer 'past) n1)
  9944. ((eq prefer 'future) (if (= cday n1) n1 n2))
  9945. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  9946. (cond
  9947. ((eq prefer 'past) n1)
  9948. ((eq prefer 'future) (if (= cday n1) n1 n2))
  9949. (t (if (= cday n1) n1 n2)))))))
  9950. (defun org-date-to-gregorian (date)
  9951. "Turn any specification of DATE into a gregorian date for the calendar."
  9952. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  9953. ((and (listp date) (= (length date) 3)) date)
  9954. ((stringp date)
  9955. (setq date (org-parse-time-string date))
  9956. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  9957. ((listp date)
  9958. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  9959. (defun org-parse-time-string (s &optional nodefault)
  9960. "Parse the standard Org-mode time string.
  9961. This should be a lot faster than the normal `parse-time-string'.
  9962. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  9963. hour and minute fields will be nil if not given."
  9964. (if (string-match org-ts-regexp0 s)
  9965. (list 0
  9966. (if (or (match-beginning 8) (not nodefault))
  9967. (string-to-number (or (match-string 8 s) "0")))
  9968. (if (or (match-beginning 7) (not nodefault))
  9969. (string-to-number (or (match-string 7 s) "0")))
  9970. (string-to-number (match-string 4 s))
  9971. (string-to-number (match-string 3 s))
  9972. (string-to-number (match-string 2 s))
  9973. nil nil nil)
  9974. (make-list 9 0)))
  9975. (defun org-timestamp-up (&optional arg)
  9976. "Increase the date item at the cursor by one.
  9977. If the cursor is on the year, change the year. If it is on the month or
  9978. the day, change that.
  9979. With prefix ARG, change by that many units."
  9980. (interactive "p")
  9981. (org-timestamp-change (prefix-numeric-value arg)))
  9982. (defun org-timestamp-down (&optional arg)
  9983. "Decrease the date item at the cursor by one.
  9984. If the cursor is on the year, change the year. If it is on the month or
  9985. the day, change that.
  9986. With prefix ARG, change by that many units."
  9987. (interactive "p")
  9988. (org-timestamp-change (- (prefix-numeric-value arg))))
  9989. (defun org-timestamp-up-day (&optional arg)
  9990. "Increase the date in the time stamp by one day.
  9991. With prefix ARG, change that many days."
  9992. (interactive "p")
  9993. (if (and (not (org-at-timestamp-p t))
  9994. (org-on-heading-p))
  9995. (org-todo 'up)
  9996. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  9997. (defun org-timestamp-down-day (&optional arg)
  9998. "Decrease the date in the time stamp by one day.
  9999. With prefix ARG, change that many days."
  10000. (interactive "p")
  10001. (if (and (not (org-at-timestamp-p t))
  10002. (org-on-heading-p))
  10003. (org-todo 'down)
  10004. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  10005. (defun org-at-timestamp-p (&optional inactive-ok)
  10006. "Determine if the cursor is in or at a timestamp."
  10007. (interactive)
  10008. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  10009. (pos (point))
  10010. (ans (or (looking-at tsr)
  10011. (save-excursion
  10012. (skip-chars-backward "^[<\n\r\t")
  10013. (if (> (point) (point-min)) (backward-char 1))
  10014. (and (looking-at tsr)
  10015. (> (- (match-end 0) pos) -1))))))
  10016. (and ans
  10017. (boundp 'org-ts-what)
  10018. (setq org-ts-what
  10019. (cond
  10020. ((= pos (match-beginning 0)) 'bracket)
  10021. ((= pos (1- (match-end 0))) 'bracket)
  10022. ((org-pos-in-match-range pos 2) 'year)
  10023. ((org-pos-in-match-range pos 3) 'month)
  10024. ((org-pos-in-match-range pos 7) 'hour)
  10025. ((org-pos-in-match-range pos 8) 'minute)
  10026. ((or (org-pos-in-match-range pos 4)
  10027. (org-pos-in-match-range pos 5)) 'day)
  10028. ((and (> pos (or (match-end 8) (match-end 5)))
  10029. (< pos (match-end 0)))
  10030. (- pos (or (match-end 8) (match-end 5))))
  10031. (t 'day))))
  10032. ans))
  10033. (defun org-toggle-timestamp-type ()
  10034. "Toggle the type (<active> or [inactive]) of a time stamp."
  10035. (interactive)
  10036. (when (org-at-timestamp-p t)
  10037. (save-excursion
  10038. (goto-char (match-beginning 0))
  10039. (insert (if (equal (char-after) ?<) "[" "<")) (delete-char 1)
  10040. (goto-char (1- (match-end 0)))
  10041. (insert (if (equal (char-after) ?>) "]" ">")) (delete-char 1))
  10042. (message "Timestamp is now %sactive"
  10043. (if (equal (char-before) ?>) "in" ""))))
  10044. (defun org-timestamp-change (n &optional what)
  10045. "Change the date in the time stamp at point.
  10046. The date will be changed by N times WHAT. WHAT can be `day', `month',
  10047. `year', `minute', `second'. If WHAT is not given, the cursor position
  10048. in the timestamp determines what will be changed."
  10049. (let ((pos (point))
  10050. with-hm inactive
  10051. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  10052. org-ts-what
  10053. extra rem
  10054. ts time time0)
  10055. (if (not (org-at-timestamp-p t))
  10056. (error "Not at a timestamp"))
  10057. (if (and (not what) (eq org-ts-what 'bracket))
  10058. (org-toggle-timestamp-type)
  10059. (if (and (not what) (not (eq org-ts-what 'day))
  10060. org-display-custom-times
  10061. (get-text-property (point) 'display)
  10062. (not (get-text-property (1- (point)) 'display)))
  10063. (setq org-ts-what 'day))
  10064. (setq org-ts-what (or what org-ts-what)
  10065. inactive (= (char-after (match-beginning 0)) ?\[)
  10066. ts (match-string 0))
  10067. (replace-match "")
  10068. (if (string-match
  10069. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\)*\\)[]>]"
  10070. ts)
  10071. (setq extra (match-string 1 ts)))
  10072. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  10073. (setq with-hm t))
  10074. (setq time0 (org-parse-time-string ts))
  10075. (when (and (eq org-ts-what 'minute)
  10076. (eq current-prefix-arg nil))
  10077. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  10078. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  10079. (setcar (cdr time0) (+ (nth 1 time0)
  10080. (if (> n 0) (- rem) (- dm rem))))))
  10081. (setq time
  10082. (encode-time (or (car time0) 0)
  10083. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  10084. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  10085. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  10086. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  10087. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  10088. (nthcdr 6 time0)))
  10089. (when (integerp org-ts-what)
  10090. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  10091. (if (eq what 'calendar)
  10092. (let ((cal-date (org-get-date-from-calendar)))
  10093. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  10094. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  10095. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  10096. (setcar time0 (or (car time0) 0))
  10097. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  10098. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  10099. (setq time (apply 'encode-time time0))))
  10100. (setq org-last-changed-timestamp
  10101. (org-insert-time-stamp time with-hm inactive nil nil extra))
  10102. (org-clock-update-time-maybe)
  10103. (goto-char pos)
  10104. ;; Try to recenter the calendar window, if any
  10105. (if (and org-calendar-follow-timestamp-change
  10106. (get-buffer-window "*Calendar*" t)
  10107. (memq org-ts-what '(day month year)))
  10108. (org-recenter-calendar (time-to-days time))))))
  10109. (defun org-modify-ts-extra (s pos n dm)
  10110. "Change the different parts of the lead-time and repeat fields in timestamp."
  10111. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  10112. ng h m new rem)
  10113. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  10114. (cond
  10115. ((or (org-pos-in-match-range pos 2)
  10116. (org-pos-in-match-range pos 3))
  10117. (setq m (string-to-number (match-string 3 s))
  10118. h (string-to-number (match-string 2 s)))
  10119. (if (org-pos-in-match-range pos 2)
  10120. (setq h (+ h n))
  10121. (setq n (* dm (org-no-warnings (signum n))))
  10122. (when (not (= 0 (setq rem (% m dm))))
  10123. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  10124. (setq m (+ m n)))
  10125. (if (< m 0) (setq m (+ m 60) h (1- h)))
  10126. (if (> m 59) (setq m (- m 60) h (1+ h)))
  10127. (setq h (min 24 (max 0 h)))
  10128. (setq ng 1 new (format "-%02d:%02d" h m)))
  10129. ((org-pos-in-match-range pos 6)
  10130. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  10131. ((org-pos-in-match-range pos 5)
  10132. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  10133. ((org-pos-in-match-range pos 9)
  10134. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  10135. ((org-pos-in-match-range pos 8)
  10136. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  10137. (when ng
  10138. (setq s (concat
  10139. (substring s 0 (match-beginning ng))
  10140. new
  10141. (substring s (match-end ng))))))
  10142. s))
  10143. (defun org-recenter-calendar (date)
  10144. "If the calendar is visible, recenter it to DATE."
  10145. (let* ((win (selected-window))
  10146. (cwin (get-buffer-window "*Calendar*" t))
  10147. (calendar-move-hook nil))
  10148. (when cwin
  10149. (select-window cwin)
  10150. (calendar-goto-date (if (listp date) date
  10151. (calendar-gregorian-from-absolute date)))
  10152. (select-window win))))
  10153. (defun org-goto-calendar (&optional arg)
  10154. "Go to the Emacs calendar at the current date.
  10155. If there is a time stamp in the current line, go to that date.
  10156. A prefix ARG can be used to force the current date."
  10157. (interactive "P")
  10158. (let ((tsr org-ts-regexp) diff
  10159. (calendar-move-hook nil)
  10160. (calendar-view-holidays-initially-flag nil)
  10161. (view-calendar-holidays-initially nil)
  10162. (calendar-view-diary-initially-flag nil)
  10163. (view-diary-entries-initially nil))
  10164. (if (or (org-at-timestamp-p)
  10165. (save-excursion
  10166. (beginning-of-line 1)
  10167. (looking-at (concat ".*" tsr))))
  10168. (let ((d1 (time-to-days (current-time)))
  10169. (d2 (time-to-days
  10170. (org-time-string-to-time (match-string 1)))))
  10171. (setq diff (- d2 d1))))
  10172. (calendar)
  10173. (calendar-goto-today)
  10174. (if (and diff (not arg)) (calendar-forward-day diff))))
  10175. (defun org-get-date-from-calendar ()
  10176. "Return a list (month day year) of date at point in calendar."
  10177. (with-current-buffer "*Calendar*"
  10178. (save-match-data
  10179. (calendar-cursor-to-date))))
  10180. (defun org-date-from-calendar ()
  10181. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  10182. If there is already a time stamp at the cursor position, update it."
  10183. (interactive)
  10184. (if (org-at-timestamp-p t)
  10185. (org-timestamp-change 0 'calendar)
  10186. (let ((cal-date (org-get-date-from-calendar)))
  10187. (org-insert-time-stamp
  10188. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  10189. (defun org-minutes-to-hh:mm-string (m)
  10190. "Compute H:MM from a number of minutes."
  10191. (let ((h (/ m 60)))
  10192. (setq m (- m (* 60 h)))
  10193. (format "%d:%02d" h m)))
  10194. (defun org-hh:mm-string-to-minutes (s)
  10195. "Convert a string H:MM to a number of minutes."
  10196. (if (string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  10197. (+ (* (string-to-number (match-string 1 s)) 60)
  10198. (string-to-number (match-string 2 s)))
  10199. 0))
  10200. ;;;; Agenda files
  10201. ;;;###autoload
  10202. (defun org-iswitchb (&optional arg)
  10203. "Use `iswitchb-read-buffer' to prompt for an Org buffer to switch to.
  10204. With a prefix argument, restrict available to files.
  10205. With two prefix arguments, restrict available buffers to agenda files.
  10206. Due to some yet unresolved reason, global function
  10207. `iswitchb-mode' needs to be active for this function to work."
  10208. (interactive "P")
  10209. (require 'iswitchb)
  10210. (let ((enabled iswitchb-mode) blist)
  10211. (or enabled (iswitchb-mode 1))
  10212. (setq blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  10213. ((equal arg '(16)) (org-buffer-list 'agenda))
  10214. (t (org-buffer-list))))
  10215. (unwind-protect
  10216. (let ((iswitchb-make-buflist-hook
  10217. (lambda ()
  10218. (setq iswitchb-temp-buflist
  10219. (mapcar 'buffer-name blist)))))
  10220. (switch-to-buffer
  10221. (iswitchb-read-buffer
  10222. "Switch-to: " nil t))
  10223. (or enabled (iswitchb-mode -1))))))
  10224. (defun org-buffer-list (&optional predicate tmp)
  10225. "Return a list of Org buffers.
  10226. PREDICATE can be either 'export, 'files or 'agenda.
  10227. 'export restrict the list to Export buffers.
  10228. 'files restrict the list to buffers visiting Org files.
  10229. 'agenda restrict the list to buffers visiting agenda files.
  10230. If TMP is non-nil, don't include temporary buffers."
  10231. (let (filter blist)
  10232. (setq filter
  10233. (cond ((eq predicate 'files) "\.org$")
  10234. ((eq predicate 'export) "\*Org .*Export")
  10235. (t "\*Org \\|\.org$")))
  10236. (setq blist
  10237. (mapcar
  10238. (lambda(b)
  10239. (let ((bname (buffer-name b))
  10240. (bfile (buffer-file-name b)))
  10241. (if (and (string-match filter bname)
  10242. (if (eq predicate 'agenda)
  10243. (member bfile
  10244. (mapcar (lambda(f) (file-truename f))
  10245. org-agenda-files)) t)
  10246. (if tmp (not (string-match "tmp" bname)) t)) b)))
  10247. (buffer-list)))
  10248. (delete nil blist)))
  10249. (defun org-agenda-files (&optional unrestricted ext)
  10250. "Get the list of agenda files.
  10251. Optional UNRESTRICTED means return the full list even if a restriction
  10252. is currently in place.
  10253. When EXT is non-nil, try to add all files that are created by adding EXT
  10254. to the file nemes. Basically, this is a way to add the archive files
  10255. to the list, by setting EXT to \"_archive\" If EXT is non-nil, but not
  10256. a string, \"_archive\" will be used."
  10257. (let ((files
  10258. (cond
  10259. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  10260. ((stringp org-agenda-files) (org-read-agenda-file-list))
  10261. ((listp org-agenda-files) org-agenda-files)
  10262. (t (error "Invalid value of `org-agenda-files'")))))
  10263. (setq files (apply 'append
  10264. (mapcar (lambda (f)
  10265. (if (file-directory-p f)
  10266. (directory-files
  10267. f t org-agenda-file-regexp)
  10268. (list f)))
  10269. files)))
  10270. (when org-agenda-skip-unavailable-files
  10271. (setq files (delq nil
  10272. (mapcar (function
  10273. (lambda (file)
  10274. (and (file-readable-p file) file)))
  10275. files))))
  10276. (when ext
  10277. (setq ext (if (and (stringp ext) (string-match "\\S-" ext))
  10278. ext "_archive"))
  10279. (setq files (apply 'append
  10280. (mapcar
  10281. (lambda (f)
  10282. (if (file-exists-p (concat f ext))
  10283. (list f (concat f ext))
  10284. (list f)))
  10285. files))))
  10286. files))
  10287. (defun org-edit-agenda-file-list ()
  10288. "Edit the list of agenda files.
  10289. Depending on setup, this either uses customize to edit the variable
  10290. `org-agenda-files', or it visits the file that is holding the list. In the
  10291. latter case, the buffer is set up in a way that saving it automatically kills
  10292. the buffer and restores the previous window configuration."
  10293. (interactive)
  10294. (if (stringp org-agenda-files)
  10295. (let ((cw (current-window-configuration)))
  10296. (find-file org-agenda-files)
  10297. (org-set-local 'org-window-configuration cw)
  10298. (org-add-hook 'after-save-hook
  10299. (lambda ()
  10300. (set-window-configuration
  10301. (prog1 org-window-configuration
  10302. (kill-buffer (current-buffer))))
  10303. (org-install-agenda-files-menu)
  10304. (message "New agenda file list installed"))
  10305. nil 'local)
  10306. (message "%s" (substitute-command-keys
  10307. "Edit list and finish with \\[save-buffer]")))
  10308. (customize-variable 'org-agenda-files)))
  10309. (defun org-store-new-agenda-file-list (list)
  10310. "Set new value for the agenda file list and save it correcly."
  10311. (if (stringp org-agenda-files)
  10312. (let ((f org-agenda-files) b)
  10313. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  10314. (with-temp-file f
  10315. (insert (mapconcat 'identity list "\n") "\n")))
  10316. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  10317. (setq org-agenda-files list)
  10318. (customize-save-variable 'org-agenda-files org-agenda-files))))
  10319. (defun org-read-agenda-file-list ()
  10320. "Read the list of agenda files from a file."
  10321. (when (file-directory-p org-agenda-files)
  10322. (error "`org-agenda-files' cannot be a single directory"))
  10323. (when (stringp org-agenda-files)
  10324. (with-temp-buffer
  10325. (insert-file-contents org-agenda-files)
  10326. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  10327. ;;;###autoload
  10328. (defun org-cycle-agenda-files ()
  10329. "Cycle through the files in `org-agenda-files'.
  10330. If the current buffer visits an agenda file, find the next one in the list.
  10331. If the current buffer does not, find the first agenda file."
  10332. (interactive)
  10333. (let* ((fs (org-agenda-files t))
  10334. (files (append fs (list (car fs))))
  10335. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  10336. file)
  10337. (unless files (error "No agenda files"))
  10338. (catch 'exit
  10339. (while (setq file (pop files))
  10340. (if (equal (file-truename file) tcf)
  10341. (when (car files)
  10342. (find-file (car files))
  10343. (throw 'exit t))))
  10344. (find-file (car fs)))
  10345. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  10346. (defun org-agenda-file-to-front (&optional to-end)
  10347. "Move/add the current file to the top of the agenda file list.
  10348. If the file is not present in the list, it is added to the front. If it is
  10349. present, it is moved there. With optional argument TO-END, add/move to the
  10350. end of the list."
  10351. (interactive "P")
  10352. (let ((org-agenda-skip-unavailable-files nil)
  10353. (file-alist (mapcar (lambda (x)
  10354. (cons (file-truename x) x))
  10355. (org-agenda-files t)))
  10356. (ctf (file-truename buffer-file-name))
  10357. x had)
  10358. (setq x (assoc ctf file-alist) had x)
  10359. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  10360. (if to-end
  10361. (setq file-alist (append (delq x file-alist) (list x)))
  10362. (setq file-alist (cons x (delq x file-alist))))
  10363. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  10364. (org-install-agenda-files-menu)
  10365. (message "File %s to %s of agenda file list"
  10366. (if had "moved" "added") (if to-end "end" "front"))))
  10367. (defun org-remove-file (&optional file)
  10368. "Remove current file from the list of files in variable `org-agenda-files'.
  10369. These are the files which are being checked for agenda entries.
  10370. Optional argument FILE means, use this file instead of the current."
  10371. (interactive)
  10372. (let* ((org-agenda-skip-unavailable-files nil)
  10373. (file (or file buffer-file-name))
  10374. (true-file (file-truename file))
  10375. (afile (abbreviate-file-name file))
  10376. (files (delq nil (mapcar
  10377. (lambda (x)
  10378. (if (equal true-file
  10379. (file-truename x))
  10380. nil x))
  10381. (org-agenda-files t)))))
  10382. (if (not (= (length files) (length (org-agenda-files t))))
  10383. (progn
  10384. (org-store-new-agenda-file-list files)
  10385. (org-install-agenda-files-menu)
  10386. (message "Removed file: %s" afile))
  10387. (message "File was not in list: %s (not removed)" afile))))
  10388. (defun org-file-menu-entry (file)
  10389. (vector file (list 'find-file file) t))
  10390. (defun org-check-agenda-file (file)
  10391. "Make sure FILE exists. If not, ask user what to do."
  10392. (when (not (file-exists-p file))
  10393. (message "non-existent file %s. [R]emove from list or [A]bort?"
  10394. (abbreviate-file-name file))
  10395. (let ((r (downcase (read-char-exclusive))))
  10396. (cond
  10397. ((equal r ?r)
  10398. (org-remove-file file)
  10399. (throw 'nextfile t))
  10400. (t (error "Abort"))))))
  10401. (defun org-get-agenda-file-buffer (file)
  10402. "Get a buffer visiting FILE. If the buffer needs to be created, add
  10403. it to the list of buffers which might be released later."
  10404. (let ((buf (org-find-base-buffer-visiting file)))
  10405. (if buf
  10406. buf ; just return it
  10407. ;; Make a new buffer and remember it
  10408. (setq buf (find-file-noselect file))
  10409. (if buf (push buf org-agenda-new-buffers))
  10410. buf)))
  10411. (defun org-release-buffers (blist)
  10412. "Release all buffers in list, asking the user for confirmation when needed.
  10413. When a buffer is unmodified, it is just killed. When modified, it is saved
  10414. \(if the user agrees) and then killed."
  10415. (let (buf file)
  10416. (while (setq buf (pop blist))
  10417. (setq file (buffer-file-name buf))
  10418. (when (and (buffer-modified-p buf)
  10419. file
  10420. (y-or-n-p (format "Save file %s? " file)))
  10421. (with-current-buffer buf (save-buffer)))
  10422. (kill-buffer buf))))
  10423. (defun org-prepare-agenda-buffers (files)
  10424. "Create buffers for all agenda files, protect archived trees and comments."
  10425. (interactive)
  10426. (let ((pa '(:org-archived t))
  10427. (pc '(:org-comment t))
  10428. (pall '(:org-archived t :org-comment t))
  10429. (inhibit-read-only t)
  10430. (rea (concat ":" org-archive-tag ":"))
  10431. bmp file re)
  10432. (save-excursion
  10433. (save-restriction
  10434. (while (setq file (pop files))
  10435. (if (bufferp file)
  10436. (set-buffer file)
  10437. (org-check-agenda-file file)
  10438. (set-buffer (org-get-agenda-file-buffer file)))
  10439. (widen)
  10440. (setq bmp (buffer-modified-p))
  10441. (org-refresh-category-properties)
  10442. (setq org-todo-keywords-for-agenda
  10443. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  10444. (setq org-done-keywords-for-agenda
  10445. (append org-done-keywords-for-agenda org-done-keywords))
  10446. (save-excursion
  10447. (remove-text-properties (point-min) (point-max) pall)
  10448. (when org-agenda-skip-archived-trees
  10449. (goto-char (point-min))
  10450. (while (re-search-forward rea nil t)
  10451. (if (org-on-heading-p t)
  10452. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  10453. (goto-char (point-min))
  10454. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  10455. (while (re-search-forward re nil t)
  10456. (add-text-properties
  10457. (match-beginning 0) (org-end-of-subtree t) pc)))
  10458. (set-buffer-modified-p bmp))))))
  10459. ;;;; Embedded LaTeX
  10460. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  10461. "Keymap for the minor `org-cdlatex-mode'.")
  10462. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  10463. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  10464. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  10465. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  10466. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  10467. (defvar org-cdlatex-texmathp-advice-is-done nil
  10468. "Flag remembering if we have applied the advice to texmathp already.")
  10469. (define-minor-mode org-cdlatex-mode
  10470. "Toggle the minor `org-cdlatex-mode'.
  10471. This mode supports entering LaTeX environment and math in LaTeX fragments
  10472. in Org-mode.
  10473. \\{org-cdlatex-mode-map}"
  10474. nil " OCDL" nil
  10475. (when org-cdlatex-mode (require 'cdlatex))
  10476. (unless org-cdlatex-texmathp-advice-is-done
  10477. (setq org-cdlatex-texmathp-advice-is-done t)
  10478. (defadvice texmathp (around org-math-always-on activate)
  10479. "Always return t in org-mode buffers.
  10480. This is because we want to insert math symbols without dollars even outside
  10481. the LaTeX math segments. If Orgmode thinks that point is actually inside
  10482. en embedded LaTeX fragement, let texmathp do its job.
  10483. \\[org-cdlatex-mode-map]"
  10484. (interactive)
  10485. (let (p)
  10486. (cond
  10487. ((not (org-mode-p)) ad-do-it)
  10488. ((eq this-command 'cdlatex-math-symbol)
  10489. (setq ad-return-value t
  10490. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  10491. (t
  10492. (let ((p (org-inside-LaTeX-fragment-p)))
  10493. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  10494. (setq ad-return-value t
  10495. texmathp-why '("Org-mode embedded math" . 0))
  10496. (if p ad-do-it)))))))))
  10497. (defun turn-on-org-cdlatex ()
  10498. "Unconditionally turn on `org-cdlatex-mode'."
  10499. (org-cdlatex-mode 1))
  10500. (defun org-inside-LaTeX-fragment-p ()
  10501. "Test if point is inside a LaTeX fragment.
  10502. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  10503. sequence appearing also before point.
  10504. Even though the matchers for math are configurable, this function assumes
  10505. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  10506. delimiters are skipped when they have been removed by customization.
  10507. The return value is nil, or a cons cell with the delimiter and
  10508. and the position of this delimiter.
  10509. This function does a reasonably good job, but can locally be fooled by
  10510. for example currency specifications. For example it will assume being in
  10511. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  10512. fragments that are properly closed, but during editing, we have to live
  10513. with the uncertainty caused by missing closing delimiters. This function
  10514. looks only before point, not after."
  10515. (catch 'exit
  10516. (let ((pos (point))
  10517. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  10518. (lim (progn
  10519. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  10520. (point)))
  10521. dd-on str (start 0) m re)
  10522. (goto-char pos)
  10523. (when dodollar
  10524. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  10525. re (nth 1 (assoc "$" org-latex-regexps)))
  10526. (while (string-match re str start)
  10527. (cond
  10528. ((= (match-end 0) (length str))
  10529. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  10530. ((= (match-end 0) (- (length str) 5))
  10531. (throw 'exit nil))
  10532. (t (setq start (match-end 0))))))
  10533. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  10534. (goto-char pos)
  10535. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  10536. (and (match-beginning 2) (throw 'exit nil))
  10537. ;; count $$
  10538. (while (re-search-backward "\\$\\$" lim t)
  10539. (setq dd-on (not dd-on)))
  10540. (goto-char pos)
  10541. (if dd-on (cons "$$" m))))))
  10542. (defun org-try-cdlatex-tab ()
  10543. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  10544. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  10545. - inside a LaTeX fragment, or
  10546. - after the first word in a line, where an abbreviation expansion could
  10547. insert a LaTeX environment."
  10548. (when org-cdlatex-mode
  10549. (cond
  10550. ((save-excursion
  10551. (skip-chars-backward "a-zA-Z0-9*")
  10552. (skip-chars-backward " \t")
  10553. (bolp))
  10554. (cdlatex-tab) t)
  10555. ((org-inside-LaTeX-fragment-p)
  10556. (cdlatex-tab) t)
  10557. (t nil))))
  10558. (defun org-cdlatex-underscore-caret (&optional arg)
  10559. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  10560. Revert to the normal definition outside of these fragments."
  10561. (interactive "P")
  10562. (if (org-inside-LaTeX-fragment-p)
  10563. (call-interactively 'cdlatex-sub-superscript)
  10564. (let (org-cdlatex-mode)
  10565. (call-interactively (key-binding (vector last-input-event))))))
  10566. (defun org-cdlatex-math-modify (&optional arg)
  10567. "Execute `cdlatex-math-modify' in LaTeX fragments.
  10568. Revert to the normal definition outside of these fragments."
  10569. (interactive "P")
  10570. (if (org-inside-LaTeX-fragment-p)
  10571. (call-interactively 'cdlatex-math-modify)
  10572. (let (org-cdlatex-mode)
  10573. (call-interactively (key-binding (vector last-input-event))))))
  10574. (defvar org-latex-fragment-image-overlays nil
  10575. "List of overlays carrying the images of latex fragments.")
  10576. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  10577. (defun org-remove-latex-fragment-image-overlays ()
  10578. "Remove all overlays with LaTeX fragment images in current buffer."
  10579. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  10580. (setq org-latex-fragment-image-overlays nil))
  10581. (defun org-preview-latex-fragment (&optional subtree)
  10582. "Preview the LaTeX fragment at point, or all locally or globally.
  10583. If the cursor is in a LaTeX fragment, create the image and overlay
  10584. it over the source code. If there is no fragment at point, display
  10585. all fragments in the current text, from one headline to the next. With
  10586. prefix SUBTREE, display all fragments in the current subtree. With a
  10587. double prefix `C-u C-u', or when the cursor is before the first headline,
  10588. display all fragments in the buffer.
  10589. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  10590. (interactive "P")
  10591. (org-remove-latex-fragment-image-overlays)
  10592. (save-excursion
  10593. (save-restriction
  10594. (let (beg end at msg)
  10595. (cond
  10596. ((or (equal subtree '(16))
  10597. (not (save-excursion
  10598. (re-search-backward (concat "^" outline-regexp) nil t))))
  10599. (setq beg (point-min) end (point-max)
  10600. msg "Creating images for buffer...%s"))
  10601. ((equal subtree '(4))
  10602. (org-back-to-heading)
  10603. (setq beg (point) end (org-end-of-subtree t)
  10604. msg "Creating images for subtree...%s"))
  10605. (t
  10606. (if (setq at (org-inside-LaTeX-fragment-p))
  10607. (goto-char (max (point-min) (- (cdr at) 2)))
  10608. (org-back-to-heading))
  10609. (setq beg (point) end (progn (outline-next-heading) (point))
  10610. msg (if at "Creating image...%s"
  10611. "Creating images for entry...%s"))))
  10612. (message msg "")
  10613. (narrow-to-region beg end)
  10614. (goto-char beg)
  10615. (org-format-latex
  10616. (concat "ltxpng/" (file-name-sans-extension
  10617. (file-name-nondirectory
  10618. buffer-file-name)))
  10619. default-directory 'overlays msg at 'forbuffer)
  10620. (message msg "done. Use `C-c C-c' to remove images.")))))
  10621. (defvar org-latex-regexps
  10622. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  10623. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  10624. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  10625. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([ .,?;:'\")\000]\\|$\\)" 2 nil)
  10626. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  10627. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  10628. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  10629. "Regular expressions for matching embedded LaTeX.")
  10630. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  10631. "Replace LaTeX fragments with links to an image, and produce images."
  10632. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  10633. (let* ((prefixnodir (file-name-nondirectory prefix))
  10634. (absprefix (expand-file-name prefix dir))
  10635. (todir (file-name-directory absprefix))
  10636. (opt org-format-latex-options)
  10637. (matchers (plist-get opt :matchers))
  10638. (re-list org-latex-regexps)
  10639. (cnt 0) txt link beg end re e checkdir
  10640. m n block linkfile movefile ov)
  10641. ;; Check if there are old images files with this prefix, and remove them
  10642. (when (file-directory-p todir)
  10643. (mapc 'delete-file
  10644. (directory-files
  10645. todir 'full
  10646. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  10647. ;; Check the different regular expressions
  10648. (while (setq e (pop re-list))
  10649. (setq m (car e) re (nth 1 e) n (nth 2 e)
  10650. block (if (nth 3 e) "\n\n" ""))
  10651. (when (member m matchers)
  10652. (goto-char (point-min))
  10653. (while (re-search-forward re nil t)
  10654. (when (or (not at) (equal (cdr at) (match-beginning n)))
  10655. (setq txt (match-string n)
  10656. beg (match-beginning n) end (match-end n)
  10657. cnt (1+ cnt)
  10658. linkfile (format "%s_%04d.png" prefix cnt)
  10659. movefile (format "%s_%04d.png" absprefix cnt)
  10660. link (concat block "[[file:" linkfile "]]" block))
  10661. (if msg (message msg cnt))
  10662. (goto-char beg)
  10663. (unless checkdir ; make sure the directory exists
  10664. (setq checkdir t)
  10665. (or (file-directory-p todir) (make-directory todir)))
  10666. (org-create-formula-image
  10667. txt movefile opt forbuffer)
  10668. (if overlays
  10669. (progn
  10670. (setq ov (org-make-overlay beg end))
  10671. (if (featurep 'xemacs)
  10672. (progn
  10673. (org-overlay-put ov 'invisible t)
  10674. (org-overlay-put
  10675. ov 'end-glyph
  10676. (make-glyph (vector 'png :file movefile))))
  10677. (org-overlay-put
  10678. ov 'display
  10679. (list 'image :type 'png :file movefile :ascent 'center)))
  10680. (push ov org-latex-fragment-image-overlays)
  10681. (goto-char end))
  10682. (delete-region beg end)
  10683. (insert link))))))))
  10684. ;; This function borrows from Ganesh Swami's latex2png.el
  10685. (defun org-create-formula-image (string tofile options buffer)
  10686. (let* ((tmpdir (if (featurep 'xemacs)
  10687. (temp-directory)
  10688. temporary-file-directory))
  10689. (texfilebase (make-temp-name
  10690. (expand-file-name "orgtex" tmpdir)))
  10691. (texfile (concat texfilebase ".tex"))
  10692. (dvifile (concat texfilebase ".dvi"))
  10693. (pngfile (concat texfilebase ".png"))
  10694. (fnh (if (featurep 'xemacs)
  10695. (font-height (get-face-font 'default))
  10696. (face-attribute 'default :height nil)))
  10697. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  10698. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  10699. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  10700. "Black"))
  10701. (bg (or (plist-get options (if buffer :background :html-background))
  10702. "Transparent")))
  10703. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  10704. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  10705. (with-temp-file texfile
  10706. (insert org-format-latex-header
  10707. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  10708. (let ((dir default-directory))
  10709. (condition-case nil
  10710. (progn
  10711. (cd tmpdir)
  10712. (call-process "latex" nil nil nil texfile))
  10713. (error nil))
  10714. (cd dir))
  10715. (if (not (file-exists-p dvifile))
  10716. (progn (message "Failed to create dvi file from %s" texfile) nil)
  10717. (call-process "dvipng" nil nil nil
  10718. "-E" "-fg" fg "-bg" bg
  10719. "-D" dpi
  10720. ;;"-x" scale "-y" scale
  10721. "-T" "tight"
  10722. "-o" pngfile
  10723. dvifile)
  10724. (if (not (file-exists-p pngfile))
  10725. (progn (message "Failed to create png file from %s" texfile) nil)
  10726. ;; Use the requested file name and clean up
  10727. (copy-file pngfile tofile 'replace)
  10728. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  10729. (delete-file (concat texfilebase e)))
  10730. pngfile))))
  10731. (defun org-dvipng-color (attr)
  10732. "Return an rgb color specification for dvipng."
  10733. (apply 'format "rgb %s %s %s"
  10734. (mapcar 'org-normalize-color
  10735. (color-values (face-attribute 'default attr nil)))))
  10736. (defun org-normalize-color (value)
  10737. "Return string to be used as color value for an RGB component."
  10738. (format "%g" (/ value 65535.0)))
  10739. ;;;; Key bindings
  10740. ;; Make `C-c C-x' a prefix key
  10741. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  10742. ;; TAB key with modifiers
  10743. (org-defkey org-mode-map "\C-i" 'org-cycle)
  10744. (org-defkey org-mode-map [(tab)] 'org-cycle)
  10745. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  10746. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  10747. (org-defkey org-mode-map "\M-\t" 'org-complete)
  10748. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  10749. ;; The following line is necessary under Suse GNU/Linux
  10750. (unless (featurep 'xemacs)
  10751. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  10752. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  10753. (define-key org-mode-map [backtab] 'org-shifttab)
  10754. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  10755. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  10756. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  10757. ;; Cursor keys with modifiers
  10758. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  10759. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  10760. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  10761. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  10762. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  10763. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  10764. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  10765. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  10766. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  10767. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  10768. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  10769. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  10770. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  10771. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  10772. ;;; Extra keys for tty access.
  10773. ;; We only set them when really needed because otherwise the
  10774. ;; menus don't show the simple keys
  10775. (when (or (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  10776. (not window-system))
  10777. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  10778. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  10779. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  10780. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  10781. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  10782. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  10783. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  10784. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  10785. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  10786. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  10787. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  10788. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  10789. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  10790. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  10791. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  10792. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  10793. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  10794. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  10795. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  10796. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  10797. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  10798. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
  10799. ;; All the other keys
  10800. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  10801. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  10802. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree)
  10803. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  10804. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  10805. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  10806. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  10807. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  10808. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  10809. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  10810. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  10811. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  10812. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  10813. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  10814. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  10815. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  10816. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  10817. (org-defkey org-mode-map "\C-c\\" 'org-tags-sparse-tree) ; Minor-mode res.
  10818. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  10819. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  10820. (org-defkey org-mode-map [(control return)] 'org-insert-heading-after-current)
  10821. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  10822. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  10823. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  10824. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  10825. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  10826. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  10827. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  10828. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  10829. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  10830. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  10831. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  10832. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  10833. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  10834. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  10835. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  10836. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  10837. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  10838. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  10839. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  10840. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  10841. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  10842. (org-defkey org-mode-map "\C-c^" 'org-sort)
  10843. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  10844. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  10845. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  10846. (org-defkey org-mode-map "\C-m" 'org-return)
  10847. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  10848. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  10849. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  10850. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  10851. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  10852. (org-defkey org-mode-map "\C-c'" 'org-table-edit-formulas)
  10853. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  10854. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  10855. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  10856. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  10857. (org-defkey org-mode-map "\C-c\C-q" 'org-table-wrap-region)
  10858. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  10859. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  10860. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  10861. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  10862. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  10863. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-cut-special)
  10864. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  10865. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  10866. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  10867. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  10868. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  10869. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  10870. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  10871. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  10872. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  10873. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  10874. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  10875. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  10876. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  10877. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  10878. (org-defkey org-mode-map "\C-c\C-xr" 'org-insert-columns-dblock)
  10879. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  10880. (when (featurep 'xemacs)
  10881. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  10882. (defvar org-table-auto-blank-field) ; defined in org-table.el
  10883. (defun org-self-insert-command (N)
  10884. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  10885. If the cursor is in a table looking at whitespace, the whitespace is
  10886. overwritten, and the table is not marked as requiring realignment."
  10887. (interactive "p")
  10888. (if (and (org-table-p)
  10889. (progn
  10890. ;; check if we blank the field, and if that triggers align
  10891. (and (featurep 'org-table) org-table-auto-blank-field
  10892. (member last-command
  10893. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  10894. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  10895. ;; got extra space, this field does not determine column width
  10896. (let (org-table-may-need-update) (org-table-blank-field))
  10897. ;; no extra space, this field may determine column width
  10898. (org-table-blank-field)))
  10899. t)
  10900. (eq N 1)
  10901. (looking-at "[^|\n]* |"))
  10902. (let (org-table-may-need-update)
  10903. (goto-char (1- (match-end 0)))
  10904. (delete-backward-char 1)
  10905. (goto-char (match-beginning 0))
  10906. (self-insert-command N))
  10907. (setq org-table-may-need-update t)
  10908. (self-insert-command N)
  10909. (org-fix-tags-on-the-fly)))
  10910. (defun org-fix-tags-on-the-fly ()
  10911. (when (and (equal (char-after (point-at-bol)) ?*)
  10912. (org-on-heading-p))
  10913. (org-align-tags-here org-tags-column)))
  10914. (defun org-delete-backward-char (N)
  10915. "Like `delete-backward-char', insert whitespace at field end in tables.
  10916. When deleting backwards, in tables this function will insert whitespace in
  10917. front of the next \"|\" separator, to keep the table aligned. The table will
  10918. still be marked for re-alignment if the field did fill the entire column,
  10919. because, in this case the deletion might narrow the column."
  10920. (interactive "p")
  10921. (if (and (org-table-p)
  10922. (eq N 1)
  10923. (string-match "|" (buffer-substring (point-at-bol) (point)))
  10924. (looking-at ".*?|"))
  10925. (let ((pos (point))
  10926. (noalign (looking-at "[^|\n\r]* |"))
  10927. (c org-table-may-need-update))
  10928. (backward-delete-char N)
  10929. (skip-chars-forward "^|")
  10930. (insert " ")
  10931. (goto-char (1- pos))
  10932. ;; noalign: if there were two spaces at the end, this field
  10933. ;; does not determine the width of the column.
  10934. (if noalign (setq org-table-may-need-update c)))
  10935. (backward-delete-char N)
  10936. (org-fix-tags-on-the-fly)))
  10937. (defun org-delete-char (N)
  10938. "Like `delete-char', but insert whitespace at field end in tables.
  10939. When deleting characters, in tables this function will insert whitespace in
  10940. front of the next \"|\" separator, to keep the table aligned. The table will
  10941. still be marked for re-alignment if the field did fill the entire column,
  10942. because, in this case the deletion might narrow the column."
  10943. (interactive "p")
  10944. (if (and (org-table-p)
  10945. (not (bolp))
  10946. (not (= (char-after) ?|))
  10947. (eq N 1))
  10948. (if (looking-at ".*?|")
  10949. (let ((pos (point))
  10950. (noalign (looking-at "[^|\n\r]* |"))
  10951. (c org-table-may-need-update))
  10952. (replace-match (concat
  10953. (substring (match-string 0) 1 -1)
  10954. " |"))
  10955. (goto-char pos)
  10956. ;; noalign: if there were two spaces at the end, this field
  10957. ;; does not determine the width of the column.
  10958. (if noalign (setq org-table-may-need-update c)))
  10959. (delete-char N))
  10960. (delete-char N)
  10961. (org-fix-tags-on-the-fly)))
  10962. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  10963. (put 'org-self-insert-command 'delete-selection t)
  10964. (put 'orgtbl-self-insert-command 'delete-selection t)
  10965. (put 'org-delete-char 'delete-selection 'supersede)
  10966. (put 'org-delete-backward-char 'delete-selection 'supersede)
  10967. ;; Make `flyspell-mode' delay after some commands
  10968. (put 'org-self-insert-command 'flyspell-delayed t)
  10969. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  10970. (put 'org-delete-char 'flyspell-delayed t)
  10971. (put 'org-delete-backward-char 'flyspell-delayed t)
  10972. ;; Make pabbrev-mode expand after org-mode commands
  10973. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  10974. (put 'orgybl-self-insert-command 'pabbrev-expand-after-command t)
  10975. ;; How to do this: Measure non-white length of current string
  10976. ;; If equal to column width, we should realign.
  10977. (defun org-remap (map &rest commands)
  10978. "In MAP, remap the functions given in COMMANDS.
  10979. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  10980. (let (new old)
  10981. (while commands
  10982. (setq old (pop commands) new (pop commands))
  10983. (if (fboundp 'command-remapping)
  10984. (org-defkey map (vector 'remap old) new)
  10985. (substitute-key-definition old new map global-map)))))
  10986. (when (eq org-enable-table-editor 'optimized)
  10987. ;; If the user wants maximum table support, we need to hijack
  10988. ;; some standard editing functions
  10989. (org-remap org-mode-map
  10990. 'self-insert-command 'org-self-insert-command
  10991. 'delete-char 'org-delete-char
  10992. 'delete-backward-char 'org-delete-backward-char)
  10993. (org-defkey org-mode-map "|" 'org-force-self-insert))
  10994. (defun org-shiftcursor-error ()
  10995. "Throw an error because Shift-Cursor command was applied in wrong context."
  10996. (error "This command is active in special context like tables, headlines or timestamps"))
  10997. (defun org-shifttab (&optional arg)
  10998. "Global visibility cycling or move to previous table field.
  10999. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  11000. on context.
  11001. See the individual commands for more information."
  11002. (interactive "P")
  11003. (cond
  11004. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  11005. (arg (message "Content view to level: ")
  11006. (org-content (prefix-numeric-value arg))
  11007. (setq org-cycle-global-status 'overview))
  11008. (t (call-interactively 'org-global-cycle))))
  11009. (defun org-shiftmetaleft ()
  11010. "Promote subtree or delete table column.
  11011. Calls `org-promote-subtree', `org-outdent-item',
  11012. or `org-table-delete-column', depending on context.
  11013. See the individual commands for more information."
  11014. (interactive)
  11015. (cond
  11016. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  11017. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  11018. ((org-at-item-p) (call-interactively 'org-outdent-item))
  11019. (t (org-shiftcursor-error))))
  11020. (defun org-shiftmetaright ()
  11021. "Demote subtree or insert table column.
  11022. Calls `org-demote-subtree', `org-indent-item',
  11023. or `org-table-insert-column', depending on context.
  11024. See the individual commands for more information."
  11025. (interactive)
  11026. (cond
  11027. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  11028. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  11029. ((org-at-item-p) (call-interactively 'org-indent-item))
  11030. (t (org-shiftcursor-error))))
  11031. (defun org-shiftmetaup (&optional arg)
  11032. "Move subtree up or kill table row.
  11033. Calls `org-move-subtree-up' or `org-table-kill-row' or
  11034. `org-move-item-up' depending on context. See the individual commands
  11035. for more information."
  11036. (interactive "P")
  11037. (cond
  11038. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  11039. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  11040. ((org-at-item-p) (call-interactively 'org-move-item-up))
  11041. (t (org-shiftcursor-error))))
  11042. (defun org-shiftmetadown (&optional arg)
  11043. "Move subtree down or insert table row.
  11044. Calls `org-move-subtree-down' or `org-table-insert-row' or
  11045. `org-move-item-down', depending on context. See the individual
  11046. commands for more information."
  11047. (interactive "P")
  11048. (cond
  11049. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  11050. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  11051. ((org-at-item-p) (call-interactively 'org-move-item-down))
  11052. (t (org-shiftcursor-error))))
  11053. (defun org-metaleft (&optional arg)
  11054. "Promote heading or move table column to left.
  11055. Calls `org-do-promote' or `org-table-move-column', depending on context.
  11056. With no specific context, calls the Emacs default `backward-word'.
  11057. See the individual commands for more information."
  11058. (interactive "P")
  11059. (cond
  11060. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  11061. ((or (org-on-heading-p) (org-region-active-p))
  11062. (call-interactively 'org-do-promote))
  11063. ((org-at-item-p) (call-interactively 'org-outdent-item))
  11064. (t (call-interactively 'backward-word))))
  11065. (defun org-metaright (&optional arg)
  11066. "Demote subtree or move table column to right.
  11067. Calls `org-do-demote' or `org-table-move-column', depending on context.
  11068. With no specific context, calls the Emacs default `forward-word'.
  11069. See the individual commands for more information."
  11070. (interactive "P")
  11071. (cond
  11072. ((org-at-table-p) (call-interactively 'org-table-move-column))
  11073. ((or (org-on-heading-p) (org-region-active-p))
  11074. (call-interactively 'org-do-demote))
  11075. ((org-at-item-p) (call-interactively 'org-indent-item))
  11076. (t (call-interactively 'forward-word))))
  11077. (defun org-metaup (&optional arg)
  11078. "Move subtree up or move table row up.
  11079. Calls `org-move-subtree-up' or `org-table-move-row' or
  11080. `org-move-item-up', depending on context. See the individual commands
  11081. for more information."
  11082. (interactive "P")
  11083. (cond
  11084. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  11085. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  11086. ((org-at-item-p) (call-interactively 'org-move-item-up))
  11087. (t (transpose-lines 1) (beginning-of-line -1))))
  11088. (defun org-metadown (&optional arg)
  11089. "Move subtree down or move table row down.
  11090. Calls `org-move-subtree-down' or `org-table-move-row' or
  11091. `org-move-item-down', depending on context. See the individual
  11092. commands for more information."
  11093. (interactive "P")
  11094. (cond
  11095. ((org-at-table-p) (call-interactively 'org-table-move-row))
  11096. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  11097. ((org-at-item-p) (call-interactively 'org-move-item-down))
  11098. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  11099. (defun org-shiftup (&optional arg)
  11100. "Increase item in timestamp or increase priority of current headline.
  11101. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  11102. depending on context. See the individual commands for more information."
  11103. (interactive "P")
  11104. (cond
  11105. ((org-at-timestamp-p t)
  11106. (call-interactively (if org-edit-timestamp-down-means-later
  11107. 'org-timestamp-down 'org-timestamp-up)))
  11108. ((org-on-heading-p) (call-interactively 'org-priority-up))
  11109. ((org-at-item-p) (call-interactively 'org-previous-item))
  11110. ((org-clocktable-try-shift 'up arg))
  11111. (t (call-interactively 'org-beginning-of-item) (beginning-of-line 1))))
  11112. (defun org-shiftdown (&optional arg)
  11113. "Decrease item in timestamp or decrease priority of current headline.
  11114. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  11115. depending on context. See the individual commands for more information."
  11116. (interactive "P")
  11117. (cond
  11118. ((org-at-timestamp-p t)
  11119. (call-interactively (if org-edit-timestamp-down-means-later
  11120. 'org-timestamp-up 'org-timestamp-down)))
  11121. ((org-on-heading-p) (call-interactively 'org-priority-down))
  11122. ((org-clocktable-try-shift 'down arg))
  11123. (t (call-interactively 'org-next-item))))
  11124. (defun org-shiftright (&optional arg)
  11125. "Next TODO keyword or timestamp one day later, depending on context."
  11126. (interactive "P")
  11127. (cond
  11128. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  11129. ((org-on-heading-p) (org-call-with-arg 'org-todo 'right))
  11130. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet nil))
  11131. ((org-at-property-p) (call-interactively 'org-property-next-allowed-value))
  11132. ((org-clocktable-try-shift 'right arg))
  11133. (t (org-shiftcursor-error))))
  11134. (defun org-shiftleft (&optional arg)
  11135. "Previous TODO keyword or timestamp one day earlier, depending on context."
  11136. (interactive "P")
  11137. (cond
  11138. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  11139. ((org-on-heading-p) (org-call-with-arg 'org-todo 'left))
  11140. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet 'previous))
  11141. ((org-at-property-p)
  11142. (call-interactively 'org-property-previous-allowed-value))
  11143. ((org-clocktable-try-shift 'left arg))
  11144. (t (org-shiftcursor-error))))
  11145. (defun org-shiftcontrolright ()
  11146. "Switch to next TODO set."
  11147. (interactive)
  11148. (cond
  11149. ((org-on-heading-p) (org-call-with-arg 'org-todo 'nextset))
  11150. (t (org-shiftcursor-error))))
  11151. (defun org-shiftcontrolleft ()
  11152. "Switch to previous TODO set."
  11153. (interactive)
  11154. (cond
  11155. ((org-on-heading-p) (org-call-with-arg 'org-todo 'previousset))
  11156. (t (org-shiftcursor-error))))
  11157. (defun org-ctrl-c-ret ()
  11158. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  11159. (interactive)
  11160. (cond
  11161. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  11162. (t (call-interactively 'org-insert-heading))))
  11163. (defun org-copy-special ()
  11164. "Copy region in table or copy current subtree.
  11165. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  11166. See the individual commands for more information."
  11167. (interactive)
  11168. (call-interactively
  11169. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  11170. (defun org-cut-special ()
  11171. "Cut region in table or cut current subtree.
  11172. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  11173. See the individual commands for more information."
  11174. (interactive)
  11175. (call-interactively
  11176. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  11177. (defun org-paste-special (arg)
  11178. "Paste rectangular region into table, or past subtree relative to level.
  11179. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  11180. See the individual commands for more information."
  11181. (interactive "P")
  11182. (if (org-at-table-p)
  11183. (org-table-paste-rectangle)
  11184. (org-paste-subtree arg)))
  11185. (defun org-ctrl-c-ctrl-c (&optional arg)
  11186. "Set tags in headline, or update according to changed information at point.
  11187. This command does many different things, depending on context:
  11188. - If the cursor is in a headline, prompt for tags and insert them
  11189. into the current line, aligned to `org-tags-column'. When called
  11190. with prefix arg, realign all tags in the current buffer.
  11191. - If the cursor is in one of the special #+KEYWORD lines, this
  11192. triggers scanning the buffer for these lines and updating the
  11193. information.
  11194. - If the cursor is inside a table, realign the table. This command
  11195. works even if the automatic table editor has been turned off.
  11196. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  11197. the entire table.
  11198. - If the cursor is a the beginning of a dynamic block, update it.
  11199. - If the cursor is inside a table created by the table.el package,
  11200. activate that table.
  11201. - If the current buffer is a remember buffer, close note and file it.
  11202. with a prefix argument, file it without further interaction to the default
  11203. location.
  11204. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  11205. links in this buffer.
  11206. - If the cursor is on a numbered item in a plain list, renumber the
  11207. ordered list.
  11208. - If the cursor is on a checkbox, toggle it."
  11209. (interactive "P")
  11210. (let ((org-enable-table-editor t))
  11211. (cond
  11212. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  11213. org-occur-highlights
  11214. org-latex-fragment-image-overlays)
  11215. (and (boundp 'org-clock-overlays) (org-remove-clock-overlays))
  11216. (org-remove-occur-highlights)
  11217. (org-remove-latex-fragment-image-overlays)
  11218. (message "Temporary highlights/overlays removed from current buffer"))
  11219. ((and (local-variable-p 'org-finish-function (current-buffer))
  11220. (fboundp org-finish-function))
  11221. (funcall org-finish-function))
  11222. ((org-at-property-p)
  11223. (call-interactively 'org-property-action))
  11224. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  11225. ((org-on-heading-p) (call-interactively 'org-set-tags))
  11226. ((org-at-table.el-p)
  11227. (require 'table)
  11228. (beginning-of-line 1)
  11229. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  11230. (call-interactively 'table-recognize-table))
  11231. ((org-at-table-p)
  11232. (org-table-maybe-eval-formula)
  11233. (if arg
  11234. (call-interactively 'org-table-recalculate)
  11235. (org-table-maybe-recalculate-line))
  11236. (call-interactively 'org-table-align))
  11237. ((org-at-item-checkbox-p)
  11238. (call-interactively 'org-toggle-checkbox))
  11239. ((org-at-item-p)
  11240. (call-interactively 'org-maybe-renumber-ordered-list))
  11241. ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
  11242. ;; Dynamic block
  11243. (beginning-of-line 1)
  11244. (org-update-dblock))
  11245. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  11246. (cond
  11247. ((equal (match-string 1) "TBLFM")
  11248. ;; Recalculate the table before this line
  11249. (save-excursion
  11250. (beginning-of-line 1)
  11251. (skip-chars-backward " \r\n\t")
  11252. (if (org-at-table-p)
  11253. (org-call-with-arg 'org-table-recalculate t))))
  11254. (t
  11255. (call-interactively 'org-mode-restart))))
  11256. (t (error "C-c C-c can do nothing useful at this location.")))))
  11257. (defun org-mode-restart ()
  11258. "Restart Org-mode, to scan again for special lines.
  11259. Also updates the keyword regular expressions."
  11260. (interactive)
  11261. (let ((org-inhibit-startup t)) (org-mode))
  11262. (message "Org-mode restarted to refresh keyword and special line setup"))
  11263. (defun org-kill-note-or-show-branches ()
  11264. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  11265. (interactive)
  11266. (if (not org-finish-function)
  11267. (call-interactively 'show-branches)
  11268. (let ((org-note-abort t))
  11269. (funcall org-finish-function))))
  11270. (defun org-return (&optional indent)
  11271. "Goto next table row or insert a newline.
  11272. Calls `org-table-next-row' or `newline', depending on context.
  11273. See the individual commands for more information."
  11274. (interactive)
  11275. (cond
  11276. ((bobp) (if indent (newline-and-indent) (newline)))
  11277. ((and (org-at-heading-p)
  11278. (looking-at
  11279. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  11280. (org-show-entry)
  11281. (end-of-line 1)
  11282. (newline))
  11283. ((org-at-table-p)
  11284. (org-table-justify-field-maybe)
  11285. (call-interactively 'org-table-next-row))
  11286. (t (if indent (newline-and-indent) (newline)))))
  11287. (defun org-return-indent ()
  11288. "Goto next table row or insert a newline and indent.
  11289. Calls `org-table-next-row' or `newline-and-indent', depending on
  11290. context. See the individual commands for more information."
  11291. (interactive)
  11292. (org-return t))
  11293. (defun org-ctrl-c-star ()
  11294. "Compute table, or change heading status of lines.
  11295. Calls `org-table-recalculate' or `org-toggle-region-headlines',
  11296. depending on context. This will also turn a plain list item or a normal
  11297. line into a subheading."
  11298. (interactive)
  11299. (cond
  11300. ((org-at-table-p)
  11301. (call-interactively 'org-table-recalculate))
  11302. ((org-region-active-p)
  11303. ;; Convert all lines in region to list items
  11304. (call-interactively 'org-toggle-region-headings))
  11305. ((org-on-heading-p)
  11306. (org-toggle-region-headings (point-at-bol)
  11307. (min (1+ (point-at-eol)) (point-max))))
  11308. ((org-at-item-p)
  11309. ;; Convert to heading
  11310. (let ((level (save-match-data
  11311. (save-excursion
  11312. (condition-case nil
  11313. (progn
  11314. (org-back-to-heading t)
  11315. (funcall outline-level))
  11316. (error 0))))))
  11317. (replace-match
  11318. (concat (make-string (org-get-valid-level level 1) ?*) " ") t t)))
  11319. (t (org-toggle-region-headings (point-at-bol)
  11320. (min (1+ (point-at-eol)) (point-max))))))
  11321. (defun org-ctrl-c-minus ()
  11322. "Insert separator line in table or modify bullet status of line.
  11323. Also turns a plain line or a region of lines into list items.
  11324. Calls `org-table-insert-hline', `org-toggle-region-items', or
  11325. `org-cycle-list-bullet', depending on context."
  11326. (interactive)
  11327. (cond
  11328. ((org-at-table-p)
  11329. (call-interactively 'org-table-insert-hline))
  11330. ((org-on-heading-p)
  11331. ;; Convert to item
  11332. (save-excursion
  11333. (beginning-of-line 1)
  11334. (if (looking-at "\\*+ ")
  11335. (replace-match (concat (make-string (- (match-end 0) (point) 1) ?\ ) "- ")))))
  11336. ((org-region-active-p)
  11337. ;; Convert all lines in region to list items
  11338. (call-interactively 'org-toggle-region-items))
  11339. ((org-in-item-p)
  11340. (call-interactively 'org-cycle-list-bullet))
  11341. (t (org-toggle-region-items (point-at-bol)
  11342. (min (1+ (point-at-eol)) (point-max))))))
  11343. (defun org-toggle-region-items (beg end)
  11344. "Convert all lines in region to list items.
  11345. If the first line is already an item, convert all list items in the region
  11346. to normal lines."
  11347. (interactive "r")
  11348. (let (l2 l)
  11349. (save-excursion
  11350. (goto-char end)
  11351. (setq l2 (org-current-line))
  11352. (goto-char beg)
  11353. (beginning-of-line 1)
  11354. (setq l (1- (org-current-line)))
  11355. (if (org-at-item-p)
  11356. ;; We already have items, de-itemize
  11357. (while (< (setq l (1+ l)) l2)
  11358. (when (org-at-item-p)
  11359. (goto-char (match-beginning 2))
  11360. (delete-region (match-beginning 2) (match-end 2))
  11361. (and (looking-at "[ \t]+") (replace-match "")))
  11362. (beginning-of-line 2))
  11363. (while (< (setq l (1+ l)) l2)
  11364. (unless (org-at-item-p)
  11365. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  11366. (replace-match "\\1- \\2")))
  11367. (beginning-of-line 2))))))
  11368. (defun org-toggle-region-headings (beg end)
  11369. "Convert all lines in region to list items.
  11370. If the first line is already an item, convert all list items in the region
  11371. to normal lines."
  11372. (interactive "r")
  11373. (let (l2 l)
  11374. (save-excursion
  11375. (goto-char end)
  11376. (setq l2 (org-current-line))
  11377. (goto-char beg)
  11378. (beginning-of-line 1)
  11379. (setq l (1- (org-current-line)))
  11380. (if (org-on-heading-p)
  11381. ;; We already have headlines, de-star them
  11382. (while (< (setq l (1+ l)) l2)
  11383. (when (org-on-heading-p t)
  11384. (and (looking-at outline-regexp) (replace-match "")))
  11385. (beginning-of-line 2))
  11386. (let* ((stars (save-excursion
  11387. (re-search-backward org-complex-heading-regexp nil t)
  11388. (or (match-string 1) "*")))
  11389. (add-stars (if org-odd-levels-only "**" "*"))
  11390. (rpl (concat stars add-stars " \\2")))
  11391. (while (< (setq l (1+ l)) l2)
  11392. (unless (org-on-heading-p)
  11393. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  11394. (replace-match rpl)))
  11395. (beginning-of-line 2)))))))
  11396. (defun org-meta-return (&optional arg)
  11397. "Insert a new heading or wrap a region in a table.
  11398. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  11399. See the individual commands for more information."
  11400. (interactive "P")
  11401. (cond
  11402. ((org-at-table-p)
  11403. (call-interactively 'org-table-wrap-region))
  11404. (t (call-interactively 'org-insert-heading))))
  11405. ;;; Menu entries
  11406. ;; Define the Org-mode menus
  11407. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  11408. '("Tbl"
  11409. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  11410. ["Next Field" org-cycle (org-at-table-p)]
  11411. ["Previous Field" org-shifttab (org-at-table-p)]
  11412. ["Next Row" org-return (org-at-table-p)]
  11413. "--"
  11414. ["Blank Field" org-table-blank-field (org-at-table-p)]
  11415. ["Edit Field" org-table-edit-field (org-at-table-p)]
  11416. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  11417. "--"
  11418. ("Column"
  11419. ["Move Column Left" org-metaleft (org-at-table-p)]
  11420. ["Move Column Right" org-metaright (org-at-table-p)]
  11421. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  11422. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  11423. ("Row"
  11424. ["Move Row Up" org-metaup (org-at-table-p)]
  11425. ["Move Row Down" org-metadown (org-at-table-p)]
  11426. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  11427. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  11428. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  11429. "--"
  11430. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  11431. ("Rectangle"
  11432. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  11433. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  11434. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  11435. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  11436. "--"
  11437. ("Calculate"
  11438. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  11439. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  11440. ["Edit Formulas" org-table-edit-formulas (org-at-table-p)]
  11441. "--"
  11442. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  11443. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  11444. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  11445. "--"
  11446. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  11447. "--"
  11448. ["Sum Column/Rectangle" org-table-sum
  11449. (or (org-at-table-p) (org-region-active-p))]
  11450. ["Which Column?" org-table-current-column (org-at-table-p)])
  11451. ["Debug Formulas"
  11452. org-table-toggle-formula-debugger
  11453. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  11454. ["Show Col/Row Numbers"
  11455. org-table-toggle-coordinate-overlays
  11456. :style toggle
  11457. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  11458. "--"
  11459. ["Create" org-table-create (and (not (org-at-table-p))
  11460. org-enable-table-editor)]
  11461. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  11462. ["Import from File" org-table-import (not (org-at-table-p))]
  11463. ["Export to File" org-table-export (org-at-table-p)]
  11464. "--"
  11465. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  11466. (easy-menu-define org-org-menu org-mode-map "Org menu"
  11467. '("Org"
  11468. ("Show/Hide"
  11469. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  11470. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  11471. ["Sparse Tree..." org-sparse-tree t]
  11472. ["Reveal Context" org-reveal t]
  11473. ["Show All" show-all t]
  11474. "--"
  11475. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  11476. "--"
  11477. ["New Heading" org-insert-heading t]
  11478. ("Navigate Headings"
  11479. ["Up" outline-up-heading t]
  11480. ["Next" outline-next-visible-heading t]
  11481. ["Previous" outline-previous-visible-heading t]
  11482. ["Next Same Level" outline-forward-same-level t]
  11483. ["Previous Same Level" outline-backward-same-level t]
  11484. "--"
  11485. ["Jump" org-goto t])
  11486. ("Edit Structure"
  11487. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  11488. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  11489. "--"
  11490. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  11491. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  11492. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  11493. "--"
  11494. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  11495. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  11496. ["Demote Heading" org-metaright (not (org-at-table-p))]
  11497. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  11498. "--"
  11499. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  11500. "--"
  11501. ["Convert to odd levels" org-convert-to-odd-levels t]
  11502. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  11503. ("Editing"
  11504. ["Emphasis..." org-emphasize t])
  11505. ("Archive"
  11506. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  11507. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  11508. ; :active t :keys "C-u C-c C-x C-a"]
  11509. ["Sparse trees open ARCHIVE trees"
  11510. (setq org-sparse-tree-open-archived-trees
  11511. (not org-sparse-tree-open-archived-trees))
  11512. :style toggle :selected org-sparse-tree-open-archived-trees]
  11513. ["Cycling opens ARCHIVE trees"
  11514. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  11515. :style toggle :selected org-cycle-open-archived-trees]
  11516. ["Agenda includes ARCHIVE trees"
  11517. (setq org-agenda-skip-archived-trees (not org-agenda-skip-archived-trees))
  11518. :style toggle :selected (not org-agenda-skip-archived-trees)]
  11519. "--"
  11520. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  11521. ; ["Check and Move Children" (org-archive-subtree '(4))
  11522. ; :active t :keys "C-u C-c C-x C-s"]
  11523. )
  11524. "--"
  11525. ("TODO Lists"
  11526. ["TODO/DONE/-" org-todo t]
  11527. ("Select keyword"
  11528. ["Next keyword" org-shiftright (org-on-heading-p)]
  11529. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  11530. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  11531. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  11532. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  11533. ["Show TODO Tree" org-show-todo-tree t]
  11534. ["Global TODO list" org-todo-list t]
  11535. "--"
  11536. ["Set Priority" org-priority t]
  11537. ["Priority Up" org-shiftup t]
  11538. ["Priority Down" org-shiftdown t])
  11539. ("TAGS and Properties"
  11540. ["Set Tags" 'org-ctrl-c-ctrl-c (org-at-heading-p)]
  11541. ["Change tag in region" 'org-change-tag-in-region (org-region-active-p)]
  11542. "--"
  11543. ["Set property" 'org-set-property t]
  11544. ["Column view of properties" org-columns t]
  11545. ["Insert Column View DBlock" org-insert-columns-dblock t])
  11546. ("Dates and Scheduling"
  11547. ["Timestamp" org-time-stamp t]
  11548. ["Timestamp (inactive)" org-time-stamp-inactive t]
  11549. ("Change Date"
  11550. ["1 Day Later" org-shiftright t]
  11551. ["1 Day Earlier" org-shiftleft t]
  11552. ["1 ... Later" org-shiftup t]
  11553. ["1 ... Earlier" org-shiftdown t])
  11554. ["Compute Time Range" org-evaluate-time-range t]
  11555. ["Schedule Item" org-schedule t]
  11556. ["Deadline" org-deadline t]
  11557. "--"
  11558. ["Custom time format" org-toggle-time-stamp-overlays
  11559. :style radio :selected org-display-custom-times]
  11560. "--"
  11561. ["Goto Calendar" org-goto-calendar t]
  11562. ["Date from Calendar" org-date-from-calendar t])
  11563. ("Logging work"
  11564. ["Clock in" org-clock-in t]
  11565. ["Clock out" org-clock-out t]
  11566. ["Clock cancel" org-clock-cancel t]
  11567. ["Goto running clock" org-clock-goto t]
  11568. ["Display times" org-clock-display t]
  11569. ["Create clock table" org-clock-report t]
  11570. "--"
  11571. ["Record DONE time"
  11572. (progn (setq org-log-done (not org-log-done))
  11573. (message "Switching to %s will %s record a timestamp"
  11574. (car org-done-keywords)
  11575. (if org-log-done "automatically" "not")))
  11576. :style toggle :selected org-log-done])
  11577. "--"
  11578. ["Agenda Command..." org-agenda t]
  11579. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  11580. ("File List for Agenda")
  11581. ("Special views current file"
  11582. ["TODO Tree" org-show-todo-tree t]
  11583. ["Check Deadlines" org-check-deadlines t]
  11584. ["Timeline" org-timeline t]
  11585. ["Tags Tree" org-tags-sparse-tree t])
  11586. "--"
  11587. ("Hyperlinks"
  11588. ["Store Link (Global)" org-store-link t]
  11589. ["Insert Link" org-insert-link t]
  11590. ["Follow Link" org-open-at-point t]
  11591. "--"
  11592. ["Next link" org-next-link t]
  11593. ["Previous link" org-previous-link t]
  11594. "--"
  11595. ["Descriptive Links"
  11596. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  11597. :style radio
  11598. :selected (member '(org-link) buffer-invisibility-spec)]
  11599. ["Literal Links"
  11600. (progn
  11601. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  11602. :style radio
  11603. :selected (not (member '(org-link) buffer-invisibility-spec))])
  11604. "--"
  11605. ["Export/Publish..." org-export t]
  11606. ("LaTeX"
  11607. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  11608. :selected org-cdlatex-mode]
  11609. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  11610. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  11611. ["Modify math symbol" org-cdlatex-math-modify
  11612. (org-inside-LaTeX-fragment-p)]
  11613. ["Export LaTeX fragments as images"
  11614. (if (featurep 'org-exp)
  11615. (setq org-export-with-LaTeX-fragments
  11616. (not org-export-with-LaTeX-fragments))
  11617. (require 'org-exp))
  11618. :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
  11619. org-export-with-LaTeX-fragments)])
  11620. "--"
  11621. ("Documentation"
  11622. ["Show Version" org-version t]
  11623. ["Info Documentation" org-info t])
  11624. ("Customize"
  11625. ["Browse Org Group" org-customize t]
  11626. "--"
  11627. ["Expand This Menu" org-create-customize-menu
  11628. (fboundp 'customize-menu-create)])
  11629. "--"
  11630. ["Refresh setup" org-mode-restart t]
  11631. ))
  11632. (defun org-info (&optional node)
  11633. "Read documentation for Org-mode in the info system.
  11634. With optional NODE, go directly to that node."
  11635. (interactive)
  11636. (info (format "(org)%s" (or node ""))))
  11637. (defun org-install-agenda-files-menu ()
  11638. (let ((bl (buffer-list)))
  11639. (save-excursion
  11640. (while bl
  11641. (set-buffer (pop bl))
  11642. (if (org-mode-p) (setq bl nil)))
  11643. (when (org-mode-p)
  11644. (easy-menu-change
  11645. '("Org") "File List for Agenda"
  11646. (append
  11647. (list
  11648. ["Edit File List" (org-edit-agenda-file-list) t]
  11649. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  11650. ["Remove Current File from List" org-remove-file t]
  11651. ["Cycle through agenda files" org-cycle-agenda-files t]
  11652. ["Occur in all agenda files" org-occur-in-agenda-files t]
  11653. "--")
  11654. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  11655. ;;;; Documentation
  11656. (defun org-require-autoloaded-modules ()
  11657. (interactive)
  11658. (mapc 'require
  11659. '(org-agenda org-archive org-clock org-colview
  11660. org-exp org-export-latex org-publish
  11661. org-remember org-table)))
  11662. (defun org-customize ()
  11663. "Call the customize function with org as argument."
  11664. (interactive)
  11665. (org-load-modules-maybe)
  11666. (org-require-autoloaded-modules)
  11667. (customize-browse 'org))
  11668. (defun org-create-customize-menu ()
  11669. "Create a full customization menu for Org-mode, insert it into the menu."
  11670. (interactive)
  11671. (org-load-modules-maybe)
  11672. (org-require-autoloaded-modules)
  11673. (if (fboundp 'customize-menu-create)
  11674. (progn
  11675. (easy-menu-change
  11676. '("Org") "Customize"
  11677. `(["Browse Org group" org-customize t]
  11678. "--"
  11679. ,(customize-menu-create 'org)
  11680. ["Set" Custom-set t]
  11681. ["Save" Custom-save t]
  11682. ["Reset to Current" Custom-reset-current t]
  11683. ["Reset to Saved" Custom-reset-saved t]
  11684. ["Reset to Standard Settings" Custom-reset-standard t]))
  11685. (message "\"Org\"-menu now contains full customization menu"))
  11686. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  11687. ;;;; Miscellaneous stuff
  11688. ;;; Generally useful functions
  11689. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  11690. "Display the given MESSAGE as a warning."
  11691. (if (fboundp 'display-warning)
  11692. (display-warning 'org message
  11693. (if (featurep 'xemacs)
  11694. 'warning
  11695. :warning))
  11696. (let ((buf (get-buffer-create "*Org warnings*")))
  11697. (with-current-buffer buf
  11698. (goto-char (point-max))
  11699. (insert "Warning (Org): " message)
  11700. (unless (bolp)
  11701. (newline)))
  11702. (display-buffer buf)
  11703. (sit-for 0))))
  11704. (defun org-plist-delete (plist property)
  11705. "Delete PROPERTY from PLIST.
  11706. This is in contrast to merely setting it to 0."
  11707. (let (p)
  11708. (while plist
  11709. (if (not (eq property (car plist)))
  11710. (setq p (plist-put p (car plist) (nth 1 plist))))
  11711. (setq plist (cddr plist)))
  11712. p))
  11713. (defun org-force-self-insert (N)
  11714. "Needed to enforce self-insert under remapping."
  11715. (interactive "p")
  11716. (self-insert-command N))
  11717. (defun org-string-width (s)
  11718. "Compute width of string, ignoring invisible characters.
  11719. This ignores character with invisibility property `org-link', and also
  11720. characters with property `org-cwidth', because these will become invisible
  11721. upon the next fontification round."
  11722. (let (b l)
  11723. (when (or (eq t buffer-invisibility-spec)
  11724. (assq 'org-link buffer-invisibility-spec))
  11725. (while (setq b (text-property-any 0 (length s)
  11726. 'invisible 'org-link s))
  11727. (setq s (concat (substring s 0 b)
  11728. (substring s (or (next-single-property-change
  11729. b 'invisible s) (length s)))))))
  11730. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  11731. (setq s (concat (substring s 0 b)
  11732. (substring s (or (next-single-property-change
  11733. b 'org-cwidth s) (length s))))))
  11734. (setq l (string-width s) b -1)
  11735. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  11736. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  11737. l))
  11738. (defun org-base-buffer (buffer)
  11739. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  11740. (if (not buffer)
  11741. buffer
  11742. (or (buffer-base-buffer buffer)
  11743. buffer)))
  11744. (defun org-trim (s)
  11745. "Remove whitespace at beginning and end of string."
  11746. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  11747. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  11748. s)
  11749. (defun org-wrap (string &optional width lines)
  11750. "Wrap string to either a number of lines, or a width in characters.
  11751. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  11752. that costs. If there is a word longer than WIDTH, the text is actually
  11753. wrapped to the length of that word.
  11754. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  11755. many lines, whatever width that takes.
  11756. The return value is a list of lines, without newlines at the end."
  11757. (let* ((words (org-split-string string "[ \t\n]+"))
  11758. (maxword (apply 'max (mapcar 'org-string-width words)))
  11759. w ll)
  11760. (cond (width
  11761. (org-do-wrap words (max maxword width)))
  11762. (lines
  11763. (setq w maxword)
  11764. (setq ll (org-do-wrap words maxword))
  11765. (if (<= (length ll) lines)
  11766. ll
  11767. (setq ll words)
  11768. (while (> (length ll) lines)
  11769. (setq w (1+ w))
  11770. (setq ll (org-do-wrap words w)))
  11771. ll))
  11772. (t (error "Cannot wrap this")))))
  11773. (defun org-do-wrap (words width)
  11774. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  11775. (let (lines line)
  11776. (while words
  11777. (setq line (pop words))
  11778. (while (and words (< (+ (length line) (length (car words))) width))
  11779. (setq line (concat line " " (pop words))))
  11780. (setq lines (push line lines)))
  11781. (nreverse lines)))
  11782. (defun org-split-string (string &optional separators)
  11783. "Splits STRING into substrings at SEPARATORS.
  11784. No empty strings are returned if there are matches at the beginning
  11785. and end of string."
  11786. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  11787. (start 0)
  11788. notfirst
  11789. (list nil))
  11790. (while (and (string-match rexp string
  11791. (if (and notfirst
  11792. (= start (match-beginning 0))
  11793. (< start (length string)))
  11794. (1+ start) start))
  11795. (< (match-beginning 0) (length string)))
  11796. (setq notfirst t)
  11797. (or (eq (match-beginning 0) 0)
  11798. (and (eq (match-beginning 0) (match-end 0))
  11799. (eq (match-beginning 0) start))
  11800. (setq list
  11801. (cons (substring string start (match-beginning 0))
  11802. list)))
  11803. (setq start (match-end 0)))
  11804. (or (eq start (length string))
  11805. (setq list
  11806. (cons (substring string start)
  11807. list)))
  11808. (nreverse list)))
  11809. (defun org-context ()
  11810. "Return a list of contexts of the current cursor position.
  11811. If several contexts apply, all are returned.
  11812. Each context entry is a list with a symbol naming the context, and
  11813. two positions indicating start and end of the context. Possible
  11814. contexts are:
  11815. :headline anywhere in a headline
  11816. :headline-stars on the leading stars in a headline
  11817. :todo-keyword on a TODO keyword (including DONE) in a headline
  11818. :tags on the TAGS in a headline
  11819. :priority on the priority cookie in a headline
  11820. :item on the first line of a plain list item
  11821. :item-bullet on the bullet/number of a plain list item
  11822. :checkbox on the checkbox in a plain list item
  11823. :table in an org-mode table
  11824. :table-special on a special filed in a table
  11825. :table-table in a table.el table
  11826. :link on a hyperlink
  11827. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  11828. :target on a <<target>>
  11829. :radio-target on a <<<radio-target>>>
  11830. :latex-fragment on a LaTeX fragment
  11831. :latex-preview on a LaTeX fragment with overlayed preview image
  11832. This function expects the position to be visible because it uses font-lock
  11833. faces as a help to recognize the following contexts: :table-special, :link,
  11834. and :keyword."
  11835. (let* ((f (get-text-property (point) 'face))
  11836. (faces (if (listp f) f (list f)))
  11837. (p (point)) clist o)
  11838. ;; First the large context
  11839. (cond
  11840. ((org-on-heading-p t)
  11841. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  11842. (when (progn
  11843. (beginning-of-line 1)
  11844. (looking-at org-todo-line-tags-regexp))
  11845. (push (org-point-in-group p 1 :headline-stars) clist)
  11846. (push (org-point-in-group p 2 :todo-keyword) clist)
  11847. (push (org-point-in-group p 4 :tags) clist))
  11848. (goto-char p)
  11849. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  11850. (if (looking-at "\\[#[A-Z0-9]\\]")
  11851. (push (org-point-in-group p 0 :priority) clist)))
  11852. ((org-at-item-p)
  11853. (push (org-point-in-group p 2 :item-bullet) clist)
  11854. (push (list :item (point-at-bol)
  11855. (save-excursion (org-end-of-item) (point)))
  11856. clist)
  11857. (and (org-at-item-checkbox-p)
  11858. (push (org-point-in-group p 0 :checkbox) clist)))
  11859. ((org-at-table-p)
  11860. (push (list :table (org-table-begin) (org-table-end)) clist)
  11861. (if (memq 'org-formula faces)
  11862. (push (list :table-special
  11863. (previous-single-property-change p 'face)
  11864. (next-single-property-change p 'face)) clist)))
  11865. ((org-at-table-p 'any)
  11866. (push (list :table-table) clist)))
  11867. (goto-char p)
  11868. ;; Now the small context
  11869. (cond
  11870. ((org-at-timestamp-p)
  11871. (push (org-point-in-group p 0 :timestamp) clist))
  11872. ((memq 'org-link faces)
  11873. (push (list :link
  11874. (previous-single-property-change p 'face)
  11875. (next-single-property-change p 'face)) clist))
  11876. ((memq 'org-special-keyword faces)
  11877. (push (list :keyword
  11878. (previous-single-property-change p 'face)
  11879. (next-single-property-change p 'face)) clist))
  11880. ((org-on-target-p)
  11881. (push (org-point-in-group p 0 :target) clist)
  11882. (goto-char (1- (match-beginning 0)))
  11883. (if (looking-at org-radio-target-regexp)
  11884. (push (org-point-in-group p 0 :radio-target) clist))
  11885. (goto-char p))
  11886. ((setq o (car (delq nil
  11887. (mapcar
  11888. (lambda (x)
  11889. (if (memq x org-latex-fragment-image-overlays) x))
  11890. (org-overlays-at (point))))))
  11891. (push (list :latex-fragment
  11892. (org-overlay-start o) (org-overlay-end o)) clist)
  11893. (push (list :latex-preview
  11894. (org-overlay-start o) (org-overlay-end o)) clist))
  11895. ((org-inside-LaTeX-fragment-p)
  11896. ;; FIXME: positions wrong.
  11897. (push (list :latex-fragment (point) (point)) clist)))
  11898. (setq clist (nreverse (delq nil clist)))
  11899. clist))
  11900. ;; FIXME: Compare with at-regexp-p Do we need both?
  11901. (defun org-in-regexp (re &optional nlines visually)
  11902. "Check if point is inside a match of regexp.
  11903. Normally only the current line is checked, but you can include NLINES extra
  11904. lines both before and after point into the search.
  11905. If VISUALLY is set, require that the cursor is not after the match but
  11906. really on, so that the block visually is on the match."
  11907. (catch 'exit
  11908. (let ((pos (point))
  11909. (eol (point-at-eol (+ 1 (or nlines 0))))
  11910. (inc (if visually 1 0)))
  11911. (save-excursion
  11912. (beginning-of-line (- 1 (or nlines 0)))
  11913. (while (re-search-forward re eol t)
  11914. (if (and (<= (match-beginning 0) pos)
  11915. (>= (+ inc (match-end 0)) pos))
  11916. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  11917. (defun org-at-regexp-p (regexp)
  11918. "Is point inside a match of REGEXP in the current line?"
  11919. (catch 'exit
  11920. (save-excursion
  11921. (let ((pos (point)) (end (point-at-eol)))
  11922. (beginning-of-line 1)
  11923. (while (re-search-forward regexp end t)
  11924. (if (and (<= (match-beginning 0) pos)
  11925. (>= (match-end 0) pos))
  11926. (throw 'exit t)))
  11927. nil))))
  11928. (defun org-occur-in-agenda-files (regexp &optional nlines)
  11929. "Call `multi-occur' with buffers for all agenda files."
  11930. (interactive "sOrg-files matching: \np")
  11931. (let* ((files (org-agenda-files))
  11932. (tnames (mapcar 'file-truename files))
  11933. (extra org-agenda-text-search-extra-files)
  11934. f)
  11935. (when (eq (car extra) 'agenda-archives)
  11936. (setq extra (cdr extra))
  11937. (setq files (org-add-archive-files files)))
  11938. (while (setq f (pop extra))
  11939. (unless (member (file-truename f) tnames)
  11940. (add-to-list 'files f 'append)
  11941. (add-to-list 'tnames (file-truename f) 'append)))
  11942. (multi-occur
  11943. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  11944. regexp)))
  11945. (if (boundp 'occur-mode-find-occurrence-hook)
  11946. ;; Emacs 23
  11947. (add-hook 'occur-mode-find-occurrence-hook
  11948. (lambda ()
  11949. (when (org-mode-p)
  11950. (org-reveal))))
  11951. ;; Emacs 22
  11952. (defadvice occur-mode-goto-occurrence
  11953. (after org-occur-reveal activate)
  11954. (and (org-mode-p) (org-reveal)))
  11955. (defadvice occur-mode-goto-occurrence-other-window
  11956. (after org-occur-reveal activate)
  11957. (and (org-mode-p) (org-reveal)))
  11958. (defadvice occur-mode-display-occurrence
  11959. (after org-occur-reveal activate)
  11960. (when (org-mode-p)
  11961. (let ((pos (occur-mode-find-occurrence)))
  11962. (with-current-buffer (marker-buffer pos)
  11963. (save-excursion
  11964. (goto-char pos)
  11965. (org-reveal)))))))
  11966. (defun org-uniquify (list)
  11967. "Remove duplicate elements from LIST."
  11968. (let (res)
  11969. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  11970. res))
  11971. (defun org-delete-all (elts list)
  11972. "Remove all elements in ELTS from LIST."
  11973. (while elts
  11974. (setq list (delete (pop elts) list)))
  11975. list)
  11976. (defun org-back-over-empty-lines ()
  11977. "Move backwards over witespace, to the beginning of the first empty line.
  11978. Returns the number of empty lines passed."
  11979. (let ((pos (point)))
  11980. (skip-chars-backward " \t\n\r")
  11981. (beginning-of-line 2)
  11982. (goto-char (min (point) pos))
  11983. (count-lines (point) pos)))
  11984. (defun org-skip-whitespace ()
  11985. (skip-chars-forward " \t\n\r"))
  11986. (defun org-point-in-group (point group &optional context)
  11987. "Check if POINT is in match-group GROUP.
  11988. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  11989. match. If the match group does ot exist or point is not inside it,
  11990. return nil."
  11991. (and (match-beginning group)
  11992. (>= point (match-beginning group))
  11993. (<= point (match-end group))
  11994. (if context
  11995. (list context (match-beginning group) (match-end group))
  11996. t)))
  11997. (defun org-switch-to-buffer-other-window (&rest args)
  11998. "Switch to buffer in a second window on the current frame.
  11999. In particular, do not allow pop-up frames."
  12000. (let (pop-up-frames special-display-buffer-names special-display-regexps
  12001. special-display-function)
  12002. (apply 'switch-to-buffer-other-window args)))
  12003. (defun org-combine-plists (&rest plists)
  12004. "Create a single property list from all plists in PLISTS.
  12005. The process starts by copying the first list, and then setting properties
  12006. from the other lists. Settings in the last list are the most significant
  12007. ones and overrule settings in the other lists."
  12008. (let ((rtn (copy-sequence (pop plists)))
  12009. p v ls)
  12010. (while plists
  12011. (setq ls (pop plists))
  12012. (while ls
  12013. (setq p (pop ls) v (pop ls))
  12014. (setq rtn (plist-put rtn p v))))
  12015. rtn))
  12016. (defun org-move-line-down (arg)
  12017. "Move the current line down. With prefix argument, move it past ARG lines."
  12018. (interactive "p")
  12019. (let ((col (current-column))
  12020. beg end pos)
  12021. (beginning-of-line 1) (setq beg (point))
  12022. (beginning-of-line 2) (setq end (point))
  12023. (beginning-of-line (+ 1 arg))
  12024. (setq pos (move-marker (make-marker) (point)))
  12025. (insert (delete-and-extract-region beg end))
  12026. (goto-char pos)
  12027. (org-move-to-column col)))
  12028. (defun org-move-line-up (arg)
  12029. "Move the current line up. With prefix argument, move it past ARG lines."
  12030. (interactive "p")
  12031. (let ((col (current-column))
  12032. beg end pos)
  12033. (beginning-of-line 1) (setq beg (point))
  12034. (beginning-of-line 2) (setq end (point))
  12035. (beginning-of-line (- arg))
  12036. (setq pos (move-marker (make-marker) (point)))
  12037. (insert (delete-and-extract-region beg end))
  12038. (goto-char pos)
  12039. (org-move-to-column col)))
  12040. (defun org-replace-escapes (string table)
  12041. "Replace %-escapes in STRING with values in TABLE.
  12042. TABLE is an association list with keys like \"%a\" and string values.
  12043. The sequences in STRING may contain normal field width and padding information,
  12044. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  12045. so values can contain further %-escapes if they are define later in TABLE."
  12046. (let ((case-fold-search nil)
  12047. e re rpl)
  12048. (while (setq e (pop table))
  12049. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  12050. (while (string-match re string)
  12051. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  12052. (cdr e)))
  12053. (setq string (replace-match rpl t t string))))
  12054. string))
  12055. (defun org-sublist (list start end)
  12056. "Return a section of LIST, from START to END.
  12057. Counting starts at 1."
  12058. (let (rtn (c start))
  12059. (setq list (nthcdr (1- start) list))
  12060. (while (and list (<= c end))
  12061. (push (pop list) rtn)
  12062. (setq c (1+ c)))
  12063. (nreverse rtn)))
  12064. (defun org-find-base-buffer-visiting (file)
  12065. "Like `find-buffer-visiting' but alway return the base buffer and
  12066. not an indirect buffer."
  12067. (let ((buf (find-buffer-visiting file)))
  12068. (if buf
  12069. (or (buffer-base-buffer buf) buf)
  12070. nil)))
  12071. (defun org-image-file-name-regexp ()
  12072. "Return regexp matching the file names of images."
  12073. (if (fboundp 'image-file-name-regexp)
  12074. (image-file-name-regexp)
  12075. (let ((image-file-name-extensions
  12076. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  12077. "xbm" "xpm" "pbm" "pgm" "ppm")))
  12078. (concat "\\."
  12079. (regexp-opt (nconc (mapcar 'upcase
  12080. image-file-name-extensions)
  12081. image-file-name-extensions)
  12082. t)
  12083. "\\'"))))
  12084. (defun org-file-image-p (file)
  12085. "Return non-nil if FILE is an image."
  12086. (save-match-data
  12087. (string-match (org-image-file-name-regexp) file)))
  12088. ;;; Paragraph filling stuff.
  12089. ;; We want this to be just right, so use the full arsenal.
  12090. (defun org-indent-line-function ()
  12091. "Indent line like previous, but further if previous was headline or item."
  12092. (interactive)
  12093. (let* ((pos (point))
  12094. (itemp (org-at-item-p))
  12095. column bpos bcol tpos tcol bullet btype bullet-type)
  12096. ;; Find the previous relevant line
  12097. (beginning-of-line 1)
  12098. (cond
  12099. ((looking-at "#") (setq column 0))
  12100. ((looking-at "\\*+ ") (setq column 0))
  12101. (t
  12102. (beginning-of-line 0)
  12103. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]"))
  12104. (beginning-of-line 0))
  12105. (cond
  12106. ((looking-at "\\*+[ \t]+")
  12107. (goto-char (match-end 0))
  12108. (setq column (current-column)))
  12109. ((org-in-item-p)
  12110. (org-beginning-of-item)
  12111. ; (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  12112. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\)?")
  12113. (setq bpos (match-beginning 1) tpos (match-end 0)
  12114. bcol (progn (goto-char bpos) (current-column))
  12115. tcol (progn (goto-char tpos) (current-column))
  12116. bullet (match-string 1)
  12117. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  12118. (if (not itemp)
  12119. (setq column tcol)
  12120. (goto-char pos)
  12121. (beginning-of-line 1)
  12122. (if (looking-at "\\S-")
  12123. (progn
  12124. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  12125. (setq bullet (match-string 1)
  12126. btype (if (string-match "[0-9]" bullet) "n" bullet))
  12127. (setq column (if (equal btype bullet-type) bcol tcol)))
  12128. (setq column (org-get-indentation)))))
  12129. (t (setq column (org-get-indentation))))))
  12130. (goto-char pos)
  12131. (if (<= (current-column) (current-indentation))
  12132. (org-indent-line-to column)
  12133. (save-excursion (org-indent-line-to column)))
  12134. (setq column (current-column))
  12135. (beginning-of-line 1)
  12136. (if (looking-at
  12137. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  12138. (replace-match (concat "\\1" (format org-property-format
  12139. (match-string 2) (match-string 3)))
  12140. t nil))
  12141. (org-move-to-column column)))
  12142. (defun org-set-autofill-regexps ()
  12143. (interactive)
  12144. ;; In the paragraph separator we include headlines, because filling
  12145. ;; text in a line directly attached to a headline would otherwise
  12146. ;; fill the headline as well.
  12147. (org-set-local 'comment-start-skip "^#+[ \t]*")
  12148. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  12149. ;; The paragraph starter includes hand-formatted lists.
  12150. (org-set-local 'paragraph-start
  12151. "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  12152. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  12153. ;; But only if the user has not turned off tables or fixed-width regions
  12154. (org-set-local
  12155. 'auto-fill-inhibit-regexp
  12156. (concat "\\*+ \\|#\\+"
  12157. "\\|[ \t]*" org-keyword-time-regexp
  12158. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  12159. (concat
  12160. "\\|[ \t]*["
  12161. (if org-enable-table-editor "|" "")
  12162. (if org-enable-fixed-width-editor ":" "")
  12163. "]"))))
  12164. ;; We use our own fill-paragraph function, to make sure that tables
  12165. ;; and fixed-width regions are not wrapped. That function will pass
  12166. ;; through to `fill-paragraph' when appropriate.
  12167. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  12168. ; Adaptive filling: To get full control, first make sure that
  12169. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  12170. (org-set-local 'adaptive-fill-regexp "\000")
  12171. (org-set-local 'adaptive-fill-function
  12172. 'org-adaptive-fill-function)
  12173. (org-set-local
  12174. 'align-mode-rules-list
  12175. '((org-in-buffer-settings
  12176. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  12177. (modes . '(org-mode))))))
  12178. (defun org-fill-paragraph (&optional justify)
  12179. "Re-align a table, pass through to fill-paragraph if no table."
  12180. (let ((table-p (org-at-table-p))
  12181. (table.el-p (org-at-table.el-p)))
  12182. (cond ((and (equal (char-after (point-at-bol)) ?*)
  12183. (save-excursion (goto-char (point-at-bol))
  12184. (looking-at outline-regexp)))
  12185. t) ; skip headlines
  12186. (table.el-p t) ; skip table.el tables
  12187. (table-p (org-table-align) t) ; align org-mode tables
  12188. (t nil)))) ; call paragraph-fill
  12189. ;; For reference, this is the default value of adaptive-fill-regexp
  12190. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  12191. (defun org-adaptive-fill-function ()
  12192. "Return a fill prefix for org-mode files.
  12193. In particular, this makes sure hanging paragraphs for hand-formatted lists
  12194. work correctly."
  12195. (cond ((looking-at "#[ \t]+")
  12196. (match-string 0))
  12197. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] \\)?")
  12198. (save-excursion
  12199. (goto-char (match-end 0))
  12200. (make-string (current-column) ?\ )))
  12201. (t nil)))
  12202. ;;; Other stuff.
  12203. (defun org-toggle-fixed-width-section (arg)
  12204. "Toggle the fixed-width export.
  12205. If there is no active region, the QUOTE keyword at the current headline is
  12206. inserted or removed. When present, it causes the text between this headline
  12207. and the next to be exported as fixed-width text, and unmodified.
  12208. If there is an active region, this command adds or removes a colon as the
  12209. first character of this line. If the first character of a line is a colon,
  12210. this line is also exported in fixed-width font."
  12211. (interactive "P")
  12212. (let* ((cc 0)
  12213. (regionp (org-region-active-p))
  12214. (beg (if regionp (region-beginning) (point)))
  12215. (end (if regionp (region-end)))
  12216. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  12217. (case-fold-search nil)
  12218. (re "[ \t]*\\(:\\)")
  12219. off)
  12220. (if regionp
  12221. (save-excursion
  12222. (goto-char beg)
  12223. (setq cc (current-column))
  12224. (beginning-of-line 1)
  12225. (setq off (looking-at re))
  12226. (while (> nlines 0)
  12227. (setq nlines (1- nlines))
  12228. (beginning-of-line 1)
  12229. (cond
  12230. (arg
  12231. (org-move-to-column cc t)
  12232. (insert ":\n")
  12233. (forward-line -1))
  12234. ((and off (looking-at re))
  12235. (replace-match "" t t nil 1))
  12236. ((not off) (org-move-to-column cc t) (insert ":")))
  12237. (forward-line 1)))
  12238. (save-excursion
  12239. (org-back-to-heading)
  12240. (if (looking-at (concat outline-regexp
  12241. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  12242. (replace-match "" t t nil 1)
  12243. (if (looking-at outline-regexp)
  12244. (progn
  12245. (goto-char (match-end 0))
  12246. (insert org-quote-string " "))))))))
  12247. ;;;; Functions extending outline functionality
  12248. (defun org-beginning-of-line (&optional arg)
  12249. "Go to the beginning of the current line. If that is invisible, continue
  12250. to a visible line beginning. This makes the function of C-a more intuitive.
  12251. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  12252. first attempt, and only move to after the tags when the cursor is already
  12253. beyond the end of the headline."
  12254. (interactive "P")
  12255. (let ((pos (point)))
  12256. (beginning-of-line 1)
  12257. (if (bobp)
  12258. nil
  12259. (backward-char 1)
  12260. (if (org-invisible-p)
  12261. (while (and (not (bobp)) (org-invisible-p))
  12262. (backward-char 1)
  12263. (beginning-of-line 1))
  12264. (forward-char 1)))
  12265. (when org-special-ctrl-a/e
  12266. (cond
  12267. ((and (looking-at org-todo-line-regexp)
  12268. (= (char-after (match-end 1)) ?\ ))
  12269. (goto-char
  12270. (if (eq org-special-ctrl-a/e t)
  12271. (cond ((> pos (match-beginning 3)) (match-beginning 3))
  12272. ((= pos (point)) (match-beginning 3))
  12273. (t (point)))
  12274. (cond ((> pos (point)) (point))
  12275. ((not (eq last-command this-command)) (point))
  12276. (t (match-beginning 3))))))
  12277. ((org-at-item-p)
  12278. (goto-char
  12279. (if (eq org-special-ctrl-a/e t)
  12280. (cond ((> pos (match-end 4)) (match-end 4))
  12281. ((= pos (point)) (match-end 4))
  12282. (t (point)))
  12283. (cond ((> pos (point)) (point))
  12284. ((not (eq last-command this-command)) (point))
  12285. (t (match-end 4))))))))))
  12286. (defun org-end-of-line (&optional arg)
  12287. "Go to the end of the line.
  12288. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  12289. first attempt, and only move to after the tags when the cursor is already
  12290. beyond the end of the headline."
  12291. (interactive "P")
  12292. (if (or (not org-special-ctrl-a/e)
  12293. (not (org-on-heading-p)))
  12294. (end-of-line arg)
  12295. (let ((pos (point)))
  12296. (beginning-of-line 1)
  12297. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  12298. (if (eq org-special-ctrl-a/e t)
  12299. (if (or (< pos (match-beginning 1))
  12300. (= pos (match-end 0)))
  12301. (goto-char (match-beginning 1))
  12302. (goto-char (match-end 0)))
  12303. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  12304. (goto-char (match-end 0))
  12305. (goto-char (match-beginning 1))))
  12306. (end-of-line arg)))))
  12307. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  12308. (define-key org-mode-map "\C-e" 'org-end-of-line)
  12309. (defun org-kill-line (&optional arg)
  12310. "Kill line, to tags or end of line."
  12311. (interactive "P")
  12312. (cond
  12313. ((or (not org-special-ctrl-k)
  12314. (bolp)
  12315. (not (org-on-heading-p)))
  12316. (call-interactively 'kill-line))
  12317. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  12318. (kill-region (point) (match-beginning 1))
  12319. (org-set-tags nil t))
  12320. (t (kill-region (point) (point-at-eol)))))
  12321. (define-key org-mode-map "\C-k" 'org-kill-line)
  12322. (defun org-invisible-p ()
  12323. "Check if point is at a character currently not visible."
  12324. ;; Early versions of noutline don't have `outline-invisible-p'.
  12325. (if (fboundp 'outline-invisible-p)
  12326. (outline-invisible-p)
  12327. (get-char-property (point) 'invisible)))
  12328. (defun org-invisible-p2 ()
  12329. "Check if point is at a character currently not visible."
  12330. (save-excursion
  12331. (if (and (eolp) (not (bobp))) (backward-char 1))
  12332. ;; Early versions of noutline don't have `outline-invisible-p'.
  12333. (if (fboundp 'outline-invisible-p)
  12334. (outline-invisible-p)
  12335. (get-char-property (point) 'invisible))))
  12336. (defalias 'org-back-to-heading 'outline-back-to-heading)
  12337. (defalias 'org-on-heading-p 'outline-on-heading-p)
  12338. (defalias 'org-at-heading-p 'outline-on-heading-p)
  12339. (defun org-at-heading-or-item-p ()
  12340. (or (org-on-heading-p) (org-at-item-p)))
  12341. (defun org-on-target-p ()
  12342. (or (org-in-regexp org-radio-target-regexp)
  12343. (org-in-regexp org-target-regexp)))
  12344. (defun org-up-heading-all (arg)
  12345. "Move to the heading line of which the present line is a subheading.
  12346. This function considers both visible and invisible heading lines.
  12347. With argument, move up ARG levels."
  12348. (if (fboundp 'outline-up-heading-all)
  12349. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  12350. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  12351. (defun org-up-heading-safe ()
  12352. "Move to the heading line of which the present line is a subheading.
  12353. This version will not throw an error. It will return the level of the
  12354. headline found, or nil if no higher level is found."
  12355. (let ((pos (point)) start-level level
  12356. (re (concat "^" outline-regexp)))
  12357. (catch 'exit
  12358. (outline-back-to-heading t)
  12359. (setq start-level (funcall outline-level))
  12360. (if (equal start-level 1) (throw 'exit nil))
  12361. (while (re-search-backward re nil t)
  12362. (setq level (funcall outline-level))
  12363. (if (< level start-level) (throw 'exit level)))
  12364. nil)))
  12365. (defun org-first-sibling-p ()
  12366. "Is this heading the first child of its parents?"
  12367. (interactive)
  12368. (let ((re (concat "^" outline-regexp))
  12369. level l)
  12370. (unless (org-at-heading-p t)
  12371. (error "Not at a heading"))
  12372. (setq level (funcall outline-level))
  12373. (save-excursion
  12374. (if (not (re-search-backward re nil t))
  12375. t
  12376. (setq l (funcall outline-level))
  12377. (< l level)))))
  12378. (defun org-goto-sibling (&optional previous)
  12379. "Goto the next sibling, even if it is invisible.
  12380. When PREVIOUS is set, go to the previous sibling instead. Returns t
  12381. when a sibling was found. When none is found, return nil and don't
  12382. move point."
  12383. (let ((fun (if previous 're-search-backward 're-search-forward))
  12384. (pos (point))
  12385. (re (concat "^" outline-regexp))
  12386. level l)
  12387. (when (condition-case nil (org-back-to-heading t) (error nil))
  12388. (setq level (funcall outline-level))
  12389. (catch 'exit
  12390. (or previous (forward-char 1))
  12391. (while (funcall fun re nil t)
  12392. (setq l (funcall outline-level))
  12393. (when (< l level) (goto-char pos) (throw 'exit nil))
  12394. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  12395. (goto-char pos)
  12396. nil))))
  12397. (defun org-show-siblings ()
  12398. "Show all siblings of the current headline."
  12399. (save-excursion
  12400. (while (org-goto-sibling) (org-flag-heading nil)))
  12401. (save-excursion
  12402. (while (org-goto-sibling 'previous)
  12403. (org-flag-heading nil))))
  12404. (defun org-show-hidden-entry ()
  12405. "Show an entry where even the heading is hidden."
  12406. (save-excursion
  12407. (org-show-entry)))
  12408. (defun org-flag-heading (flag &optional entry)
  12409. "Flag the current heading. FLAG non-nil means make invisible.
  12410. When ENTRY is non-nil, show the entire entry."
  12411. (save-excursion
  12412. (org-back-to-heading t)
  12413. ;; Check if we should show the entire entry
  12414. (if entry
  12415. (progn
  12416. (org-show-entry)
  12417. (save-excursion
  12418. (and (outline-next-heading)
  12419. (org-flag-heading nil))))
  12420. (outline-flag-region (max (point-min) (1- (point)))
  12421. (save-excursion (outline-end-of-heading) (point))
  12422. flag))))
  12423. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  12424. ;; This is an exact copy of the original function, but it uses
  12425. ;; `org-back-to-heading', to make it work also in invisible
  12426. ;; trees. And is uses an invisible-OK argument.
  12427. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  12428. (org-back-to-heading invisible-OK)
  12429. (let ((first t)
  12430. (level (funcall outline-level)))
  12431. (while (and (not (eobp))
  12432. (or first (> (funcall outline-level) level)))
  12433. (setq first nil)
  12434. (outline-next-heading))
  12435. (unless to-heading
  12436. (if (memq (preceding-char) '(?\n ?\^M))
  12437. (progn
  12438. ;; Go to end of line before heading
  12439. (forward-char -1)
  12440. (if (memq (preceding-char) '(?\n ?\^M))
  12441. ;; leave blank line before heading
  12442. (forward-char -1))))))
  12443. (point))
  12444. (defun org-show-subtree ()
  12445. "Show everything after this heading at deeper levels."
  12446. (outline-flag-region
  12447. (point)
  12448. (save-excursion
  12449. (outline-end-of-subtree) (outline-next-heading) (point))
  12450. nil))
  12451. (defun org-show-entry ()
  12452. "Show the body directly following this heading.
  12453. Show the heading too, if it is currently invisible."
  12454. (interactive)
  12455. (save-excursion
  12456. (condition-case nil
  12457. (progn
  12458. (org-back-to-heading t)
  12459. (outline-flag-region
  12460. (max (point-min) (1- (point)))
  12461. (save-excursion
  12462. (re-search-forward
  12463. (concat "[\r\n]\\(" outline-regexp "\\)") nil 'move)
  12464. (or (match-beginning 1) (point-max)))
  12465. nil))
  12466. (error nil))))
  12467. (defun org-make-options-regexp (kwds)
  12468. "Make a regular expression for keyword lines."
  12469. (concat
  12470. "^"
  12471. "#?[ \t]*\\+\\("
  12472. (mapconcat 'regexp-quote kwds "\\|")
  12473. "\\):[ \t]*"
  12474. "\\(.+\\)"))
  12475. ;; Make isearch reveal the necessary context
  12476. (defun org-isearch-end ()
  12477. "Reveal context after isearch exits."
  12478. (when isearch-success ; only if search was successful
  12479. (if (featurep 'xemacs)
  12480. ;; Under XEmacs, the hook is run in the correct place,
  12481. ;; we directly show the context.
  12482. (org-show-context 'isearch)
  12483. ;; In Emacs the hook runs *before* restoring the overlays.
  12484. ;; So we have to use a one-time post-command-hook to do this.
  12485. ;; (Emacs 22 has a special variable, see function `org-mode')
  12486. (unless (and (boundp 'isearch-mode-end-hook-quit)
  12487. isearch-mode-end-hook-quit)
  12488. ;; Only when the isearch was not quitted.
  12489. (org-add-hook 'post-command-hook 'org-isearch-post-command
  12490. 'append 'local)))))
  12491. (defun org-isearch-post-command ()
  12492. "Remove self from hook, and show context."
  12493. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  12494. (org-show-context 'isearch))
  12495. ;;;; Integration with and fixes for other packages
  12496. ;;; Imenu support
  12497. (defvar org-imenu-markers nil
  12498. "All markers currently used by Imenu.")
  12499. (make-variable-buffer-local 'org-imenu-markers)
  12500. (defun org-imenu-new-marker (&optional pos)
  12501. "Return a new marker for use by Imenu, and remember the marker."
  12502. (let ((m (make-marker)))
  12503. (move-marker m (or pos (point)))
  12504. (push m org-imenu-markers)
  12505. m))
  12506. (defun org-imenu-get-tree ()
  12507. "Produce the index for Imenu."
  12508. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  12509. (setq org-imenu-markers nil)
  12510. (let* ((n org-imenu-depth)
  12511. (re (concat "^" outline-regexp))
  12512. (subs (make-vector (1+ n) nil))
  12513. (last-level 0)
  12514. m tree level head)
  12515. (save-excursion
  12516. (save-restriction
  12517. (widen)
  12518. (goto-char (point-max))
  12519. (while (re-search-backward re nil t)
  12520. (setq level (org-reduced-level (funcall outline-level)))
  12521. (when (<= level n)
  12522. (looking-at org-complex-heading-regexp)
  12523. (setq head (org-match-string-no-properties 4)
  12524. m (org-imenu-new-marker))
  12525. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  12526. (if (>= level last-level)
  12527. (push (cons head m) (aref subs level))
  12528. (push (cons head (aref subs (1+ level))) (aref subs level))
  12529. (loop for i from (1+ level) to n do (aset subs i nil)))
  12530. (setq last-level level)))))
  12531. (aref subs 1)))
  12532. (eval-after-load "imenu"
  12533. '(progn
  12534. (add-hook 'imenu-after-jump-hook
  12535. (lambda () (org-show-context 'org-goto)))))
  12536. ;; Speedbar support
  12537. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  12538. "Overlay marking the agenda restriction line in speedbar.")
  12539. (org-overlay-put org-speedbar-restriction-lock-overlay
  12540. 'face 'org-agenda-restriction-lock)
  12541. (org-overlay-put org-speedbar-restriction-lock-overlay
  12542. 'help-echo "Agendas are currently limited to this item.")
  12543. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  12544. (defun org-speedbar-set-agenda-restriction ()
  12545. "Restrict future agenda commands to the location at point in speedbar.
  12546. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  12547. (interactive)
  12548. (require 'org-agenda)
  12549. (let (p m tp np dir txt w)
  12550. (cond
  12551. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  12552. 'org-imenu t))
  12553. (setq m (get-text-property p 'org-imenu-marker))
  12554. (save-excursion
  12555. (save-restriction
  12556. (set-buffer (marker-buffer m))
  12557. (goto-char m)
  12558. (org-agenda-set-restriction-lock 'subtree))))
  12559. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  12560. 'speedbar-function 'speedbar-find-file))
  12561. (setq tp (previous-single-property-change
  12562. (1+ p) 'speedbar-function)
  12563. np (next-single-property-change
  12564. tp 'speedbar-function)
  12565. dir (speedbar-line-directory)
  12566. txt (buffer-substring-no-properties (or tp (point-min))
  12567. (or np (point-max))))
  12568. (save-excursion
  12569. (save-restriction
  12570. (set-buffer (find-file-noselect
  12571. (let ((default-directory dir))
  12572. (expand-file-name txt))))
  12573. (unless (org-mode-p)
  12574. (error "Cannot restrict to non-Org-mode file"))
  12575. (org-agenda-set-restriction-lock 'file))))
  12576. (t (error "Don't know how to restrict Org-mode's agenda")))
  12577. (org-move-overlay org-speedbar-restriction-lock-overlay
  12578. (point-at-bol) (point-at-eol))
  12579. (setq current-prefix-arg nil)
  12580. (org-agenda-maybe-redo)))
  12581. (eval-after-load "speedbar"
  12582. '(progn
  12583. (speedbar-add-supported-extension ".org")
  12584. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  12585. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  12586. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  12587. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  12588. (add-hook 'speedbar-visiting-tag-hook
  12589. (lambda () (org-show-context 'org-goto)))))
  12590. ;;; Fixes and Hacks for problems with other packages
  12591. ;; Make flyspell not check words in links, to not mess up our keymap
  12592. (defun org-mode-flyspell-verify ()
  12593. "Don't let flyspell put overlays at active buttons."
  12594. (not (get-text-property (point) 'keymap)))
  12595. ;; Make `bookmark-jump' show the jump location if it was hidden.
  12596. (eval-after-load "bookmark"
  12597. '(if (boundp 'bookmark-after-jump-hook)
  12598. ;; We can use the hook
  12599. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  12600. ;; Hook not available, use advice
  12601. (defadvice bookmark-jump (after org-make-visible activate)
  12602. "Make the position visible."
  12603. (org-bookmark-jump-unhide))))
  12604. (defun org-bookmark-jump-unhide ()
  12605. "Unhide the current position, to show the bookmark location."
  12606. (and (org-mode-p)
  12607. (or (org-invisible-p)
  12608. (save-excursion (goto-char (max (point-min) (1- (point))))
  12609. (org-invisible-p)))
  12610. (org-show-context 'bookmark-jump)))
  12611. ;; Make session.el ignore our circular variable
  12612. (eval-after-load "session"
  12613. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  12614. ;;;; Experimental code
  12615. (defun org-closed-in-range ()
  12616. "Sparse tree of items closed in a certain time range.
  12617. Still experimental, may disappear in the future."
  12618. (interactive)
  12619. ;; Get the time interval from the user.
  12620. (let* ((time1 (time-to-seconds
  12621. (org-read-date nil 'to-time nil "Starting date: ")))
  12622. (time2 (time-to-seconds
  12623. (org-read-date nil 'to-time nil "End date:")))
  12624. ;; callback function
  12625. (callback (lambda ()
  12626. (let ((time
  12627. (time-to-seconds
  12628. (apply 'encode-time
  12629. (org-parse-time-string
  12630. (match-string 1))))))
  12631. ;; check if time in interval
  12632. (and (>= time time1) (<= time time2))))))
  12633. ;; make tree, check each match with the callback
  12634. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  12635. ;;;; Finish up
  12636. (provide 'org)
  12637. (run-hooks 'org-load-hook)
  12638. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  12639. ;;; org.el ends here