org.el 503 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697
  1. ;;; org.el --- Outline-based notes management and organizer
  2. ;; Carstens outline-mode for keeping track of everything.
  3. ;; Copyright (C) 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
  4. ;;
  5. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  6. ;; Keywords: outlines, hypermedia, calendar, wp
  7. ;; Homepage: http://orgmode.org
  8. ;; Version: 6.02b
  9. ;;
  10. ;; This file is part of GNU Emacs.
  11. ;;
  12. ;; GNU Emacs is free software; you can redistribute it and/or modify
  13. ;; it under the terms of the GNU General Public License as published by
  14. ;; the Free Software Foundation; either version 3, or (at your option)
  15. ;; any later version.
  16. ;; GNU Emacs is distributed in the hope that it will be useful,
  17. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. ;; GNU General Public License for more details.
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs; see the file COPYING. If not, write to the
  22. ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  23. ;; Boston, MA 02110-1301, USA.
  24. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  25. ;;
  26. ;;; Commentary:
  27. ;;
  28. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  29. ;; project planning with a fast and effective plain-text system.
  30. ;;
  31. ;; Org-mode develops organizational tasks around NOTES files that contain
  32. ;; information about projects as plain text. Org-mode is implemented on
  33. ;; top of outline-mode, which makes it possible to keep the content of
  34. ;; large files well structured. Visibility cycling and structure editing
  35. ;; help to work with the tree. Tables are easily created with a built-in
  36. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  37. ;; and scheduling. It dynamically compiles entries into an agenda that
  38. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  39. ;; Plain text URL-like links connect to websites, emails, Usenet
  40. ;; messages, BBDB entries, and any files related to the projects. For
  41. ;; printing and sharing of notes, an Org-mode file can be exported as a
  42. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  43. ;; iCalendar file. It can also serve as a publishing tool for a set of
  44. ;; linked webpages.
  45. ;;
  46. ;; Installation and Activation
  47. ;; ---------------------------
  48. ;; See the corresponding sections in the manual at
  49. ;;
  50. ;; http://orgmode.org/org.html#Installation
  51. ;;
  52. ;; Documentation
  53. ;; -------------
  54. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  55. ;; distribution also contains a PDF version of it. At the homepage of
  56. ;; Org-mode, you can read the same text online as HTML. There is also an
  57. ;; excellent reference card made by Philip Rooke. This card can be found
  58. ;; in the etc/ directory of Emacs 22.
  59. ;;
  60. ;; A list of recent changes can be found at
  61. ;; http://orgmode.org/Changes.html
  62. ;;
  63. ;;; Code:
  64. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  65. (defvar org-table-formula-constants-local nil
  66. "Local version of `org-table-formula-constants'.")
  67. (make-variable-buffer-local 'org-table-formula-constants-local)
  68. ;;;; Require other packages
  69. (eval-when-compile
  70. (require 'cl)
  71. (require 'gnus-sum)
  72. (require 'calendar))
  73. ;; For XEmacs, noutline is not yet provided by outline.el, so arrange for
  74. ;; the file noutline.el being loaded.
  75. (if (featurep 'xemacs) (condition-case nil (require 'noutline)))
  76. ;; We require noutline, which might be provided in outline.el
  77. (require 'outline) (require 'noutline)
  78. ;; Other stuff we need.
  79. (require 'time-date)
  80. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  81. (require 'easymenu)
  82. (require 'org-macs)
  83. (require 'org-compat)
  84. (require 'org-faces)
  85. ;;;; Customization variables
  86. ;;; Version
  87. (defconst org-version "6.02b"
  88. "The version number of the file org.el.")
  89. (defun org-version (&optional here)
  90. "Show the org-mode version in the echo area.
  91. With prefix arg HERE, insert it at point."
  92. (interactive "P")
  93. (let ((version (format "Org-mode version %s" org-version)))
  94. (message version)
  95. (if here
  96. (insert version))))
  97. ;;; Compatibility constants
  98. ;;; The custom variables
  99. (defgroup org nil
  100. "Outline-based notes management and organizer."
  101. :tag "Org"
  102. :group 'outlines
  103. :group 'hypermedia
  104. :group 'calendar)
  105. (defcustom org-load-hook nil
  106. "Hook that is run after org.el has been loaded."
  107. :group 'org
  108. :type 'hook)
  109. (defvar org-modules) ; defined below
  110. (defvar org-modules-loaded nil
  111. "Have the modules been loaded already?")
  112. (defun org-load-modules-maybe (&optional force)
  113. "Load all extensions listed in `org-default-extensions'."
  114. (when (or force (not org-modules-loaded))
  115. (mapc (lambda (ext)
  116. (condition-case nil (require ext)
  117. (error (message "Problems while trying to load feature `%s'" ext))))
  118. org-modules)
  119. (setq org-modules-loaded t)))
  120. (defun org-set-modules (var value)
  121. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  122. (set var value)
  123. (when (featurep 'org)
  124. (org-load-modules-maybe 'force)))
  125. (defcustom org-modules '(org-bbdb org-bibtex org-gnus org-info org-infojs org-irc org-mew org-mhe org-rmail org-vm org-wl)
  126. "Modules that should always be loaded together with org.el.
  127. If a description starts with <C>, the file is not part of emacs
  128. and loading it will require that you have downloaded and properly installed
  129. the org-mode distribution.
  130. You can also use this system to load external packages (i.e. neither Org
  131. core modules, not modules from the CONTRIB directory). Just add symbols
  132. to the end of the list. If the package is called org-xyz.e, then you need
  133. to add the symbol `xyz', and the package must have a call to
  134. (provide 'org-xyz)"
  135. :group 'org
  136. :set 'org-set-modules
  137. :type
  138. '(set :greedy t
  139. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  140. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  141. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  142. (const :tag " info: Links to Info nodes" org-info)
  143. (const :tag " infojs: Set up Sebastian Rose's JavaScript org-info.js" org-infojs)
  144. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  145. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  146. (const :tag " mew Links to Mew folders/messages" org-mew)
  147. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  148. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  149. (const :tag " vm: Links to VM folders/messages" org-vm)
  150. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  151. (const :tag " mouse: Additional mouse support" org-mouse)
  152. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  153. (const :tag "C bookmark: Org links to bookmarks" org-bookmark)
  154. (const :tag "C depend: TODO dependencies for Org-mode" org-depend)
  155. (const :tag "C elisp-symbol: Org links to emacs-lisp symbols" org-elisp-symbol)
  156. (const :tag "C expiry: Expiry mechanism for Org entries" org-expiry)
  157. (const :tag "C id: Global id's for identifying entries" org-id)
  158. (const :tag "C interactive-query: Interactive modification of tags query" org-interactive-query)
  159. (const :tag "C mairix: Hook mairix search into Org for different MUAs" org-mairix)
  160. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  161. (const :tag "C mew: Support for links to messages in Mew" org-mew)
  162. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  163. (const :tag "C registry: A registry for Org links" org-registry)
  164. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  165. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  166. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  167. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  168. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  169. (defgroup org-startup nil
  170. "Options concerning startup of Org-mode."
  171. :tag "Org Startup"
  172. :group 'org)
  173. (defcustom org-startup-folded t
  174. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  175. This can also be configured on a per-file basis by adding one of
  176. the following lines anywhere in the buffer:
  177. #+STARTUP: fold
  178. #+STARTUP: nofold
  179. #+STARTUP: content"
  180. :group 'org-startup
  181. :type '(choice
  182. (const :tag "nofold: show all" nil)
  183. (const :tag "fold: overview" t)
  184. (const :tag "content: all headlines" content)))
  185. (defcustom org-startup-truncated t
  186. "Non-nil means, entering Org-mode will set `truncate-lines'.
  187. This is useful since some lines containing links can be very long and
  188. uninteresting. Also tables look terrible when wrapped."
  189. :group 'org-startup
  190. :type 'boolean)
  191. (defcustom org-startup-align-all-tables nil
  192. "Non-nil means, align all tables when visiting a file.
  193. This is useful when the column width in tables is forced with <N> cookies
  194. in table fields. Such tables will look correct only after the first re-align.
  195. This can also be configured on a per-file basis by adding one of
  196. the following lines anywhere in the buffer:
  197. #+STARTUP: align
  198. #+STARTUP: noalign"
  199. :group 'org-startup
  200. :type 'boolean)
  201. (defcustom org-insert-mode-line-in-empty-file nil
  202. "Non-nil means insert the first line setting Org-mode in empty files.
  203. When the function `org-mode' is called interactively in an empty file, this
  204. normally means that the file name does not automatically trigger Org-mode.
  205. To ensure that the file will always be in Org-mode in the future, a
  206. line enforcing Org-mode will be inserted into the buffer, if this option
  207. has been set."
  208. :group 'org-startup
  209. :type 'boolean)
  210. (defcustom org-replace-disputed-keys nil
  211. "Non-nil means use alternative key bindings for some keys.
  212. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  213. These keys are also used by other packages like `CUA-mode' or `windmove.el'.
  214. If you want to use Org-mode together with one of these other modes,
  215. or more generally if you would like to move some Org-mode commands to
  216. other keys, set this variable and configure the keys with the variable
  217. `org-disputed-keys'.
  218. This option is only relevant at load-time of Org-mode, and must be set
  219. *before* org.el is loaded. Changing it requires a restart of Emacs to
  220. become effective."
  221. :group 'org-startup
  222. :type 'boolean)
  223. (if (fboundp 'defvaralias)
  224. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  225. (defcustom org-disputed-keys
  226. '(([(shift up)] . [(meta p)])
  227. ([(shift down)] . [(meta n)])
  228. ([(shift left)] . [(meta -)])
  229. ([(shift right)] . [(meta +)])
  230. ([(control shift right)] . [(meta shift +)])
  231. ([(control shift left)] . [(meta shift -)]))
  232. "Keys for which Org-mode and other modes compete.
  233. This is an alist, cars are the default keys, second element specifies
  234. the alternative to use when `org-replace-disputed-keys' is t.
  235. Keys can be specified in any syntax supported by `define-key'.
  236. The value of this option takes effect only at Org-mode's startup,
  237. therefore you'll have to restart Emacs to apply it after changing."
  238. :group 'org-startup
  239. :type 'alist)
  240. (defun org-key (key)
  241. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  242. Or return the original if not disputed."
  243. (if org-replace-disputed-keys
  244. (let* ((nkey (key-description key))
  245. (x (org-find-if (lambda (x)
  246. (equal (key-description (car x)) nkey))
  247. org-disputed-keys)))
  248. (if x (cdr x) key))
  249. key))
  250. (defun org-find-if (predicate seq)
  251. (catch 'exit
  252. (while seq
  253. (if (funcall predicate (car seq))
  254. (throw 'exit (car seq))
  255. (pop seq)))))
  256. (defun org-defkey (keymap key def)
  257. "Define a key, possibly translated, as returned by `org-key'."
  258. (define-key keymap (org-key key) def))
  259. (defcustom org-ellipsis nil
  260. "The ellipsis to use in the Org-mode outline.
  261. When nil, just use the standard three dots. When a string, use that instead,
  262. When a face, use the standart 3 dots, but with the specified face.
  263. The change affects only Org-mode (which will then use its own display table).
  264. Changing this requires executing `M-x org-mode' in a buffer to become
  265. effective."
  266. :group 'org-startup
  267. :type '(choice (const :tag "Default" nil)
  268. (face :tag "Face" :value org-warning)
  269. (string :tag "String" :value "...#")))
  270. (defvar org-display-table nil
  271. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  272. (defgroup org-keywords nil
  273. "Keywords in Org-mode."
  274. :tag "Org Keywords"
  275. :group 'org)
  276. (defcustom org-deadline-string "DEADLINE:"
  277. "String to mark deadline entries.
  278. A deadline is this string, followed by a time stamp. Should be a word,
  279. terminated by a colon. You can insert a schedule keyword and
  280. a timestamp with \\[org-deadline].
  281. Changes become only effective after restarting Emacs."
  282. :group 'org-keywords
  283. :type 'string)
  284. (defcustom org-scheduled-string "SCHEDULED:"
  285. "String to mark scheduled TODO entries.
  286. A schedule is this string, followed by a time stamp. Should be a word,
  287. terminated by a colon. You can insert a schedule keyword and
  288. a timestamp with \\[org-schedule].
  289. Changes become only effective after restarting Emacs."
  290. :group 'org-keywords
  291. :type 'string)
  292. (defcustom org-closed-string "CLOSED:"
  293. "String used as the prefix for timestamps logging closing a TODO entry."
  294. :group 'org-keywords
  295. :type 'string)
  296. (defcustom org-clock-string "CLOCK:"
  297. "String used as prefix for timestamps clocking work hours on an item."
  298. :group 'org-keywords
  299. :type 'string)
  300. (defcustom org-comment-string "COMMENT"
  301. "Entries starting with this keyword will never be exported.
  302. An entry can be toggled between COMMENT and normal with
  303. \\[org-toggle-comment].
  304. Changes become only effective after restarting Emacs."
  305. :group 'org-keywords
  306. :type 'string)
  307. (defcustom org-quote-string "QUOTE"
  308. "Entries starting with this keyword will be exported in fixed-width font.
  309. Quoting applies only to the text in the entry following the headline, and does
  310. not extend beyond the next headline, even if that is lower level.
  311. An entry can be toggled between QUOTE and normal with
  312. \\[org-toggle-fixed-width-section]."
  313. :group 'org-keywords
  314. :type 'string)
  315. (defconst org-repeat-re
  316. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\([.+]?\\+[0-9]+[dwmy]\\)"
  317. "Regular expression for specifying repeated events.
  318. After a match, group 1 contains the repeat expression.")
  319. (defgroup org-structure nil
  320. "Options concerning the general structure of Org-mode files."
  321. :tag "Org Structure"
  322. :group 'org)
  323. (defgroup org-reveal-location nil
  324. "Options about how to make context of a location visible."
  325. :tag "Org Reveal Location"
  326. :group 'org-structure)
  327. (defconst org-context-choice
  328. '(choice
  329. (const :tag "Always" t)
  330. (const :tag "Never" nil)
  331. (repeat :greedy t :tag "Individual contexts"
  332. (cons
  333. (choice :tag "Context"
  334. (const agenda)
  335. (const org-goto)
  336. (const occur-tree)
  337. (const tags-tree)
  338. (const link-search)
  339. (const mark-goto)
  340. (const bookmark-jump)
  341. (const isearch)
  342. (const default))
  343. (boolean))))
  344. "Contexts for the reveal options.")
  345. (defcustom org-show-hierarchy-above '((default . t))
  346. "Non-nil means, show full hierarchy when revealing a location.
  347. Org-mode often shows locations in an org-mode file which might have
  348. been invisible before. When this is set, the hierarchy of headings
  349. above the exposed location is shown.
  350. Turning this off for example for sparse trees makes them very compact.
  351. Instead of t, this can also be an alist specifying this option for different
  352. contexts. Valid contexts are
  353. agenda when exposing an entry from the agenda
  354. org-goto when using the command `org-goto' on key C-c C-j
  355. occur-tree when using the command `org-occur' on key C-c /
  356. tags-tree when constructing a sparse tree based on tags matches
  357. link-search when exposing search matches associated with a link
  358. mark-goto when exposing the jump goal of a mark
  359. bookmark-jump when exposing a bookmark location
  360. isearch when exiting from an incremental search
  361. default default for all contexts not set explicitly"
  362. :group 'org-reveal-location
  363. :type org-context-choice)
  364. (defcustom org-show-following-heading '((default . nil))
  365. "Non-nil means, show following heading when revealing a location.
  366. Org-mode often shows locations in an org-mode file which might have
  367. been invisible before. When this is set, the heading following the
  368. match is shown.
  369. Turning this off for example for sparse trees makes them very compact,
  370. but makes it harder to edit the location of the match. In such a case,
  371. use the command \\[org-reveal] to show more context.
  372. Instead of t, this can also be an alist specifying this option for different
  373. contexts. See `org-show-hierarchy-above' for valid contexts."
  374. :group 'org-reveal-location
  375. :type org-context-choice)
  376. (defcustom org-show-siblings '((default . nil) (isearch t))
  377. "Non-nil means, show all sibling heading when revealing a location.
  378. Org-mode often shows locations in an org-mode file which might have
  379. been invisible before. When this is set, the sibling of the current entry
  380. heading are all made visible. If `org-show-hierarchy-above' is t,
  381. the same happens on each level of the hierarchy above the current entry.
  382. By default this is on for the isearch context, off for all other contexts.
  383. Turning this off for example for sparse trees makes them very compact,
  384. but makes it harder to edit the location of the match. In such a case,
  385. use the command \\[org-reveal] to show more context.
  386. Instead of t, this can also be an alist specifying this option for different
  387. contexts. See `org-show-hierarchy-above' for valid contexts."
  388. :group 'org-reveal-location
  389. :type org-context-choice)
  390. (defcustom org-show-entry-below '((default . nil))
  391. "Non-nil means, show the entry below a headline when revealing a location.
  392. Org-mode often shows locations in an org-mode file which might have
  393. been invisible before. When this is set, the text below the headline that is
  394. exposed is also shown.
  395. By default this is off for all contexts.
  396. Instead of t, this can also be an alist specifying this option for different
  397. contexts. See `org-show-hierarchy-above' for valid contexts."
  398. :group 'org-reveal-location
  399. :type org-context-choice)
  400. (defcustom org-indirect-buffer-display 'other-window
  401. "How should indirect tree buffers be displayed?
  402. This applies to indirect buffers created with the commands
  403. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  404. Valid values are:
  405. current-window Display in the current window
  406. other-window Just display in another window.
  407. dedicated-frame Create one new frame, and re-use it each time.
  408. new-frame Make a new frame each time. Note that in this case
  409. previously-made indirect buffers are kept, and you need to
  410. kill these buffers yourself."
  411. :group 'org-structure
  412. :group 'org-agenda-windows
  413. :type '(choice
  414. (const :tag "In current window" current-window)
  415. (const :tag "In current frame, other window" other-window)
  416. (const :tag "Each time a new frame" new-frame)
  417. (const :tag "One dedicated frame" dedicated-frame)))
  418. (defgroup org-cycle nil
  419. "Options concerning visibility cycling in Org-mode."
  420. :tag "Org Cycle"
  421. :group 'org-structure)
  422. (defcustom org-drawers '("PROPERTIES" "CLOCK")
  423. "Names of drawers. Drawers are not opened by cycling on the headline above.
  424. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  425. this:
  426. :DRAWERNAME:
  427. .....
  428. :END:
  429. The drawer \"PROPERTIES\" is special for capturing properties through
  430. the property API.
  431. Drawers can be defined on the per-file basis with a line like:
  432. #+DRAWERS: HIDDEN STATE PROPERTIES"
  433. :group 'org-structure
  434. :type '(repeat (string :tag "Drawer Name")))
  435. (defcustom org-cycle-global-at-bob nil
  436. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  437. This makes it possible to do global cycling without having to use S-TAB or
  438. C-u TAB. For this special case to work, the first line of the buffer
  439. must not be a headline - it may be empty ot some other text. When used in
  440. this way, `org-cycle-hook' is disables temporarily, to make sure the
  441. cursor stays at the beginning of the buffer.
  442. When this option is nil, don't do anything special at the beginning
  443. of the buffer."
  444. :group 'org-cycle
  445. :type 'boolean)
  446. (defcustom org-cycle-emulate-tab t
  447. "Where should `org-cycle' emulate TAB.
  448. nil Never
  449. white Only in completely white lines
  450. whitestart Only at the beginning of lines, before the first non-white char
  451. t Everywhere except in headlines
  452. exc-hl-bol Everywhere except at the start of a headline
  453. If TAB is used in a place where it does not emulate TAB, the current subtree
  454. visibility is cycled."
  455. :group 'org-cycle
  456. :type '(choice (const :tag "Never" nil)
  457. (const :tag "Only in completely white lines" white)
  458. (const :tag "Before first char in a line" whitestart)
  459. (const :tag "Everywhere except in headlines" t)
  460. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  461. ))
  462. (defcustom org-cycle-separator-lines 2
  463. "Number of empty lines needed to keep an empty line between collapsed trees.
  464. If you leave an empty line between the end of a subtree and the following
  465. headline, this empty line is hidden when the subtree is folded.
  466. Org-mode will leave (exactly) one empty line visible if the number of
  467. empty lines is equal or larger to the number given in this variable.
  468. So the default 2 means, at least 2 empty lines after the end of a subtree
  469. are needed to produce free space between a collapsed subtree and the
  470. following headline.
  471. Special case: when 0, never leave empty lines in collapsed view."
  472. :group 'org-cycle
  473. :type 'integer)
  474. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  475. org-cycle-hide-drawers
  476. org-cycle-show-empty-lines
  477. org-optimize-window-after-visibility-change)
  478. "Hook that is run after `org-cycle' has changed the buffer visibility.
  479. The function(s) in this hook must accept a single argument which indicates
  480. the new state that was set by the most recent `org-cycle' command. The
  481. argument is a symbol. After a global state change, it can have the values
  482. `overview', `content', or `all'. After a local state change, it can have
  483. the values `folded', `children', or `subtree'."
  484. :group 'org-cycle
  485. :type 'hook)
  486. (defgroup org-edit-structure nil
  487. "Options concerning structure editing in Org-mode."
  488. :tag "Org Edit Structure"
  489. :group 'org-structure)
  490. (defcustom org-odd-levels-only nil
  491. "Non-nil means, skip even levels and only use odd levels for the outline.
  492. This has the effect that two stars are being added/taken away in
  493. promotion/demotion commands. It also influences how levels are
  494. handled by the exporters.
  495. Changing it requires restart of `font-lock-mode' to become effective
  496. for fontification also in regions already fontified.
  497. You may also set this on a per-file basis by adding one of the following
  498. lines to the buffer:
  499. #+STARTUP: odd
  500. #+STARTUP: oddeven"
  501. :group 'org-edit-structure
  502. :group 'org-font-lock
  503. :type 'boolean)
  504. (defcustom org-adapt-indentation t
  505. "Non-nil means, adapt indentation when promoting and demoting.
  506. When this is set and the *entire* text in an entry is indented, the
  507. indentation is increased by one space in a demotion command, and
  508. decreased by one in a promotion command. If any line in the entry
  509. body starts at column 0, indentation is not changed at all."
  510. :group 'org-edit-structure
  511. :type 'boolean)
  512. (defcustom org-special-ctrl-a/e nil
  513. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  514. When t, `C-a' will bring back the cursor to the beginning of the
  515. headline text, i.e. after the stars and after a possible TODO keyword.
  516. In an item, this will be the position after the bullet.
  517. When the cursor is already at that position, another `C-a' will bring
  518. it to the beginning of the line.
  519. `C-e' will jump to the end of the headline, ignoring the presence of tags
  520. in the headline. A second `C-e' will then jump to the true end of the
  521. line, after any tags.
  522. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  523. and only a directly following, identical keypress will bring the cursor
  524. to the special positions."
  525. :group 'org-edit-structure
  526. :type '(choice
  527. (const :tag "off" nil)
  528. (const :tag "after bullet first" t)
  529. (const :tag "border first" reversed)))
  530. (if (fboundp 'defvaralias)
  531. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  532. (defcustom org-special-ctrl-k nil
  533. "Non-nil means `C-k' will behave specially in headlines.
  534. When nil, `C-k' will call the default `kill-line' command.
  535. When t, the following will happen while the cursor is in the headline:
  536. - When the cursor is at the beginning of a headline, kill the entire
  537. line and possible the folded subtree below the line.
  538. - When in the middle of the headline text, kill the headline up to the tags.
  539. - When after the headline text, kill the tags."
  540. :group 'org-edit-structure
  541. :type 'boolean)
  542. (defcustom org-M-RET-may-split-line '((default . t))
  543. "Non-nil means, M-RET will split the line at the cursor position.
  544. When nil, it will go to the end of the line before making a
  545. new line.
  546. You may also set this option in a different way for different
  547. contexts. Valid contexts are:
  548. headline when creating a new headline
  549. item when creating a new item
  550. table in a table field
  551. default the value to be used for all contexts not explicitly
  552. customized"
  553. :group 'org-structure
  554. :group 'org-table
  555. :type '(choice
  556. (const :tag "Always" t)
  557. (const :tag "Never" nil)
  558. (repeat :greedy t :tag "Individual contexts"
  559. (cons
  560. (choice :tag "Context"
  561. (const headline)
  562. (const item)
  563. (const table)
  564. (const default))
  565. (boolean)))))
  566. (defcustom org-blank-before-new-entry '((heading . nil)
  567. (plain-list-item . nil))
  568. "Should `org-insert-heading' leave a blank line before new heading/item?
  569. The value is an alist, with `heading' and `plain-list-item' as car,
  570. and a boolean flag as cdr."
  571. :group 'org-edit-structure
  572. :type '(list
  573. (cons (const heading) (boolean))
  574. (cons (const plain-list-item) (boolean))))
  575. (defcustom org-insert-heading-hook nil
  576. "Hook being run after inserting a new heading."
  577. :group 'org-edit-structure
  578. :type 'hook)
  579. (defcustom org-enable-fixed-width-editor t
  580. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  581. This currently only means, they are never auto-wrapped.
  582. When nil, such lines will be treated like ordinary lines.
  583. See also the QUOTE keyword."
  584. :group 'org-edit-structure
  585. :type 'boolean)
  586. (defcustom org-goto-auto-isearch t
  587. "Non-nil means, typing characters in org-goto starts incremental search."
  588. :group 'org-edit-structure
  589. :type 'boolean)
  590. (defgroup org-sparse-trees nil
  591. "Options concerning sparse trees in Org-mode."
  592. :tag "Org Sparse Trees"
  593. :group 'org-structure)
  594. (defcustom org-highlight-sparse-tree-matches t
  595. "Non-nil means, highlight all matches that define a sparse tree.
  596. The highlights will automatically disappear the next time the buffer is
  597. changed by an edit command."
  598. :group 'org-sparse-trees
  599. :type 'boolean)
  600. (defcustom org-remove-highlights-with-change t
  601. "Non-nil means, any change to the buffer will remove temporary highlights.
  602. Such highlights are created by `org-occur' and `org-clock-display'.
  603. When nil, `C-c C-c needs to be used to get rid of the highlights.
  604. The highlights created by `org-preview-latex-fragment' always need
  605. `C-c C-c' to be removed."
  606. :group 'org-sparse-trees
  607. :group 'org-time
  608. :type 'boolean)
  609. (defcustom org-occur-hook '(org-first-headline-recenter)
  610. "Hook that is run after `org-occur' has constructed a sparse tree.
  611. This can be used to recenter the window to show as much of the structure
  612. as possible."
  613. :group 'org-sparse-trees
  614. :type 'hook)
  615. (defgroup org-plain-lists nil
  616. "Options concerning plain lists in Org-mode."
  617. :tag "Org Plain lists"
  618. :group 'org-structure)
  619. (defcustom org-cycle-include-plain-lists nil
  620. "Non-nil means, include plain lists into visibility cycling.
  621. This means that during cycling, plain list items will *temporarily* be
  622. interpreted as outline headlines with a level given by 1000+i where i is the
  623. indentation of the bullet. In all other operations, plain list items are
  624. not seen as headlines. For example, you cannot assign a TODO keyword to
  625. such an item."
  626. :group 'org-plain-lists
  627. :type 'boolean)
  628. (defcustom org-plain-list-ordered-item-terminator t
  629. "The character that makes a line with leading number an ordered list item.
  630. Valid values are ?. and ?\). To get both terminators, use t. While
  631. ?. may look nicer, it creates the danger that a line with leading
  632. number may be incorrectly interpreted as an item. ?\) therefore is
  633. the safe choice."
  634. :group 'org-plain-lists
  635. :type '(choice (const :tag "dot like in \"2.\"" ?.)
  636. (const :tag "paren like in \"2)\"" ?\))
  637. (const :tab "both" t)))
  638. (defcustom org-empty-line-terminates-plain-lists nil
  639. "Non-nil means, an empty line ends all plain list levels.
  640. When nil, empty lines are part of the preceeding item."
  641. :group 'org-plain-lists
  642. :type 'boolean)
  643. (defcustom org-auto-renumber-ordered-lists t
  644. "Non-nil means, automatically renumber ordered plain lists.
  645. Renumbering happens when the sequence have been changed with
  646. \\[org-shiftmetaup] or \\[org-shiftmetadown]. After other editing commands,
  647. use \\[org-ctrl-c-ctrl-c] to trigger renumbering."
  648. :group 'org-plain-lists
  649. :type 'boolean)
  650. (defcustom org-provide-checkbox-statistics t
  651. "Non-nil means, update checkbox statistics after insert and toggle.
  652. When this is set, checkbox statistics is updated each time you either insert
  653. a new checkbox with \\[org-insert-todo-heading] or toggle a checkbox
  654. with \\[org-ctrl-c-ctrl-c\\]."
  655. :group 'org-plain-lists
  656. :type 'boolean)
  657. (defgroup org-imenu-and-speedbar nil
  658. "Options concerning imenu and speedbar in Org-mode."
  659. :tag "Org Imenu and Speedbar"
  660. :group 'org-structure)
  661. (defcustom org-imenu-depth 2
  662. "The maximum level for Imenu access to Org-mode headlines.
  663. This also applied for speedbar access."
  664. :group 'org-imenu-and-speedbar
  665. :type 'number)
  666. (defgroup org-table nil
  667. "Options concerning tables in Org-mode."
  668. :tag "Org Table"
  669. :group 'org)
  670. (defcustom org-enable-table-editor 'optimized
  671. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  672. When nil, such lines will be treated like ordinary lines.
  673. When equal to the symbol `optimized', the table editor will be optimized to
  674. do the following:
  675. - Automatic overwrite mode in front of whitespace in table fields.
  676. This makes the structure of the table stay in tact as long as the edited
  677. field does not exceed the column width.
  678. - Minimize the number of realigns. Normally, the table is aligned each time
  679. TAB or RET are pressed to move to another field. With optimization this
  680. happens only if changes to a field might have changed the column width.
  681. Optimization requires replacing the functions `self-insert-command',
  682. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  683. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  684. very good at guessing when a re-align will be necessary, but you can always
  685. force one with \\[org-ctrl-c-ctrl-c].
  686. If you would like to use the optimized version in Org-mode, but the
  687. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  688. This variable can be used to turn on and off the table editor during a session,
  689. but in order to toggle optimization, a restart is required.
  690. See also the variable `org-table-auto-blank-field'."
  691. :group 'org-table
  692. :type '(choice
  693. (const :tag "off" nil)
  694. (const :tag "on" t)
  695. (const :tag "on, optimized" optimized)))
  696. (defcustom org-table-tab-recognizes-table.el t
  697. "Non-nil means, TAB will automatically notice a table.el table.
  698. When it sees such a table, it moves point into it and - if necessary -
  699. calls `table-recognize-table'."
  700. :group 'org-table-editing
  701. :type 'boolean)
  702. (defgroup org-link nil
  703. "Options concerning links in Org-mode."
  704. :tag "Org Link"
  705. :group 'org)
  706. (defvar org-link-abbrev-alist-local nil
  707. "Buffer-local version of `org-link-abbrev-alist', which see.
  708. The value of this is taken from the #+LINK lines.")
  709. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  710. (defcustom org-link-abbrev-alist nil
  711. "Alist of link abbreviations.
  712. The car of each element is a string, to be replaced at the start of a link.
  713. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  714. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  715. [[linkkey:tag][description]]
  716. If REPLACE is a string, the tag will simply be appended to create the link.
  717. If the string contains \"%s\", the tag will be inserted there.
  718. REPLACE may also be a function that will be called with the tag as the
  719. only argument to create the link, which should be returned as a string.
  720. See the manual for examples."
  721. :group 'org-link
  722. :type 'alist)
  723. (defcustom org-descriptive-links t
  724. "Non-nil means, hide link part and only show description of bracket links.
  725. Bracket links are like [[link][descritpion]]. This variable sets the initial
  726. state in new org-mode buffers. The setting can then be toggled on a
  727. per-buffer basis from the Org->Hyperlinks menu."
  728. :group 'org-link
  729. :type 'boolean)
  730. (defcustom org-link-file-path-type 'adaptive
  731. "How the path name in file links should be stored.
  732. Valid values are:
  733. relative Relative to the current directory, i.e. the directory of the file
  734. into which the link is being inserted.
  735. absolute Absolute path, if possible with ~ for home directory.
  736. noabbrev Absolute path, no abbreviation of home directory.
  737. adaptive Use relative path for files in the current directory and sub-
  738. directories of it. For other files, use an absolute path."
  739. :group 'org-link
  740. :type '(choice
  741. (const relative)
  742. (const absolute)
  743. (const noabbrev)
  744. (const adaptive)))
  745. (defcustom org-activate-links '(bracket angle plain radio tag date)
  746. "Types of links that should be activated in Org-mode files.
  747. This is a list of symbols, each leading to the activation of a certain link
  748. type. In principle, it does not hurt to turn on most link types - there may
  749. be a small gain when turning off unused link types. The types are:
  750. bracket The recommended [[link][description]] or [[link]] links with hiding.
  751. angular Links in angular brackes that may contain whitespace like
  752. <bbdb:Carsten Dominik>.
  753. plain Plain links in normal text, no whitespace, like http://google.com.
  754. radio Text that is matched by a radio target, see manual for details.
  755. tag Tag settings in a headline (link to tag search).
  756. date Time stamps (link to calendar).
  757. Changing this variable requires a restart of Emacs to become effective."
  758. :group 'org-link
  759. :type '(set (const :tag "Double bracket links (new style)" bracket)
  760. (const :tag "Angular bracket links (old style)" angular)
  761. (const :tag "Plain text links" plain)
  762. (const :tag "Radio target matches" radio)
  763. (const :tag "Tags" tag)
  764. (const :tag "Timestamps" date)))
  765. (defcustom org-make-link-description-function nil
  766. "Function to use to generate link descriptions from links. If
  767. nil the link location will be used. This function must take two
  768. parameters; the first is the link and the second the description
  769. org-insert-link has generated, and should return the description
  770. to use."
  771. :group 'org-link
  772. :type 'function)
  773. (defgroup org-link-store nil
  774. "Options concerning storing links in Org-mode."
  775. :tag "Org Store Link"
  776. :group 'org-link)
  777. (defcustom org-email-link-description-format "Email %c: %.30s"
  778. "Format of the description part of a link to an email or usenet message.
  779. The following %-excapes will be replaced by corresponding information:
  780. %F full \"From\" field
  781. %f name, taken from \"From\" field, address if no name
  782. %T full \"To\" field
  783. %t first name in \"To\" field, address if no name
  784. %c correspondent. Unually \"from NAME\", but if you sent it yourself, it
  785. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  786. %s subject
  787. %m message-id.
  788. You may use normal field width specification between the % and the letter.
  789. This is for example useful to limit the length of the subject.
  790. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  791. :group 'org-link-store
  792. :type 'string)
  793. (defcustom org-from-is-user-regexp
  794. (let (r1 r2)
  795. (when (and user-mail-address (not (string= user-mail-address "")))
  796. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  797. (when (and user-full-name (not (string= user-full-name "")))
  798. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  799. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  800. "Regexp mached against the \"From:\" header of an email or usenet message.
  801. It should match if the message is from the user him/herself."
  802. :group 'org-link-store
  803. :type 'regexp)
  804. (defcustom org-context-in-file-links t
  805. "Non-nil means, file links from `org-store-link' contain context.
  806. A search string will be added to the file name with :: as separator and
  807. used to find the context when the link is activated by the command
  808. `org-open-at-point'.
  809. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  810. negates this setting for the duration of the command."
  811. :group 'org-link-store
  812. :type 'boolean)
  813. (defcustom org-keep-stored-link-after-insertion nil
  814. "Non-nil means, keep link in list for entire session.
  815. The command `org-store-link' adds a link pointing to the current
  816. location to an internal list. These links accumulate during a session.
  817. The command `org-insert-link' can be used to insert links into any
  818. Org-mode file (offering completion for all stored links). When this
  819. option is nil, every link which has been inserted once using \\[org-insert-link]
  820. will be removed from the list, to make completing the unused links
  821. more efficient."
  822. :group 'org-link-store
  823. :type 'boolean)
  824. (defgroup org-link-follow nil
  825. "Options concerning following links in Org-mode."
  826. :tag "Org Follow Link"
  827. :group 'org-link)
  828. (defcustom org-follow-link-hook nil
  829. "Hook that is run after a link has been followed."
  830. :group 'org-link-follow
  831. :type 'hook)
  832. (defcustom org-tab-follows-link nil
  833. "Non-nil means, on links TAB will follow the link.
  834. Needs to be set before org.el is loaded."
  835. :group 'org-link-follow
  836. :type 'boolean)
  837. (defcustom org-return-follows-link nil
  838. "Non-nil means, on links RET will follow the link.
  839. Needs to be set before org.el is loaded."
  840. :group 'org-link-follow
  841. :type 'boolean)
  842. (defcustom org-mouse-1-follows-link
  843. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  844. "Non-nil means, mouse-1 on a link will follow the link.
  845. A longer mouse click will still set point. Does not work on XEmacs.
  846. Needs to be set before org.el is loaded."
  847. :group 'org-link-follow
  848. :type 'boolean)
  849. (defcustom org-mark-ring-length 4
  850. "Number of different positions to be recorded in the ring
  851. Changing this requires a restart of Emacs to work correctly."
  852. :group 'org-link-follow
  853. :type 'interger)
  854. (defcustom org-link-frame-setup
  855. '((vm . vm-visit-folder-other-frame)
  856. (gnus . gnus-other-frame)
  857. (file . find-file-other-window))
  858. "Setup the frame configuration for following links.
  859. When following a link with Emacs, it may often be useful to display
  860. this link in another window or frame. This variable can be used to
  861. set this up for the different types of links.
  862. For VM, use any of
  863. `vm-visit-folder'
  864. `vm-visit-folder-other-frame'
  865. For Gnus, use any of
  866. `gnus'
  867. `gnus-other-frame'
  868. For FILE, use any of
  869. `find-file'
  870. `find-file-other-window'
  871. `find-file-other-frame'
  872. For the calendar, use the variable `calendar-setup'.
  873. For BBDB, it is currently only possible to display the matches in
  874. another window."
  875. :group 'org-link-follow
  876. :type '(list
  877. (cons (const vm)
  878. (choice
  879. (const vm-visit-folder)
  880. (const vm-visit-folder-other-window)
  881. (const vm-visit-folder-other-frame)))
  882. (cons (const gnus)
  883. (choice
  884. (const gnus)
  885. (const gnus-other-frame)))
  886. (cons (const file)
  887. (choice
  888. (const find-file)
  889. (const find-file-other-window)
  890. (const find-file-other-frame)))))
  891. (defcustom org-display-internal-link-with-indirect-buffer nil
  892. "Non-nil means, use indirect buffer to display infile links.
  893. Activating internal links (from one location in a file to another location
  894. in the same file) normally just jumps to the location. When the link is
  895. activated with a C-u prefix (or with mouse-3), the link is displayed in
  896. another window. When this option is set, the other window actually displays
  897. an indirect buffer clone of the current buffer, to avoid any visibility
  898. changes to the current buffer."
  899. :group 'org-link-follow
  900. :type 'boolean)
  901. (defcustom org-open-non-existing-files nil
  902. "Non-nil means, `org-open-file' will open non-existing files.
  903. When nil, an error will be generated."
  904. :group 'org-link-follow
  905. :type 'boolean)
  906. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  907. "Function and arguments to call for following mailto links.
  908. This is a list with the first element being a lisp function, and the
  909. remaining elements being arguments to the function. In string arguments,
  910. %a will be replaced by the address, and %s will be replaced by the subject
  911. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  912. :group 'org-link-follow
  913. :type '(choice
  914. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  915. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  916. (const :tag "message-mail" (message-mail "%a" "%s"))
  917. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  918. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  919. "Non-nil means, ask for confirmation before executing shell links.
  920. Shell links can be dangerous: just think about a link
  921. [[shell:rm -rf ~/*][Google Search]]
  922. This link would show up in your Org-mode document as \"Google Search\",
  923. but really it would remove your entire home directory.
  924. Therefore we advise against setting this variable to nil.
  925. Just change it to `y-or-n-p' of you want to confirm with a
  926. single keystroke rather than having to type \"yes\"."
  927. :group 'org-link-follow
  928. :type '(choice
  929. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  930. (const :tag "with y-or-n (faster)" y-or-n-p)
  931. (const :tag "no confirmation (dangerous)" nil)))
  932. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  933. "Non-nil means, ask for confirmation before executing Emacs Lisp links.
  934. Elisp links can be dangerous: just think about a link
  935. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  936. This link would show up in your Org-mode document as \"Google Search\",
  937. but really it would remove your entire home directory.
  938. Therefore we advise against setting this variable to nil.
  939. Just change it to `y-or-n-p' of you want to confirm with a
  940. single keystroke rather than having to type \"yes\"."
  941. :group 'org-link-follow
  942. :type '(choice
  943. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  944. (const :tag "with y-or-n (faster)" y-or-n-p)
  945. (const :tag "no confirmation (dangerous)" nil)))
  946. (defconst org-file-apps-defaults-gnu
  947. '((remote . emacs)
  948. (t . mailcap))
  949. "Default file applications on a UNIX or GNU/Linux system.
  950. See `org-file-apps'.")
  951. (defconst org-file-apps-defaults-macosx
  952. '((remote . emacs)
  953. (t . "open %s")
  954. ("ps" . "gv %s")
  955. ("ps.gz" . "gv %s")
  956. ("eps" . "gv %s")
  957. ("eps.gz" . "gv %s")
  958. ("dvi" . "xdvi %s")
  959. ("fig" . "xfig %s"))
  960. "Default file applications on a MacOS X system.
  961. The system \"open\" is known as a default, but we use X11 applications
  962. for some files for which the OS does not have a good default.
  963. See `org-file-apps'.")
  964. (defconst org-file-apps-defaults-windowsnt
  965. (list
  966. '(remote . emacs)
  967. (cons t
  968. (list (if (featurep 'xemacs)
  969. 'mswindows-shell-execute
  970. 'w32-shell-execute)
  971. "open" 'file)))
  972. "Default file applications on a Windows NT system.
  973. The system \"open\" is used for most files.
  974. See `org-file-apps'.")
  975. (defcustom org-file-apps
  976. '(
  977. ("txt" . emacs)
  978. ("tex" . emacs)
  979. ("ltx" . emacs)
  980. ("org" . emacs)
  981. ("el" . emacs)
  982. ("bib" . emacs)
  983. )
  984. "External applications for opening `file:path' items in a document.
  985. Org-mode uses system defaults for different file types, but
  986. you can use this variable to set the application for a given file
  987. extension. The entries in this list are cons cells where the car identifies
  988. files and the cdr the corresponding command. Possible values for the
  989. file identifier are
  990. \"ext\" A string identifying an extension
  991. `directory' Matches a directory
  992. `remote' Matches a remote file, accessible through tramp or efs.
  993. Remote files most likely should be visited through Emacs
  994. because external applications cannot handle such paths.
  995. t Default for all remaining files
  996. Possible values for the command are:
  997. `emacs' The file will be visited by the current Emacs process.
  998. `default' Use the default application for this file type.
  999. string A command to be executed by a shell; %s will be replaced
  1000. by the path to the file.
  1001. sexp A Lisp form which will be evaluated. The file path will
  1002. be available in the Lisp variable `file'.
  1003. For more examples, see the system specific constants
  1004. `org-file-apps-defaults-macosx'
  1005. `org-file-apps-defaults-windowsnt'
  1006. `org-file-apps-defaults-gnu'."
  1007. :group 'org-link-follow
  1008. :type '(repeat
  1009. (cons (choice :value ""
  1010. (string :tag "Extension")
  1011. (const :tag "Default for unrecognized files" t)
  1012. (const :tag "Remote file" remote)
  1013. (const :tag "Links to a directory" directory))
  1014. (choice :value ""
  1015. (const :tag "Visit with Emacs" emacs)
  1016. (const :tag "Use system default" default)
  1017. (string :tag "Command")
  1018. (sexp :tag "Lisp form")))))
  1019. (defgroup org-refile nil
  1020. "Options concerning refiling entries in Org-mode."
  1021. :tag "Org Remember"
  1022. :group 'org)
  1023. (defcustom org-directory "~/org"
  1024. "Directory with org files.
  1025. This directory will be used as default to prompt for org files.
  1026. Used by the hooks for remember.el."
  1027. :group 'org-refile
  1028. :group 'org-remember
  1029. :type 'directory)
  1030. (defcustom org-default-notes-file "~/.notes"
  1031. "Default target for storing notes.
  1032. Used by the hooks for remember.el. This can be a string, or nil to mean
  1033. the value of `remember-data-file'.
  1034. You can set this on a per-template basis with the variable
  1035. `org-remember-templates'."
  1036. :group 'org-refile
  1037. :group 'org-remember
  1038. :type '(choice
  1039. (const :tag "Default from remember-data-file" nil)
  1040. file))
  1041. (defcustom org-goto-interface 'outline
  1042. "The default interface to be used for `org-goto'.
  1043. Allowed vaues are:
  1044. outline The interface shows an outline of the relevant file
  1045. and the correct heading is found by moving through
  1046. the outline or by searching with incremental search.
  1047. outline-path-completion Headlines in the current buffer are offered via
  1048. completion."
  1049. :group 'org-refile
  1050. :type '(choice
  1051. (const :tag "Outline" outline)
  1052. (const :tag "Outline-path-completion" outline-path-completion)))
  1053. (defcustom org-reverse-note-order nil
  1054. "Non-nil means, store new notes at the beginning of a file or entry.
  1055. When nil, new notes will be filed to the end of a file or entry.
  1056. This can also be a list with cons cells of regular expressions that
  1057. are matched against file names, and values."
  1058. :group 'org-remember
  1059. :type '(choice
  1060. (const :tag "Reverse always" t)
  1061. (const :tag "Reverse never" nil)
  1062. (repeat :tag "By file name regexp"
  1063. (cons regexp boolean))))
  1064. (defcustom org-refile-targets nil
  1065. "Targets for refiling entries with \\[org-refile].
  1066. This is list of cons cells. Each cell contains:
  1067. - a specification of the files to be considered, either a list of files,
  1068. or a symbol whose function or variable value will be used to retrieve
  1069. a file name or a list of file names. Nil means, refile to a different
  1070. heading in the current buffer.
  1071. - A specification of how to find candidate refile targets. This may be
  1072. any of
  1073. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1074. This tag has to be present in all target headlines, inheritance will
  1075. not be considered.
  1076. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1077. todo keyword.
  1078. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1079. headlines that are refiling targets.
  1080. - a cons cell (:level . N). Any headline of level N is considered a target.
  1081. - a cons cell (:maxlevel . N). Any headline with level <= N is a target."
  1082. :group 'org-remember
  1083. :type '(repeat
  1084. (cons
  1085. (choice :value org-agenda-files
  1086. (const :tag "All agenda files" org-agenda-files)
  1087. (const :tag "Current buffer" nil)
  1088. (function) (variable) (file))
  1089. (choice :tag "Identify target headline by"
  1090. (cons :tag "Specific tag" (const :tag) (string))
  1091. (cons :tag "TODO keyword" (const :todo) (string))
  1092. (cons :tag "Regular expression" (const :regexp) (regexp))
  1093. (cons :tag "Level number" (const :level) (integer))
  1094. (cons :tag "Max Level number" (const :maxlevel) (integer))))))
  1095. (defcustom org-refile-use-outline-path nil
  1096. "Non-nil means, provide refile targets as paths.
  1097. So a level 3 headline will be available as level1/level2/level3.
  1098. When the value is `file', also include the file name (without directory)
  1099. into the path. When `full-file-path', include the full file path."
  1100. :group 'org-remember
  1101. :type '(choice
  1102. (const :tag "Not" nil)
  1103. (const :tag "Yes" t)
  1104. (const :tag "Start with file name" file)
  1105. (const :tag "Start with full file path" full-file-path)))
  1106. (defgroup org-todo nil
  1107. "Options concerning TODO items in Org-mode."
  1108. :tag "Org TODO"
  1109. :group 'org)
  1110. (defgroup org-progress nil
  1111. "Options concerning Progress logging in Org-mode."
  1112. :tag "Org Progress"
  1113. :group 'org-time)
  1114. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1115. "List of TODO entry keyword sequences and their interpretation.
  1116. \\<org-mode-map>This is a list of sequences.
  1117. Each sequence starts with a symbol, either `sequence' or `type',
  1118. indicating if the keywords should be interpreted as a sequence of
  1119. action steps, or as different types of TODO items. The first
  1120. keywords are states requiring action - these states will select a headline
  1121. for inclusion into the global TODO list Org-mode produces. If one of
  1122. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1123. signify that no further action is necessary. If \"|\" is not found,
  1124. the last keyword is treated as the only DONE state of the sequence.
  1125. The command \\[org-todo] cycles an entry through these states, and one
  1126. additional state where no keyword is present. For details about this
  1127. cycling, see the manual.
  1128. TODO keywords and interpretation can also be set on a per-file basis with
  1129. the special #+SEQ_TODO and #+TYP_TODO lines.
  1130. Each keyword can optionally specify a character for fast state selection
  1131. \(in combination with the variable `org-use-fast-todo-selection')
  1132. and specifiers for state change logging, using the same syntax
  1133. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1134. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1135. indicates to record a time stamp each time this state is selected.
  1136. Each keyword may also specify if a timestamp or a note should be
  1137. recorded when entering or leaving the state, by adding additional
  1138. characters in the parenthesis after the keyword. This looks like this:
  1139. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1140. record only the time of the state change. With X and Y being either
  1141. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1142. Y when leaving the state if and only if the *target* state does not
  1143. define X. You may omit any of the fast-selection key or X or /Y,
  1144. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1145. For backward compatibility, this variable may also be just a list
  1146. of keywords - in this case the interptetation (sequence or type) will be
  1147. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1148. :group 'org-todo
  1149. :group 'org-keywords
  1150. :type '(choice
  1151. (repeat :tag "Old syntax, just keywords"
  1152. (string :tag "Keyword"))
  1153. (repeat :tag "New syntax"
  1154. (cons
  1155. (choice
  1156. :tag "Interpretation"
  1157. (const :tag "Sequence (cycling hits every state)" sequence)
  1158. (const :tag "Type (cycling directly to DONE)" type))
  1159. (repeat
  1160. (string :tag "Keyword"))))))
  1161. (defvar org-todo-keywords-1 nil
  1162. "All TODO and DONE keywords active in a buffer.")
  1163. (make-variable-buffer-local 'org-todo-keywords-1)
  1164. (defvar org-todo-keywords-for-agenda nil)
  1165. (defvar org-done-keywords-for-agenda nil)
  1166. (defvar org-agenda-contributing-files nil)
  1167. (defvar org-not-done-keywords nil)
  1168. (make-variable-buffer-local 'org-not-done-keywords)
  1169. (defvar org-done-keywords nil)
  1170. (make-variable-buffer-local 'org-done-keywords)
  1171. (defvar org-todo-heads nil)
  1172. (make-variable-buffer-local 'org-todo-heads)
  1173. (defvar org-todo-sets nil)
  1174. (make-variable-buffer-local 'org-todo-sets)
  1175. (defvar org-todo-log-states nil)
  1176. (make-variable-buffer-local 'org-todo-log-states)
  1177. (defvar org-todo-kwd-alist nil)
  1178. (make-variable-buffer-local 'org-todo-kwd-alist)
  1179. (defvar org-todo-key-alist nil)
  1180. (make-variable-buffer-local 'org-todo-key-alist)
  1181. (defvar org-todo-key-trigger nil)
  1182. (make-variable-buffer-local 'org-todo-key-trigger)
  1183. (defcustom org-todo-interpretation 'sequence
  1184. "Controls how TODO keywords are interpreted.
  1185. This variable is in principle obsolete and is only used for
  1186. backward compatibility, if the interpretation of todo keywords is
  1187. not given already in `org-todo-keywords'. See that variable for
  1188. more information."
  1189. :group 'org-todo
  1190. :group 'org-keywords
  1191. :type '(choice (const sequence)
  1192. (const type)))
  1193. (defcustom org-use-fast-todo-selection 'prefix
  1194. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1195. This variable describes if and under what circumstances the cycling
  1196. mechanism for TODO keywords will be replaced by a single-key, direct
  1197. selection scheme.
  1198. When nil, fast selection is never used.
  1199. When the symbol `prefix', it will be used when `org-todo' is called with
  1200. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1201. in an agenda buffer.
  1202. When t, fast selection is used by default. In this case, the prefix
  1203. argument forces cycling instead.
  1204. In all cases, the special interface is only used if access keys have actually
  1205. been assigned by the user, i.e. if keywords in the configuration are followed
  1206. by a letter in parenthesis, like TODO(t)."
  1207. :group 'org-todo
  1208. :type '(choice
  1209. (const :tag "Never" nil)
  1210. (const :tag "By default" t)
  1211. (const :tag "Only with C-u C-c C-t" prefix)))
  1212. (defcustom org-after-todo-state-change-hook nil
  1213. "Hook which is run after the state of a TODO item was changed.
  1214. The new state (a string with a TODO keyword, or nil) is available in the
  1215. Lisp variable `state'."
  1216. :group 'org-todo
  1217. :type 'hook)
  1218. (defcustom org-log-done nil
  1219. "Non-nil means, record a CLOSED timestamp when moving an entry to DONE.
  1220. When equal to the list (done), also prompt for a closing note.
  1221. This can also be configured on a per-file basis by adding one of
  1222. the following lines anywhere in the buffer:
  1223. #+STARTUP: logdone
  1224. #+STARTUP: lognotedone
  1225. #+STARTUP: nologdone"
  1226. :group 'org-todo
  1227. :group 'org-progress
  1228. :type '(choice
  1229. (const :tag "No logging" nil)
  1230. (const :tag "Record CLOSED timestamp" time)
  1231. (const :tag "Record CLOSED timestamp with closing note." note)))
  1232. ;; Normalize old uses of org-log-done.
  1233. (cond
  1234. ((eq org-log-done t) (setq org-log-done 'time))
  1235. ((and (listp org-log-done) (memq 'done org-log-done))
  1236. (setq org-log-done 'note)))
  1237. (defcustom org-log-note-clock-out nil
  1238. "Non-nil means, recored a note when clocking out of an item.
  1239. This can also be configured on a per-file basis by adding one of
  1240. the following lines anywhere in the buffer:
  1241. #+STARTUP: lognoteclock-out
  1242. #+STARTUP: nolognoteclock-out"
  1243. :group 'org-todo
  1244. :group 'org-progress
  1245. :type 'boolean)
  1246. (defcustom org-log-done-with-time t
  1247. "Non-nil means, the CLOSED time stamp will contain date and time.
  1248. When nil, only the date will be recorded."
  1249. :group 'org-progress
  1250. :type 'boolean)
  1251. (defcustom org-log-note-headings
  1252. '((done . "CLOSING NOTE %t")
  1253. (state . "State %-12s %t")
  1254. (note . "Note taken on %t")
  1255. (clock-out . ""))
  1256. "Headings for notes added to entries.
  1257. The value is an alist, with the car being a symbol indicating the note
  1258. context, and the cdr is the heading to be used. The heading may also be the
  1259. empty string.
  1260. %t in the heading will be replaced by a time stamp.
  1261. %s will be replaced by the new TODO state, in double quotes.
  1262. %u will be replaced by the user name.
  1263. %U will be replaced by the full user name."
  1264. :group 'org-todo
  1265. :group 'org-progress
  1266. :type '(list :greedy t
  1267. (cons (const :tag "Heading when closing an item" done) string)
  1268. (cons (const :tag
  1269. "Heading when changing todo state (todo sequence only)"
  1270. state) string)
  1271. (cons (const :tag "Heading when just taking a note" note) string)
  1272. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1273. (unless (assq 'note org-log-note-headings)
  1274. (push '(note . "%t") org-log-note-headings))
  1275. (defcustom org-log-states-order-reversed t
  1276. "Non-nil means, the latest state change note will be directly after heading.
  1277. When nil, the notes will be orderer according to time."
  1278. :group 'org-todo
  1279. :group 'org-progress
  1280. :type 'boolean)
  1281. (defcustom org-log-repeat 'time
  1282. "Non-nil means, record moving through the DONE state when triggering repeat.
  1283. An auto-repeating tasks is immediately switched back to TODO when marked
  1284. done. If you are not logging state changes (by adding \"@\" or \"!\" to
  1285. the TODO keyword definition, or recording a cloing note by setting
  1286. `org-log-done', there will be no record of the task moving trhough DONE.
  1287. This variable forces taking a note anyway. Possible values are:
  1288. nil Don't force a record
  1289. time Record a time stamp
  1290. note Record a note
  1291. This option can also be set with on a per-file-basis with
  1292. #+STARTUP: logrepeat
  1293. #+STARTUP: lognoterepeat
  1294. #+STARTUP: nologrepeat
  1295. You can have local logging settings for a subtree by setting the LOGGING
  1296. property to one or more of these keywords."
  1297. :group 'org-todo
  1298. :group 'org-progress
  1299. :type '(choice
  1300. (const :tag "Don't force a record" nil)
  1301. (const :tag "Force recording the DONE state" time)
  1302. (const :tag "Force recording a note with the DONE state" note)))
  1303. (defgroup org-priorities nil
  1304. "Priorities in Org-mode."
  1305. :tag "Org Priorities"
  1306. :group 'org-todo)
  1307. (defcustom org-highest-priority ?A
  1308. "The highest priority of TODO items. A character like ?A, ?B etc.
  1309. Must have a smaller ASCII number than `org-lowest-priority'."
  1310. :group 'org-priorities
  1311. :type 'character)
  1312. (defcustom org-lowest-priority ?C
  1313. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1314. Must have a larger ASCII number than `org-highest-priority'."
  1315. :group 'org-priorities
  1316. :type 'character)
  1317. (defcustom org-default-priority ?B
  1318. "The default priority of TODO items.
  1319. This is the priority an item get if no explicit priority is given."
  1320. :group 'org-priorities
  1321. :type 'character)
  1322. (defcustom org-priority-start-cycle-with-default t
  1323. "Non-nil means, start with default priority when starting to cycle.
  1324. When this is nil, the first step in the cycle will be (depending on the
  1325. command used) one higher or lower that the default priority."
  1326. :group 'org-priorities
  1327. :type 'boolean)
  1328. (defgroup org-time nil
  1329. "Options concerning time stamps and deadlines in Org-mode."
  1330. :tag "Org Time"
  1331. :group 'org)
  1332. (defcustom org-insert-labeled-timestamps-at-point nil
  1333. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1334. When nil, these labeled time stamps are forces into the second line of an
  1335. entry, just after the headline. When scheduling from the global TODO list,
  1336. the time stamp will always be forced into the second line."
  1337. :group 'org-time
  1338. :type 'boolean)
  1339. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1340. "Formats for `format-time-string' which are used for time stamps.
  1341. It is not recommended to change this constant.")
  1342. (defcustom org-time-stamp-rounding-minutes '(0 5)
  1343. "Number of minutes to round time stamps to.
  1344. These are two values, the first applies when first creating a time stamp.
  1345. The second applies when changing it with the commands `S-up' and `S-down'.
  1346. When changing the time stamp, this means that it will change in steps
  1347. of N minutes, as given by the second value.
  1348. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  1349. numbers should be factors of 60, so for example 5, 10, 15.
  1350. When this is larger than 1, you can still force an exact time-stamp by using
  1351. a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
  1352. and by using a prefix arg to `S-up/down' to specify the exact number
  1353. of minutes to shift."
  1354. :group 'org-time
  1355. :get '(lambda (var) ; Make sure all entries have 5 elements
  1356. (if (integerp (default-value var))
  1357. (list (default-value var) 5)
  1358. (default-value var)))
  1359. :type '(list
  1360. (integer :tag "when inserting times")
  1361. (integer :tag "when modifying times")))
  1362. ;; Normalize old customizations of this variable.
  1363. (when (integerp org-time-stamp-rounding-minutes)
  1364. (setq org-time-stamp-rounding-minutes
  1365. (list org-time-stamp-rounding-minutes
  1366. org-time-stamp-rounding-minutes)))
  1367. (defcustom org-display-custom-times nil
  1368. "Non-nil means, overlay custom formats over all time stamps.
  1369. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1370. To turn this on on a per-file basis, insert anywhere in the file:
  1371. #+STARTUP: customtime"
  1372. :group 'org-time
  1373. :set 'set-default
  1374. :type 'sexp)
  1375. (make-variable-buffer-local 'org-display-custom-times)
  1376. (defcustom org-time-stamp-custom-formats
  1377. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1378. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1379. These are overlayed over the default ISO format if the variable
  1380. `org-display-custom-times' is set. Time like %H:%M should be at the
  1381. end of the second format."
  1382. :group 'org-time
  1383. :type 'sexp)
  1384. (defun org-time-stamp-format (&optional long inactive)
  1385. "Get the right format for a time string."
  1386. (let ((f (if long (cdr org-time-stamp-formats)
  1387. (car org-time-stamp-formats))))
  1388. (if inactive
  1389. (concat "[" (substring f 1 -1) "]")
  1390. f)))
  1391. (defcustom org-deadline-warning-days 14
  1392. "No. of days before expiration during which a deadline becomes active.
  1393. This variable governs the display in sparse trees and in the agenda.
  1394. When 0 or negative, it means use this number (the absolute value of it)
  1395. even if a deadline has a different individual lead time specified."
  1396. :group 'org-time
  1397. :group 'org-agenda-daily/weekly
  1398. :type 'number)
  1399. (defcustom org-read-date-prefer-future t
  1400. "Non-nil means, assume future for incomplete date input from user.
  1401. This affects the following situations:
  1402. 1. The user gives a day, but no month.
  1403. For example, if today is the 15th, and you enter \"3\", Org-mode will
  1404. read this as the third of *next* month. However, if you enter \"17\",
  1405. it will be considered as *this* month.
  1406. 2. The user gives a month but not a year.
  1407. For example, if it is april and you enter \"feb 2\", this will be read
  1408. as feb 2, *next* year. \"May 5\", however, will be this year.
  1409. Currently this does not work for ISO week specifications.
  1410. When this option is nil, the current month and year will always be used
  1411. as defaults."
  1412. :group 'org-time
  1413. :type 'boolean)
  1414. (defcustom org-read-date-display-live t
  1415. "Non-nil means, display current interpretation of date prompt live.
  1416. This display will be in an overlay, in the minibuffer."
  1417. :group 'org-time
  1418. :type 'boolean)
  1419. (defcustom org-read-date-popup-calendar t
  1420. "Non-nil means, pop up a calendar when prompting for a date.
  1421. In the calendar, the date can be selected with mouse-1. However, the
  1422. minibuffer will also be active, and you can simply enter the date as well.
  1423. When nil, only the minibuffer will be available."
  1424. :group 'org-time
  1425. :type 'boolean)
  1426. (if (fboundp 'defvaralias)
  1427. (defvaralias 'org-popup-calendar-for-date-prompt
  1428. 'org-read-date-popup-calendar))
  1429. (defcustom org-extend-today-until 0
  1430. "The hour when your day really ends.
  1431. This has influence for the following applications:
  1432. - When switching the agenda to \"today\". It it is still earlier than
  1433. the time given here, the day recognized as TODAY is actually yesterday.
  1434. - When a date is read from the user and it is still before the time given
  1435. here, the current date and time will be assumed to be yesterday, 23:59.
  1436. FIXME:
  1437. IMPORTANT: This is still a very experimental feature, it may disappear
  1438. again or it may be extended to mean more things."
  1439. :group 'org-time
  1440. :type 'number)
  1441. (defcustom org-edit-timestamp-down-means-later nil
  1442. "Non-nil means, S-down will increase the time in a time stamp.
  1443. When nil, S-up will increase."
  1444. :group 'org-time
  1445. :type 'boolean)
  1446. (defcustom org-calendar-follow-timestamp-change t
  1447. "Non-nil means, make the calendar window follow timestamp changes.
  1448. When a timestamp is modified and the calendar window is visible, it will be
  1449. moved to the new date."
  1450. :group 'org-time
  1451. :type 'boolean)
  1452. (defgroup org-tags nil
  1453. "Options concerning tags in Org-mode."
  1454. :tag "Org Tags"
  1455. :group 'org)
  1456. (defcustom org-tag-alist nil
  1457. "List of tags allowed in Org-mode files.
  1458. When this list is nil, Org-mode will base TAG input on what is already in the
  1459. buffer.
  1460. The value of this variable is an alist, the car of each entry must be a
  1461. keyword as a string, the cdr may be a character that is used to select
  1462. that tag through the fast-tag-selection interface.
  1463. See the manual for details."
  1464. :group 'org-tags
  1465. :type '(repeat
  1466. (choice
  1467. (cons (string :tag "Tag name")
  1468. (character :tag "Access char"))
  1469. (const :tag "Start radio group" (:startgroup))
  1470. (const :tag "End radio group" (:endgroup)))))
  1471. (defcustom org-use-fast-tag-selection 'auto
  1472. "Non-nil means, use fast tag selection scheme.
  1473. This is a special interface to select and deselect tags with single keys.
  1474. When nil, fast selection is never used.
  1475. When the symbol `auto', fast selection is used if and only if selection
  1476. characters for tags have been configured, either through the variable
  1477. `org-tag-alist' or through a #+TAGS line in the buffer.
  1478. When t, fast selection is always used and selection keys are assigned
  1479. automatically if necessary."
  1480. :group 'org-tags
  1481. :type '(choice
  1482. (const :tag "Always" t)
  1483. (const :tag "Never" nil)
  1484. (const :tag "When selection characters are configured" 'auto)))
  1485. (defcustom org-fast-tag-selection-single-key nil
  1486. "Non-nil means, fast tag selection exits after first change.
  1487. When nil, you have to press RET to exit it.
  1488. During fast tag selection, you can toggle this flag with `C-c'.
  1489. This variable can also have the value `expert'. In this case, the window
  1490. displaying the tags menu is not even shown, until you press C-c again."
  1491. :group 'org-tags
  1492. :type '(choice
  1493. (const :tag "No" nil)
  1494. (const :tag "Yes" t)
  1495. (const :tag "Expert" expert)))
  1496. (defvar org-fast-tag-selection-include-todo nil
  1497. "Non-nil means, fast tags selection interface will also offer TODO states.
  1498. This is an undocumented feature, you should not rely on it.")
  1499. (defcustom org-tags-column (if (featurep 'xemacs) -79 -80)
  1500. "The column to which tags should be indented in a headline.
  1501. If this number is positive, it specifies the column. If it is negative,
  1502. it means that the tags should be flushright to that column. For example,
  1503. -80 works well for a normal 80 character screen."
  1504. :group 'org-tags
  1505. :type 'integer)
  1506. (defcustom org-auto-align-tags t
  1507. "Non-nil means, realign tags after pro/demotion of TODO state change.
  1508. These operations change the length of a headline and therefore shift
  1509. the tags around. With this options turned on, after each such operation
  1510. the tags are again aligned to `org-tags-column'."
  1511. :group 'org-tags
  1512. :type 'boolean)
  1513. (defcustom org-use-tag-inheritance t
  1514. "Non-nil means, tags in levels apply also for sublevels.
  1515. When nil, only the tags directly given in a specific line apply there.
  1516. If you turn off this option, you very likely want to turn on the
  1517. companion option `org-tags-match-list-sublevels'.
  1518. This may also be a list of tags that should be inherited, or a regexp that
  1519. matches tags that should be inherited."
  1520. :group 'org-tags
  1521. :type '(choice
  1522. (const :tag "Not" nil)
  1523. (const :tag "Always" t)
  1524. (repeat :tag "Specific tags" (string :tag "Tag"))
  1525. (regexp :tag "Tags matched by regexp")))
  1526. (defun org-tag-inherit-p (tag)
  1527. "Check if TAG is one that should be inherited."
  1528. (cond
  1529. ((eq org-use-tag-inheritance t) t)
  1530. ((not org-use-tag-inheritance) nil)
  1531. ((stringp org-use-tag-inheritance)
  1532. (string-match org-use-tag-inheritance tag))
  1533. ((listp org-use-tag-inheritance)
  1534. (member tag org-use-tag-inheritance))
  1535. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  1536. (defcustom org-tags-match-list-sublevels nil
  1537. "Non-nil means list also sublevels of headlines matching tag search.
  1538. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  1539. the sublevels of a headline matching a tag search often also match
  1540. the same search. Listing all of them can create very long lists.
  1541. Setting this variable to nil causes subtrees of a match to be skipped.
  1542. This option is off by default, because inheritance in on. If you turn
  1543. inheritance off, you very likely want to turn this option on.
  1544. As a special case, if the tag search is restricted to TODO items, the
  1545. value of this variable is ignored and sublevels are always checked, to
  1546. make sure all corresponding TODO items find their way into the list."
  1547. :group 'org-tags
  1548. :type 'boolean)
  1549. (defvar org-tags-history nil
  1550. "History of minibuffer reads for tags.")
  1551. (defvar org-last-tags-completion-table nil
  1552. "The last used completion table for tags.")
  1553. (defvar org-after-tags-change-hook nil
  1554. "Hook that is run after the tags in a line have changed.")
  1555. (defgroup org-properties nil
  1556. "Options concerning properties in Org-mode."
  1557. :tag "Org Properties"
  1558. :group 'org)
  1559. (defcustom org-property-format "%-10s %s"
  1560. "How property key/value pairs should be formatted by `indent-line'.
  1561. When `indent-line' hits a property definition, it will format the line
  1562. according to this format, mainly to make sure that the values are
  1563. lined-up with respect to each other."
  1564. :group 'org-properties
  1565. :type 'string)
  1566. (defcustom org-use-property-inheritance nil
  1567. "Non-nil means, properties apply also for sublevels.
  1568. This setting is chiefly used during property searches. Turning it on can
  1569. cause significant overhead when doing a search, which is why it is not
  1570. on by default.
  1571. When nil, only the properties directly given in the current entry count.
  1572. When t, every property is inherited. The value may also be a list of
  1573. properties that should have inheritance, or a regular expression matching
  1574. properties that should be inherited.
  1575. However, note that some special properties use inheritance under special
  1576. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  1577. and the properties ending in \"_ALL\" when they are used as descriptor
  1578. for valid values of a property.
  1579. Note for programmers:
  1580. When querying an entry with `org-entry-get', you can control if inheritance
  1581. should be used. By default, `org-entry-get' looks only at the local
  1582. properties. You can request inheritance by setting the inherit argument
  1583. to t (to force inheritance) or to `selective' (to respect the setting
  1584. in this variable)."
  1585. :group 'org-properties
  1586. :type '(choice
  1587. (const :tag "Not" nil)
  1588. (const :tag "Always" t)
  1589. (repeat :tag "Specific properties" (string :tag "Property"))
  1590. (regexp :tag "Properties matched by regexp")))
  1591. (defun org-property-inherit-p (property)
  1592. "Check if PROPERTY is one that should be inherited."
  1593. (cond
  1594. ((eq org-use-property-inheritance t) t)
  1595. ((not org-use-property-inheritance) nil)
  1596. ((stringp org-use-property-inheritance)
  1597. (string-match org-use-property-inheritance property))
  1598. ((listp org-use-property-inheritance)
  1599. (member property org-use-property-inheritance))
  1600. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  1601. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  1602. "The default column format, if no other format has been defined.
  1603. This variable can be set on the per-file basis by inserting a line
  1604. #+COLUMNS: %25ITEM ....."
  1605. :group 'org-properties
  1606. :type 'string)
  1607. (defcustom org-effort-property "Effort"
  1608. "The property that is being used to keep track of effort estimates.
  1609. Effort estimates given in this property need to have the format H:MM."
  1610. :group 'org-properties
  1611. :group 'org-progress
  1612. :type '(string :tag "Property"))
  1613. (defcustom org-global-properties nil
  1614. "List of property/value pairs that can be inherited by any entry.
  1615. You can set buffer-local values for this by adding lines like
  1616. #+PROPERTY: NAME VALUE"
  1617. :group 'org-properties
  1618. :type '(repeat
  1619. (cons (string :tag "Property")
  1620. (string :tag "Value"))))
  1621. (defvar org-local-properties nil
  1622. "List of property/value pairs that can be inherited by any entry.
  1623. Valid for the current buffer.
  1624. This variable is populated from #+PROPERTY lines.")
  1625. (defgroup org-agenda nil
  1626. "Options concerning agenda views in Org-mode."
  1627. :tag "Org Agenda"
  1628. :group 'org)
  1629. (defvar org-category nil
  1630. "Variable used by org files to set a category for agenda display.
  1631. Such files should use a file variable to set it, for example
  1632. # -*- mode: org; org-category: \"ELisp\"
  1633. or contain a special line
  1634. #+CATEGORY: ELisp
  1635. If the file does not specify a category, then file's base name
  1636. is used instead.")
  1637. (make-variable-buffer-local 'org-category)
  1638. (defcustom org-agenda-files nil
  1639. "The files to be used for agenda display.
  1640. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  1641. \\[org-remove-file]. You can also use customize to edit the list.
  1642. If an entry is a directory, all files in that directory that are matched by
  1643. `org-agenda-file-regexp' will be part of the file list.
  1644. If the value of the variable is not a list but a single file name, then
  1645. the list of agenda files is actually stored and maintained in that file, one
  1646. agenda file per line."
  1647. :group 'org-agenda
  1648. :type '(choice
  1649. (repeat :tag "List of files and directories" file)
  1650. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  1651. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  1652. "Regular expression to match files for `org-agenda-files'.
  1653. If any element in the list in that variable contains a directory instead
  1654. of a normal file, all files in that directory that are matched by this
  1655. regular expression will be included."
  1656. :group 'org-agenda
  1657. :type 'regexp)
  1658. (defcustom org-agenda-text-search-extra-files nil
  1659. "List of extra files to be searched by text search commands.
  1660. These files will be search in addition to the agenda files by the
  1661. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  1662. Note that these files will only be searched for text search commands,
  1663. not for the other agenda views like todo lists, tag searches or the weekly
  1664. agenda. This variable is intended to list notes and possibly archive files
  1665. that should also be searched by these two commands.
  1666. In fact, if the first element in the list is the symbol `agenda-archives',
  1667. than all archive files of all agenda files will be added to the search
  1668. scope."
  1669. :group 'org-agenda
  1670. :type '(set :greedy t
  1671. (const :tag "Agenda Archives" agenda-archives)
  1672. (repeat :inline t (file))))
  1673. (if (fboundp 'defvaralias)
  1674. (defvaralias 'org-agenda-multi-occur-extra-files
  1675. 'org-agenda-text-search-extra-files))
  1676. (defcustom org-agenda-skip-unavailable-files nil
  1677. "t means to just skip non-reachable files in `org-agenda-files'.
  1678. Nil means to remove them, after a query, from the list."
  1679. :group 'org-agenda
  1680. :type 'boolean)
  1681. (defcustom org-calendar-to-agenda-key [?c]
  1682. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  1683. The command `org-calendar-goto-agenda' will be bound to this key. The
  1684. default is the character `c' because then `c' can be used to switch back and
  1685. forth between agenda and calendar."
  1686. :group 'org-agenda
  1687. :type 'sexp)
  1688. (eval-after-load "calendar"
  1689. '(org-defkey calendar-mode-map org-calendar-to-agenda-key
  1690. 'org-calendar-goto-agenda))
  1691. (defgroup org-latex nil
  1692. "Options for embedding LaTeX code into Org-mode."
  1693. :tag "Org LaTeX"
  1694. :group 'org)
  1695. (defcustom org-format-latex-options
  1696. '(:foreground default :background default :scale 1.0
  1697. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  1698. :matchers ("begin" "$" "$$" "\\(" "\\["))
  1699. "Options for creating images from LaTeX fragments.
  1700. This is a property list with the following properties:
  1701. :foreground the foreground color for images embedded in emacs, e.g. \"Black\".
  1702. `default' means use the forground of the default face.
  1703. :background the background color, or \"Transparent\".
  1704. `default' means use the background of the default face.
  1705. :scale a scaling factor for the size of the images
  1706. :html-foreground, :html-background, :html-scale
  1707. The same numbers for HTML export.
  1708. :matchers a list indicating which matchers should be used to
  1709. find LaTeX fragments. Valid members of this list are:
  1710. \"begin\" find environments
  1711. \"$\" find math expressions surrounded by $...$
  1712. \"$$\" find math expressions surrounded by $$....$$
  1713. \"\\(\" find math expressions surrounded by \\(...\\)
  1714. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  1715. :group 'org-latex
  1716. :type 'plist)
  1717. (defcustom org-format-latex-header "\\documentclass{article}
  1718. \\usepackage{fullpage} % do not remove
  1719. \\usepackage{amssymb}
  1720. \\usepackage[usenames]{color}
  1721. \\usepackage{amsmath}
  1722. \\usepackage{latexsym}
  1723. \\usepackage[mathscr]{eucal}
  1724. \\pagestyle{empty} % do not remove"
  1725. "The document header used for processing LaTeX fragments."
  1726. :group 'org-latex
  1727. :type 'string)
  1728. (defgroup org-font-lock nil
  1729. "Font-lock settings for highlighting in Org-mode."
  1730. :tag "Org Font Lock"
  1731. :group 'org)
  1732. (defcustom org-level-color-stars-only nil
  1733. "Non-nil means fontify only the stars in each headline.
  1734. When nil, the entire headline is fontified.
  1735. Changing it requires restart of `font-lock-mode' to become effective
  1736. also in regions already fontified."
  1737. :group 'org-font-lock
  1738. :type 'boolean)
  1739. (defcustom org-hide-leading-stars nil
  1740. "Non-nil means, hide the first N-1 stars in a headline.
  1741. This works by using the face `org-hide' for these stars. This
  1742. face is white for a light background, and black for a dark
  1743. background. You may have to customize the face `org-hide' to
  1744. make this work.
  1745. Changing it requires restart of `font-lock-mode' to become effective
  1746. also in regions already fontified.
  1747. You may also set this on a per-file basis by adding one of the following
  1748. lines to the buffer:
  1749. #+STARTUP: hidestars
  1750. #+STARTUP: showstars"
  1751. :group 'org-font-lock
  1752. :type 'boolean)
  1753. (defcustom org-fontify-done-headline nil
  1754. "Non-nil means, change the face of a headline if it is marked DONE.
  1755. Normally, only the TODO/DONE keyword indicates the state of a headline.
  1756. When this is non-nil, the headline after the keyword is set to the
  1757. `org-headline-done' as an additional indication."
  1758. :group 'org-font-lock
  1759. :type 'boolean)
  1760. (defcustom org-fontify-emphasized-text t
  1761. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  1762. Changing this variable requires a restart of Emacs to take effect."
  1763. :group 'org-font-lock
  1764. :type 'boolean)
  1765. (defcustom org-highlight-latex-fragments-and-specials nil
  1766. "Non-nil means, fontify what is treated specially by the exporters."
  1767. :group 'org-font-lock
  1768. :type 'boolean)
  1769. (defcustom org-hide-emphasis-markers nil
  1770. "Non-nil mean font-lock should hide the emphasis marker characters."
  1771. :group 'org-font-lock
  1772. :type 'boolean)
  1773. (defvar org-emph-re nil
  1774. "Regular expression for matching emphasis.")
  1775. (defvar org-verbatim-re nil
  1776. "Regular expression for matching verbatim text.")
  1777. (defvar org-emphasis-regexp-components) ; defined just below
  1778. (defvar org-emphasis-alist) ; defined just below
  1779. (defun org-set-emph-re (var val)
  1780. "Set variable and compute the emphasis regular expression."
  1781. (set var val)
  1782. (when (and (boundp 'org-emphasis-alist)
  1783. (boundp 'org-emphasis-regexp-components)
  1784. org-emphasis-alist org-emphasis-regexp-components)
  1785. (let* ((e org-emphasis-regexp-components)
  1786. (pre (car e))
  1787. (post (nth 1 e))
  1788. (border (nth 2 e))
  1789. (body (nth 3 e))
  1790. (nl (nth 4 e))
  1791. (stacked (and nil (nth 5 e))) ; stacked is no longer allowed, forced to nil
  1792. (body1 (concat body "*?"))
  1793. (markers (mapconcat 'car org-emphasis-alist ""))
  1794. (vmarkers (mapconcat
  1795. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  1796. org-emphasis-alist "")))
  1797. ;; make sure special characters appear at the right position in the class
  1798. (if (string-match "\\^" markers)
  1799. (setq markers (concat (replace-match "" t t markers) "^")))
  1800. (if (string-match "-" markers)
  1801. (setq markers (concat (replace-match "" t t markers) "-")))
  1802. (if (string-match "\\^" vmarkers)
  1803. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  1804. (if (string-match "-" vmarkers)
  1805. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  1806. (if (> nl 0)
  1807. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  1808. (int-to-string nl) "\\}")))
  1809. ;; Make the regexp
  1810. (setq org-emph-re
  1811. (concat "\\([" pre (if (and nil stacked) markers) "]\\|^\\)"
  1812. "\\("
  1813. "\\([" markers "]\\)"
  1814. "\\("
  1815. "[^" border "]\\|"
  1816. "[^" border (if (and nil stacked) markers) "]"
  1817. body1
  1818. "[^" border (if (and nil stacked) markers) "]"
  1819. "\\)"
  1820. "\\3\\)"
  1821. "\\([" post (if (and nil stacked) markers) "]\\|$\\)"))
  1822. (setq org-verbatim-re
  1823. (concat "\\([" pre "]\\|^\\)"
  1824. "\\("
  1825. "\\([" vmarkers "]\\)"
  1826. "\\("
  1827. "[^" border "]\\|"
  1828. "[^" border "]"
  1829. body1
  1830. "[^" border "]"
  1831. "\\)"
  1832. "\\3\\)"
  1833. "\\([" post "]\\|$\\)")))))
  1834. (defcustom org-emphasis-regexp-components
  1835. '(" \t('\"" "- \t.,:?;'\")" " \t\r\n,\"'" "." 1)
  1836. "Components used to build the regular expression for emphasis.
  1837. This is a list with 6 entries. Terminology: In an emphasis string
  1838. like \" *strong word* \", we call the initial space PREMATCH, the final
  1839. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  1840. and \"trong wor\" is the body. The different components in this variable
  1841. specify what is allowed/forbidden in each part:
  1842. pre Chars allowed as prematch. Beginning of line will be allowed too.
  1843. post Chars allowed as postmatch. End of line will be allowed too.
  1844. border The chars *forbidden* as border characters.
  1845. body-regexp A regexp like \".\" to match a body character. Don't use
  1846. non-shy groups here, and don't allow newline here.
  1847. newline The maximum number of newlines allowed in an emphasis exp.
  1848. Use customize to modify this, or restart Emacs after changing it."
  1849. :group 'org-font-lock
  1850. :set 'org-set-emph-re
  1851. :type '(list
  1852. (sexp :tag "Allowed chars in pre ")
  1853. (sexp :tag "Allowed chars in post ")
  1854. (sexp :tag "Forbidden chars in border ")
  1855. (sexp :tag "Regexp for body ")
  1856. (integer :tag "number of newlines allowed")
  1857. (option (boolean :tag "Stacking (DISABLED) "))))
  1858. (defcustom org-emphasis-alist
  1859. `(("*" bold "<b>" "</b>")
  1860. ("/" italic "<i>" "</i>")
  1861. ("_" underline "<u>" "</u>")
  1862. ("=" org-code "<code>" "</code>" verbatim)
  1863. ("~" org-verbatim "" "" verbatim)
  1864. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  1865. "<del>" "</del>")
  1866. )
  1867. "Special syntax for emphasized text.
  1868. Text starting and ending with a special character will be emphasized, for
  1869. example *bold*, _underlined_ and /italic/. This variable sets the marker
  1870. characters, the face to be used by font-lock for highlighting in Org-mode
  1871. Emacs buffers, and the HTML tags to be used for this.
  1872. Use customize to modify this, or restart Emacs after changing it."
  1873. :group 'org-font-lock
  1874. :set 'org-set-emph-re
  1875. :type '(repeat
  1876. (list
  1877. (string :tag "Marker character")
  1878. (choice
  1879. (face :tag "Font-lock-face")
  1880. (plist :tag "Face property list"))
  1881. (string :tag "HTML start tag")
  1882. (string :tag "HTML end tag")
  1883. (option (const verbatim)))))
  1884. ;;; Miscellaneous options
  1885. (defgroup org-completion nil
  1886. "Completion in Org-mode."
  1887. :tag "Org Completion"
  1888. :group 'org)
  1889. (defcustom org-completion-fallback-command 'hippie-expand
  1890. "The expansion command called by \\[org-complete] in normal context.
  1891. Normal means, no org-mode-specific context."
  1892. :group 'org-completion
  1893. :type 'function)
  1894. ;;; Functions and variables from ther packages
  1895. ;; Declared here to avoid compiler warnings
  1896. ;; XEmacs only
  1897. (defvar outline-mode-menu-heading)
  1898. (defvar outline-mode-menu-show)
  1899. (defvar outline-mode-menu-hide)
  1900. (defvar zmacs-regions) ; XEmacs regions
  1901. ;; Emacs only
  1902. (defvar mark-active)
  1903. ;; Various packages
  1904. (declare-function calendar-absolute-from-iso "cal-iso" (&optional date))
  1905. (declare-function calendar-forward-day "cal-move" (arg))
  1906. (declare-function calendar-goto-date "cal-move" (date))
  1907. (declare-function calendar-goto-today "cal-move" ())
  1908. (declare-function calendar-iso-from-absolute "cal-iso" (&optional date))
  1909. (defvar calc-embedded-close-formula)
  1910. (defvar calc-embedded-open-formula)
  1911. (declare-function cdlatex-tab "ext:cdlatex" ())
  1912. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  1913. (defvar font-lock-unfontify-region-function)
  1914. (declare-function iswitchb-mode "iswitchb" (&optional arg))
  1915. (declare-function iswitchb-read-buffer (prompt &optional default require-match start matches-set))
  1916. (defvar iswitchb-temp-buflist)
  1917. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  1918. (declare-function org-agenda-skip "org-agenda" ())
  1919. (declare-function org-format-agenda-item "org-agenda"
  1920. (extra txt &optional category tags dotime noprefix remove-re))
  1921. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  1922. (declare-function org-agenda-change-all-lines "org-agenda"
  1923. (newhead hdmarker &optional fixface))
  1924. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  1925. (declare-function org-agenda-maybe-redo "org-agenda" ())
  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. (defvar org-clock-marker (make-marker)
  2071. "Marker recording the last clock-in.")
  2072. (eval-and-compile
  2073. (org-autoload
  2074. "org-clock"
  2075. '(org-clock-in org-clock-out org-clock-cancel
  2076. org-clock-goto org-clock-sum org-clock-display
  2077. org-remove-clock-overlays org-clock-report
  2078. org-clocktable-shift org-dblock-write:clocktable
  2079. org-get-clocktable)))
  2080. (defun org-clock-update-time-maybe ()
  2081. "If this is a CLOCK line, update it and return t.
  2082. Otherwise, return nil."
  2083. (interactive)
  2084. (save-excursion
  2085. (beginning-of-line 1)
  2086. (skip-chars-forward " \t")
  2087. (when (looking-at org-clock-string)
  2088. (let ((re (concat "[ \t]*" org-clock-string
  2089. " *[[<]\\([^]>]+\\)[]>]-+[[<]\\([^]>]+\\)[]>]"
  2090. "\\([ \t]*=>.*\\)?"))
  2091. ts te h m s)
  2092. (if (not (looking-at re))
  2093. nil
  2094. (and (match-end 3) (delete-region (match-beginning 3) (match-end 3)))
  2095. (end-of-line 1)
  2096. (setq ts (match-string 1)
  2097. te (match-string 2))
  2098. (setq s (- (time-to-seconds
  2099. (apply 'encode-time (org-parse-time-string te)))
  2100. (time-to-seconds
  2101. (apply 'encode-time (org-parse-time-string ts))))
  2102. h (floor (/ s 3600))
  2103. s (- s (* 3600 h))
  2104. m (floor (/ s 60))
  2105. s (- s (* 60 s)))
  2106. (insert " => " (format "%2d:%02d" h m))
  2107. t)))))
  2108. (defun org-check-running-clock ()
  2109. "Check if the current buffer contains the running clock.
  2110. If yes, offer to stop it and to save the buffer with the changes."
  2111. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2112. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  2113. (buffer-name))))
  2114. (org-clock-out)
  2115. (when (y-or-n-p "Save changed buffer?")
  2116. (save-buffer))))
  2117. (defun org-clocktable-try-shift (dir n)
  2118. "Check if this line starts a clock table, if yes, shift the time block."
  2119. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  2120. (org-clocktable-shift dir n)))
  2121. ;; Autoload archiving code
  2122. ;; The stuff that is needed for cycling and tags has to be defined here.
  2123. (defgroup org-archive nil
  2124. "Options concerning archiving in Org-mode."
  2125. :tag "Org Archive"
  2126. :group 'org-structure)
  2127. (defcustom org-archive-location "%s_archive::"
  2128. "The location where subtrees should be archived.
  2129. Otherwise, the value of this variable is a string, consisting of two
  2130. parts, separated by a double-colon.
  2131. The first part is a file name - when omitted, archiving happens in the same
  2132. file. %s will be replaced by the current file name (without directory part).
  2133. Archiving to a different file is useful to keep archived entries from
  2134. contributing to the Org-mode Agenda.
  2135. The part after the double colon is a headline. The archived entries will be
  2136. filed under that headline. When omitted, the subtrees are simply filed away
  2137. at the end of the file, as top-level entries.
  2138. Here are a few examples:
  2139. \"%s_archive::\"
  2140. If the current file is Projects.org, archive in file
  2141. Projects.org_archive, as top-level trees. This is the default.
  2142. \"::* Archived Tasks\"
  2143. Archive in the current file, under the top-level headline
  2144. \"* Archived Tasks\".
  2145. \"~/org/archive.org::\"
  2146. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  2147. \"basement::** Finished Tasks\"
  2148. Archive in file ./basement (relative path), as level 3 trees
  2149. below the level 2 heading \"** Finished Tasks\".
  2150. You may set this option on a per-file basis by adding to the buffer a
  2151. line like
  2152. #+ARCHIVE: basement::** Finished Tasks
  2153. You may also define it locally for a subtree by setting an ARCHIVE property
  2154. in the entry. If such a property is found in an entry, or anywhere up
  2155. the hierarchy, it will be used."
  2156. :group 'org-archive
  2157. :type 'string)
  2158. (defcustom org-archive-tag "ARCHIVE"
  2159. "The tag that marks a subtree as archived.
  2160. An archived subtree does not open during visibility cycling, and does
  2161. not contribute to the agenda listings.
  2162. After changing this, font-lock must be restarted in the relevant buffers to
  2163. get the proper fontification."
  2164. :group 'org-archive
  2165. :group 'org-keywords
  2166. :type 'string)
  2167. (defcustom org-agenda-skip-archived-trees t
  2168. "Non-nil means, the agenda will skip any items located in archived trees.
  2169. An archived tree is a tree marked with the tag ARCHIVE."
  2170. :group 'org-archive
  2171. :group 'org-agenda-skip
  2172. :type 'boolean)
  2173. (defcustom org-cycle-open-archived-trees nil
  2174. "Non-nil means, `org-cycle' will open archived trees.
  2175. An archived tree is a tree marked with the tag ARCHIVE.
  2176. When nil, archived trees will stay folded. You can still open them with
  2177. normal outline commands like `show-all', but not with the cycling commands."
  2178. :group 'org-archive
  2179. :group 'org-cycle
  2180. :type 'boolean)
  2181. (defcustom org-sparse-tree-open-archived-trees nil
  2182. "Non-nil means sparse tree construction shows matches in archived trees.
  2183. When nil, matches in these trees are highlighted, but the trees are kept in
  2184. collapsed state."
  2185. :group 'org-archive
  2186. :group 'org-sparse-trees
  2187. :type 'boolean)
  2188. (defun org-cycle-hide-archived-subtrees (state)
  2189. "Re-hide all archived subtrees after a visibility state change."
  2190. (when (and (not org-cycle-open-archived-trees)
  2191. (not (memq state '(overview folded))))
  2192. (save-excursion
  2193. (let* ((globalp (memq state '(contents all)))
  2194. (beg (if globalp (point-min) (point)))
  2195. (end (if globalp (point-max) (org-end-of-subtree t))))
  2196. (org-hide-archived-subtrees beg end)
  2197. (goto-char beg)
  2198. (if (looking-at (concat ".*:" org-archive-tag ":"))
  2199. (message "%s" (substitute-command-keys
  2200. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  2201. (defun org-force-cycle-archived ()
  2202. "Cycle subtree even if it is archived."
  2203. (interactive)
  2204. (setq this-command 'org-cycle)
  2205. (let ((org-cycle-open-archived-trees t))
  2206. (call-interactively 'org-cycle)))
  2207. (defun org-hide-archived-subtrees (beg end)
  2208. "Re-hide all archived subtrees after a visibility state change."
  2209. (save-excursion
  2210. (let* ((re (concat ":" org-archive-tag ":")))
  2211. (goto-char beg)
  2212. (while (re-search-forward re end t)
  2213. (and (org-on-heading-p) (hide-subtree))
  2214. (org-end-of-subtree t)))))
  2215. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  2216. (eval-and-compile
  2217. (org-autoload "org-archive"
  2218. '(org-add-archive-files org-archive-subtree
  2219. org-archive-to-archive-sibling org-toggle-archive-tag)))
  2220. ;; Autoload Column View Code
  2221. (declare-function org-columns-number-to-string "org-colview")
  2222. (declare-function org-columns-get-format-and-top-level "org-colview")
  2223. (declare-function org-columns-compute "org-colview")
  2224. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  2225. '(org-columns-number-to-string org-columns-get-format-and-top-level
  2226. org-columns-compute org-agenda-columns org-columns-remove-overlays
  2227. org-columns org-insert-columns-dblock))
  2228. ;;; Variables for pre-computed regular expressions, all buffer local
  2229. (defvar org-drawer-regexp nil
  2230. "Matches first line of a hidden block.")
  2231. (make-variable-buffer-local 'org-drawer-regexp)
  2232. (defvar org-todo-regexp nil
  2233. "Matches any of the TODO state keywords.")
  2234. (make-variable-buffer-local 'org-todo-regexp)
  2235. (defvar org-not-done-regexp nil
  2236. "Matches any of the TODO state keywords except the last one.")
  2237. (make-variable-buffer-local 'org-not-done-regexp)
  2238. (defvar org-todo-line-regexp nil
  2239. "Matches a headline and puts TODO state into group 2 if present.")
  2240. (make-variable-buffer-local 'org-todo-line-regexp)
  2241. (defvar org-complex-heading-regexp nil
  2242. "Matches a headline and puts everything into groups:
  2243. group 1: the stars
  2244. group 2: The todo keyword, maybe
  2245. group 3: Priority cookie
  2246. group 4: True headline
  2247. group 5: Tags")
  2248. (make-variable-buffer-local 'org-complex-heading-regexp)
  2249. (defvar org-todo-line-tags-regexp nil
  2250. "Matches a headline and puts TODO state into group 2 if present.
  2251. Also put tags into group 4 if tags are present.")
  2252. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  2253. (defvar org-nl-done-regexp nil
  2254. "Matches newline followed by a headline with the DONE keyword.")
  2255. (make-variable-buffer-local 'org-nl-done-regexp)
  2256. (defvar org-looking-at-done-regexp nil
  2257. "Matches the DONE keyword a point.")
  2258. (make-variable-buffer-local 'org-looking-at-done-regexp)
  2259. (defvar org-ds-keyword-length 12
  2260. "Maximum length of the Deadline and SCHEDULED keywords.")
  2261. (make-variable-buffer-local 'org-ds-keyword-length)
  2262. (defvar org-deadline-regexp nil
  2263. "Matches the DEADLINE keyword.")
  2264. (make-variable-buffer-local 'org-deadline-regexp)
  2265. (defvar org-deadline-time-regexp nil
  2266. "Matches the DEADLINE keyword together with a time stamp.")
  2267. (make-variable-buffer-local 'org-deadline-time-regexp)
  2268. (defvar org-deadline-line-regexp nil
  2269. "Matches the DEADLINE keyword and the rest of the line.")
  2270. (make-variable-buffer-local 'org-deadline-line-regexp)
  2271. (defvar org-scheduled-regexp nil
  2272. "Matches the SCHEDULED keyword.")
  2273. (make-variable-buffer-local 'org-scheduled-regexp)
  2274. (defvar org-scheduled-time-regexp nil
  2275. "Matches the SCHEDULED keyword together with a time stamp.")
  2276. (make-variable-buffer-local 'org-scheduled-time-regexp)
  2277. (defvar org-closed-time-regexp nil
  2278. "Matches the CLOSED keyword together with a time stamp.")
  2279. (make-variable-buffer-local 'org-closed-time-regexp)
  2280. (defvar org-keyword-time-regexp nil
  2281. "Matches any of the 4 keywords, together with the time stamp.")
  2282. (make-variable-buffer-local 'org-keyword-time-regexp)
  2283. (defvar org-keyword-time-not-clock-regexp nil
  2284. "Matches any of the 3 keywords, together with the time stamp.")
  2285. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  2286. (defvar org-maybe-keyword-time-regexp nil
  2287. "Matches a timestamp, possibly preceeded by a keyword.")
  2288. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  2289. (defvar org-planning-or-clock-line-re nil
  2290. "Matches a line with planning or clock info.")
  2291. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  2292. (defconst org-plain-time-of-day-regexp
  2293. (concat
  2294. "\\(\\<[012]?[0-9]"
  2295. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2296. "\\(--?"
  2297. "\\(\\<[012]?[0-9]"
  2298. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2299. "\\)?")
  2300. "Regular expression to match a plain time or time range.
  2301. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2302. groups carry important information:
  2303. 0 the full match
  2304. 1 the first time, range or not
  2305. 8 the second time, if it is a range.")
  2306. (defconst org-plain-time-extension-regexp
  2307. (concat
  2308. "\\(\\<[012]?[0-9]"
  2309. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2310. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  2311. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  2312. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2313. groups carry important information:
  2314. 0 the full match
  2315. 7 hours of duration
  2316. 9 minutes of duration")
  2317. (defconst org-stamp-time-of-day-regexp
  2318. (concat
  2319. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  2320. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  2321. "\\(--?"
  2322. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  2323. "Regular expression to match a timestamp time or time range.
  2324. After a match, the following groups carry important information:
  2325. 0 the full match
  2326. 1 date plus weekday, for backreferencing to make sure both times on same day
  2327. 2 the first time, range or not
  2328. 4 the second time, if it is a range.")
  2329. (defconst org-startup-options
  2330. '(("fold" org-startup-folded t)
  2331. ("overview" org-startup-folded t)
  2332. ("nofold" org-startup-folded nil)
  2333. ("showall" org-startup-folded nil)
  2334. ("content" org-startup-folded content)
  2335. ("hidestars" org-hide-leading-stars t)
  2336. ("showstars" org-hide-leading-stars nil)
  2337. ("odd" org-odd-levels-only t)
  2338. ("oddeven" org-odd-levels-only nil)
  2339. ("align" org-startup-align-all-tables t)
  2340. ("noalign" org-startup-align-all-tables nil)
  2341. ("customtime" org-display-custom-times t)
  2342. ("logdone" org-log-done time)
  2343. ("lognotedone" org-log-done note)
  2344. ("nologdone" org-log-done nil)
  2345. ("lognoteclock-out" org-log-note-clock-out t)
  2346. ("nolognoteclock-out" org-log-note-clock-out nil)
  2347. ("logrepeat" org-log-repeat state)
  2348. ("lognoterepeat" org-log-repeat note)
  2349. ("nologrepeat" org-log-repeat nil)
  2350. ("constcgs" constants-unit-system cgs)
  2351. ("constSI" constants-unit-system SI))
  2352. "Variable associated with STARTUP options for org-mode.
  2353. Each element is a list of three items: The startup options as written
  2354. in the #+STARTUP line, the corresponding variable, and the value to
  2355. set this variable to if the option is found. An optional forth element PUSH
  2356. means to push this value onto the list in the variable.")
  2357. (defun org-set-regexps-and-options ()
  2358. "Precompute regular expressions for current buffer."
  2359. (when (org-mode-p)
  2360. (org-set-local 'org-todo-kwd-alist nil)
  2361. (org-set-local 'org-todo-key-alist nil)
  2362. (org-set-local 'org-todo-key-trigger nil)
  2363. (org-set-local 'org-todo-keywords-1 nil)
  2364. (org-set-local 'org-done-keywords nil)
  2365. (org-set-local 'org-todo-heads nil)
  2366. (org-set-local 'org-todo-sets nil)
  2367. (org-set-local 'org-todo-log-states nil)
  2368. (let ((re (org-make-options-regexp
  2369. '("CATEGORY" "SEQ_TODO" "TYP_TODO" "TODO" "COLUMNS"
  2370. "STARTUP" "ARCHIVE" "TAGS" "LINK" "PRIORITIES"
  2371. "CONSTANTS" "PROPERTY" "DRAWERS")))
  2372. (splitre "[ \t]+")
  2373. kwds kws0 kwsa key log value cat arch tags const links hw dws
  2374. tail sep kws1 prio props drawers)
  2375. (save-excursion
  2376. (save-restriction
  2377. (widen)
  2378. (goto-char (point-min))
  2379. (while (re-search-forward re nil t)
  2380. (setq key (match-string 1) value (org-match-string-no-properties 2))
  2381. (cond
  2382. ((equal key "CATEGORY")
  2383. (if (string-match "[ \t]+$" value)
  2384. (setq value (replace-match "" t t value)))
  2385. (setq cat value))
  2386. ((member key '("SEQ_TODO" "TODO"))
  2387. (push (cons 'sequence (org-split-string value splitre)) kwds))
  2388. ((equal key "TYP_TODO")
  2389. (push (cons 'type (org-split-string value splitre)) kwds))
  2390. ((equal key "TAGS")
  2391. (setq tags (append tags (org-split-string value splitre))))
  2392. ((equal key "COLUMNS")
  2393. (org-set-local 'org-columns-default-format value))
  2394. ((equal key "LINK")
  2395. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  2396. (push (cons (match-string 1 value)
  2397. (org-trim (match-string 2 value)))
  2398. links)))
  2399. ((equal key "PRIORITIES")
  2400. (setq prio (org-split-string value " +")))
  2401. ((equal key "PROPERTY")
  2402. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  2403. (push (cons (match-string 1 value) (match-string 2 value))
  2404. props)))
  2405. ((equal key "DRAWERS")
  2406. (setq drawers (org-split-string value splitre)))
  2407. ((equal key "CONSTANTS")
  2408. (setq const (append const (org-split-string value splitre))))
  2409. ((equal key "STARTUP")
  2410. (let ((opts (org-split-string value splitre))
  2411. l var val)
  2412. (while (setq l (pop opts))
  2413. (when (setq l (assoc l org-startup-options))
  2414. (setq var (nth 1 l) val (nth 2 l))
  2415. (if (not (nth 3 l))
  2416. (set (make-local-variable var) val)
  2417. (if (not (listp (symbol-value var)))
  2418. (set (make-local-variable var) nil))
  2419. (set (make-local-variable var) (symbol-value var))
  2420. (add-to-list var val))))))
  2421. ((equal key "ARCHIVE")
  2422. (string-match " *$" value)
  2423. (setq arch (replace-match "" t t value))
  2424. (remove-text-properties 0 (length arch)
  2425. '(face t fontified t) arch)))
  2426. )))
  2427. (when cat
  2428. (org-set-local 'org-category (intern cat))
  2429. (push (cons "CATEGORY" cat) props))
  2430. (when prio
  2431. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  2432. (setq prio (mapcar 'string-to-char prio))
  2433. (org-set-local 'org-highest-priority (nth 0 prio))
  2434. (org-set-local 'org-lowest-priority (nth 1 prio))
  2435. (org-set-local 'org-default-priority (nth 2 prio)))
  2436. (and props (org-set-local 'org-local-properties (nreverse props)))
  2437. (and drawers (org-set-local 'org-drawers drawers))
  2438. (and arch (org-set-local 'org-archive-location arch))
  2439. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  2440. ;; Process the TODO keywords
  2441. (unless kwds
  2442. ;; Use the global values as if they had been given locally.
  2443. (setq kwds (default-value 'org-todo-keywords))
  2444. (if (stringp (car kwds))
  2445. (setq kwds (list (cons org-todo-interpretation
  2446. (default-value 'org-todo-keywords)))))
  2447. (setq kwds (reverse kwds)))
  2448. (setq kwds (nreverse kwds))
  2449. (let (inter kws kw)
  2450. (while (setq kws (pop kwds))
  2451. (setq inter (pop kws) sep (member "|" kws)
  2452. kws0 (delete "|" (copy-sequence kws))
  2453. kwsa nil
  2454. kws1 (mapcar
  2455. (lambda (x)
  2456. ;; 1 2
  2457. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  2458. (progn
  2459. (setq kw (match-string 1 x)
  2460. key (and (match-end 2) (match-string 2 x))
  2461. log (org-extract-log-state-settings x))
  2462. (push (cons kw (and key (string-to-char key))) kwsa)
  2463. (and log (push log org-todo-log-states))
  2464. kw)
  2465. (error "Invalid TODO keyword %s" x)))
  2466. kws0)
  2467. kwsa (if kwsa (append '((:startgroup))
  2468. (nreverse kwsa)
  2469. '((:endgroup))))
  2470. hw (car kws1)
  2471. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  2472. tail (list inter hw (car dws) (org-last dws)))
  2473. (add-to-list 'org-todo-heads hw 'append)
  2474. (push kws1 org-todo-sets)
  2475. (setq org-done-keywords (append org-done-keywords dws nil))
  2476. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  2477. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  2478. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  2479. (setq org-todo-sets (nreverse org-todo-sets)
  2480. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  2481. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  2482. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  2483. ;; Process the constants
  2484. (when const
  2485. (let (e cst)
  2486. (while (setq e (pop const))
  2487. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  2488. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  2489. (setq org-table-formula-constants-local cst)))
  2490. ;; Process the tags.
  2491. (when tags
  2492. (let (e tgs)
  2493. (while (setq e (pop tags))
  2494. (cond
  2495. ((equal e "{") (push '(:startgroup) tgs))
  2496. ((equal e "}") (push '(:endgroup) tgs))
  2497. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  2498. (push (cons (match-string 1 e)
  2499. (string-to-char (match-string 2 e)))
  2500. tgs))
  2501. (t (push (list e) tgs))))
  2502. (org-set-local 'org-tag-alist nil)
  2503. (while (setq e (pop tgs))
  2504. (or (and (stringp (car e))
  2505. (assoc (car e) org-tag-alist))
  2506. (push e org-tag-alist))))))
  2507. ;; Compute the regular expressions and other local variables
  2508. (if (not org-done-keywords)
  2509. (setq org-done-keywords (list (org-last org-todo-keywords-1))))
  2510. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  2511. (length org-scheduled-string)
  2512. (length org-clock-string)
  2513. (length org-closed-string)))
  2514. org-drawer-regexp
  2515. (concat "^[ \t]*:\\("
  2516. (mapconcat 'regexp-quote org-drawers "\\|")
  2517. "\\):[ \t]*$")
  2518. org-not-done-keywords
  2519. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  2520. org-todo-regexp
  2521. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  2522. "\\|") "\\)\\>")
  2523. org-not-done-regexp
  2524. (concat "\\<\\("
  2525. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  2526. "\\)\\>")
  2527. org-todo-line-regexp
  2528. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  2529. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2530. "\\)\\>\\)?[ \t]*\\(.*\\)")
  2531. org-complex-heading-regexp
  2532. (concat "^\\(\\*+\\)\\(?:[ \t]+\\("
  2533. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2534. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  2535. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  2536. org-nl-done-regexp
  2537. (concat "\n\\*+[ \t]+"
  2538. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  2539. "\\)" "\\>")
  2540. org-todo-line-tags-regexp
  2541. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  2542. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2543. (org-re
  2544. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  2545. org-looking-at-done-regexp
  2546. (concat "^" "\\(?:"
  2547. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  2548. "\\>")
  2549. org-deadline-regexp (concat "\\<" org-deadline-string)
  2550. org-deadline-time-regexp
  2551. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  2552. org-deadline-line-regexp
  2553. (concat "\\<\\(" org-deadline-string "\\).*")
  2554. org-scheduled-regexp
  2555. (concat "\\<" org-scheduled-string)
  2556. org-scheduled-time-regexp
  2557. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  2558. org-closed-time-regexp
  2559. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  2560. org-keyword-time-regexp
  2561. (concat "\\<\\(" org-scheduled-string
  2562. "\\|" org-deadline-string
  2563. "\\|" org-closed-string
  2564. "\\|" org-clock-string "\\)"
  2565. " *[[<]\\([^]>]+\\)[]>]")
  2566. org-keyword-time-not-clock-regexp
  2567. (concat "\\<\\(" org-scheduled-string
  2568. "\\|" org-deadline-string
  2569. "\\|" org-closed-string
  2570. "\\)"
  2571. " *[[<]\\([^]>]+\\)[]>]")
  2572. org-maybe-keyword-time-regexp
  2573. (concat "\\(\\<\\(" org-scheduled-string
  2574. "\\|" org-deadline-string
  2575. "\\|" org-closed-string
  2576. "\\|" org-clock-string "\\)\\)?"
  2577. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  2578. org-planning-or-clock-line-re
  2579. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  2580. "\\|" org-deadline-string
  2581. "\\|" org-closed-string "\\|" org-clock-string
  2582. "\\)\\>\\)")
  2583. )
  2584. (org-compute-latex-and-specials-regexp)
  2585. (org-set-font-lock-defaults)))
  2586. (defun org-extract-log-state-settings (x)
  2587. "Extract the log state setting from a TODO keyword string.
  2588. This will extract info from a string like \"WAIT(w@/!)\"."
  2589. (let (kw key log1 log2)
  2590. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  2591. (setq kw (match-string 1 x)
  2592. key (and (match-end 2) (match-string 2 x))
  2593. log1 (and (match-end 3) (match-string 3 x))
  2594. log2 (and (match-end 4) (match-string 4 x)))
  2595. (and (or log1 log2)
  2596. (list kw
  2597. (and log1 (if (equal log1 "!") 'time 'note))
  2598. (and log2 (if (equal log2 "!") 'time 'note)))))))
  2599. (defun org-remove-keyword-keys (list)
  2600. "Remove a pair of parenthesis at the end of each string in LIST."
  2601. (mapcar (lambda (x)
  2602. (if (string-match "(.*)$" x)
  2603. (substring x 0 (match-beginning 0))
  2604. x))
  2605. list))
  2606. ;; FIXME: this could be done much better, using second characters etc.
  2607. (defun org-assign-fast-keys (alist)
  2608. "Assign fast keys to a keyword-key alist.
  2609. Respect keys that are already there."
  2610. (let (new e k c c1 c2 (char ?a))
  2611. (while (setq e (pop alist))
  2612. (cond
  2613. ((equal e '(:startgroup)) (push e new))
  2614. ((equal e '(:endgroup)) (push e new))
  2615. (t
  2616. (setq k (car e) c2 nil)
  2617. (if (cdr e)
  2618. (setq c (cdr e))
  2619. ;; automatically assign a character.
  2620. (setq c1 (string-to-char
  2621. (downcase (substring
  2622. k (if (= (string-to-char k) ?@) 1 0)))))
  2623. (if (or (rassoc c1 new) (rassoc c1 alist))
  2624. (while (or (rassoc char new) (rassoc char alist))
  2625. (setq char (1+ char)))
  2626. (setq c2 c1))
  2627. (setq c (or c2 char)))
  2628. (push (cons k c) new))))
  2629. (nreverse new)))
  2630. ;;; Some variables used in various places
  2631. (defvar org-window-configuration nil
  2632. "Used in various places to store a window configuration.")
  2633. (defvar org-finish-function nil
  2634. "Function to be called when `C-c C-c' is used.
  2635. This is for getting out of special buffers like remember.")
  2636. ;; FIXME: Occasionally check by commenting these, to make sure
  2637. ;; no other functions uses these, forgetting to let-bind them.
  2638. (defvar entry)
  2639. (defvar state)
  2640. (defvar last-state)
  2641. (defvar date)
  2642. (defvar description)
  2643. ;; Defined somewhere in this file, but used before definition.
  2644. (defvar org-html-entities)
  2645. (defvar org-struct-menu)
  2646. (defvar org-org-menu)
  2647. (defvar org-tbl-menu)
  2648. (defvar org-agenda-keymap)
  2649. ;;;; Define the Org-mode
  2650. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  2651. (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."))
  2652. ;; We use a before-change function to check if a table might need
  2653. ;; an update.
  2654. (defvar org-table-may-need-update t
  2655. "Indicates that a table might need an update.
  2656. This variable is set by `org-before-change-function'.
  2657. `org-table-align' sets it back to nil.")
  2658. (defun org-before-change-function (beg end)
  2659. "Every change indicates that a table might need an update."
  2660. (setq org-table-may-need-update t))
  2661. (defvar org-mode-map)
  2662. (defvar org-mode-hook nil)
  2663. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  2664. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  2665. (defvar org-table-buffer-is-an nil)
  2666. (defconst org-outline-regexp "\\*+ ")
  2667. ;;;###autoload
  2668. (define-derived-mode org-mode outline-mode "Org"
  2669. "Outline-based notes management and organizer, alias
  2670. \"Carsten's outline-mode for keeping track of everything.\"
  2671. Org-mode develops organizational tasks around a NOTES file which
  2672. contains information about projects as plain text. Org-mode is
  2673. implemented on top of outline-mode, which is ideal to keep the content
  2674. of large files well structured. It supports ToDo items, deadlines and
  2675. time stamps, which magically appear in the diary listing of the Emacs
  2676. calendar. Tables are easily created with a built-in table editor.
  2677. Plain text URL-like links connect to websites, emails (VM), Usenet
  2678. messages (Gnus), BBDB entries, and any files related to the project.
  2679. For printing and sharing of notes, an Org-mode file (or a part of it)
  2680. can be exported as a structured ASCII or HTML file.
  2681. The following commands are available:
  2682. \\{org-mode-map}"
  2683. ;; Get rid of Outline menus, they are not needed
  2684. ;; Need to do this here because define-derived-mode sets up
  2685. ;; the keymap so late. Still, it is a waste to call this each time
  2686. ;; we switch another buffer into org-mode.
  2687. (if (featurep 'xemacs)
  2688. (when (boundp 'outline-mode-menu-heading)
  2689. ;; Assume this is Greg's port, it used easymenu
  2690. (easy-menu-remove outline-mode-menu-heading)
  2691. (easy-menu-remove outline-mode-menu-show)
  2692. (easy-menu-remove outline-mode-menu-hide))
  2693. (define-key org-mode-map [menu-bar headings] 'undefined)
  2694. (define-key org-mode-map [menu-bar hide] 'undefined)
  2695. (define-key org-mode-map [menu-bar show] 'undefined))
  2696. (org-load-modules-maybe)
  2697. (easy-menu-add org-org-menu)
  2698. (easy-menu-add org-tbl-menu)
  2699. (org-install-agenda-files-menu)
  2700. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  2701. (org-add-to-invisibility-spec '(org-cwidth))
  2702. (when (featurep 'xemacs)
  2703. (org-set-local 'line-move-ignore-invisible t))
  2704. (org-set-local 'outline-regexp org-outline-regexp)
  2705. (org-set-local 'outline-level 'org-outline-level)
  2706. (when (and org-ellipsis
  2707. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  2708. (fboundp 'make-glyph-code))
  2709. (unless org-display-table
  2710. (setq org-display-table (make-display-table)))
  2711. (set-display-table-slot
  2712. org-display-table 4
  2713. (vconcat (mapcar
  2714. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  2715. org-ellipsis)))
  2716. (if (stringp org-ellipsis) org-ellipsis "..."))))
  2717. (setq buffer-display-table org-display-table))
  2718. (org-set-regexps-and-options)
  2719. ;; Calc embedded
  2720. (org-set-local 'calc-embedded-open-mode "# ")
  2721. (modify-syntax-entry ?# "<")
  2722. (modify-syntax-entry ?@ "w")
  2723. (if org-startup-truncated (setq truncate-lines t))
  2724. (org-set-local 'font-lock-unfontify-region-function
  2725. 'org-unfontify-region)
  2726. ;; Activate before-change-function
  2727. (org-set-local 'org-table-may-need-update t)
  2728. (org-add-hook 'before-change-functions 'org-before-change-function nil
  2729. 'local)
  2730. ;; Check for running clock before killing a buffer
  2731. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  2732. ;; Paragraphs and auto-filling
  2733. (org-set-autofill-regexps)
  2734. (setq indent-line-function 'org-indent-line-function)
  2735. (org-update-radio-target-regexp)
  2736. ;; Comment characters
  2737. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  2738. (org-set-local 'comment-padding " ")
  2739. ;; Align options lines
  2740. (org-set-local
  2741. 'align-mode-rules-list
  2742. '((org-in-buffer-settings
  2743. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  2744. (modes . '(org-mode)))))
  2745. ;; Imenu
  2746. (org-set-local 'imenu-create-index-function
  2747. 'org-imenu-get-tree)
  2748. ;; Make isearch reveal context
  2749. (if (or (featurep 'xemacs)
  2750. (not (boundp 'outline-isearch-open-invisible-function)))
  2751. ;; Emacs 21 and XEmacs make use of the hook
  2752. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  2753. ;; Emacs 22 deals with this through a special variable
  2754. (org-set-local 'outline-isearch-open-invisible-function
  2755. (lambda (&rest ignore) (org-show-context 'isearch))))
  2756. ;; If empty file that did not turn on org-mode automatically, make it to.
  2757. (if (and org-insert-mode-line-in-empty-file
  2758. (interactive-p)
  2759. (= (point-min) (point-max)))
  2760. (insert "# -*- mode: org -*-\n\n"))
  2761. (unless org-inhibit-startup
  2762. (when org-startup-align-all-tables
  2763. (let ((bmp (buffer-modified-p)))
  2764. (org-table-map-tables 'org-table-align)
  2765. (set-buffer-modified-p bmp)))
  2766. (org-cycle-hide-drawers 'all)
  2767. (cond
  2768. ((eq org-startup-folded t)
  2769. (org-cycle '(4)))
  2770. ((eq org-startup-folded 'content)
  2771. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  2772. (org-cycle '(4)) (org-cycle '(4)))))))
  2773. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  2774. (defun org-current-time ()
  2775. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  2776. (if (> (car org-time-stamp-rounding-minutes) 1)
  2777. (let ((r (car org-time-stamp-rounding-minutes))
  2778. (time (decode-time)))
  2779. (apply 'encode-time
  2780. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  2781. (nthcdr 2 time))))
  2782. (current-time)))
  2783. ;;;; Font-Lock stuff, including the activators
  2784. (defvar org-mouse-map (make-sparse-keymap))
  2785. (org-defkey org-mouse-map
  2786. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  2787. (org-defkey org-mouse-map
  2788. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  2789. (when org-mouse-1-follows-link
  2790. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  2791. (when org-tab-follows-link
  2792. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  2793. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  2794. (when org-return-follows-link
  2795. (org-defkey org-mouse-map [(return)] 'org-open-at-point)
  2796. (org-defkey org-mouse-map "\C-m" 'org-open-at-point))
  2797. (require 'font-lock)
  2798. (defconst org-non-link-chars "]\t\n\r<>")
  2799. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  2800. "shell" "elisp"))
  2801. (defvar org-link-types-re nil
  2802. "Matches a link that has a url-like prefix like \"http:\"")
  2803. (defvar org-link-re-with-space nil
  2804. "Matches a link with spaces, optional angular brackets around it.")
  2805. (defvar org-link-re-with-space2 nil
  2806. "Matches a link with spaces, optional angular brackets around it.")
  2807. (defvar org-angle-link-re nil
  2808. "Matches link with angular brackets, spaces are allowed.")
  2809. (defvar org-plain-link-re nil
  2810. "Matches plain link, without spaces.")
  2811. (defvar org-bracket-link-regexp nil
  2812. "Matches a link in double brackets.")
  2813. (defvar org-bracket-link-analytic-regexp nil
  2814. "Regular expression used to analyze links.
  2815. Here is what the match groups contain after a match:
  2816. 1: http:
  2817. 2: http
  2818. 3: path
  2819. 4: [desc]
  2820. 5: desc")
  2821. (defvar org-any-link-re nil
  2822. "Regular expression matching any link.")
  2823. (defun org-make-link-regexps ()
  2824. "Update the link regular expressions.
  2825. This should be called after the variable `org-link-types' has changed."
  2826. (setq org-link-types-re
  2827. (concat
  2828. "\\`\\(" (mapconcat 'identity org-link-types "\\|") "\\):")
  2829. org-link-re-with-space
  2830. (concat
  2831. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2832. "\\([^" org-non-link-chars " ]"
  2833. "[^" org-non-link-chars "]*"
  2834. "[^" org-non-link-chars " ]\\)>?")
  2835. org-link-re-with-space2
  2836. (concat
  2837. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2838. "\\([^" org-non-link-chars " ]"
  2839. "[^]\t\n\r]*"
  2840. "[^" org-non-link-chars " ]\\)>?")
  2841. org-angle-link-re
  2842. (concat
  2843. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2844. "\\([^" org-non-link-chars " ]"
  2845. "[^" org-non-link-chars "]*"
  2846. "\\)>")
  2847. org-plain-link-re
  2848. (concat
  2849. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2850. "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  2851. org-bracket-link-regexp
  2852. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  2853. org-bracket-link-analytic-regexp
  2854. (concat
  2855. "\\[\\["
  2856. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  2857. "\\([^]]+\\)"
  2858. "\\]"
  2859. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  2860. "\\]")
  2861. org-any-link-re
  2862. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  2863. org-angle-link-re "\\)\\|\\("
  2864. org-plain-link-re "\\)")))
  2865. (org-make-link-regexps)
  2866. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  2867. "Regular expression for fast time stamp matching.")
  2868. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  2869. "Regular expression for fast time stamp matching.")
  2870. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  2871. "Regular expression matching time strings for analysis.
  2872. This one does not require the space after the date, so it can be used
  2873. on a string that terminates immediately after the date.")
  2874. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  2875. "Regular expression matching time strings for analysis.")
  2876. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  2877. "Regular expression matching time stamps, with groups.")
  2878. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  2879. "Regular expression matching time stamps (also [..]), with groups.")
  2880. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  2881. "Regular expression matching a time stamp range.")
  2882. (defconst org-tr-regexp-both
  2883. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  2884. "Regular expression matching a time stamp range.")
  2885. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  2886. org-ts-regexp "\\)?")
  2887. "Regular expression matching a time stamp or time stamp range.")
  2888. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  2889. org-ts-regexp-both "\\)?")
  2890. "Regular expression matching a time stamp or time stamp range.
  2891. The time stamps may be either active or inactive.")
  2892. (defvar org-emph-face nil)
  2893. (defun org-do-emphasis-faces (limit)
  2894. "Run through the buffer and add overlays to links."
  2895. (let (rtn)
  2896. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  2897. (if (not (= (char-after (match-beginning 3))
  2898. (char-after (match-beginning 4))))
  2899. (progn
  2900. (setq rtn t)
  2901. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  2902. 'face
  2903. (nth 1 (assoc (match-string 3)
  2904. org-emphasis-alist)))
  2905. (add-text-properties (match-beginning 2) (match-end 2)
  2906. '(font-lock-multiline t))
  2907. (when org-hide-emphasis-markers
  2908. (add-text-properties (match-end 4) (match-beginning 5)
  2909. '(invisible org-link))
  2910. (add-text-properties (match-beginning 3) (match-end 3)
  2911. '(invisible org-link)))))
  2912. (backward-char 1))
  2913. rtn))
  2914. (defun org-emphasize (&optional char)
  2915. "Insert or change an emphasis, i.e. a font like bold or italic.
  2916. If there is an active region, change that region to a new emphasis.
  2917. If there is no region, just insert the marker characters and position
  2918. the cursor between them.
  2919. CHAR should be either the marker character, or the first character of the
  2920. HTML tag associated with that emphasis. If CHAR is a space, the means
  2921. to remove the emphasis of the selected region.
  2922. If char is not given (for example in an interactive call) it
  2923. will be prompted for."
  2924. (interactive)
  2925. (let ((eal org-emphasis-alist) e det
  2926. (erc org-emphasis-regexp-components)
  2927. (prompt "")
  2928. (string "") beg end move tag c s)
  2929. (if (org-region-active-p)
  2930. (setq beg (region-beginning) end (region-end)
  2931. string (buffer-substring beg end))
  2932. (setq move t))
  2933. (while (setq e (pop eal))
  2934. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  2935. c (aref tag 0))
  2936. (push (cons c (string-to-char (car e))) det)
  2937. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  2938. (substring tag 1)))))
  2939. (unless char
  2940. (message "%s" (concat "Emphasis marker or tag:" prompt))
  2941. (setq char (read-char-exclusive)))
  2942. (setq char (or (cdr (assoc char det)) char))
  2943. (if (equal char ?\ )
  2944. (setq s "" move nil)
  2945. (unless (assoc (char-to-string char) org-emphasis-alist)
  2946. (error "No such emphasis marker: \"%c\"" char))
  2947. (setq s (char-to-string char)))
  2948. (while (and (> (length string) 1)
  2949. (equal (substring string 0 1) (substring string -1))
  2950. (assoc (substring string 0 1) org-emphasis-alist))
  2951. (setq string (substring string 1 -1)))
  2952. (setq string (concat s string s))
  2953. (if beg (delete-region beg end))
  2954. (unless (or (bolp)
  2955. (string-match (concat "[" (nth 0 erc) "\n]")
  2956. (char-to-string (char-before (point)))))
  2957. (insert " "))
  2958. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  2959. (char-to-string (char-after (point))))
  2960. (insert " ") (backward-char 1))
  2961. (insert string)
  2962. (and move (backward-char 1))))
  2963. (defconst org-nonsticky-props
  2964. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  2965. (defun org-activate-plain-links (limit)
  2966. "Run through the buffer and add overlays to links."
  2967. (catch 'exit
  2968. (let (f)
  2969. (while (re-search-forward org-plain-link-re limit t)
  2970. (setq f (get-text-property (match-beginning 0) 'face))
  2971. (if (or (eq f 'org-tag)
  2972. (and (listp f) (memq 'org-tag f)))
  2973. nil
  2974. (add-text-properties (match-beginning 0) (match-end 0)
  2975. (list 'mouse-face 'highlight
  2976. 'rear-nonsticky org-nonsticky-props
  2977. 'keymap org-mouse-map
  2978. ))
  2979. (throw 'exit t))))))
  2980. (defun org-activate-code (limit)
  2981. (if (re-search-forward "^[ \t]*\\(:.*\\)" limit t)
  2982. (unless (get-text-property (match-beginning 1) 'face)
  2983. (remove-text-properties (match-beginning 0) (match-end 0)
  2984. '(display t invisible t intangible t))
  2985. t)))
  2986. (defun org-activate-angle-links (limit)
  2987. "Run through the buffer and add overlays to links."
  2988. (if (re-search-forward org-angle-link-re limit t)
  2989. (progn
  2990. (add-text-properties (match-beginning 0) (match-end 0)
  2991. (list 'mouse-face 'highlight
  2992. 'rear-nonsticky org-nonsticky-props
  2993. 'keymap org-mouse-map
  2994. ))
  2995. t)))
  2996. (defun org-activate-bracket-links (limit)
  2997. "Run through the buffer and add overlays to bracketed links."
  2998. (if (re-search-forward org-bracket-link-regexp limit t)
  2999. (let* ((help (concat "LINK: "
  3000. (org-match-string-no-properties 1)))
  3001. ;; FIXME: above we should remove the escapes.
  3002. ;; but that requires another match, protecting match data,
  3003. ;; a lot of overhead for font-lock.
  3004. (ip (org-maybe-intangible
  3005. (list 'invisible 'org-link 'rear-nonsticky org-nonsticky-props
  3006. 'keymap org-mouse-map 'mouse-face 'highlight
  3007. 'font-lock-multiline t 'help-echo help)))
  3008. (vp (list 'rear-nonsticky org-nonsticky-props
  3009. 'keymap org-mouse-map 'mouse-face 'highlight
  3010. ' font-lock-multiline t 'help-echo help)))
  3011. ;; We need to remove the invisible property here. Table narrowing
  3012. ;; may have made some of this invisible.
  3013. (remove-text-properties (match-beginning 0) (match-end 0)
  3014. '(invisible nil))
  3015. (if (match-end 3)
  3016. (progn
  3017. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  3018. (add-text-properties (match-beginning 3) (match-end 3) vp)
  3019. (add-text-properties (match-end 3) (match-end 0) ip))
  3020. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  3021. (add-text-properties (match-beginning 1) (match-end 1) vp)
  3022. (add-text-properties (match-end 1) (match-end 0) ip))
  3023. t)))
  3024. (defun org-activate-dates (limit)
  3025. "Run through the buffer and add overlays to dates."
  3026. (if (re-search-forward org-tsr-regexp-both limit t)
  3027. (progn
  3028. (add-text-properties (match-beginning 0) (match-end 0)
  3029. (list 'mouse-face 'highlight
  3030. 'rear-nonsticky org-nonsticky-props
  3031. 'keymap org-mouse-map))
  3032. (when org-display-custom-times
  3033. (if (match-end 3)
  3034. (org-display-custom-time (match-beginning 3) (match-end 3)))
  3035. (org-display-custom-time (match-beginning 1) (match-end 1)))
  3036. t)))
  3037. (defvar org-target-link-regexp nil
  3038. "Regular expression matching radio targets in plain text.")
  3039. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  3040. "Regular expression matching a link target.")
  3041. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  3042. "Regular expression matching a radio target.")
  3043. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  3044. "Regular expression matching any target.")
  3045. (defun org-activate-target-links (limit)
  3046. "Run through the buffer and add overlays to target matches."
  3047. (when org-target-link-regexp
  3048. (let ((case-fold-search t))
  3049. (if (re-search-forward org-target-link-regexp limit t)
  3050. (progn
  3051. (add-text-properties (match-beginning 0) (match-end 0)
  3052. (list 'mouse-face 'highlight
  3053. 'rear-nonsticky org-nonsticky-props
  3054. 'keymap org-mouse-map
  3055. 'help-echo "Radio target link"
  3056. 'org-linked-text t))
  3057. t)))))
  3058. (defun org-update-radio-target-regexp ()
  3059. "Find all radio targets in this file and update the regular expression."
  3060. (interactive)
  3061. (when (memq 'radio org-activate-links)
  3062. (setq org-target-link-regexp
  3063. (org-make-target-link-regexp (org-all-targets 'radio)))
  3064. (org-restart-font-lock)))
  3065. (defun org-hide-wide-columns (limit)
  3066. (let (s e)
  3067. (setq s (text-property-any (point) (or limit (point-max))
  3068. 'org-cwidth t))
  3069. (when s
  3070. (setq e (next-single-property-change s 'org-cwidth))
  3071. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  3072. (goto-char e)
  3073. t)))
  3074. (defvar org-latex-and-specials-regexp nil
  3075. "Regular expression for highlighting export special stuff.")
  3076. (defvar org-match-substring-regexp)
  3077. (defvar org-match-substring-with-braces-regexp)
  3078. (defvar org-export-html-special-string-regexps)
  3079. (defun org-compute-latex-and-specials-regexp ()
  3080. "Compute regular expression for stuff treated specially by exporters."
  3081. (if (not org-highlight-latex-fragments-and-specials)
  3082. (org-set-local 'org-latex-and-specials-regexp nil)
  3083. (require 'org-exp)
  3084. (let*
  3085. ((matchers (plist-get org-format-latex-options :matchers))
  3086. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  3087. org-latex-regexps)))
  3088. (options (org-combine-plists (org-default-export-plist)
  3089. (org-infile-export-plist)))
  3090. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  3091. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  3092. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  3093. (org-export-html-expand (plist-get options :expand-quoted-html))
  3094. (org-export-with-special-strings (plist-get options :special-strings))
  3095. (re-sub
  3096. (cond
  3097. ((equal org-export-with-sub-superscripts '{})
  3098. (list org-match-substring-with-braces-regexp))
  3099. (org-export-with-sub-superscripts
  3100. (list org-match-substring-regexp))
  3101. (t nil)))
  3102. (re-latex
  3103. (if org-export-with-LaTeX-fragments
  3104. (mapcar (lambda (x) (nth 1 x)) latexs)))
  3105. (re-macros
  3106. (if org-export-with-TeX-macros
  3107. (list (concat "\\\\"
  3108. (regexp-opt
  3109. (append (mapcar 'car org-html-entities)
  3110. (if (boundp 'org-latex-entities)
  3111. org-latex-entities nil))
  3112. 'words))) ; FIXME
  3113. ))
  3114. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  3115. (re-special (if org-export-with-special-strings
  3116. (mapcar (lambda (x) (car x))
  3117. org-export-html-special-string-regexps)))
  3118. (re-rest
  3119. (delq nil
  3120. (list
  3121. (if org-export-html-expand "@<[^>\n]+>")
  3122. ))))
  3123. (org-set-local
  3124. 'org-latex-and-specials-regexp
  3125. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  3126. re-rest) "\\|")))))
  3127. (defun org-do-latex-and-special-faces (limit)
  3128. "Run through the buffer and add overlays to links."
  3129. (when org-latex-and-specials-regexp
  3130. (let (rtn d)
  3131. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  3132. limit t))
  3133. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  3134. 'face))
  3135. '(org-code org-verbatim underline)))
  3136. (progn
  3137. (setq rtn t
  3138. d (cond ((member (char-after (1+ (match-beginning 0)))
  3139. '(?_ ?^)) 1)
  3140. (t 0)))
  3141. (font-lock-prepend-text-property
  3142. (+ d (match-beginning 0)) (match-end 0)
  3143. 'face 'org-latex-and-export-specials)
  3144. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  3145. '(font-lock-multiline t)))))
  3146. rtn)))
  3147. (defun org-restart-font-lock ()
  3148. "Restart font-lock-mode, to force refontification."
  3149. (when (and (boundp 'font-lock-mode) font-lock-mode)
  3150. (font-lock-mode -1)
  3151. (font-lock-mode 1)))
  3152. (defun org-all-targets (&optional radio)
  3153. "Return a list of all targets in this file.
  3154. With optional argument RADIO, only find radio targets."
  3155. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  3156. rtn)
  3157. (save-excursion
  3158. (goto-char (point-min))
  3159. (while (re-search-forward re nil t)
  3160. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  3161. rtn)))
  3162. (defun org-make-target-link-regexp (targets)
  3163. "Make regular expression matching all strings in TARGETS.
  3164. The regular expression finds the targets also if there is a line break
  3165. between words."
  3166. (and targets
  3167. (concat
  3168. "\\<\\("
  3169. (mapconcat
  3170. (lambda (x)
  3171. (while (string-match " +" x)
  3172. (setq x (replace-match "\\s-+" t t x)))
  3173. x)
  3174. targets
  3175. "\\|")
  3176. "\\)\\>")))
  3177. (defun org-activate-tags (limit)
  3178. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  3179. (progn
  3180. (add-text-properties (match-beginning 1) (match-end 1)
  3181. (list 'mouse-face 'highlight
  3182. 'rear-nonsticky org-nonsticky-props
  3183. 'keymap org-mouse-map))
  3184. t)))
  3185. (defun org-outline-level ()
  3186. (save-excursion
  3187. (looking-at outline-regexp)
  3188. (if (match-beginning 1)
  3189. (+ (org-get-string-indentation (match-string 1)) 1000)
  3190. (1- (- (match-end 0) (match-beginning 0))))))
  3191. (defvar org-font-lock-keywords nil)
  3192. (defconst org-property-re (org-re "^[ \t]*\\(:\\([[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  3193. "Regular expression matching a property line.")
  3194. (defvar org-font-lock-hook nil
  3195. "Functions to be called for special font loch stuff.")
  3196. (defun org-font-lock-hook (limit)
  3197. (run-hook-with-args 'org-font-lock-hook limit))
  3198. (defun org-set-font-lock-defaults ()
  3199. (let* ((em org-fontify-emphasized-text)
  3200. (lk org-activate-links)
  3201. (org-font-lock-extra-keywords
  3202. (list
  3203. ;; Call the hook
  3204. '(org-font-lock-hook)
  3205. ;; Headlines
  3206. '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
  3207. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  3208. ;; Table lines
  3209. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  3210. (1 'org-table t))
  3211. ;; Table internals
  3212. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  3213. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  3214. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  3215. ;; Drawers
  3216. (list org-drawer-regexp '(0 'org-special-keyword t))
  3217. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  3218. ;; Properties
  3219. (list org-property-re
  3220. '(1 'org-special-keyword t)
  3221. '(3 'org-property-value t))
  3222. (if org-format-transports-properties-p
  3223. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  3224. ;; Links
  3225. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  3226. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  3227. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  3228. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  3229. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  3230. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  3231. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  3232. '(org-hide-wide-columns (0 nil append))
  3233. ;; TODO lines
  3234. (list (concat "^\\*+[ \t]+" org-todo-regexp)
  3235. '(1 (org-get-todo-face 1) t))
  3236. ;; DONE
  3237. (if org-fontify-done-headline
  3238. (list (concat "^[*]+ +\\<\\("
  3239. (mapconcat 'regexp-quote org-done-keywords "\\|")
  3240. "\\)\\(.*\\)")
  3241. '(2 'org-headline-done t))
  3242. nil)
  3243. ;; Priorities
  3244. (list (concat "\\[#[A-Z0-9]\\]") '(0 'org-special-keyword t))
  3245. ;; Special keywords
  3246. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  3247. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  3248. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  3249. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  3250. ;; Emphasis
  3251. (if em
  3252. (if (featurep 'xemacs)
  3253. '(org-do-emphasis-faces (0 nil append))
  3254. '(org-do-emphasis-faces)))
  3255. ;; Checkboxes
  3256. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  3257. 2 'bold prepend)
  3258. (if org-provide-checkbox-statistics
  3259. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  3260. (0 (org-get-checkbox-statistics-face) t)))
  3261. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  3262. '(1 'org-archived prepend))
  3263. ;; Specials
  3264. '(org-do-latex-and-special-faces)
  3265. ;; Code
  3266. '(org-activate-code (1 'org-code t))
  3267. ;; COMMENT
  3268. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  3269. "\\|" org-quote-string "\\)\\>")
  3270. '(1 'org-special-keyword t))
  3271. '("^#.*" (0 'font-lock-comment-face t))
  3272. )))
  3273. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  3274. ;; Now set the full font-lock-keywords
  3275. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  3276. (org-set-local 'font-lock-defaults
  3277. '(org-font-lock-keywords t nil nil backward-paragraph))
  3278. (kill-local-variable 'font-lock-keywords) nil))
  3279. (defvar org-m nil)
  3280. (defvar org-l nil)
  3281. (defvar org-f nil)
  3282. (defun org-get-level-face (n)
  3283. "Get the right face for match N in font-lock matching of healdines."
  3284. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  3285. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  3286. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  3287. (cond
  3288. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  3289. ((eq n 2) org-f)
  3290. (t (if org-level-color-stars-only nil org-f))))
  3291. (defun org-get-todo-face (kwd)
  3292. "Get the right face for a TODO keyword KWD.
  3293. If KWD is a number, get the corresponding match group."
  3294. (if (numberp kwd) (setq kwd (match-string kwd)))
  3295. (or (cdr (assoc kwd org-todo-keyword-faces))
  3296. (and (member kwd org-done-keywords) 'org-done)
  3297. 'org-todo))
  3298. (defun org-unfontify-region (beg end &optional maybe_loudly)
  3299. "Remove fontification and activation overlays from links."
  3300. (font-lock-default-unfontify-region beg end)
  3301. (let* ((buffer-undo-list t)
  3302. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  3303. (inhibit-modification-hooks t)
  3304. deactivate-mark buffer-file-name buffer-file-truename)
  3305. (remove-text-properties beg end
  3306. '(mouse-face t keymap t org-linked-text t
  3307. invisible t intangible t))))
  3308. ;;;; Visibility cycling, including org-goto and indirect buffer
  3309. ;;; Cycling
  3310. (defvar org-cycle-global-status nil)
  3311. (make-variable-buffer-local 'org-cycle-global-status)
  3312. (defvar org-cycle-subtree-status nil)
  3313. (make-variable-buffer-local 'org-cycle-subtree-status)
  3314. ;;;###autoload
  3315. (defun org-cycle (&optional arg)
  3316. "Visibility cycling for Org-mode.
  3317. - When this function is called with a prefix argument, rotate the entire
  3318. buffer through 3 states (global cycling)
  3319. 1. OVERVIEW: Show only top-level headlines.
  3320. 2. CONTENTS: Show all headlines of all levels, but no body text.
  3321. 3. SHOW ALL: Show everything.
  3322. - When point is at the beginning of a headline, rotate the subtree started
  3323. by this line through 3 different states (local cycling)
  3324. 1. FOLDED: Only the main headline is shown.
  3325. 2. CHILDREN: The main headline and the direct children are shown.
  3326. From this state, you can move to one of the children
  3327. and zoom in further.
  3328. 3. SUBTREE: Show the entire subtree, including body text.
  3329. - When there is a numeric prefix, go up to a heading with level ARG, do
  3330. a `show-subtree' and return to the previous cursor position. If ARG
  3331. is negative, go up that many levels.
  3332. - When point is not at the beginning of a headline, execute
  3333. `indent-relative', like TAB normally does. See the option
  3334. `org-cycle-emulate-tab' for details.
  3335. - Special case: if point is at the beginning of the buffer and there is
  3336. no headline in line 1, this function will act as if called with prefix arg.
  3337. But only if also the variable `org-cycle-global-at-bob' is t."
  3338. (interactive "P")
  3339. (org-load-modules-maybe)
  3340. (let* ((outline-regexp
  3341. (if (and (org-mode-p) org-cycle-include-plain-lists)
  3342. "\\(?:\\*+ \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"
  3343. outline-regexp))
  3344. (bob-special (and org-cycle-global-at-bob (bobp)
  3345. (not (looking-at outline-regexp))))
  3346. (org-cycle-hook
  3347. (if bob-special
  3348. (delq 'org-optimize-window-after-visibility-change
  3349. (copy-sequence org-cycle-hook))
  3350. org-cycle-hook))
  3351. (pos (point)))
  3352. (if (or bob-special (equal arg '(4)))
  3353. ;; special case: use global cycling
  3354. (setq arg t))
  3355. (cond
  3356. ((org-at-table-p 'any)
  3357. ;; Enter the table or move to the next field in the table
  3358. (or (org-table-recognize-table.el)
  3359. (progn
  3360. (if arg (org-table-edit-field t)
  3361. (org-table-justify-field-maybe)
  3362. (call-interactively 'org-table-next-field)))))
  3363. ((eq arg t) ;; Global cycling
  3364. (cond
  3365. ((and (eq last-command this-command)
  3366. (eq org-cycle-global-status 'overview))
  3367. ;; We just created the overview - now do table of contents
  3368. ;; This can be slow in very large buffers, so indicate action
  3369. (message "CONTENTS...")
  3370. (org-content)
  3371. (message "CONTENTS...done")
  3372. (setq org-cycle-global-status 'contents)
  3373. (run-hook-with-args 'org-cycle-hook 'contents))
  3374. ((and (eq last-command this-command)
  3375. (eq org-cycle-global-status 'contents))
  3376. ;; We just showed the table of contents - now show everything
  3377. (show-all)
  3378. (message "SHOW ALL")
  3379. (setq org-cycle-global-status 'all)
  3380. (run-hook-with-args 'org-cycle-hook 'all))
  3381. (t
  3382. ;; Default action: go to overview
  3383. (org-overview)
  3384. (message "OVERVIEW")
  3385. (setq org-cycle-global-status 'overview)
  3386. (run-hook-with-args 'org-cycle-hook 'overview))))
  3387. ((and org-drawers org-drawer-regexp
  3388. (save-excursion
  3389. (beginning-of-line 1)
  3390. (looking-at org-drawer-regexp)))
  3391. ;; Toggle block visibility
  3392. (org-flag-drawer
  3393. (not (get-char-property (match-end 0) 'invisible))))
  3394. ((integerp arg)
  3395. ;; Show-subtree, ARG levels up from here.
  3396. (save-excursion
  3397. (org-back-to-heading)
  3398. (outline-up-heading (if (< arg 0) (- arg)
  3399. (- (funcall outline-level) arg)))
  3400. (org-show-subtree)))
  3401. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  3402. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  3403. ;; At a heading: rotate between three different views
  3404. (org-back-to-heading)
  3405. (let ((goal-column 0) eoh eol eos)
  3406. ;; First, some boundaries
  3407. (save-excursion
  3408. (org-back-to-heading)
  3409. (save-excursion
  3410. (beginning-of-line 2)
  3411. (while (and (not (eobp)) ;; this is like `next-line'
  3412. (get-char-property (1- (point)) 'invisible))
  3413. (beginning-of-line 2)) (setq eol (point)))
  3414. (outline-end-of-heading) (setq eoh (point))
  3415. (org-end-of-subtree t)
  3416. (unless (eobp)
  3417. (skip-chars-forward " \t\n")
  3418. (beginning-of-line 1) ; in case this is an item
  3419. )
  3420. (setq eos (1- (point))))
  3421. ;; Find out what to do next and set `this-command'
  3422. (cond
  3423. ((= eos eoh)
  3424. ;; Nothing is hidden behind this heading
  3425. (message "EMPTY ENTRY")
  3426. (setq org-cycle-subtree-status nil)
  3427. (save-excursion
  3428. (goto-char eos)
  3429. (outline-next-heading)
  3430. (if (org-invisible-p) (org-flag-heading nil))))
  3431. ((or (>= eol eos)
  3432. (not (string-match "\\S-" (buffer-substring eol eos))))
  3433. ;; Entire subtree is hidden in one line: open it
  3434. (org-show-entry)
  3435. (show-children)
  3436. (message "CHILDREN")
  3437. (save-excursion
  3438. (goto-char eos)
  3439. (outline-next-heading)
  3440. (if (org-invisible-p) (org-flag-heading nil)))
  3441. (setq org-cycle-subtree-status 'children)
  3442. (run-hook-with-args 'org-cycle-hook 'children))
  3443. ((and (eq last-command this-command)
  3444. (eq org-cycle-subtree-status 'children))
  3445. ;; We just showed the children, now show everything.
  3446. (org-show-subtree)
  3447. (message "SUBTREE")
  3448. (setq org-cycle-subtree-status 'subtree)
  3449. (run-hook-with-args 'org-cycle-hook 'subtree))
  3450. (t
  3451. ;; Default action: hide the subtree.
  3452. (hide-subtree)
  3453. (message "FOLDED")
  3454. (setq org-cycle-subtree-status 'folded)
  3455. (run-hook-with-args 'org-cycle-hook 'folded)))))
  3456. ;; TAB emulation
  3457. (buffer-read-only (org-back-to-heading))
  3458. ((org-try-cdlatex-tab))
  3459. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  3460. (or (not (bolp))
  3461. (not (looking-at outline-regexp))))
  3462. (call-interactively (global-key-binding "\t")))
  3463. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  3464. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  3465. (or (and (eq org-cycle-emulate-tab 'white)
  3466. (= (match-end 0) (point-at-eol)))
  3467. (and (eq org-cycle-emulate-tab 'whitestart)
  3468. (>= (match-end 0) pos))))
  3469. t
  3470. (eq org-cycle-emulate-tab t))
  3471. (call-interactively (global-key-binding "\t")))
  3472. (t (save-excursion
  3473. (org-back-to-heading)
  3474. (org-cycle))))))
  3475. ;;;###autoload
  3476. (defun org-global-cycle (&optional arg)
  3477. "Cycle the global visibility. For details see `org-cycle'."
  3478. (interactive "P")
  3479. (let ((org-cycle-include-plain-lists
  3480. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  3481. (if (integerp arg)
  3482. (progn
  3483. (show-all)
  3484. (hide-sublevels arg)
  3485. (setq org-cycle-global-status 'contents))
  3486. (org-cycle '(4)))))
  3487. (defun org-overview ()
  3488. "Switch to overview mode, shoing only top-level headlines.
  3489. Really, this shows all headlines with level equal or greater than the level
  3490. of the first headline in the buffer. This is important, because if the
  3491. first headline is not level one, then (hide-sublevels 1) gives confusing
  3492. results."
  3493. (interactive)
  3494. (let ((level (save-excursion
  3495. (goto-char (point-min))
  3496. (if (re-search-forward (concat "^" outline-regexp) nil t)
  3497. (progn
  3498. (goto-char (match-beginning 0))
  3499. (funcall outline-level))))))
  3500. (and level (hide-sublevels level))))
  3501. (defun org-content (&optional arg)
  3502. "Show all headlines in the buffer, like a table of contents.
  3503. With numerical argument N, show content up to level N."
  3504. (interactive "P")
  3505. (save-excursion
  3506. ;; Visit all headings and show their offspring
  3507. (and (integerp arg) (org-overview))
  3508. (goto-char (point-max))
  3509. (catch 'exit
  3510. (while (and (progn (condition-case nil
  3511. (outline-previous-visible-heading 1)
  3512. (error (goto-char (point-min))))
  3513. t)
  3514. (looking-at outline-regexp))
  3515. (if (integerp arg)
  3516. (show-children (1- arg))
  3517. (show-branches))
  3518. (if (bobp) (throw 'exit nil))))))
  3519. (defun org-optimize-window-after-visibility-change (state)
  3520. "Adjust the window after a change in outline visibility.
  3521. This function is the default value of the hook `org-cycle-hook'."
  3522. (when (get-buffer-window (current-buffer))
  3523. (cond
  3524. ; ((eq state 'overview) (org-first-headline-recenter 1))
  3525. ; ((eq state 'overview) (org-beginning-of-line))
  3526. ((eq state 'content) nil)
  3527. ((eq state 'all) nil)
  3528. ((eq state 'folded) nil)
  3529. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  3530. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  3531. (defun org-compact-display-after-subtree-move ()
  3532. (let (beg end)
  3533. (save-excursion
  3534. (if (org-up-heading-safe)
  3535. (progn
  3536. (hide-subtree)
  3537. (show-entry)
  3538. (show-children)
  3539. (org-cycle-show-empty-lines 'children)
  3540. (org-cycle-hide-drawers 'children))
  3541. (org-overview)))))
  3542. (defun org-cycle-show-empty-lines (state)
  3543. "Show empty lines above all visible headlines.
  3544. The region to be covered depends on STATE when called through
  3545. `org-cycle-hook'. Lisp program can use t for STATE to get the
  3546. entire buffer covered. Note that an empty line is only shown if there
  3547. are at least `org-cycle-separator-lines' empty lines before the headeline."
  3548. (when (> org-cycle-separator-lines 0)
  3549. (save-excursion
  3550. (let* ((n org-cycle-separator-lines)
  3551. (re (cond
  3552. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  3553. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  3554. (t (let ((ns (number-to-string (- n 2))))
  3555. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  3556. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  3557. beg end)
  3558. (cond
  3559. ((memq state '(overview contents t))
  3560. (setq beg (point-min) end (point-max)))
  3561. ((memq state '(children folded))
  3562. (setq beg (point) end (progn (org-end-of-subtree t t)
  3563. (beginning-of-line 2)
  3564. (point)))))
  3565. (when beg
  3566. (goto-char beg)
  3567. (while (re-search-forward re end t)
  3568. (if (not (get-char-property (match-end 1) 'invisible))
  3569. (outline-flag-region
  3570. (match-beginning 1) (match-end 1) nil)))))))
  3571. ;; Never hide empty lines at the end of the file.
  3572. (save-excursion
  3573. (goto-char (point-max))
  3574. (outline-previous-heading)
  3575. (outline-end-of-heading)
  3576. (if (and (looking-at "[ \t\n]+")
  3577. (= (match-end 0) (point-max)))
  3578. (outline-flag-region (point) (match-end 0) nil))))
  3579. (defun org-cycle-hide-drawers (state)
  3580. "Re-hide all drawers after a visibility state change."
  3581. (when (and (org-mode-p)
  3582. (not (memq state '(overview folded))))
  3583. (save-excursion
  3584. (let* ((globalp (memq state '(contents all)))
  3585. (beg (if globalp (point-min) (point)))
  3586. (end (if globalp (point-max) (org-end-of-subtree t))))
  3587. (goto-char beg)
  3588. (while (re-search-forward org-drawer-regexp end t)
  3589. (org-flag-drawer t))))))
  3590. (defun org-flag-drawer (flag)
  3591. (save-excursion
  3592. (beginning-of-line 1)
  3593. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  3594. (let ((b (match-end 0))
  3595. (outline-regexp org-outline-regexp))
  3596. (if (re-search-forward
  3597. "^[ \t]*:END:"
  3598. (save-excursion (outline-next-heading) (point)) t)
  3599. (outline-flag-region b (point-at-eol) flag)
  3600. (error ":END: line missing"))))))
  3601. (defun org-subtree-end-visible-p ()
  3602. "Is the end of the current subtree visible?"
  3603. (pos-visible-in-window-p
  3604. (save-excursion (org-end-of-subtree t) (point))))
  3605. (defun org-first-headline-recenter (&optional N)
  3606. "Move cursor to the first headline and recenter the headline.
  3607. Optional argument N means, put the headline into the Nth line of the window."
  3608. (goto-char (point-min))
  3609. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  3610. (beginning-of-line)
  3611. (recenter (prefix-numeric-value N))))
  3612. ;;; Org-goto
  3613. (defvar org-goto-window-configuration nil)
  3614. (defvar org-goto-marker nil)
  3615. (defvar org-goto-map
  3616. (let ((map (make-sparse-keymap)))
  3617. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  3618. (while (setq cmd (pop cmds))
  3619. (substitute-key-definition cmd cmd map global-map)))
  3620. (suppress-keymap map)
  3621. (org-defkey map "\C-m" 'org-goto-ret)
  3622. (org-defkey map [(return)] 'org-goto-ret)
  3623. (org-defkey map [(left)] 'org-goto-left)
  3624. (org-defkey map [(right)] 'org-goto-right)
  3625. (org-defkey map [(control ?g)] 'org-goto-quit)
  3626. (org-defkey map "\C-i" 'org-cycle)
  3627. (org-defkey map [(tab)] 'org-cycle)
  3628. (org-defkey map [(down)] 'outline-next-visible-heading)
  3629. (org-defkey map [(up)] 'outline-previous-visible-heading)
  3630. (if org-goto-auto-isearch
  3631. (if (fboundp 'define-key-after)
  3632. (define-key-after map [t] 'org-goto-local-auto-isearch)
  3633. nil)
  3634. (org-defkey map "q" 'org-goto-quit)
  3635. (org-defkey map "n" 'outline-next-visible-heading)
  3636. (org-defkey map "p" 'outline-previous-visible-heading)
  3637. (org-defkey map "f" 'outline-forward-same-level)
  3638. (org-defkey map "b" 'outline-backward-same-level)
  3639. (org-defkey map "u" 'outline-up-heading))
  3640. (org-defkey map "/" 'org-occur)
  3641. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  3642. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  3643. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  3644. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  3645. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  3646. map))
  3647. (defconst org-goto-help
  3648. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  3649. RET=jump to location [Q]uit and return to previous location
  3650. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  3651. (defvar org-goto-start-pos) ; dynamically scoped parameter
  3652. (defun org-goto (&optional alternative-interface)
  3653. "Look up a different location in the current file, keeping current visibility.
  3654. When you want look-up or go to a different location in a document, the
  3655. fastest way is often to fold the entire buffer and then dive into the tree.
  3656. This method has the disadvantage, that the previous location will be folded,
  3657. which may not be what you want.
  3658. This command works around this by showing a copy of the current buffer
  3659. in an indirect buffer, in overview mode. You can dive into the tree in
  3660. that copy, use org-occur and incremental search to find a location.
  3661. When pressing RET or `Q', the command returns to the original buffer in
  3662. which the visibility is still unchanged. After RET is will also jump to
  3663. the location selected in the indirect buffer and expose the
  3664. the headline hierarchy above."
  3665. (interactive "P")
  3666. (let* ((org-refile-targets '((nil . (:maxlevel . 10))))
  3667. (org-refile-use-outline-path t)
  3668. (interface
  3669. (if (not alternative-interface)
  3670. org-goto-interface
  3671. (if (eq org-goto-interface 'outline)
  3672. 'outline-path-completion
  3673. 'outline)))
  3674. (org-goto-start-pos (point))
  3675. (selected-point
  3676. (if (eq interface 'outline)
  3677. (car (org-get-location (current-buffer) org-goto-help))
  3678. (nth 3 (org-refile-get-location "Goto: ")))))
  3679. (if selected-point
  3680. (progn
  3681. (org-mark-ring-push org-goto-start-pos)
  3682. (goto-char selected-point)
  3683. (if (or (org-invisible-p) (org-invisible-p2))
  3684. (org-show-context 'org-goto)))
  3685. (message "Quit"))))
  3686. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  3687. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  3688. (defvar org-goto-local-auto-isearch-map) ; defined below
  3689. (defun org-get-location (buf help)
  3690. "Let the user select a location in the Org-mode buffer BUF.
  3691. This function uses a recursive edit. It returns the selected position
  3692. or nil."
  3693. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  3694. (isearch-hide-immediately nil)
  3695. (isearch-search-fun-function
  3696. (lambda () 'org-goto-local-search-forward-headings))
  3697. (org-goto-selected-point org-goto-exit-command))
  3698. (save-excursion
  3699. (save-window-excursion
  3700. (delete-other-windows)
  3701. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  3702. (switch-to-buffer
  3703. (condition-case nil
  3704. (make-indirect-buffer (current-buffer) "*org-goto*")
  3705. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  3706. (with-output-to-temp-buffer "*Help*"
  3707. (princ help))
  3708. (shrink-window-if-larger-than-buffer (get-buffer-window "*Help*"))
  3709. (setq buffer-read-only nil)
  3710. (let ((org-startup-truncated t)
  3711. (org-startup-folded nil)
  3712. (org-startup-align-all-tables nil))
  3713. (org-mode)
  3714. (org-overview))
  3715. (setq buffer-read-only t)
  3716. (if (and (boundp 'org-goto-start-pos)
  3717. (integer-or-marker-p org-goto-start-pos))
  3718. (let ((org-show-hierarchy-above t)
  3719. (org-show-siblings t)
  3720. (org-show-following-heading t))
  3721. (goto-char org-goto-start-pos)
  3722. (and (org-invisible-p) (org-show-context)))
  3723. (goto-char (point-min)))
  3724. (org-beginning-of-line)
  3725. (message "Select location and press RET")
  3726. (use-local-map org-goto-map)
  3727. (recursive-edit)
  3728. ))
  3729. (kill-buffer "*org-goto*")
  3730. (cons org-goto-selected-point org-goto-exit-command)))
  3731. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  3732. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  3733. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  3734. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  3735. (defun org-goto-local-search-forward-headings (string bound noerror)
  3736. "Search and make sure that anu matches are in headlines."
  3737. (catch 'return
  3738. (while (search-forward string bound noerror)
  3739. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  3740. (and (member :headline context)
  3741. (not (member :tags context))))
  3742. (throw 'return (point))))))
  3743. (defun org-goto-local-auto-isearch ()
  3744. "Start isearch."
  3745. (interactive)
  3746. (goto-char (point-min))
  3747. (let ((keys (this-command-keys)))
  3748. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  3749. (isearch-mode t)
  3750. (isearch-process-search-char (string-to-char keys)))))
  3751. (defun org-goto-ret (&optional arg)
  3752. "Finish `org-goto' by going to the new location."
  3753. (interactive "P")
  3754. (setq org-goto-selected-point (point)
  3755. org-goto-exit-command 'return)
  3756. (throw 'exit nil))
  3757. (defun org-goto-left ()
  3758. "Finish `org-goto' by going to the new location."
  3759. (interactive)
  3760. (if (org-on-heading-p)
  3761. (progn
  3762. (beginning-of-line 1)
  3763. (setq org-goto-selected-point (point)
  3764. org-goto-exit-command 'left)
  3765. (throw 'exit nil))
  3766. (error "Not on a heading")))
  3767. (defun org-goto-right ()
  3768. "Finish `org-goto' by going to the new location."
  3769. (interactive)
  3770. (if (org-on-heading-p)
  3771. (progn
  3772. (setq org-goto-selected-point (point)
  3773. org-goto-exit-command 'right)
  3774. (throw 'exit nil))
  3775. (error "Not on a heading")))
  3776. (defun org-goto-quit ()
  3777. "Finish `org-goto' without cursor motion."
  3778. (interactive)
  3779. (setq org-goto-selected-point nil)
  3780. (setq org-goto-exit-command 'quit)
  3781. (throw 'exit nil))
  3782. ;;; Indirect buffer display of subtrees
  3783. (defvar org-indirect-dedicated-frame nil
  3784. "This is the frame being used for indirect tree display.")
  3785. (defvar org-last-indirect-buffer nil)
  3786. (defun org-tree-to-indirect-buffer (&optional arg)
  3787. "Create indirect buffer and narrow it to current subtree.
  3788. With numerical prefix ARG, go up to this level and then take that tree.
  3789. If ARG is negative, go up that many levels.
  3790. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  3791. indirect buffer previously made with this command, to avoid proliferation of
  3792. indirect buffers. However, when you call the command with a `C-u' prefix, or
  3793. when `org-indirect-buffer-display' is `new-frame', the last buffer
  3794. is kept so that you can work with several indirect buffers at the same time.
  3795. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  3796. requests that a new frame be made for the new buffer, so that the dedicated
  3797. frame is not changed."
  3798. (interactive "P")
  3799. (let ((cbuf (current-buffer))
  3800. (cwin (selected-window))
  3801. (pos (point))
  3802. beg end level heading ibuf)
  3803. (save-excursion
  3804. (org-back-to-heading t)
  3805. (when (numberp arg)
  3806. (setq level (org-outline-level))
  3807. (if (< arg 0) (setq arg (+ level arg)))
  3808. (while (> (setq level (org-outline-level)) arg)
  3809. (outline-up-heading 1 t)))
  3810. (setq beg (point)
  3811. heading (org-get-heading))
  3812. (org-end-of-subtree t) (setq end (point)))
  3813. (if (and (buffer-live-p org-last-indirect-buffer)
  3814. (not (eq org-indirect-buffer-display 'new-frame))
  3815. (not arg))
  3816. (kill-buffer org-last-indirect-buffer))
  3817. (setq ibuf (org-get-indirect-buffer cbuf)
  3818. org-last-indirect-buffer ibuf)
  3819. (cond
  3820. ((or (eq org-indirect-buffer-display 'new-frame)
  3821. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  3822. (select-frame (make-frame))
  3823. (delete-other-windows)
  3824. (switch-to-buffer ibuf)
  3825. (org-set-frame-title heading))
  3826. ((eq org-indirect-buffer-display 'dedicated-frame)
  3827. (raise-frame
  3828. (select-frame (or (and org-indirect-dedicated-frame
  3829. (frame-live-p org-indirect-dedicated-frame)
  3830. org-indirect-dedicated-frame)
  3831. (setq org-indirect-dedicated-frame (make-frame)))))
  3832. (delete-other-windows)
  3833. (switch-to-buffer ibuf)
  3834. (org-set-frame-title (concat "Indirect: " heading)))
  3835. ((eq org-indirect-buffer-display 'current-window)
  3836. (switch-to-buffer ibuf))
  3837. ((eq org-indirect-buffer-display 'other-window)
  3838. (pop-to-buffer ibuf))
  3839. (t (error "Invalid value.")))
  3840. (if (featurep 'xemacs)
  3841. (save-excursion (org-mode) (turn-on-font-lock)))
  3842. (narrow-to-region beg end)
  3843. (show-all)
  3844. (goto-char pos)
  3845. (and (window-live-p cwin) (select-window cwin))))
  3846. (defun org-get-indirect-buffer (&optional buffer)
  3847. (setq buffer (or buffer (current-buffer)))
  3848. (let ((n 1) (base (buffer-name buffer)) bname)
  3849. (while (buffer-live-p
  3850. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  3851. (setq n (1+ n)))
  3852. (condition-case nil
  3853. (make-indirect-buffer buffer bname 'clone)
  3854. (error (make-indirect-buffer buffer bname)))))
  3855. (defun org-set-frame-title (title)
  3856. "Set the title of the current frame to the string TITLE."
  3857. ;; FIXME: how to name a single frame in XEmacs???
  3858. (unless (featurep 'xemacs)
  3859. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  3860. ;;;; Structure editing
  3861. ;;; Inserting headlines
  3862. (defun org-insert-heading (&optional force-heading)
  3863. "Insert a new heading or item with same depth at point.
  3864. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  3865. If point is at the beginning of a headline, insert a sibling before the
  3866. current headline. If point is not at the beginning, do not split the line,
  3867. but create the new hedline after the current line."
  3868. (interactive "P")
  3869. (if (= (buffer-size) 0)
  3870. (insert "\n* ")
  3871. (when (or force-heading (not (org-insert-item)))
  3872. (let* ((head (save-excursion
  3873. (condition-case nil
  3874. (progn
  3875. (org-back-to-heading)
  3876. (match-string 0))
  3877. (error "*"))))
  3878. (blank (cdr (assq 'heading org-blank-before-new-entry)))
  3879. pos)
  3880. (cond
  3881. ((and (org-on-heading-p) (bolp)
  3882. (or (bobp)
  3883. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  3884. ;; insert before the current line
  3885. (open-line (if blank 2 1)))
  3886. ((and (bolp)
  3887. (or (bobp)
  3888. (save-excursion
  3889. (backward-char 1) (not (org-invisible-p)))))
  3890. ;; insert right here
  3891. nil)
  3892. (t
  3893. ;; in the middle of the line
  3894. (org-show-entry)
  3895. (let ((split
  3896. (org-get-alist-option org-M-RET-may-split-line 'headline))
  3897. tags pos)
  3898. (if (org-on-heading-p)
  3899. (progn
  3900. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  3901. (setq tags (and (match-end 2) (match-string 2)))
  3902. (and (match-end 1)
  3903. (delete-region (match-beginning 1) (match-end 1)))
  3904. (setq pos (point-at-bol))
  3905. (or split (end-of-line 1))
  3906. (delete-horizontal-space)
  3907. (newline (if blank 2 1))
  3908. (when tags
  3909. (save-excursion
  3910. (goto-char pos)
  3911. (end-of-line 1)
  3912. (insert " " tags)
  3913. (org-set-tags nil 'align))))
  3914. (or split (end-of-line 1))
  3915. (newline (if blank 2 1))))))
  3916. (insert head) (just-one-space)
  3917. (setq pos (point))
  3918. (end-of-line 1)
  3919. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  3920. (run-hooks 'org-insert-heading-hook)))))
  3921. (defun org-get-heading (&optional no-tags)
  3922. "Return the heading of the current entry, without the stars."
  3923. (save-excursion
  3924. (org-back-to-heading t)
  3925. (if (looking-at
  3926. (if no-tags
  3927. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  3928. "\\*+[ \t]+\\([^\r\n]*\\)"))
  3929. (match-string 1) "")))
  3930. (defun org-insert-heading-after-current ()
  3931. "Insert a new heading with same level as current, after current subtree."
  3932. (interactive)
  3933. (org-back-to-heading)
  3934. (org-insert-heading)
  3935. (org-move-subtree-down)
  3936. (end-of-line 1))
  3937. (defun org-insert-todo-heading (arg)
  3938. "Insert a new heading with the same level and TODO state as current heading.
  3939. If the heading has no TODO state, or if the state is DONE, use the first
  3940. state (TODO by default). Also with prefix arg, force first state."
  3941. (interactive "P")
  3942. (when (not (org-insert-item 'checkbox))
  3943. (org-insert-heading)
  3944. (save-excursion
  3945. (org-back-to-heading)
  3946. (outline-previous-heading)
  3947. (looking-at org-todo-line-regexp))
  3948. (if (or arg
  3949. (not (match-beginning 2))
  3950. (member (match-string 2) org-done-keywords))
  3951. (insert (car org-todo-keywords-1) " ")
  3952. (insert (match-string 2) " "))))
  3953. (defun org-insert-subheading (arg)
  3954. "Insert a new subheading and demote it.
  3955. Works for outline headings and for plain lists alike."
  3956. (interactive "P")
  3957. (org-insert-heading arg)
  3958. (cond
  3959. ((org-on-heading-p) (org-do-demote))
  3960. ((org-at-item-p) (org-indent-item 1))))
  3961. (defun org-insert-todo-subheading (arg)
  3962. "Insert a new subheading with TODO keyword or checkbox and demote it.
  3963. Works for outline headings and for plain lists alike."
  3964. (interactive "P")
  3965. (org-insert-todo-heading arg)
  3966. (cond
  3967. ((org-on-heading-p) (org-do-demote))
  3968. ((org-at-item-p) (org-indent-item 1))))
  3969. ;;; Promotion and Demotion
  3970. (defun org-promote-subtree ()
  3971. "Promote the entire subtree.
  3972. See also `org-promote'."
  3973. (interactive)
  3974. (save-excursion
  3975. (org-map-tree 'org-promote))
  3976. (org-fix-position-after-promote))
  3977. (defun org-demote-subtree ()
  3978. "Demote the entire subtree. See `org-demote'.
  3979. See also `org-promote'."
  3980. (interactive)
  3981. (save-excursion
  3982. (org-map-tree 'org-demote))
  3983. (org-fix-position-after-promote))
  3984. (defun org-do-promote ()
  3985. "Promote the current heading higher up the tree.
  3986. If the region is active in `transient-mark-mode', promote all headings
  3987. in the region."
  3988. (interactive)
  3989. (save-excursion
  3990. (if (org-region-active-p)
  3991. (org-map-region 'org-promote (region-beginning) (region-end))
  3992. (org-promote)))
  3993. (org-fix-position-after-promote))
  3994. (defun org-do-demote ()
  3995. "Demote the current heading lower down the tree.
  3996. If the region is active in `transient-mark-mode', demote all headings
  3997. in the region."
  3998. (interactive)
  3999. (save-excursion
  4000. (if (org-region-active-p)
  4001. (org-map-region 'org-demote (region-beginning) (region-end))
  4002. (org-demote)))
  4003. (org-fix-position-after-promote))
  4004. (defun org-fix-position-after-promote ()
  4005. "Make sure that after pro/demotion cursor position is right."
  4006. (let ((pos (point)))
  4007. (when (save-excursion
  4008. (beginning-of-line 1)
  4009. (looking-at org-todo-line-regexp)
  4010. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  4011. (cond ((eobp) (insert " "))
  4012. ((eolp) (insert " "))
  4013. ((equal (char-after) ?\ ) (forward-char 1))))))
  4014. (defun org-reduced-level (l)
  4015. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  4016. (defun org-get-valid-level (level &optional change)
  4017. "Rectify a level change under the influence of `org-odd-levels-only'
  4018. LEVEL is a current level, CHANGE is by how much the level should be
  4019. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  4020. even level numbers will become the next higher odd number."
  4021. (if org-odd-levels-only
  4022. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  4023. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  4024. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  4025. (max 1 (+ level change))))
  4026. (if (boundp 'define-obsolete-function-alias)
  4027. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  4028. (define-obsolete-function-alias 'org-get-legal-level
  4029. 'org-get-valid-level)
  4030. (define-obsolete-function-alias 'org-get-legal-level
  4031. 'org-get-valid-level "23.1")))
  4032. (defun org-promote ()
  4033. "Promote the current heading higher up the tree.
  4034. If the region is active in `transient-mark-mode', promote all headings
  4035. in the region."
  4036. (org-back-to-heading t)
  4037. (let* ((level (save-match-data (funcall outline-level)))
  4038. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  4039. (diff (abs (- level (length up-head) -1))))
  4040. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  4041. (replace-match up-head nil t)
  4042. ;; Fixup tag positioning
  4043. (and org-auto-align-tags (org-set-tags nil t))
  4044. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  4045. (defun org-demote ()
  4046. "Demote the current heading lower down the tree.
  4047. If the region is active in `transient-mark-mode', demote all headings
  4048. in the region."
  4049. (org-back-to-heading t)
  4050. (let* ((level (save-match-data (funcall outline-level)))
  4051. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  4052. (diff (abs (- level (length down-head) -1))))
  4053. (replace-match down-head nil t)
  4054. ;; Fixup tag positioning
  4055. (and org-auto-align-tags (org-set-tags nil t))
  4056. (if org-adapt-indentation (org-fixup-indentation diff))))
  4057. (defun org-map-tree (fun)
  4058. "Call FUN for every heading underneath the current one."
  4059. (org-back-to-heading)
  4060. (let ((level (funcall outline-level)))
  4061. (save-excursion
  4062. (funcall fun)
  4063. (while (and (progn
  4064. (outline-next-heading)
  4065. (> (funcall outline-level) level))
  4066. (not (eobp)))
  4067. (funcall fun)))))
  4068. (defun org-map-region (fun beg end)
  4069. "Call FUN for every heading between BEG and END."
  4070. (let ((org-ignore-region t))
  4071. (save-excursion
  4072. (setq end (copy-marker end))
  4073. (goto-char beg)
  4074. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  4075. (< (point) end))
  4076. (funcall fun))
  4077. (while (and (progn
  4078. (outline-next-heading)
  4079. (< (point) end))
  4080. (not (eobp)))
  4081. (funcall fun)))))
  4082. (defun org-fixup-indentation (diff)
  4083. "Change the indentation in the current entry by DIFF
  4084. However, if any line in the current entry has no indentation, or if it
  4085. would end up with no indentation after the change, nothing at all is done."
  4086. (save-excursion
  4087. (let ((end (save-excursion (outline-next-heading)
  4088. (point-marker)))
  4089. (prohibit (if (> diff 0)
  4090. "^\\S-"
  4091. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  4092. col)
  4093. (unless (save-excursion (end-of-line 1)
  4094. (re-search-forward prohibit end t))
  4095. (while (and (< (point) end)
  4096. (re-search-forward "^[ \t]+" end t))
  4097. (goto-char (match-end 0))
  4098. (setq col (current-column))
  4099. (if (< diff 0) (replace-match ""))
  4100. (indent-to (+ diff col))))
  4101. (move-marker end nil))))
  4102. (defun org-convert-to-odd-levels ()
  4103. "Convert an org-mode file with all levels allowed to one with odd levels.
  4104. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  4105. level 5 etc."
  4106. (interactive)
  4107. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  4108. (let ((org-odd-levels-only nil) n)
  4109. (save-excursion
  4110. (goto-char (point-min))
  4111. (while (re-search-forward "^\\*\\*+ " nil t)
  4112. (setq n (- (length (match-string 0)) 2))
  4113. (while (>= (setq n (1- n)) 0)
  4114. (org-demote))
  4115. (end-of-line 1))))))
  4116. (defun org-convert-to-oddeven-levels ()
  4117. "Convert an org-mode file with only odd levels to one with odd and even levels.
  4118. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  4119. section with an even level, conversion would destroy the structure of the file. An error
  4120. is signaled in this case."
  4121. (interactive)
  4122. (goto-char (point-min))
  4123. ;; First check if there are no even levels
  4124. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  4125. (org-show-context t)
  4126. (error "Not all levels are odd in this file. Conversion not possible."))
  4127. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  4128. (let ((org-odd-levels-only nil) n)
  4129. (save-excursion
  4130. (goto-char (point-min))
  4131. (while (re-search-forward "^\\*\\*+ " nil t)
  4132. (setq n (/ (1- (length (match-string 0))) 2))
  4133. (while (>= (setq n (1- n)) 0)
  4134. (org-promote))
  4135. (end-of-line 1))))))
  4136. (defun org-tr-level (n)
  4137. "Make N odd if required."
  4138. (if org-odd-levels-only (1+ (/ n 2)) n))
  4139. ;;; Vertical tree motion, cutting and pasting of subtrees
  4140. (defun org-move-subtree-up (&optional arg)
  4141. "Move the current subtree up past ARG headlines of the same level."
  4142. (interactive "p")
  4143. (org-move-subtree-down (- (prefix-numeric-value arg))))
  4144. (defun org-move-subtree-down (&optional arg)
  4145. "Move the current subtree down past ARG headlines of the same level."
  4146. (interactive "p")
  4147. (setq arg (prefix-numeric-value arg))
  4148. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  4149. 'outline-get-last-sibling))
  4150. (ins-point (make-marker))
  4151. (cnt (abs arg))
  4152. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  4153. ;; Select the tree
  4154. (org-back-to-heading)
  4155. (setq beg0 (point))
  4156. (save-excursion
  4157. (setq ne-beg (org-back-over-empty-lines))
  4158. (setq beg (point)))
  4159. (save-match-data
  4160. (save-excursion (outline-end-of-heading)
  4161. (setq folded (org-invisible-p)))
  4162. (outline-end-of-subtree))
  4163. (outline-next-heading)
  4164. (setq ne-end (org-back-over-empty-lines))
  4165. (setq end (point))
  4166. (goto-char beg0)
  4167. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  4168. ;; include less whitespace
  4169. (save-excursion
  4170. (goto-char beg)
  4171. (forward-line (- ne-beg ne-end))
  4172. (setq beg (point))))
  4173. ;; Find insertion point, with error handling
  4174. (while (> cnt 0)
  4175. (or (and (funcall movfunc) (looking-at outline-regexp))
  4176. (progn (goto-char beg0)
  4177. (error "Cannot move past superior level or buffer limit")))
  4178. (setq cnt (1- cnt)))
  4179. (if (> arg 0)
  4180. ;; Moving forward - still need to move over subtree
  4181. (progn (org-end-of-subtree t t)
  4182. (save-excursion
  4183. (org-back-over-empty-lines)
  4184. (or (bolp) (newline)))))
  4185. (setq ne-ins (org-back-over-empty-lines))
  4186. (move-marker ins-point (point))
  4187. (setq txt (buffer-substring beg end))
  4188. (delete-region beg end)
  4189. (outline-flag-region (1- beg) beg nil)
  4190. (outline-flag-region (1- (point)) (point) nil)
  4191. (insert txt)
  4192. (or (bolp) (insert "\n"))
  4193. (setq ins-end (point))
  4194. (goto-char ins-point)
  4195. (org-skip-whitespace)
  4196. (when (and (< arg 0)
  4197. (org-first-sibling-p)
  4198. (> ne-ins ne-beg))
  4199. ;; Move whitespace back to beginning
  4200. (save-excursion
  4201. (goto-char ins-end)
  4202. (let ((kill-whole-line t))
  4203. (kill-line (- ne-ins ne-beg)) (point)))
  4204. (insert (make-string (- ne-ins ne-beg) ?\n)))
  4205. (move-marker ins-point nil)
  4206. (org-compact-display-after-subtree-move)
  4207. (unless folded
  4208. (org-show-entry)
  4209. (show-children)
  4210. (org-cycle-hide-drawers 'children))))
  4211. (defvar org-subtree-clip ""
  4212. "Clipboard for cut and paste of subtrees.
  4213. This is actually only a copy of the kill, because we use the normal kill
  4214. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  4215. (defvar org-subtree-clip-folded nil
  4216. "Was the last copied subtree folded?
  4217. This is used to fold the tree back after pasting.")
  4218. (defun org-cut-subtree (&optional n)
  4219. "Cut the current subtree into the clipboard.
  4220. With prefix arg N, cut this many sequential subtrees.
  4221. This is a short-hand for marking the subtree and then cutting it."
  4222. (interactive "p")
  4223. (org-copy-subtree n 'cut))
  4224. (defun org-copy-subtree (&optional n cut)
  4225. "Cut the current subtree into the clipboard.
  4226. With prefix arg N, cut this many sequential subtrees.
  4227. This is a short-hand for marking the subtree and then copying it.
  4228. If CUT is non-nil, actually cut the subtree."
  4229. (interactive "p")
  4230. (let (beg end folded (beg0 (point)))
  4231. (if (interactive-p)
  4232. (org-back-to-heading nil) ; take what looks like a subtree
  4233. (org-back-to-heading t)) ; take what is really there
  4234. (org-back-over-empty-lines)
  4235. (setq beg (point))
  4236. (skip-chars-forward " \t\r\n")
  4237. (save-match-data
  4238. (save-excursion (outline-end-of-heading)
  4239. (setq folded (org-invisible-p)))
  4240. (condition-case nil
  4241. (outline-forward-same-level (1- n))
  4242. (error nil))
  4243. (org-end-of-subtree t t))
  4244. (org-back-over-empty-lines)
  4245. (setq end (point))
  4246. (goto-char beg0)
  4247. (when (> end beg)
  4248. (setq org-subtree-clip-folded folded)
  4249. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  4250. (setq org-subtree-clip (current-kill 0))
  4251. (message "%s: Subtree(s) with %d characters"
  4252. (if cut "Cut" "Copied")
  4253. (length org-subtree-clip)))))
  4254. (defun org-paste-subtree (&optional level tree)
  4255. "Paste the clipboard as a subtree, with modification of headline level.
  4256. The entire subtree is promoted or demoted in order to match a new headline
  4257. level. By default, the new level is derived from the visible headings
  4258. before and after the insertion point, and taken to be the inferior headline
  4259. level of the two. So if the previous visible heading is level 3 and the
  4260. next is level 4 (or vice versa), level 4 will be used for insertion.
  4261. This makes sure that the subtree remains an independent subtree and does
  4262. not swallow low level entries.
  4263. You can also force a different level, either by using a numeric prefix
  4264. argument, or by inserting the heading marker by hand. For example, if the
  4265. cursor is after \"*****\", then the tree will be shifted to level 5.
  4266. If you want to insert the tree as is, just use \\[yank].
  4267. If optional TREE is given, use this text instead of the kill ring."
  4268. (interactive "P")
  4269. (unless (org-kill-is-subtree-p tree)
  4270. (error "%s"
  4271. (substitute-command-keys
  4272. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  4273. (let* ((txt (or tree (and kill-ring (current-kill 0))))
  4274. (^re (concat "^\\(" outline-regexp "\\)"))
  4275. (re (concat "\\(" outline-regexp "\\)"))
  4276. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  4277. (old-level (if (string-match ^re txt)
  4278. (- (match-end 0) (match-beginning 0) 1)
  4279. -1))
  4280. (force-level (cond (level (prefix-numeric-value level))
  4281. ((string-match
  4282. ^re_ (buffer-substring (point-at-bol) (point)))
  4283. (- (match-end 1) (match-beginning 1)))
  4284. (t nil)))
  4285. (previous-level (save-excursion
  4286. (condition-case nil
  4287. (progn
  4288. (outline-previous-visible-heading 1)
  4289. (if (looking-at re)
  4290. (- (match-end 0) (match-beginning 0) 1)
  4291. 1))
  4292. (error 1))))
  4293. (next-level (save-excursion
  4294. (condition-case nil
  4295. (progn
  4296. (or (looking-at outline-regexp)
  4297. (outline-next-visible-heading 1))
  4298. (if (looking-at re)
  4299. (- (match-end 0) (match-beginning 0) 1)
  4300. 1))
  4301. (error 1))))
  4302. (new-level (or force-level (max previous-level next-level)))
  4303. (shift (if (or (= old-level -1)
  4304. (= new-level -1)
  4305. (= old-level new-level))
  4306. 0
  4307. (- new-level old-level)))
  4308. (delta (if (> shift 0) -1 1))
  4309. (func (if (> shift 0) 'org-demote 'org-promote))
  4310. (org-odd-levels-only nil)
  4311. beg end)
  4312. ;; Remove the forced level indicator
  4313. (if force-level
  4314. (delete-region (point-at-bol) (point)))
  4315. ;; Paste
  4316. (beginning-of-line 1)
  4317. (org-back-over-empty-lines)
  4318. (setq beg (point))
  4319. (insert-before-markers txt)
  4320. (unless (string-match "\n\\'" txt) (insert "\n"))
  4321. (setq end (point))
  4322. (goto-char beg)
  4323. (skip-chars-forward " \t\n\r")
  4324. (setq beg (point))
  4325. ;; Shift if necessary
  4326. (unless (= shift 0)
  4327. (save-restriction
  4328. (narrow-to-region beg end)
  4329. (while (not (= shift 0))
  4330. (org-map-region func (point-min) (point-max))
  4331. (setq shift (+ delta shift)))
  4332. (goto-char (point-min))))
  4333. (when (interactive-p)
  4334. (message "Clipboard pasted as level %d subtree" new-level))
  4335. (if (and kill-ring
  4336. (eq org-subtree-clip (current-kill 0))
  4337. org-subtree-clip-folded)
  4338. ;; The tree was folded before it was killed/copied
  4339. (hide-subtree))))
  4340. (defun org-kill-is-subtree-p (&optional txt)
  4341. "Check if the current kill is an outline subtree, or a set of trees.
  4342. Returns nil if kill does not start with a headline, or if the first
  4343. headline level is not the largest headline level in the tree.
  4344. So this will actually accept several entries of equal levels as well,
  4345. which is OK for `org-paste-subtree'.
  4346. If optional TXT is given, check this string instead of the current kill."
  4347. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  4348. (start-level (and kill
  4349. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  4350. org-outline-regexp "\\)")
  4351. kill)
  4352. (- (match-end 2) (match-beginning 2) 1)))
  4353. (re (concat "^" org-outline-regexp))
  4354. (start (1+ (match-beginning 2))))
  4355. (if (not start-level)
  4356. (progn
  4357. nil) ;; does not even start with a heading
  4358. (catch 'exit
  4359. (while (setq start (string-match re kill (1+ start)))
  4360. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  4361. (throw 'exit nil)))
  4362. t))))
  4363. (defun org-narrow-to-subtree ()
  4364. "Narrow buffer to the current subtree."
  4365. (interactive)
  4366. (save-excursion
  4367. (save-match-data
  4368. (narrow-to-region
  4369. (progn (org-back-to-heading) (point))
  4370. (progn (org-end-of-subtree t t) (point))))))
  4371. ;;; Outline Sorting
  4372. (defun org-sort (with-case)
  4373. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  4374. Optional argument WITH-CASE means sort case-sensitively."
  4375. (interactive "P")
  4376. (if (org-at-table-p)
  4377. (org-call-with-arg 'org-table-sort-lines with-case)
  4378. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  4379. (defun org-sort-remove-invisible (s)
  4380. (remove-text-properties 0 (length s) org-rm-props s)
  4381. (while (string-match org-bracket-link-regexp s)
  4382. (setq s (replace-match (if (match-end 2)
  4383. (match-string 3 s)
  4384. (match-string 1 s)) t t s)))
  4385. s)
  4386. (defvar org-priority-regexp) ; defined later in the file
  4387. (defun org-sort-entries-or-items (&optional with-case sorting-type getkey-func property)
  4388. "Sort entries on a certain level of an outline tree.
  4389. If there is an active region, the entries in the region are sorted.
  4390. Else, if the cursor is before the first entry, sort the top-level items.
  4391. Else, the children of the entry at point are sorted.
  4392. Sorting can be alphabetically, numerically, and by date/time as given by
  4393. the first time stamp in the entry. The command prompts for the sorting
  4394. type unless it has been given to the function through the SORTING-TYPE
  4395. argument, which needs to a character, any of (?n ?N ?a ?A ?t ?T ?p ?P ?f ?F).
  4396. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  4397. called with point at the beginning of the record. It must return either
  4398. a string or a number that should serve as the sorting key for that record.
  4399. Comparing entries ignores case by default. However, with an optional argument
  4400. WITH-CASE, the sorting considers case as well."
  4401. (interactive "P")
  4402. (let ((case-func (if with-case 'identity 'downcase))
  4403. start beg end stars re re2
  4404. txt what tmp plain-list-p)
  4405. ;; Find beginning and end of region to sort
  4406. (cond
  4407. ((org-region-active-p)
  4408. ;; we will sort the region
  4409. (setq end (region-end)
  4410. what "region")
  4411. (goto-char (region-beginning))
  4412. (if (not (org-on-heading-p)) (outline-next-heading))
  4413. (setq start (point)))
  4414. ((org-at-item-p)
  4415. ;; we will sort this plain list
  4416. (org-beginning-of-item-list) (setq start (point))
  4417. (org-end-of-item-list) (setq end (point))
  4418. (goto-char start)
  4419. (setq plain-list-p t
  4420. what "plain list"))
  4421. ((or (org-on-heading-p)
  4422. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  4423. ;; we will sort the children of the current headline
  4424. (org-back-to-heading)
  4425. (setq start (point)
  4426. end (progn (org-end-of-subtree t t)
  4427. (org-back-over-empty-lines)
  4428. (point))
  4429. what "children")
  4430. (goto-char start)
  4431. (show-subtree)
  4432. (outline-next-heading))
  4433. (t
  4434. ;; we will sort the top-level entries in this file
  4435. (goto-char (point-min))
  4436. (or (org-on-heading-p) (outline-next-heading))
  4437. (setq start (point) end (point-max) what "top-level")
  4438. (goto-char start)
  4439. (show-all)))
  4440. (setq beg (point))
  4441. (if (>= beg end) (error "Nothing to sort"))
  4442. (unless plain-list-p
  4443. (looking-at "\\(\\*+\\)")
  4444. (setq stars (match-string 1)
  4445. re (concat "^" (regexp-quote stars) " +")
  4446. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  4447. txt (buffer-substring beg end))
  4448. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  4449. (if (and (not (equal stars "*")) (string-match re2 txt))
  4450. (error "Region to sort contains a level above the first entry")))
  4451. (unless sorting-type
  4452. (message
  4453. (if plain-list-p
  4454. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  4455. "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:")
  4456. what)
  4457. (setq sorting-type (read-char-exclusive))
  4458. (and (= (downcase sorting-type) ?f)
  4459. (setq getkey-func
  4460. (completing-read "Sort using function: "
  4461. obarray 'fboundp t nil nil))
  4462. (setq getkey-func (intern getkey-func)))
  4463. (and (= (downcase sorting-type) ?r)
  4464. (setq property
  4465. (completing-read "Property: "
  4466. (mapcar 'list (org-buffer-property-keys t))
  4467. nil t))))
  4468. (message "Sorting entries...")
  4469. (save-restriction
  4470. (narrow-to-region start end)
  4471. (let ((dcst (downcase sorting-type))
  4472. (now (current-time)))
  4473. (sort-subr
  4474. (/= dcst sorting-type)
  4475. ;; This function moves to the beginning character of the "record" to
  4476. ;; be sorted.
  4477. (if plain-list-p
  4478. (lambda nil
  4479. (if (org-at-item-p) t (goto-char (point-max))))
  4480. (lambda nil
  4481. (if (re-search-forward re nil t)
  4482. (goto-char (match-beginning 0))
  4483. (goto-char (point-max)))))
  4484. ;; This function moves to the last character of the "record" being
  4485. ;; sorted.
  4486. (if plain-list-p
  4487. 'org-end-of-item
  4488. (lambda nil
  4489. (save-match-data
  4490. (condition-case nil
  4491. (outline-forward-same-level 1)
  4492. (error
  4493. (goto-char (point-max)))))))
  4494. ;; This function returns the value that gets sorted against.
  4495. (if plain-list-p
  4496. (lambda nil
  4497. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  4498. (cond
  4499. ((= dcst ?n)
  4500. (string-to-number (buffer-substring (match-end 0)
  4501. (point-at-eol))))
  4502. ((= dcst ?a)
  4503. (buffer-substring (match-end 0) (point-at-eol)))
  4504. ((= dcst ?t)
  4505. (if (re-search-forward org-ts-regexp
  4506. (point-at-eol) t)
  4507. (org-time-string-to-time (match-string 0))
  4508. now))
  4509. ((= dcst ?f)
  4510. (if getkey-func
  4511. (progn
  4512. (setq tmp (funcall getkey-func))
  4513. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  4514. tmp)
  4515. (error "Invalid key function `%s'" getkey-func)))
  4516. (t (error "Invalid sorting type `%c'" sorting-type)))))
  4517. (lambda nil
  4518. (cond
  4519. ((= dcst ?n)
  4520. (if (looking-at outline-regexp)
  4521. (string-to-number (buffer-substring (match-end 0)
  4522. (point-at-eol)))
  4523. nil))
  4524. ((= dcst ?a)
  4525. (funcall case-func (buffer-substring (point-at-bol)
  4526. (point-at-eol))))
  4527. ((= dcst ?t)
  4528. (if (re-search-forward org-ts-regexp
  4529. (save-excursion
  4530. (forward-line 2)
  4531. (point)) t)
  4532. (org-time-string-to-time (match-string 0))
  4533. now))
  4534. ((= dcst ?p)
  4535. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  4536. (string-to-char (match-string 2))
  4537. org-default-priority))
  4538. ((= dcst ?r)
  4539. (or (org-entry-get nil property) ""))
  4540. ((= dcst ?o)
  4541. (if (looking-at org-complex-heading-regexp)
  4542. (- 9999 (length (member (match-string 2)
  4543. org-todo-keywords-1)))))
  4544. ((= dcst ?f)
  4545. (if getkey-func
  4546. (progn
  4547. (setq tmp (funcall getkey-func))
  4548. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  4549. tmp)
  4550. (error "Invalid key function `%s'" getkey-func)))
  4551. (t (error "Invalid sorting type `%c'" sorting-type)))))
  4552. nil
  4553. (cond
  4554. ((= dcst ?a) 'string<)
  4555. ((= dcst ?t) 'time-less-p)
  4556. (t nil)))))
  4557. (message "Sorting entries...done")))
  4558. (defun org-do-sort (table what &optional with-case sorting-type)
  4559. "Sort TABLE of WHAT according to SORTING-TYPE.
  4560. The user will be prompted for the SORTING-TYPE if the call to this
  4561. function does not specify it. WHAT is only for the prompt, to indicate
  4562. what is being sorted. The sorting key will be extracted from
  4563. the car of the elements of the table.
  4564. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  4565. (unless sorting-type
  4566. (message
  4567. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  4568. what)
  4569. (setq sorting-type (read-char-exclusive)))
  4570. (let ((dcst (downcase sorting-type))
  4571. extractfun comparefun)
  4572. ;; Define the appropriate functions
  4573. (cond
  4574. ((= dcst ?n)
  4575. (setq extractfun 'string-to-number
  4576. comparefun (if (= dcst sorting-type) '< '>)))
  4577. ((= dcst ?a)
  4578. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  4579. (lambda(x) (downcase (org-sort-remove-invisible x))))
  4580. comparefun (if (= dcst sorting-type)
  4581. 'string<
  4582. (lambda (a b) (and (not (string< a b))
  4583. (not (string= a b)))))))
  4584. ((= dcst ?t)
  4585. (setq extractfun
  4586. (lambda (x)
  4587. (if (string-match org-ts-regexp x)
  4588. (time-to-seconds
  4589. (org-time-string-to-time (match-string 0 x)))
  4590. 0))
  4591. comparefun (if (= dcst sorting-type) '< '>)))
  4592. (t (error "Invalid sorting type `%c'" sorting-type)))
  4593. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  4594. table)
  4595. (lambda (a b) (funcall comparefun (car a) (car b))))))
  4596. ;;;; Plain list items, including checkboxes
  4597. ;;; Plain list items
  4598. (defun org-at-item-p ()
  4599. "Is point in a line starting a hand-formatted item?"
  4600. (let ((llt org-plain-list-ordered-item-terminator))
  4601. (save-excursion
  4602. (goto-char (point-at-bol))
  4603. (looking-at
  4604. (cond
  4605. ((eq llt t) "\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  4606. ((= llt ?.) "\\([ \t]*\\([-+]\\|\\([0-9]+\\.\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  4607. ((= llt ?\)) "\\([ \t]*\\([-+]\\|\\([0-9]+))\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  4608. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))))))
  4609. (defun org-in-item-p ()
  4610. "It the cursor inside a plain list item.
  4611. Does not have to be the first line."
  4612. (save-excursion
  4613. (condition-case nil
  4614. (progn
  4615. (org-beginning-of-item)
  4616. (org-at-item-p)
  4617. t)
  4618. (error nil))))
  4619. (defun org-insert-item (&optional checkbox)
  4620. "Insert a new item at the current level.
  4621. Return t when things worked, nil when we are not in an item."
  4622. (when (save-excursion
  4623. (condition-case nil
  4624. (progn
  4625. (org-beginning-of-item)
  4626. (org-at-item-p)
  4627. (if (org-invisible-p) (error "Invisible item"))
  4628. t)
  4629. (error nil)))
  4630. (let* ((bul (match-string 0))
  4631. (eow (save-excursion (beginning-of-line 1) (looking-at "[ \t]*")
  4632. (match-end 0)))
  4633. (blank (cdr (assq 'plain-list-item org-blank-before-new-entry)))
  4634. pos)
  4635. (cond
  4636. ((and (org-at-item-p) (<= (point) eow))
  4637. ;; before the bullet
  4638. (beginning-of-line 1)
  4639. (open-line (if blank 2 1)))
  4640. ((<= (point) eow)
  4641. (beginning-of-line 1))
  4642. (t
  4643. (unless (org-get-alist-option org-M-RET-may-split-line 'item)
  4644. (end-of-line 1)
  4645. (delete-horizontal-space))
  4646. (newline (if blank 2 1))))
  4647. (insert bul (if checkbox "[ ]" ""))
  4648. (just-one-space)
  4649. (setq pos (point))
  4650. (end-of-line 1)
  4651. (unless (= (point) pos) (just-one-space) (backward-delete-char 1)))
  4652. (org-maybe-renumber-ordered-list)
  4653. (and checkbox (org-update-checkbox-count-maybe))
  4654. t))
  4655. ;;; Checkboxes
  4656. (defun org-at-item-checkbox-p ()
  4657. "Is point at a line starting a plain-list item with a checklet?"
  4658. (and (org-at-item-p)
  4659. (save-excursion
  4660. (goto-char (match-end 0))
  4661. (skip-chars-forward " \t")
  4662. (looking-at "\\[[- X]\\]"))))
  4663. (defun org-toggle-checkbox (&optional arg)
  4664. "Toggle the checkbox in the current line."
  4665. (interactive "P")
  4666. (catch 'exit
  4667. (let (beg end status (firstnew 'unknown))
  4668. (cond
  4669. ((org-region-active-p)
  4670. (setq beg (region-beginning) end (region-end)))
  4671. ((org-on-heading-p)
  4672. (setq beg (point) end (save-excursion (outline-next-heading) (point))))
  4673. ((org-at-item-checkbox-p)
  4674. (let ((pos (point)))
  4675. (replace-match
  4676. (cond (arg "[-]")
  4677. ((member (match-string 0) '("[ ]" "[-]")) "[X]")
  4678. (t "[ ]"))
  4679. t t)
  4680. (goto-char pos))
  4681. (throw 'exit t))
  4682. (t (error "Not at a checkbox or heading, and no active region")))
  4683. (save-excursion
  4684. (goto-char beg)
  4685. (while (< (point) end)
  4686. (when (org-at-item-checkbox-p)
  4687. (setq status (equal (match-string 0) "[X]"))
  4688. (when (eq firstnew 'unknown)
  4689. (setq firstnew (not status)))
  4690. (replace-match
  4691. (if (if arg (not status) firstnew) "[X]" "[ ]") t t))
  4692. (beginning-of-line 2)))))
  4693. (org-update-checkbox-count-maybe))
  4694. (defun org-update-checkbox-count-maybe ()
  4695. "Update checkbox statistics unless turned off by user."
  4696. (when org-provide-checkbox-statistics
  4697. (org-update-checkbox-count)))
  4698. (defun org-update-checkbox-count (&optional all)
  4699. "Update the checkbox statistics in the current section.
  4700. This will find all statistic cookies like [57%] and [6/12] and update them
  4701. with the current numbers. With optional prefix argument ALL, do this for
  4702. the whole buffer."
  4703. (interactive "P")
  4704. (save-excursion
  4705. (let* ((buffer-invisibility-spec (org-inhibit-invisibility)) ; Emacs 21
  4706. (beg (condition-case nil
  4707. (progn (outline-back-to-heading) (point))
  4708. (error (point-min))))
  4709. (end (move-marker (make-marker)
  4710. (progn (outline-next-heading) (point))))
  4711. (re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  4712. (re-box "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)")
  4713. (re-find (concat re "\\|" re-box))
  4714. beg-cookie end-cookie is-percent c-on c-off lim
  4715. eline curr-ind next-ind continue-from startsearch
  4716. (cstat 0)
  4717. )
  4718. (when all
  4719. (goto-char (point-min))
  4720. (outline-next-heading)
  4721. (setq beg (point) end (point-max)))
  4722. (goto-char end)
  4723. ;; find each statistic cookie
  4724. (while (re-search-backward re-find beg t)
  4725. (setq beg-cookie (match-beginning 1)
  4726. end-cookie (match-end 1)
  4727. cstat (+ cstat (if end-cookie 1 0))
  4728. startsearch (point-at-eol)
  4729. continue-from (point-at-bol)
  4730. is-percent (match-beginning 2)
  4731. lim (cond
  4732. ((org-on-heading-p) (outline-next-heading) (point))
  4733. ((org-at-item-p) (org-end-of-item) (point))
  4734. (t nil))
  4735. c-on 0
  4736. c-off 0)
  4737. (when lim
  4738. ;; find first checkbox for this cookie and gather
  4739. ;; statistics from all that are at this indentation level
  4740. (goto-char startsearch)
  4741. (if (re-search-forward re-box lim t)
  4742. (progn
  4743. (org-beginning-of-item)
  4744. (setq curr-ind (org-get-indentation))
  4745. (setq next-ind curr-ind)
  4746. (while (and (bolp) (org-at-item-p) (= curr-ind next-ind))
  4747. (save-excursion (end-of-line) (setq eline (point)))
  4748. (if (re-search-forward re-box eline t)
  4749. (if (member (match-string 2) '("[ ]" "[-]"))
  4750. (setq c-off (1+ c-off))
  4751. (setq c-on (1+ c-on))
  4752. )
  4753. )
  4754. (org-end-of-item)
  4755. (setq next-ind (org-get-indentation))
  4756. )))
  4757. (goto-char continue-from)
  4758. ;; update cookie
  4759. (when end-cookie
  4760. (delete-region beg-cookie end-cookie)
  4761. (goto-char beg-cookie)
  4762. (insert
  4763. (if is-percent
  4764. (format "[%d%%]" (/ (* 100 c-on) (max 1 (+ c-on c-off))))
  4765. (format "[%d/%d]" c-on (+ c-on c-off)))))
  4766. ;; update items checkbox if it has one
  4767. (when (org-at-item-p)
  4768. (org-beginning-of-item)
  4769. (when (and (> (+ c-on c-off) 0)
  4770. (re-search-forward re-box (point-at-eol) t))
  4771. (setq beg-cookie (match-beginning 2)
  4772. end-cookie (match-end 2))
  4773. (delete-region beg-cookie end-cookie)
  4774. (goto-char beg-cookie)
  4775. (cond ((= c-off 0) (insert "[X]"))
  4776. ((= c-on 0) (insert "[ ]"))
  4777. (t (insert "[-]")))
  4778. )))
  4779. (goto-char continue-from))
  4780. (when (interactive-p)
  4781. (message "Checkbox satistics updated %s (%d places)"
  4782. (if all "in entire file" "in current outline entry") cstat)))))
  4783. (defun org-get-checkbox-statistics-face ()
  4784. "Select the face for checkbox statistics.
  4785. The face will be `org-done' when all relevant boxes are checked. Otherwise
  4786. it will be `org-todo'."
  4787. (if (match-end 1)
  4788. (if (equal (match-string 1) "100%") 'org-done 'org-todo)
  4789. (if (and (> (match-end 2) (match-beginning 2))
  4790. (equal (match-string 2) (match-string 3)))
  4791. 'org-done
  4792. 'org-todo)))
  4793. (defun org-get-indentation (&optional line)
  4794. "Get the indentation of the current line, interpreting tabs.
  4795. When LINE is given, assume it represents a line and compute its indentation."
  4796. (if line
  4797. (if (string-match "^ *" (org-remove-tabs line))
  4798. (match-end 0))
  4799. (save-excursion
  4800. (beginning-of-line 1)
  4801. (skip-chars-forward " \t")
  4802. (current-column))))
  4803. (defun org-remove-tabs (s &optional width)
  4804. "Replace tabulators in S with spaces.
  4805. Assumes that s is a single line, starting in column 0."
  4806. (setq width (or width tab-width))
  4807. (while (string-match "\t" s)
  4808. (setq s (replace-match
  4809. (make-string
  4810. (- (* width (/ (+ (match-beginning 0) width) width))
  4811. (match-beginning 0)) ?\ )
  4812. t t s)))
  4813. s)
  4814. (defun org-fix-indentation (line ind)
  4815. "Fix indentation in LINE.
  4816. IND is a cons cell with target and minimum indentation.
  4817. If the current indenation in LINE is smaller than the minimum,
  4818. leave it alone. If it is larger than ind, set it to the target."
  4819. (let* ((l (org-remove-tabs line))
  4820. (i (org-get-indentation l))
  4821. (i1 (car ind)) (i2 (cdr ind)))
  4822. (if (>= i i2) (setq l (substring line i2)))
  4823. (if (> i1 0)
  4824. (concat (make-string i1 ?\ ) l)
  4825. l)))
  4826. (defun org-beginning-of-item ()
  4827. "Go to the beginning of the current hand-formatted item.
  4828. If the cursor is not in an item, throw an error."
  4829. (interactive)
  4830. (let ((pos (point))
  4831. (limit (save-excursion
  4832. (condition-case nil
  4833. (progn
  4834. (org-back-to-heading)
  4835. (beginning-of-line 2) (point))
  4836. (error (point-min)))))
  4837. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  4838. ind ind1)
  4839. (if (org-at-item-p)
  4840. (beginning-of-line 1)
  4841. (beginning-of-line 1)
  4842. (skip-chars-forward " \t")
  4843. (setq ind (current-column))
  4844. (if (catch 'exit
  4845. (while t
  4846. (beginning-of-line 0)
  4847. (if (or (bobp) (< (point) limit)) (throw 'exit nil))
  4848. (if (looking-at "[ \t]*$")
  4849. (setq ind1 ind-empty)
  4850. (skip-chars-forward " \t")
  4851. (setq ind1 (current-column)))
  4852. (if (< ind1 ind)
  4853. (progn (beginning-of-line 1) (throw 'exit (org-at-item-p))))))
  4854. nil
  4855. (goto-char pos)
  4856. (error "Not in an item")))))
  4857. (defun org-end-of-item ()
  4858. "Go to the end of the current hand-formatted item.
  4859. If the cursor is not in an item, throw an error."
  4860. (interactive)
  4861. (let* ((pos (point))
  4862. ind1
  4863. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  4864. (limit (save-excursion (outline-next-heading) (point)))
  4865. (ind (save-excursion
  4866. (org-beginning-of-item)
  4867. (skip-chars-forward " \t")
  4868. (current-column)))
  4869. (end (catch 'exit
  4870. (while t
  4871. (beginning-of-line 2)
  4872. (if (eobp) (throw 'exit (point)))
  4873. (if (>= (point) limit) (throw 'exit (point-at-bol)))
  4874. (if (looking-at "[ \t]*$")
  4875. (setq ind1 ind-empty)
  4876. (skip-chars-forward " \t")
  4877. (setq ind1 (current-column)))
  4878. (if (<= ind1 ind)
  4879. (throw 'exit (point-at-bol)))))))
  4880. (if end
  4881. (goto-char end)
  4882. (goto-char pos)
  4883. (error "Not in an item"))))
  4884. (defun org-next-item ()
  4885. "Move to the beginning of the next item in the current plain list.
  4886. Error if not at a plain list, or if this is the last item in the list."
  4887. (interactive)
  4888. (let (ind ind1 (pos (point)))
  4889. (org-beginning-of-item)
  4890. (setq ind (org-get-indentation))
  4891. (org-end-of-item)
  4892. (setq ind1 (org-get-indentation))
  4893. (unless (and (org-at-item-p) (= ind ind1))
  4894. (goto-char pos)
  4895. (error "On last item"))))
  4896. (defun org-previous-item ()
  4897. "Move to the beginning of the previous item in the current plain list.
  4898. Error if not at a plain list, or if this is the first item in the list."
  4899. (interactive)
  4900. (let (beg ind ind1 (pos (point)))
  4901. (org-beginning-of-item)
  4902. (setq beg (point))
  4903. (setq ind (org-get-indentation))
  4904. (goto-char beg)
  4905. (catch 'exit
  4906. (while t
  4907. (beginning-of-line 0)
  4908. (if (looking-at "[ \t]*$")
  4909. nil
  4910. (if (<= (setq ind1 (org-get-indentation)) ind)
  4911. (throw 'exit t)))))
  4912. (condition-case nil
  4913. (if (or (not (org-at-item-p))
  4914. (< ind1 (1- ind)))
  4915. (error "")
  4916. (org-beginning-of-item))
  4917. (error (goto-char pos)
  4918. (error "On first item")))))
  4919. (defun org-first-list-item-p ()
  4920. "Is this heading the item in a plain list?"
  4921. (unless (org-at-item-p)
  4922. (error "Not at a plain list item"))
  4923. (org-beginning-of-item)
  4924. (= (point) (save-excursion (org-beginning-of-item-list))))
  4925. (defun org-move-item-down ()
  4926. "Move the plain list item at point down, i.e. swap with following item.
  4927. Subitems (items with larger indentation) are considered part of the item,
  4928. so this really moves item trees."
  4929. (interactive)
  4930. (let (beg beg0 end end0 ind ind1 (pos (point)) txt ne-end ne-beg)
  4931. (org-beginning-of-item)
  4932. (setq beg0 (point))
  4933. (save-excursion
  4934. (setq ne-beg (org-back-over-empty-lines))
  4935. (setq beg (point)))
  4936. (goto-char beg0)
  4937. (setq ind (org-get-indentation))
  4938. (org-end-of-item)
  4939. (setq end0 (point))
  4940. (setq ind1 (org-get-indentation))
  4941. (setq ne-end (org-back-over-empty-lines))
  4942. (setq end (point))
  4943. (goto-char beg0)
  4944. (when (and (org-first-list-item-p) (< ne-end ne-beg))
  4945. ;; include less whitespace
  4946. (save-excursion
  4947. (goto-char beg)
  4948. (forward-line (- ne-beg ne-end))
  4949. (setq beg (point))))
  4950. (goto-char end0)
  4951. (if (and (org-at-item-p) (= ind ind1))
  4952. (progn
  4953. (org-end-of-item)
  4954. (org-back-over-empty-lines)
  4955. (setq txt (buffer-substring beg end))
  4956. (save-excursion
  4957. (delete-region beg end))
  4958. (setq pos (point))
  4959. (insert txt)
  4960. (goto-char pos) (org-skip-whitespace)
  4961. (org-maybe-renumber-ordered-list))
  4962. (goto-char pos)
  4963. (error "Cannot move this item further down"))))
  4964. (defun org-move-item-up (arg)
  4965. "Move the plain list item at point up, i.e. swap with previous item.
  4966. Subitems (items with larger indentation) are considered part of the item,
  4967. so this really moves item trees."
  4968. (interactive "p")
  4969. (let (beg beg0 end ind ind1 (pos (point)) txt
  4970. ne-beg ne-ins ins-end)
  4971. (org-beginning-of-item)
  4972. (setq beg0 (point))
  4973. (setq ind (org-get-indentation))
  4974. (save-excursion
  4975. (setq ne-beg (org-back-over-empty-lines))
  4976. (setq beg (point)))
  4977. (goto-char beg0)
  4978. (org-end-of-item)
  4979. (setq end (point))
  4980. (goto-char beg0)
  4981. (catch 'exit
  4982. (while t
  4983. (beginning-of-line 0)
  4984. (if (looking-at "[ \t]*$")
  4985. (if org-empty-line-terminates-plain-lists
  4986. (progn
  4987. (goto-char pos)
  4988. (error "Cannot move this item further up"))
  4989. nil)
  4990. (if (<= (setq ind1 (org-get-indentation)) ind)
  4991. (throw 'exit t)))))
  4992. (condition-case nil
  4993. (org-beginning-of-item)
  4994. (error (goto-char beg)
  4995. (error "Cannot move this item further up")))
  4996. (setq ind1 (org-get-indentation))
  4997. (if (and (org-at-item-p) (= ind ind1))
  4998. (progn
  4999. (setq ne-ins (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. (setq ins-end (point))
  5006. (goto-char pos) (org-skip-whitespace)
  5007. (when (and (org-first-list-item-p) (> ne-ins ne-beg))
  5008. ;; Move whitespace back to beginning
  5009. (save-excursion
  5010. (goto-char ins-end)
  5011. (let ((kill-whole-line t))
  5012. (kill-line (- ne-ins ne-beg)) (point)))
  5013. (insert (make-string (- ne-ins ne-beg) ?\n)))
  5014. (org-maybe-renumber-ordered-list))
  5015. (goto-char pos)
  5016. (error "Cannot move this item further up"))))
  5017. (defun org-maybe-renumber-ordered-list ()
  5018. "Renumber the ordered list at point if setup allows it.
  5019. This tests the user option `org-auto-renumber-ordered-lists' before
  5020. doing the renumbering."
  5021. (interactive)
  5022. (when (and org-auto-renumber-ordered-lists
  5023. (org-at-item-p))
  5024. (if (match-beginning 3)
  5025. (org-renumber-ordered-list 1)
  5026. (org-fix-bullet-type))))
  5027. (defun org-maybe-renumber-ordered-list-safe ()
  5028. (condition-case nil
  5029. (save-excursion
  5030. (org-maybe-renumber-ordered-list))
  5031. (error nil)))
  5032. (defun org-cycle-list-bullet (&optional which)
  5033. "Cycle through the different itemize/enumerate bullets.
  5034. This cycle the entire list level through the sequence:
  5035. `-' -> `+' -> `*' -> `1.' -> `1)'
  5036. If WHICH is a string, use that as the new bullet. If WHICH is an integer,
  5037. 0 meand `-', 1 means `+' etc."
  5038. (interactive "P")
  5039. (org-preserve-lc
  5040. (org-beginning-of-item-list)
  5041. (org-at-item-p)
  5042. (beginning-of-line 1)
  5043. (let ((current (match-string 0))
  5044. (prevp (eq which 'previous))
  5045. new)
  5046. (setq new (cond
  5047. ((and (numberp which)
  5048. (nth (1- which) '("-" "+" "*" "1." "1)"))))
  5049. ((string-match "-" current) (if prevp "1)" "+"))
  5050. ((string-match "\\+" current)
  5051. (if prevp "-" (if (looking-at "\\S-") "1." "*")))
  5052. ((string-match "\\*" current) (if prevp "+" "1."))
  5053. ((string-match "\\." current) (if prevp "*" "1)"))
  5054. ((string-match ")" current) (if prevp "1." "-"))
  5055. (t (error "This should not happen"))))
  5056. (and (looking-at "\\([ \t]*\\)\\S-+") (replace-match (concat "\\1" new)))
  5057. (org-fix-bullet-type)
  5058. (org-maybe-renumber-ordered-list))))
  5059. (defun org-get-string-indentation (s)
  5060. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  5061. (let ((n -1) (i 0) (w tab-width) c)
  5062. (catch 'exit
  5063. (while (< (setq n (1+ n)) (length s))
  5064. (setq c (aref s n))
  5065. (cond ((= c ?\ ) (setq i (1+ i)))
  5066. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  5067. (t (throw 'exit t)))))
  5068. i))
  5069. (defun org-renumber-ordered-list (arg)
  5070. "Renumber an ordered plain list.
  5071. Cursor needs to be in the first line of an item, the line that starts
  5072. with something like \"1.\" or \"2)\"."
  5073. (interactive "p")
  5074. (unless (and (org-at-item-p)
  5075. (match-beginning 3))
  5076. (error "This is not an ordered list"))
  5077. (let ((line (org-current-line))
  5078. (col (current-column))
  5079. (ind (org-get-string-indentation
  5080. (buffer-substring (point-at-bol) (match-beginning 3))))
  5081. ;; (term (substring (match-string 3) -1))
  5082. ind1 (n (1- arg))
  5083. fmt)
  5084. ;; find where this list begins
  5085. (org-beginning-of-item-list)
  5086. (looking-at "[ \t]*[0-9]+\\([.)]\\)")
  5087. (setq fmt (concat "%d" (match-string 1)))
  5088. (beginning-of-line 0)
  5089. ;; walk forward and replace these numbers
  5090. (catch 'exit
  5091. (while t
  5092. (catch 'next
  5093. (beginning-of-line 2)
  5094. (if (eobp) (throw 'exit nil))
  5095. (if (looking-at "[ \t]*$") (throw 'next nil))
  5096. (skip-chars-forward " \t") (setq ind1 (current-column))
  5097. (if (> ind1 ind) (throw 'next t))
  5098. (if (< ind1 ind) (throw 'exit t))
  5099. (if (not (org-at-item-p)) (throw 'exit nil))
  5100. (delete-region (match-beginning 2) (match-end 2))
  5101. (goto-char (match-beginning 2))
  5102. (insert (format fmt (setq n (1+ n)))))))
  5103. (goto-line line)
  5104. (org-move-to-column col)))
  5105. (defun org-fix-bullet-type ()
  5106. "Make sure all items in this list have the same bullet as the firsst item."
  5107. (interactive)
  5108. (unless (org-at-item-p) (error "This is not a list"))
  5109. (let ((line (org-current-line))
  5110. (col (current-column))
  5111. (ind (current-indentation))
  5112. ind1 bullet)
  5113. ;; find where this list begins
  5114. (org-beginning-of-item-list)
  5115. (beginning-of-line 1)
  5116. ;; find out what the bullet type is
  5117. (looking-at "[ \t]*\\(\\S-+\\)")
  5118. (setq bullet (match-string 1))
  5119. ;; walk forward and replace these numbers
  5120. (beginning-of-line 0)
  5121. (catch 'exit
  5122. (while t
  5123. (catch 'next
  5124. (beginning-of-line 2)
  5125. (if (eobp) (throw 'exit nil))
  5126. (if (looking-at "[ \t]*$") (throw 'next nil))
  5127. (skip-chars-forward " \t") (setq ind1 (current-column))
  5128. (if (> ind1 ind) (throw 'next t))
  5129. (if (< ind1 ind) (throw 'exit t))
  5130. (if (not (org-at-item-p)) (throw 'exit nil))
  5131. (skip-chars-forward " \t")
  5132. (looking-at "\\S-+")
  5133. (replace-match bullet))))
  5134. (goto-line line)
  5135. (org-move-to-column col)
  5136. (if (string-match "[0-9]" bullet)
  5137. (org-renumber-ordered-list 1))))
  5138. (defun org-beginning-of-item-list ()
  5139. "Go to the beginning of the current item list.
  5140. I.e. to the first item in this list."
  5141. (interactive)
  5142. (org-beginning-of-item)
  5143. (let ((pos (point-at-bol))
  5144. (ind (org-get-indentation))
  5145. ind1)
  5146. ;; find where this list begins
  5147. (catch 'exit
  5148. (while t
  5149. (catch 'next
  5150. (beginning-of-line 0)
  5151. (if (looking-at "[ \t]*$")
  5152. (throw (if (bobp) 'exit 'next) t))
  5153. (skip-chars-forward " \t") (setq ind1 (current-column))
  5154. (if (or (< ind1 ind)
  5155. (and (= ind1 ind)
  5156. (not (org-at-item-p)))
  5157. (bobp))
  5158. (throw 'exit t)
  5159. (when (org-at-item-p) (setq pos (point-at-bol)))))))
  5160. (goto-char pos)))
  5161. (defun org-end-of-item-list ()
  5162. "Go to the end of the current item list.
  5163. I.e. to the text after the last item."
  5164. (interactive)
  5165. (org-beginning-of-item)
  5166. (let ((pos (point-at-bol))
  5167. (ind (org-get-indentation))
  5168. ind1)
  5169. ;; find where this list begins
  5170. (catch 'exit
  5171. (while t
  5172. (catch 'next
  5173. (beginning-of-line 2)
  5174. (if (looking-at "[ \t]*$")
  5175. (throw (if (eobp) 'exit 'next) t))
  5176. (skip-chars-forward " \t") (setq ind1 (current-column))
  5177. (if (or (< ind1 ind)
  5178. (and (= ind1 ind)
  5179. (not (org-at-item-p)))
  5180. (eobp))
  5181. (progn
  5182. (setq pos (point-at-bol))
  5183. (throw 'exit t))))))
  5184. (goto-char pos)))
  5185. (defvar org-last-indent-begin-marker (make-marker))
  5186. (defvar org-last-indent-end-marker (make-marker))
  5187. (defun org-outdent-item (arg)
  5188. "Outdent a local list item."
  5189. (interactive "p")
  5190. (org-indent-item (- arg)))
  5191. (defun org-indent-item (arg)
  5192. "Indent a local list item."
  5193. (interactive "p")
  5194. (unless (org-at-item-p)
  5195. (error "Not on an item"))
  5196. (save-excursion
  5197. (let (beg end ind ind1 tmp delta ind-down ind-up)
  5198. (if (memq last-command '(org-shiftmetaright org-shiftmetaleft))
  5199. (setq beg org-last-indent-begin-marker
  5200. end org-last-indent-end-marker)
  5201. (org-beginning-of-item)
  5202. (setq beg (move-marker org-last-indent-begin-marker (point)))
  5203. (org-end-of-item)
  5204. (setq end (move-marker org-last-indent-end-marker (point))))
  5205. (goto-char beg)
  5206. (setq tmp (org-item-indent-positions)
  5207. ind (car tmp)
  5208. ind-down (nth 2 tmp)
  5209. ind-up (nth 1 tmp)
  5210. delta (if (> arg 0)
  5211. (if ind-down (- ind-down ind) 2)
  5212. (if ind-up (- ind-up ind) -2)))
  5213. (if (< (+ delta ind) 0) (error "Cannot outdent beyond margin"))
  5214. (while (< (point) end)
  5215. (beginning-of-line 1)
  5216. (skip-chars-forward " \t") (setq ind1 (current-column))
  5217. (delete-region (point-at-bol) (point))
  5218. (or (eolp) (org-indent-to-column (+ ind1 delta)))
  5219. (beginning-of-line 2))))
  5220. (org-fix-bullet-type)
  5221. (org-maybe-renumber-ordered-list-safe)
  5222. (save-excursion
  5223. (beginning-of-line 0)
  5224. (condition-case nil (org-beginning-of-item) (error nil))
  5225. (org-maybe-renumber-ordered-list-safe)))
  5226. (defun org-item-indent-positions ()
  5227. "Return indentation for plain list items.
  5228. This returns a list with three values: The current indentation, the
  5229. parent indentation and the indentation a child should habe.
  5230. Assumes cursor in item line."
  5231. (let* ((bolpos (point-at-bol))
  5232. (ind (org-get-indentation))
  5233. ind-down ind-up pos)
  5234. (save-excursion
  5235. (org-beginning-of-item-list)
  5236. (skip-chars-backward "\n\r \t")
  5237. (when (org-in-item-p)
  5238. (org-beginning-of-item)
  5239. (setq ind-up (org-get-indentation))))
  5240. (setq pos (point))
  5241. (save-excursion
  5242. (cond
  5243. ((and (condition-case nil (progn (org-previous-item) t)
  5244. (error nil))
  5245. (or (forward-char 1) t)
  5246. (re-search-forward "^\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)" bolpos t))
  5247. (setq ind-down (org-get-indentation)))
  5248. ((and (goto-char pos)
  5249. (org-at-item-p))
  5250. (goto-char (match-end 0))
  5251. (skip-chars-forward " \t")
  5252. (setq ind-down (current-column)))))
  5253. (list ind ind-up ind-down)))
  5254. ;;; The orgstruct minor mode
  5255. ;; Define a minor mode which can be used in other modes in order to
  5256. ;; integrate the org-mode structure editing commands.
  5257. ;; This is really a hack, because the org-mode structure commands use
  5258. ;; keys which normally belong to the major mode. Here is how it
  5259. ;; works: The minor mode defines all the keys necessary to operate the
  5260. ;; structure commands, but wraps the commands into a function which
  5261. ;; tests if the cursor is currently at a headline or a plain list
  5262. ;; item. If that is the case, the structure command is used,
  5263. ;; temporarily setting many Org-mode variables like regular
  5264. ;; expressions for filling etc. However, when any of those keys is
  5265. ;; used at a different location, function uses `key-binding' to look
  5266. ;; up if the key has an associated command in another currently active
  5267. ;; keymap (minor modes, major mode, global), and executes that
  5268. ;; command. There might be problems if any of the keys is otherwise
  5269. ;; used as a prefix key.
  5270. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  5271. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  5272. ;; addresses this by checking explicitly for both bindings.
  5273. (defvar orgstruct-mode-map (make-sparse-keymap)
  5274. "Keymap for the minor `orgstruct-mode'.")
  5275. (defvar org-local-vars nil
  5276. "List of local variables, for use by `orgstruct-mode'")
  5277. ;;;###autoload
  5278. (define-minor-mode orgstruct-mode
  5279. "Toggle the minor more `orgstruct-mode'.
  5280. This mode is for using Org-mode structure commands in other modes.
  5281. The following key behave as if Org-mode was active, if the cursor
  5282. is on a headline, or on a plain list item (both in the definition
  5283. of Org-mode).
  5284. M-up Move entry/item up
  5285. M-down Move entry/item down
  5286. M-left Promote
  5287. M-right Demote
  5288. M-S-up Move entry/item up
  5289. M-S-down Move entry/item down
  5290. M-S-left Promote subtree
  5291. M-S-right Demote subtree
  5292. M-q Fill paragraph and items like in Org-mode
  5293. C-c ^ Sort entries
  5294. C-c - Cycle list bullet
  5295. TAB Cycle item visibility
  5296. M-RET Insert new heading/item
  5297. S-M-RET Insert new TODO heading / Chekbox item
  5298. C-c C-c Set tags / toggle checkbox"
  5299. nil " OrgStruct" nil
  5300. (org-load-modules-maybe)
  5301. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  5302. ;;;###autoload
  5303. (defun turn-on-orgstruct ()
  5304. "Unconditionally turn on `orgstruct-mode'."
  5305. (orgstruct-mode 1))
  5306. ;;;###autoload
  5307. (defun turn-on-orgstruct++ ()
  5308. "Unconditionally turn on `orgstruct-mode', and force org-mode indentations.
  5309. In addition to setting orgstruct-mode, this also exports all indentation and
  5310. autofilling variables from org-mode into the buffer. Note that turning
  5311. off orgstruct-mode will *not* remove these additional settings."
  5312. (orgstruct-mode 1)
  5313. (let (var val)
  5314. (mapc
  5315. (lambda (x)
  5316. (when (string-match
  5317. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5318. (symbol-name (car x)))
  5319. (setq var (car x) val (nth 1 x))
  5320. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  5321. org-local-vars)))
  5322. (defun orgstruct-error ()
  5323. "Error when there is no default binding for a structure key."
  5324. (interactive)
  5325. (error "This key has no function outside structure elements"))
  5326. (defun orgstruct-setup ()
  5327. "Setup orgstruct keymaps."
  5328. (let ((nfunc 0)
  5329. (bindings
  5330. (list
  5331. '([(meta up)] org-metaup)
  5332. '([(meta down)] org-metadown)
  5333. '([(meta left)] org-metaleft)
  5334. '([(meta right)] org-metaright)
  5335. '([(meta shift up)] org-shiftmetaup)
  5336. '([(meta shift down)] org-shiftmetadown)
  5337. '([(meta shift left)] org-shiftmetaleft)
  5338. '([(meta shift right)] org-shiftmetaright)
  5339. '([(shift up)] org-shiftup)
  5340. '([(shift down)] org-shiftdown)
  5341. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  5342. '("\M-q" fill-paragraph)
  5343. '("\C-c^" org-sort)
  5344. '("\C-c-" org-cycle-list-bullet)))
  5345. elt key fun cmd)
  5346. (while (setq elt (pop bindings))
  5347. (setq nfunc (1+ nfunc))
  5348. (setq key (org-key (car elt))
  5349. fun (nth 1 elt)
  5350. cmd (orgstruct-make-binding fun nfunc key))
  5351. (org-defkey orgstruct-mode-map key cmd))
  5352. ;; Special treatment needed for TAB and RET
  5353. (org-defkey orgstruct-mode-map [(tab)]
  5354. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  5355. (org-defkey orgstruct-mode-map "\C-i"
  5356. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  5357. (org-defkey orgstruct-mode-map "\M-\C-m"
  5358. (orgstruct-make-binding 'org-insert-heading 105
  5359. "\M-\C-m" [(meta return)]))
  5360. (org-defkey orgstruct-mode-map [(meta return)]
  5361. (orgstruct-make-binding 'org-insert-heading 106
  5362. [(meta return)] "\M-\C-m"))
  5363. (org-defkey orgstruct-mode-map [(shift meta return)]
  5364. (orgstruct-make-binding 'org-insert-todo-heading 107
  5365. [(meta return)] "\M-\C-m"))
  5366. (unless org-local-vars
  5367. (setq org-local-vars (org-get-local-variables)))
  5368. t))
  5369. (defun orgstruct-make-binding (fun n &rest keys)
  5370. "Create a function for binding in the structure minor mode.
  5371. FUN is the command to call inside a table. N is used to create a unique
  5372. command name. KEYS are keys that should be checked in for a command
  5373. to execute outside of tables."
  5374. (eval
  5375. (list 'defun
  5376. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  5377. '(arg)
  5378. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  5379. "Outside of structure, run the binding of `"
  5380. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  5381. "'.")
  5382. '(interactive "p")
  5383. (list 'if
  5384. '(org-context-p 'headline 'item)
  5385. (list 'org-run-like-in-org-mode (list 'quote fun))
  5386. (list 'let '(orgstruct-mode)
  5387. (list 'call-interactively
  5388. (append '(or)
  5389. (mapcar (lambda (k)
  5390. (list 'key-binding k))
  5391. keys)
  5392. '('orgstruct-error))))))))
  5393. (defun org-context-p (&rest contexts)
  5394. "Check if local context is and of CONTEXTS.
  5395. Possible values in the list of contexts are `table', `headline', and `item'."
  5396. (let ((pos (point)))
  5397. (goto-char (point-at-bol))
  5398. (prog1 (or (and (memq 'table contexts)
  5399. (looking-at "[ \t]*|"))
  5400. (and (memq 'headline contexts)
  5401. (looking-at "\\*+"))
  5402. (and (memq 'item contexts)
  5403. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)")))
  5404. (goto-char pos))))
  5405. (defun org-get-local-variables ()
  5406. "Return a list of all local variables in an org-mode buffer."
  5407. (let (varlist)
  5408. (with-current-buffer (get-buffer-create "*Org tmp*")
  5409. (erase-buffer)
  5410. (org-mode)
  5411. (setq varlist (buffer-local-variables)))
  5412. (kill-buffer "*Org tmp*")
  5413. (delq nil
  5414. (mapcar
  5415. (lambda (x)
  5416. (setq x
  5417. (if (symbolp x)
  5418. (list x)
  5419. (list (car x) (list 'quote (cdr x)))))
  5420. (if (string-match
  5421. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5422. (symbol-name (car x)))
  5423. x nil))
  5424. varlist))))
  5425. ;;;###autoload
  5426. (defun org-run-like-in-org-mode (cmd)
  5427. (org-load-modules-maybe)
  5428. (unless org-local-vars
  5429. (setq org-local-vars (org-get-local-variables)))
  5430. (eval (list 'let org-local-vars
  5431. (list 'call-interactively (list 'quote cmd)))))
  5432. ;;;; Archiving
  5433. (defun org-get-category (&optional pos)
  5434. "Get the category applying to position POS."
  5435. (get-text-property (or pos (point)) 'org-category))
  5436. (defun org-refresh-category-properties ()
  5437. "Refresh category text properties in the buffer."
  5438. (let ((def-cat (cond
  5439. ((null org-category)
  5440. (if buffer-file-name
  5441. (file-name-sans-extension
  5442. (file-name-nondirectory buffer-file-name))
  5443. "???"))
  5444. ((symbolp org-category) (symbol-name org-category))
  5445. (t org-category)))
  5446. beg end cat pos optionp)
  5447. (org-unmodified
  5448. (save-excursion
  5449. (save-restriction
  5450. (widen)
  5451. (goto-char (point-min))
  5452. (put-text-property (point) (point-max) 'org-category def-cat)
  5453. (while (re-search-forward
  5454. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  5455. (setq pos (match-end 0)
  5456. optionp (equal (char-after (match-beginning 0)) ?#)
  5457. cat (org-trim (match-string 2)))
  5458. (if optionp
  5459. (setq beg (point-at-bol) end (point-max))
  5460. (org-back-to-heading t)
  5461. (setq beg (point) end (org-end-of-subtree t t)))
  5462. (put-text-property beg end 'org-category cat)
  5463. (goto-char pos)))))))
  5464. ;;;; Link Stuff
  5465. ;;; Link abbreviations
  5466. (defun org-link-expand-abbrev (link)
  5467. "Apply replacements as defined in `org-link-abbrev-alist."
  5468. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  5469. (let* ((key (match-string 1 link))
  5470. (as (or (assoc key org-link-abbrev-alist-local)
  5471. (assoc key org-link-abbrev-alist)))
  5472. (tag (and (match-end 2) (match-string 3 link)))
  5473. rpl)
  5474. (if (not as)
  5475. link
  5476. (setq rpl (cdr as))
  5477. (cond
  5478. ((symbolp rpl) (funcall rpl tag))
  5479. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  5480. (t (concat rpl tag)))))
  5481. link))
  5482. ;;; Storing and inserting links
  5483. (defvar org-insert-link-history nil
  5484. "Minibuffer history for links inserted with `org-insert-link'.")
  5485. (defvar org-stored-links nil
  5486. "Contains the links stored with `org-store-link'.")
  5487. (defvar org-store-link-plist nil
  5488. "Plist with info about the most recently link created with `org-store-link'.")
  5489. (defvar org-link-protocols nil
  5490. "Link protocols added to Org-mode using `org-add-link-type'.")
  5491. (defvar org-store-link-functions nil
  5492. "List of functions that are called to create and store a link.
  5493. Each function will be called in turn until one returns a non-nil
  5494. value. Each function should check if it is responsible for creating
  5495. this link (for example by looking at the major mode).
  5496. If not, it must exit and return nil.
  5497. If yes, it should return a non-nil value after a calling
  5498. `org-store-link-props' with a list of properties and values.
  5499. Special properties are:
  5500. :type The link prefix. like \"http\". This must be given.
  5501. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  5502. This is obligatory as well.
  5503. :description Optional default description for the second pair
  5504. of brackets in an Org-mode link. The user can still change
  5505. this when inserting this link into an Org-mode buffer.
  5506. In addition to these, any additional properties can be specified
  5507. and then used in remember templates.")
  5508. (defun org-add-link-type (type &optional follow export)
  5509. "Add TYPE to the list of `org-link-types'.
  5510. Re-compute all regular expressions depending on `org-link-types'
  5511. FOLLOW and EXPORT are two functions.
  5512. FOLLOW should take the link path as the single argument and do whatever
  5513. is necessary to follow the link, for example find a file or display
  5514. a mail message.
  5515. EXPORT should format the link path for export to one of the export formats.
  5516. It should be a function accepting three arguments:
  5517. path the path of the link, the text after the prefix (like \"http:\")
  5518. desc the description of the link, if any, nil if there was no descripton
  5519. format the export format, a symbol like `html' or `latex'.
  5520. The function may use the FORMAT information to return different values
  5521. depending on the format. The return value will be put literally into
  5522. the exported file.
  5523. Org-mode has a built-in default for exporting links. If you are happy with
  5524. this default, there is no need to define an export function for the link
  5525. type. For a simple example of an export function, see `org-bbdb.el'."
  5526. (add-to-list 'org-link-types type t)
  5527. (org-make-link-regexps)
  5528. (if (assoc type org-link-protocols)
  5529. (setcdr (assoc type org-link-protocols) (list follow export))
  5530. (push (list type follow export) org-link-protocols)))
  5531. ;;;###autoload
  5532. (defun org-store-link (arg)
  5533. "\\<org-mode-map>Store an org-link to the current location.
  5534. This link is added to `org-stored-links' and can later be inserted
  5535. into an org-buffer with \\[org-insert-link].
  5536. For some link types, a prefix arg is interpreted:
  5537. For links to usenet articles, arg negates `org-usenet-links-prefer-google'.
  5538. For file links, arg negates `org-context-in-file-links'."
  5539. (interactive "P")
  5540. (org-load-modules-maybe)
  5541. (setq org-store-link-plist nil) ; reset
  5542. (let (link cpltxt desc description search txt)
  5543. (cond
  5544. ((run-hook-with-args-until-success 'org-store-link-functions)
  5545. (setq link (plist-get org-store-link-plist :link)
  5546. desc (or (plist-get org-store-link-plist :description) link)))
  5547. ((eq major-mode 'calendar-mode)
  5548. (let ((cd (calendar-cursor-to-date)))
  5549. (setq link
  5550. (format-time-string
  5551. (car org-time-stamp-formats)
  5552. (apply 'encode-time
  5553. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  5554. nil nil nil))))
  5555. (org-store-link-props :type "calendar" :date cd)))
  5556. ((eq major-mode 'w3-mode)
  5557. (setq cpltxt (url-view-url t)
  5558. link (org-make-link cpltxt))
  5559. (org-store-link-props :type "w3" :url (url-view-url t)))
  5560. ((eq major-mode 'w3m-mode)
  5561. (setq cpltxt (or w3m-current-title w3m-current-url)
  5562. link (org-make-link w3m-current-url))
  5563. (org-store-link-props :type "w3m" :url (url-view-url t)))
  5564. ((setq search (run-hook-with-args-until-success
  5565. 'org-create-file-search-functions))
  5566. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  5567. "::" search))
  5568. (setq cpltxt (or description link)))
  5569. ((eq major-mode 'image-mode)
  5570. (setq cpltxt (concat "file:"
  5571. (abbreviate-file-name buffer-file-name))
  5572. link (org-make-link cpltxt))
  5573. (org-store-link-props :type "image" :file buffer-file-name))
  5574. ((eq major-mode 'dired-mode)
  5575. ;; link to the file in the current line
  5576. (setq cpltxt (concat "file:"
  5577. (abbreviate-file-name
  5578. (expand-file-name
  5579. (dired-get-filename nil t))))
  5580. link (org-make-link cpltxt)))
  5581. ((and buffer-file-name (org-mode-p))
  5582. ;; Just link to current headline
  5583. (setq cpltxt (concat "file:"
  5584. (abbreviate-file-name buffer-file-name)))
  5585. ;; Add a context search string
  5586. (when (org-xor org-context-in-file-links arg)
  5587. ;; Check if we are on a target
  5588. (if (org-in-regexp "<<\\(.*?\\)>>")
  5589. (setq cpltxt (concat cpltxt "::" (match-string 1)))
  5590. (setq txt (cond
  5591. ((org-on-heading-p) nil)
  5592. ((org-region-active-p)
  5593. (buffer-substring (region-beginning) (region-end)))
  5594. (t nil)))
  5595. (when (or (null txt) (string-match "\\S-" txt))
  5596. (setq cpltxt
  5597. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  5598. desc "NONE"))))
  5599. (if (string-match "::\\'" cpltxt)
  5600. (setq cpltxt (substring cpltxt 0 -2)))
  5601. (setq link (org-make-link cpltxt)))
  5602. ((buffer-file-name (buffer-base-buffer))
  5603. ;; Just link to this file here.
  5604. (setq cpltxt (concat "file:"
  5605. (abbreviate-file-name
  5606. (buffer-file-name (buffer-base-buffer)))))
  5607. ;; Add a context string
  5608. (when (org-xor org-context-in-file-links arg)
  5609. (setq txt (if (org-region-active-p)
  5610. (buffer-substring (region-beginning) (region-end))
  5611. (buffer-substring (point-at-bol) (point-at-eol))))
  5612. ;; Only use search option if there is some text.
  5613. (when (string-match "\\S-" txt)
  5614. (setq cpltxt
  5615. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  5616. desc "NONE")))
  5617. (setq link (org-make-link cpltxt)))
  5618. ((interactive-p)
  5619. (error "Cannot link to a buffer which is not visiting a file"))
  5620. (t (setq link nil)))
  5621. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  5622. (setq link (or link cpltxt)
  5623. desc (or desc cpltxt))
  5624. (if (equal desc "NONE") (setq desc nil))
  5625. (if (and (interactive-p) link)
  5626. (progn
  5627. (setq org-stored-links
  5628. (cons (list link desc) org-stored-links))
  5629. (message "Stored: %s" (or desc link)))
  5630. (and link (org-make-link-string link desc)))))
  5631. (defun org-store-link-props (&rest plist)
  5632. "Store link properties, extract names and addresses."
  5633. (let (x adr)
  5634. (when (setq x (plist-get plist :from))
  5635. (setq adr (mail-extract-address-components x))
  5636. (plist-put plist :fromname (car adr))
  5637. (plist-put plist :fromaddress (nth 1 adr)))
  5638. (when (setq x (plist-get plist :to))
  5639. (setq adr (mail-extract-address-components x))
  5640. (plist-put plist :toname (car adr))
  5641. (plist-put plist :toaddress (nth 1 adr))))
  5642. (let ((from (plist-get plist :from))
  5643. (to (plist-get plist :to)))
  5644. (when (and from to org-from-is-user-regexp)
  5645. (plist-put plist :fromto
  5646. (if (string-match org-from-is-user-regexp from)
  5647. (concat "to %t")
  5648. (concat "from %f")))))
  5649. (setq org-store-link-plist plist))
  5650. (defun org-add-link-props (&rest plist)
  5651. "Add these properties to the link property list."
  5652. (let (key value)
  5653. (while plist
  5654. (setq key (pop plist) value (pop plist))
  5655. (setq org-store-link-plist
  5656. (plist-put org-store-link-plist key value)))))
  5657. (defun org-email-link-description (&optional fmt)
  5658. "Return the description part of an email link.
  5659. This takes information from `org-store-link-plist' and formats it
  5660. according to FMT (default from `org-email-link-description-format')."
  5661. (setq fmt (or fmt org-email-link-description-format))
  5662. (let* ((p org-store-link-plist)
  5663. (to (plist-get p :toaddress))
  5664. (from (plist-get p :fromaddress))
  5665. (table
  5666. (list
  5667. (cons "%c" (plist-get p :fromto))
  5668. (cons "%F" (plist-get p :from))
  5669. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  5670. (cons "%T" (plist-get p :to))
  5671. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  5672. (cons "%s" (plist-get p :subject))
  5673. (cons "%m" (plist-get p :message-id)))))
  5674. (when (string-match "%c" fmt)
  5675. ;; Check if the user wrote this message
  5676. (if (and org-from-is-user-regexp from to
  5677. (save-match-data (string-match org-from-is-user-regexp from)))
  5678. (setq fmt (replace-match "to %t" t t fmt))
  5679. (setq fmt (replace-match "from %f" t t fmt))))
  5680. (org-replace-escapes fmt table)))
  5681. (defun org-make-org-heading-search-string (&optional string heading)
  5682. "Make search string for STRING or current headline."
  5683. (interactive)
  5684. (let ((s (or string (org-get-heading))))
  5685. (unless (and string (not heading))
  5686. ;; We are using a headline, clean up garbage in there.
  5687. (if (string-match org-todo-regexp s)
  5688. (setq s (replace-match "" t t s)))
  5689. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  5690. (setq s (replace-match "" t t s)))
  5691. (setq s (org-trim s))
  5692. (if (string-match (concat "^\\(" org-quote-string "\\|"
  5693. org-comment-string "\\)") s)
  5694. (setq s (replace-match "" t t s)))
  5695. (while (string-match org-ts-regexp s)
  5696. (setq s (replace-match "" t t s))))
  5697. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  5698. (setq s (replace-match " " t t s)))
  5699. (or string (setq s (concat "*" s))) ; Add * for headlines
  5700. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  5701. (defun org-make-link (&rest strings)
  5702. "Concatenate STRINGS."
  5703. (apply 'concat strings))
  5704. (defun org-make-link-string (link &optional description)
  5705. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  5706. (unless (string-match "\\S-" link)
  5707. (error "Empty link"))
  5708. (when (stringp description)
  5709. ;; Remove brackets from the description, they are fatal.
  5710. (while (string-match "\\[" description)
  5711. (setq description (replace-match "{" t t description)))
  5712. (while (string-match "\\]" description)
  5713. (setq description (replace-match "}" t t description))))
  5714. (when (equal (org-link-escape link) description)
  5715. ;; No description needed, it is identical
  5716. (setq description nil))
  5717. (when (and (not description)
  5718. (not (equal link (org-link-escape link))))
  5719. (setq description link))
  5720. (concat "[[" (org-link-escape link) "]"
  5721. (if description (concat "[" description "]") "")
  5722. "]"))
  5723. (defconst org-link-escape-chars
  5724. '((?\ . "%20")
  5725. (?\[ . "%5B")
  5726. (?\] . "%5D")
  5727. (?\340 . "%E0") ; `a
  5728. (?\342 . "%E2") ; ^a
  5729. (?\347 . "%E7") ; ,c
  5730. (?\350 . "%E8") ; `e
  5731. (?\351 . "%E9") ; 'e
  5732. (?\352 . "%EA") ; ^e
  5733. (?\356 . "%EE") ; ^i
  5734. (?\364 . "%F4") ; ^o
  5735. (?\371 . "%F9") ; `u
  5736. (?\373 . "%FB") ; ^u
  5737. (?\; . "%3B")
  5738. (?? . "%3F")
  5739. (?= . "%3D")
  5740. (?+ . "%2B")
  5741. )
  5742. "Association list of escapes for some characters problematic in links.
  5743. This is the list that is used for internal purposes.")
  5744. (defconst org-link-escape-chars-browser
  5745. '((?\ . "%20")) ; 32 for the SPC char
  5746. "Association list of escapes for some characters problematic in links.
  5747. This is the list that is used before handing over to the browser.")
  5748. (defun org-link-escape (text &optional table)
  5749. "Escape charaters in TEXT that are problematic for links."
  5750. (setq table (or table org-link-escape-chars))
  5751. (when text
  5752. (let ((re (mapconcat (lambda (x) (regexp-quote
  5753. (char-to-string (car x))))
  5754. table "\\|")))
  5755. (while (string-match re text)
  5756. (setq text
  5757. (replace-match
  5758. (cdr (assoc (string-to-char (match-string 0 text))
  5759. table))
  5760. t t text)))
  5761. text)))
  5762. (defun org-link-unescape (text &optional table)
  5763. "Reverse the action of `org-link-escape'."
  5764. (setq table (or table org-link-escape-chars))
  5765. (when text
  5766. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  5767. table "\\|")))
  5768. (while (string-match re text)
  5769. (setq text
  5770. (replace-match
  5771. (char-to-string (car (rassoc (match-string 0 text) table)))
  5772. t t text)))
  5773. text)))
  5774. (defun org-xor (a b)
  5775. "Exclusive or."
  5776. (if a (not b) b))
  5777. (defun org-get-header (header)
  5778. "Find a header field in the current buffer."
  5779. (save-excursion
  5780. (goto-char (point-min))
  5781. (let ((case-fold-search t) s)
  5782. (cond
  5783. ((eq header 'from)
  5784. (if (re-search-forward "^From:\\s-+\\(.*\\)" nil t)
  5785. (setq s (match-string 1)))
  5786. (while (string-match "\"" s)
  5787. (setq s (replace-match "" t t s)))
  5788. (if (string-match "[<(].*" s)
  5789. (setq s (replace-match "" t t s))))
  5790. ((eq header 'message-id)
  5791. (if (re-search-forward "^message-id:\\s-+\\(.*\\)" nil t)
  5792. (setq s (match-string 1))))
  5793. ((eq header 'subject)
  5794. (if (re-search-forward "^subject:\\s-+\\(.*\\)" nil t)
  5795. (setq s (match-string 1)))))
  5796. (if (string-match "\\`[ \t\]+" s) (setq s (replace-match "" t t s)))
  5797. (if (string-match "[ \t\]+\\'" s) (setq s (replace-match "" t t s)))
  5798. s)))
  5799. (defun org-fixup-message-id-for-http (s)
  5800. "Replace special characters in a message id, so it can be used in an http query."
  5801. (while (string-match "<" s)
  5802. (setq s (replace-match "%3C" t t s)))
  5803. (while (string-match ">" s)
  5804. (setq s (replace-match "%3E" t t s)))
  5805. (while (string-match "@" s)
  5806. (setq s (replace-match "%40" t t s)))
  5807. s)
  5808. ;;;###autoload
  5809. (defun org-insert-link-global ()
  5810. "Insert a link like Org-mode does.
  5811. This command can be called in any mode to insert a link in Org-mode syntax."
  5812. (interactive)
  5813. (org-load-modules-maybe)
  5814. (org-run-like-in-org-mode 'org-insert-link))
  5815. (defun org-insert-link (&optional complete-file link-location)
  5816. "Insert a link. At the prompt, enter the link.
  5817. Completion can be used to select a link previously stored with
  5818. `org-store-link'. When the empty string is entered (i.e. if you just
  5819. press RET at the prompt), the link defaults to the most recently
  5820. stored link. As SPC triggers completion in the minibuffer, you need to
  5821. use M-SPC or C-q SPC to force the insertion of a space character.
  5822. You will also be prompted for a description, and if one is given, it will
  5823. be displayed in the buffer instead of the link.
  5824. If there is already a link at point, this command will allow you to edit link
  5825. and description parts.
  5826. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  5827. be selected using completion. The path to the file will be relative to the
  5828. current directory if the file is in the current directory or a subdirectory.
  5829. Otherwise, the link will be the absolute path as completed in the minibuffer
  5830. \(i.e. normally ~/path/to/file).
  5831. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  5832. the current directory or below. With three \\[universal-argument] prefixes, negate the meaning
  5833. of `org-keep-stored-link-after-insertion'.
  5834. If `org-make-link-description-function' is non-nil, this function will be
  5835. called with the link target, and the result will be the default
  5836. link description.
  5837. If the LINK-LOCATION parameter is non-nil, this value will be
  5838. used as the link location instead of reading one interactively."
  5839. (interactive "P")
  5840. (let* ((wcf (current-window-configuration))
  5841. (region (if (org-region-active-p)
  5842. (buffer-substring (region-beginning) (region-end))))
  5843. (remove (and region (list (region-beginning) (region-end))))
  5844. (desc region)
  5845. tmphist ; byte-compile incorrectly complains about this
  5846. (link link-location)
  5847. entry file)
  5848. (cond
  5849. (link-location) ; specified by arg, just use it.
  5850. ((org-in-regexp org-bracket-link-regexp 1)
  5851. ;; We do have a link at point, and we are going to edit it.
  5852. (setq remove (list (match-beginning 0) (match-end 0)))
  5853. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  5854. (setq link (read-string "Link: "
  5855. (org-link-unescape
  5856. (org-match-string-no-properties 1)))))
  5857. ((or (org-in-regexp org-angle-link-re)
  5858. (org-in-regexp org-plain-link-re))
  5859. ;; Convert to bracket link
  5860. (setq remove (list (match-beginning 0) (match-end 0))
  5861. link (read-string "Link: "
  5862. (org-remove-angle-brackets (match-string 0)))))
  5863. ((equal complete-file '(4))
  5864. ;; Completing read for file names.
  5865. (setq file (read-file-name "File: "))
  5866. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  5867. (pwd1 (file-name-as-directory (abbreviate-file-name
  5868. (expand-file-name ".")))))
  5869. (cond
  5870. ((equal complete-file '(16))
  5871. (setq link (org-make-link
  5872. "file:"
  5873. (abbreviate-file-name (expand-file-name file)))))
  5874. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  5875. (setq link (org-make-link "file:" (match-string 1 file))))
  5876. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  5877. (expand-file-name file))
  5878. (setq link (org-make-link
  5879. "file:" (match-string 1 (expand-file-name file)))))
  5880. (t (setq link (org-make-link "file:" file))))))
  5881. (t
  5882. ;; Read link, with completion for stored links.
  5883. (with-output-to-temp-buffer "*Org Links*"
  5884. (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
  5885. (when org-stored-links
  5886. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  5887. (princ (mapconcat
  5888. (lambda (x)
  5889. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  5890. (reverse org-stored-links) "\n"))))
  5891. (let ((cw (selected-window)))
  5892. (select-window (get-buffer-window "*Org Links*"))
  5893. (shrink-window-if-larger-than-buffer)
  5894. (setq truncate-lines t)
  5895. (select-window cw))
  5896. ;; Fake a link history, containing the stored links.
  5897. (setq tmphist (append (mapcar 'car org-stored-links)
  5898. org-insert-link-history))
  5899. (unwind-protect
  5900. (setq link (org-completing-read
  5901. "Link: "
  5902. (append
  5903. (mapcar (lambda (x) (list (concat (car x) ":")))
  5904. (append org-link-abbrev-alist-local org-link-abbrev-alist))
  5905. (mapcar (lambda (x) (list (concat x ":")))
  5906. org-link-types))
  5907. nil nil nil
  5908. 'tmphist
  5909. (or (car (car org-stored-links)))))
  5910. (set-window-configuration wcf)
  5911. (kill-buffer "*Org Links*"))
  5912. (setq entry (assoc link org-stored-links))
  5913. (or entry (push link org-insert-link-history))
  5914. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  5915. (not org-keep-stored-link-after-insertion))
  5916. (setq org-stored-links (delq (assoc link org-stored-links)
  5917. org-stored-links)))
  5918. (setq desc (or desc (nth 1 entry)))))
  5919. (if (string-match org-plain-link-re link)
  5920. ;; URL-like link, normalize the use of angular brackets.
  5921. (setq link (org-make-link (org-remove-angle-brackets link))))
  5922. ;; Check if we are linking to the current file with a search option
  5923. ;; If yes, simplify the link by using only the search option.
  5924. (when (and buffer-file-name
  5925. (string-match "\\<file:\\(.+?\\)::\\([^>]+\\)" link))
  5926. (let* ((path (match-string 1 link))
  5927. (case-fold-search nil)
  5928. (search (match-string 2 link)))
  5929. (save-match-data
  5930. (if (equal (file-truename buffer-file-name) (file-truename path))
  5931. ;; We are linking to this same file, with a search option
  5932. (setq link search)))))
  5933. ;; Check if we can/should use a relative path. If yes, simplify the link
  5934. (when (string-match "\\<file:\\(.*\\)" link)
  5935. (let* ((path (match-string 1 link))
  5936. (origpath path)
  5937. (case-fold-search nil))
  5938. (cond
  5939. ((eq org-link-file-path-type 'absolute)
  5940. (setq path (abbreviate-file-name (expand-file-name path))))
  5941. ((eq org-link-file-path-type 'noabbrev)
  5942. (setq path (expand-file-name path)))
  5943. ((eq org-link-file-path-type 'relative)
  5944. (setq path (file-relative-name path)))
  5945. (t
  5946. (save-match-data
  5947. (if (string-match (concat "^" (regexp-quote
  5948. (file-name-as-directory
  5949. (expand-file-name "."))))
  5950. (expand-file-name path))
  5951. ;; We are linking a file with relative path name.
  5952. (setq path (substring (expand-file-name path)
  5953. (match-end 0)))))))
  5954. (setq link (concat "file:" path))
  5955. (if (equal desc origpath)
  5956. (setq desc path))))
  5957. (if org-make-link-description-function
  5958. (setq desc (funcall org-make-link-description-function link desc)))
  5959. (setq desc (read-string "Description: " desc))
  5960. (unless (string-match "\\S-" desc) (setq desc nil))
  5961. (if remove (apply 'delete-region remove))
  5962. (insert (org-make-link-string link desc))))
  5963. (defun org-completing-read (&rest args)
  5964. (let ((minibuffer-local-completion-map
  5965. (copy-keymap minibuffer-local-completion-map)))
  5966. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  5967. (apply 'completing-read args)))
  5968. ;;; Opening/following a link
  5969. (defvar org-link-search-failed nil)
  5970. (defun org-next-link ()
  5971. "Move forward to the next link.
  5972. If the link is in hidden text, expose it."
  5973. (interactive)
  5974. (when (and org-link-search-failed (eq this-command last-command))
  5975. (goto-char (point-min))
  5976. (message "Link search wrapped back to beginning of buffer"))
  5977. (setq org-link-search-failed nil)
  5978. (let* ((pos (point))
  5979. (ct (org-context))
  5980. (a (assoc :link ct)))
  5981. (if a (goto-char (nth 2 a)))
  5982. (if (re-search-forward org-any-link-re nil t)
  5983. (progn
  5984. (goto-char (match-beginning 0))
  5985. (if (org-invisible-p) (org-show-context)))
  5986. (goto-char pos)
  5987. (setq org-link-search-failed t)
  5988. (error "No further link found"))))
  5989. (defun org-previous-link ()
  5990. "Move backward to the previous link.
  5991. If the link is in hidden text, expose it."
  5992. (interactive)
  5993. (when (and org-link-search-failed (eq this-command last-command))
  5994. (goto-char (point-max))
  5995. (message "Link search wrapped back to end of buffer"))
  5996. (setq org-link-search-failed nil)
  5997. (let* ((pos (point))
  5998. (ct (org-context))
  5999. (a (assoc :link ct)))
  6000. (if a (goto-char (nth 1 a)))
  6001. (if (re-search-backward org-any-link-re nil t)
  6002. (progn
  6003. (goto-char (match-beginning 0))
  6004. (if (org-invisible-p) (org-show-context)))
  6005. (goto-char pos)
  6006. (setq org-link-search-failed t)
  6007. (error "No further link found"))))
  6008. (defun org-find-file-at-mouse (ev)
  6009. "Open file link or URL at mouse."
  6010. (interactive "e")
  6011. (mouse-set-point ev)
  6012. (org-open-at-point 'in-emacs))
  6013. (defun org-open-at-mouse (ev)
  6014. "Open file link or URL at mouse."
  6015. (interactive "e")
  6016. (mouse-set-point ev)
  6017. (org-open-at-point))
  6018. (defvar org-window-config-before-follow-link nil
  6019. "The window configuration before following a link.
  6020. This is saved in case the need arises to restore it.")
  6021. (defvar org-open-link-marker (make-marker)
  6022. "Marker pointing to the location where `org-open-at-point; was called.")
  6023. ;;;###autoload
  6024. (defun org-open-at-point-global ()
  6025. "Follow a link like Org-mode does.
  6026. This command can be called in any mode to follow a link that has
  6027. Org-mode syntax."
  6028. (interactive)
  6029. (org-run-like-in-org-mode 'org-open-at-point))
  6030. ;;;###autoload
  6031. (defun org-open-link-from-string (s &optional arg)
  6032. "Open a link in the string S, as if it was in Org-mode."
  6033. (interactive "sLink: \nP")
  6034. (with-temp-buffer
  6035. (let ((org-inhibit-startup t))
  6036. (org-mode)
  6037. (insert s)
  6038. (goto-char (point-min))
  6039. (org-open-at-point arg))))
  6040. (defun org-open-at-point (&optional in-emacs)
  6041. "Open link at or after point.
  6042. If there is no link at point, this function will search forward up to
  6043. the end of the current subtree.
  6044. Normally, files will be opened by an appropriate application. If the
  6045. optional argument IN-EMACS is non-nil, Emacs will visit the file."
  6046. (interactive "P")
  6047. (org-load-modules-maybe)
  6048. (move-marker org-open-link-marker (point))
  6049. (setq org-window-config-before-follow-link (current-window-configuration))
  6050. (org-remove-occur-highlights nil nil t)
  6051. (if (org-at-timestamp-p t)
  6052. (org-follow-timestamp-link)
  6053. (let (type path link line search (pos (point)))
  6054. (catch 'match
  6055. (save-excursion
  6056. (skip-chars-forward "^]\n\r")
  6057. (when (org-in-regexp org-bracket-link-regexp)
  6058. (setq link (org-link-unescape (org-match-string-no-properties 1)))
  6059. (while (string-match " *\n *" link)
  6060. (setq link (replace-match " " t t link)))
  6061. (setq link (org-link-expand-abbrev link))
  6062. (if (string-match org-link-re-with-space2 link)
  6063. (setq type (match-string 1 link) path (match-string 2 link))
  6064. (setq type "thisfile" path link))
  6065. (throw 'match t)))
  6066. (when (get-text-property (point) 'org-linked-text)
  6067. (setq type "thisfile"
  6068. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  6069. (1+ (point)) (point))
  6070. path (buffer-substring
  6071. (previous-single-property-change pos 'org-linked-text)
  6072. (next-single-property-change pos 'org-linked-text)))
  6073. (throw 'match t))
  6074. (save-excursion
  6075. (when (or (org-in-regexp org-angle-link-re)
  6076. (org-in-regexp org-plain-link-re))
  6077. (setq type (match-string 1) path (match-string 2))
  6078. (throw 'match t)))
  6079. (when (org-in-regexp "\\<\\([^><\n]+\\)\\>")
  6080. (setq type "tree-match"
  6081. path (match-string 1))
  6082. (throw 'match t))
  6083. (save-excursion
  6084. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  6085. (setq type "tags"
  6086. path (match-string 1))
  6087. (while (string-match ":" path)
  6088. (setq path (replace-match "+" t t path)))
  6089. (throw 'match t))))
  6090. (unless path
  6091. (error "No link found"))
  6092. ;; Remove any trailing spaces in path
  6093. (if (string-match " +\\'" path)
  6094. (setq path (replace-match "" t t path)))
  6095. (cond
  6096. ((assoc type org-link-protocols)
  6097. (funcall (nth 1 (assoc type org-link-protocols)) path))
  6098. ((equal type "mailto")
  6099. (let ((cmd (car org-link-mailto-program))
  6100. (args (cdr org-link-mailto-program)) args1
  6101. (address path) (subject "") a)
  6102. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  6103. (setq address (match-string 1 path)
  6104. subject (org-link-escape (match-string 2 path))))
  6105. (while args
  6106. (cond
  6107. ((not (stringp (car args))) (push (pop args) args1))
  6108. (t (setq a (pop args))
  6109. (if (string-match "%a" a)
  6110. (setq a (replace-match address t t a)))
  6111. (if (string-match "%s" a)
  6112. (setq a (replace-match subject t t a)))
  6113. (push a args1))))
  6114. (apply cmd (nreverse args1))))
  6115. ((member type '("http" "https" "ftp" "news"))
  6116. (browse-url (concat type ":" (org-link-escape
  6117. path org-link-escape-chars-browser))))
  6118. ((member type '("message"))
  6119. (browse-url (concat type ":" path)))
  6120. ((string= type "tags")
  6121. (org-tags-view in-emacs path))
  6122. ((string= type "thisfile")
  6123. (if in-emacs
  6124. (switch-to-buffer-other-window
  6125. (org-get-buffer-for-internal-link (current-buffer)))
  6126. (org-mark-ring-push))
  6127. (let ((cmd `(org-link-search
  6128. ,path
  6129. ,(cond ((equal in-emacs '(4)) 'occur)
  6130. ((equal in-emacs '(16)) 'org-occur)
  6131. (t nil))
  6132. ,pos)))
  6133. (condition-case nil (eval cmd)
  6134. (error (progn (widen) (eval cmd))))))
  6135. ((string= type "tree-match")
  6136. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  6137. ((string= type "file")
  6138. (if (string-match "::\\([0-9]+\\)\\'" path)
  6139. (setq line (string-to-number (match-string 1 path))
  6140. path (substring path 0 (match-beginning 0)))
  6141. (if (string-match "::\\(.+\\)\\'" path)
  6142. (setq search (match-string 1 path)
  6143. path (substring path 0 (match-beginning 0)))))
  6144. (if (string-match "[*?{]" (file-name-nondirectory path))
  6145. (dired path)
  6146. (org-open-file path in-emacs line search)))
  6147. ((string= type "news")
  6148. (require 'org-gnus)
  6149. (org-gnus-follow-link path))
  6150. ((string= type "shell")
  6151. (let ((cmd path))
  6152. (if (or (not org-confirm-shell-link-function)
  6153. (funcall org-confirm-shell-link-function
  6154. (format "Execute \"%s\" in shell? "
  6155. (org-add-props cmd nil
  6156. 'face 'org-warning))))
  6157. (progn
  6158. (message "Executing %s" cmd)
  6159. (shell-command cmd))
  6160. (error "Abort"))))
  6161. ((string= type "elisp")
  6162. (let ((cmd path))
  6163. (if (or (not org-confirm-elisp-link-function)
  6164. (funcall org-confirm-elisp-link-function
  6165. (format "Execute \"%s\" as elisp? "
  6166. (org-add-props cmd nil
  6167. 'face 'org-warning))))
  6168. (message "%s => %s" cmd (eval (read cmd)))
  6169. (error "Abort"))))
  6170. (t
  6171. (browse-url-at-point)))))
  6172. (move-marker org-open-link-marker nil)
  6173. (run-hook-with-args 'org-follow-link-hook))
  6174. ;;;; Time estimates
  6175. (defun org-get-effort (&optional pom)
  6176. "Get the effort estimate for the current entry."
  6177. (org-entry-get pom org-effort-property))
  6178. ;;; File search
  6179. (defvar org-create-file-search-functions nil
  6180. "List of functions to construct the right search string for a file link.
  6181. These functions are called in turn with point at the location to
  6182. which the link should point.
  6183. A function in the hook should first test if it would like to
  6184. handle this file type, for example by checking the major-mode or
  6185. the file extension. If it decides not to handle this file, it
  6186. should just return nil to give other functions a chance. If it
  6187. does handle the file, it must return the search string to be used
  6188. when following the link. The search string will be part of the
  6189. file link, given after a double colon, and `org-open-at-point'
  6190. will automatically search for it. If special measures must be
  6191. taken to make the search successful, another function should be
  6192. added to the companion hook `org-execute-file-search-functions',
  6193. which see.
  6194. A function in this hook may also use `setq' to set the variable
  6195. `description' to provide a suggestion for the descriptive text to
  6196. be used for this link when it gets inserted into an Org-mode
  6197. buffer with \\[org-insert-link].")
  6198. (defvar org-execute-file-search-functions nil
  6199. "List of functions to execute a file search triggered by a link.
  6200. Functions added to this hook must accept a single argument, the
  6201. search string that was part of the file link, the part after the
  6202. double colon. The function must first check if it would like to
  6203. handle this search, for example by checking the major-mode or the
  6204. file extension. If it decides not to handle this search, it
  6205. should just return nil to give other functions a chance. If it
  6206. does handle the search, it must return a non-nil value to keep
  6207. other functions from trying.
  6208. Each function can access the current prefix argument through the
  6209. variable `current-prefix-argument'. Note that a single prefix is
  6210. used to force opening a link in Emacs, so it may be good to only
  6211. use a numeric or double prefix to guide the search function.
  6212. In case this is needed, a function in this hook can also restore
  6213. the window configuration before `org-open-at-point' was called using:
  6214. (set-window-configuration org-window-config-before-follow-link)")
  6215. (defun org-link-search (s &optional type avoid-pos)
  6216. "Search for a link search option.
  6217. If S is surrounded by forward slashes, it is interpreted as a
  6218. regular expression. In org-mode files, this will create an `org-occur'
  6219. sparse tree. In ordinary files, `occur' will be used to list matches.
  6220. If the current buffer is in `dired-mode', grep will be used to search
  6221. in all files. If AVOID-POS is given, ignore matches near that position."
  6222. (let ((case-fold-search t)
  6223. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  6224. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  6225. (append '(("") (" ") ("\t") ("\n"))
  6226. org-emphasis-alist)
  6227. "\\|") "\\)"))
  6228. (pos (point))
  6229. (pre nil) (post nil)
  6230. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  6231. (cond
  6232. ;; First check if there are any special
  6233. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  6234. ;; Now try the builtin stuff
  6235. ((save-excursion
  6236. (goto-char (point-min))
  6237. (and
  6238. (re-search-forward
  6239. (concat "<<" (regexp-quote s0) ">>") nil t)
  6240. (setq type 'dedicated
  6241. pos (match-beginning 0))))
  6242. ;; There is an exact target for this
  6243. (goto-char pos))
  6244. ((string-match "^/\\(.*\\)/$" s)
  6245. ;; A regular expression
  6246. (cond
  6247. ((org-mode-p)
  6248. (org-occur (match-string 1 s)))
  6249. ;;((eq major-mode 'dired-mode)
  6250. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  6251. (t (org-do-occur (match-string 1 s)))))
  6252. (t
  6253. ;; A normal search strings
  6254. (when (equal (string-to-char s) ?*)
  6255. ;; Anchor on headlines, post may include tags.
  6256. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  6257. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  6258. s (substring s 1)))
  6259. (remove-text-properties
  6260. 0 (length s)
  6261. '(face nil mouse-face nil keymap nil fontified nil) s)
  6262. ;; Make a series of regular expressions to find a match
  6263. (setq words (org-split-string s "[ \n\r\t]+")
  6264. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  6265. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  6266. "\\)" markers)
  6267. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  6268. re2a (concat "[ \t\r\n]" re2a_)
  6269. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  6270. re4 (concat "[^a-zA-Z_]" re4_)
  6271. re1 (concat pre re2 post)
  6272. re3 (concat pre (if pre re4_ re4) post)
  6273. re5 (concat pre ".*" re4)
  6274. re2 (concat pre re2)
  6275. re2a (concat pre (if pre re2a_ re2a))
  6276. re4 (concat pre (if pre re4_ re4))
  6277. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  6278. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  6279. re5 "\\)"
  6280. ))
  6281. (cond
  6282. ((eq type 'org-occur) (org-occur reall))
  6283. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  6284. (t (goto-char (point-min))
  6285. (setq type 'fuzzy)
  6286. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  6287. (org-search-not-self 1 re1 nil t)
  6288. (org-search-not-self 1 re2 nil t)
  6289. (org-search-not-self 1 re2a nil t)
  6290. (org-search-not-self 1 re3 nil t)
  6291. (org-search-not-self 1 re4 nil t)
  6292. (org-search-not-self 1 re5 nil t)
  6293. )
  6294. (goto-char (match-beginning 1))
  6295. (goto-char pos)
  6296. (error "No match")))))
  6297. (t
  6298. ;; Normal string-search
  6299. (goto-char (point-min))
  6300. (if (search-forward s nil t)
  6301. (goto-char (match-beginning 0))
  6302. (error "No match"))))
  6303. (and (org-mode-p) (org-show-context 'link-search))
  6304. type))
  6305. (defun org-search-not-self (group &rest args)
  6306. "Execute `re-search-forward', but only accept matches that do not
  6307. enclose the position of `org-open-link-marker'."
  6308. (let ((m org-open-link-marker))
  6309. (catch 'exit
  6310. (while (apply 're-search-forward args)
  6311. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  6312. (goto-char (match-end group))
  6313. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  6314. (> (match-beginning 0) (marker-position m))
  6315. (< (match-end 0) (marker-position m)))
  6316. (save-match-data
  6317. (or (not (org-in-regexp
  6318. org-bracket-link-analytic-regexp 1))
  6319. (not (match-end 4)) ; no description
  6320. (and (<= (match-beginning 4) (point))
  6321. (>= (match-end 4) (point))))))
  6322. (throw 'exit (point))))))))
  6323. (defun org-get-buffer-for-internal-link (buffer)
  6324. "Return a buffer to be used for displaying the link target of internal links."
  6325. (cond
  6326. ((not org-display-internal-link-with-indirect-buffer)
  6327. buffer)
  6328. ((string-match "(Clone)$" (buffer-name buffer))
  6329. (message "Buffer is already a clone, not making another one")
  6330. ;; we also do not modify visibility in this case
  6331. buffer)
  6332. (t ; make a new indirect buffer for displaying the link
  6333. (let* ((bn (buffer-name buffer))
  6334. (ibn (concat bn "(Clone)"))
  6335. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  6336. (with-current-buffer ib (org-overview))
  6337. ib))))
  6338. (defun org-do-occur (regexp &optional cleanup)
  6339. "Call the Emacs command `occur'.
  6340. If CLEANUP is non-nil, remove the printout of the regular expression
  6341. in the *Occur* buffer. This is useful if the regex is long and not useful
  6342. to read."
  6343. (occur regexp)
  6344. (when cleanup
  6345. (let ((cwin (selected-window)) win beg end)
  6346. (when (setq win (get-buffer-window "*Occur*"))
  6347. (select-window win))
  6348. (goto-char (point-min))
  6349. (when (re-search-forward "match[a-z]+" nil t)
  6350. (setq beg (match-end 0))
  6351. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  6352. (setq end (1- (match-beginning 0)))))
  6353. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  6354. (goto-char (point-min))
  6355. (select-window cwin))))
  6356. ;;; The mark ring for links jumps
  6357. (defvar org-mark-ring nil
  6358. "Mark ring for positions before jumps in Org-mode.")
  6359. (defvar org-mark-ring-last-goto nil
  6360. "Last position in the mark ring used to go back.")
  6361. ;; Fill and close the ring
  6362. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  6363. (loop for i from 1 to org-mark-ring-length do
  6364. (push (make-marker) org-mark-ring))
  6365. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  6366. org-mark-ring)
  6367. (defun org-mark-ring-push (&optional pos buffer)
  6368. "Put the current position or POS into the mark ring and rotate it."
  6369. (interactive)
  6370. (setq pos (or pos (point)))
  6371. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  6372. (move-marker (car org-mark-ring)
  6373. (or pos (point))
  6374. (or buffer (current-buffer)))
  6375. (message "%s"
  6376. (substitute-command-keys
  6377. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  6378. (defun org-mark-ring-goto (&optional n)
  6379. "Jump to the previous position in the mark ring.
  6380. With prefix arg N, jump back that many stored positions. When
  6381. called several times in succession, walk through the entire ring.
  6382. Org-mode commands jumping to a different position in the current file,
  6383. or to another Org-mode file, automatically push the old position
  6384. onto the ring."
  6385. (interactive "p")
  6386. (let (p m)
  6387. (if (eq last-command this-command)
  6388. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  6389. (setq p org-mark-ring))
  6390. (setq org-mark-ring-last-goto p)
  6391. (setq m (car p))
  6392. (switch-to-buffer (marker-buffer m))
  6393. (goto-char m)
  6394. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  6395. (defun org-remove-angle-brackets (s)
  6396. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  6397. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  6398. s)
  6399. (defun org-add-angle-brackets (s)
  6400. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  6401. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  6402. s)
  6403. ;;; Following specific links
  6404. (defun org-follow-timestamp-link ()
  6405. (cond
  6406. ((org-at-date-range-p t)
  6407. (let ((org-agenda-start-on-weekday)
  6408. (t1 (match-string 1))
  6409. (t2 (match-string 2)))
  6410. (setq t1 (time-to-days (org-time-string-to-time t1))
  6411. t2 (time-to-days (org-time-string-to-time t2)))
  6412. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  6413. ((org-at-timestamp-p t)
  6414. (org-agenda-list nil (time-to-days (org-time-string-to-time
  6415. (substring (match-string 1) 0 10)))
  6416. 1))
  6417. (t (error "This should not happen"))))
  6418. ;;; Following file links
  6419. (defvar org-wait nil)
  6420. (defun org-open-file (path &optional in-emacs line search)
  6421. "Open the file at PATH.
  6422. First, this expands any special file name abbreviations. Then the
  6423. configuration variable `org-file-apps' is checked if it contains an
  6424. entry for this file type, and if yes, the corresponding command is launched.
  6425. If no application is found, Emacs simply visits the file.
  6426. With optional argument IN-EMACS, Emacs will visit the file.
  6427. Optional LINE specifies a line to go to, optional SEARCH a string to
  6428. search for. If LINE or SEARCH is given, the file will always be
  6429. opened in Emacs.
  6430. If the file does not exist, an error is thrown."
  6431. (setq in-emacs (or in-emacs line search))
  6432. (let* ((file (if (equal path "")
  6433. buffer-file-name
  6434. (substitute-in-file-name (expand-file-name path))))
  6435. (apps (append org-file-apps (org-default-apps)))
  6436. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  6437. (dirp (if remp nil (file-directory-p file)))
  6438. (dfile (downcase file))
  6439. (old-buffer (current-buffer))
  6440. (old-pos (point))
  6441. (old-mode major-mode)
  6442. ext cmd)
  6443. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  6444. (setq ext (match-string 1 dfile))
  6445. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  6446. (setq ext (match-string 1 dfile))))
  6447. (if in-emacs
  6448. (setq cmd 'emacs)
  6449. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  6450. (and dirp (cdr (assoc 'directory apps)))
  6451. (cdr (assoc ext apps))
  6452. (cdr (assoc t apps)))))
  6453. (when (eq cmd 'mailcap)
  6454. (require 'mailcap)
  6455. (mailcap-parse-mailcaps)
  6456. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  6457. (command (mailcap-mime-info mime-type)))
  6458. (if (stringp command)
  6459. (setq cmd command)
  6460. (setq cmd 'emacs))))
  6461. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  6462. (not (file-exists-p file))
  6463. (not org-open-non-existing-files))
  6464. (error "No such file: %s" file))
  6465. (cond
  6466. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  6467. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  6468. (while (string-match "['\"]%s['\"]" cmd)
  6469. (setq cmd (replace-match "%s" t t cmd)))
  6470. (while (string-match "%s" cmd)
  6471. (setq cmd (replace-match
  6472. (save-match-data (shell-quote-argument file))
  6473. t t cmd)))
  6474. (save-window-excursion
  6475. (start-process-shell-command cmd nil cmd)
  6476. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  6477. ))
  6478. ((or (stringp cmd)
  6479. (eq cmd 'emacs))
  6480. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  6481. (widen)
  6482. (if line (goto-line line)
  6483. (if search (org-link-search search))))
  6484. ((consp cmd)
  6485. (eval cmd))
  6486. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  6487. (and (org-mode-p) (eq old-mode 'org-mode)
  6488. (or (not (equal old-buffer (current-buffer)))
  6489. (not (equal old-pos (point))))
  6490. (org-mark-ring-push old-pos old-buffer))))
  6491. (defun org-default-apps ()
  6492. "Return the default applications for this operating system."
  6493. (cond
  6494. ((eq system-type 'darwin)
  6495. org-file-apps-defaults-macosx)
  6496. ((eq system-type 'windows-nt)
  6497. org-file-apps-defaults-windowsnt)
  6498. (t org-file-apps-defaults-gnu)))
  6499. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  6500. (defun org-file-remote-p (file)
  6501. "Test whether FILE specifies a location on a remote system.
  6502. Return non-nil if the location is indeed remote.
  6503. For example, the filename \"/user@host:/foo\" specifies a location
  6504. on the system \"/user@host:\"."
  6505. (cond ((fboundp 'file-remote-p)
  6506. (file-remote-p file))
  6507. ((fboundp 'tramp-handle-file-remote-p)
  6508. (tramp-handle-file-remote-p file))
  6509. ((and (boundp 'ange-ftp-name-format)
  6510. (string-match (car ange-ftp-name-format) file))
  6511. t)
  6512. (t nil)))
  6513. ;;;; Refiling
  6514. (defun org-get-org-file ()
  6515. "Read a filename, with default directory `org-directory'."
  6516. (let ((default (or org-default-notes-file remember-data-file)))
  6517. (read-file-name (format "File name [%s]: " default)
  6518. (file-name-as-directory org-directory)
  6519. default)))
  6520. (defun org-notes-order-reversed-p ()
  6521. "Check if the current file should receive notes in reversed order."
  6522. (cond
  6523. ((not org-reverse-note-order) nil)
  6524. ((eq t org-reverse-note-order) t)
  6525. ((not (listp org-reverse-note-order)) nil)
  6526. (t (catch 'exit
  6527. (let ((all org-reverse-note-order)
  6528. entry)
  6529. (while (setq entry (pop all))
  6530. (if (string-match (car entry) buffer-file-name)
  6531. (throw 'exit (cdr entry))))
  6532. nil)))))
  6533. (defvar org-refile-target-table nil
  6534. "The list of refile targets, created by `org-refile'.")
  6535. (defvar org-agenda-new-buffers nil
  6536. "Buffers created to visit agenda files.")
  6537. (defun org-get-refile-targets (&optional default-buffer)
  6538. "Produce a table with refile targets."
  6539. (let ((entries (or org-refile-targets '((nil . (:level . 1)))))
  6540. targets txt re files f desc descre)
  6541. (with-current-buffer (or default-buffer (current-buffer))
  6542. (while (setq entry (pop entries))
  6543. (setq files (car entry) desc (cdr entry))
  6544. (cond
  6545. ((null files) (setq files (list (current-buffer))))
  6546. ((eq files 'org-agenda-files)
  6547. (setq files (org-agenda-files 'unrestricted)))
  6548. ((and (symbolp files) (fboundp files))
  6549. (setq files (funcall files)))
  6550. ((and (symbolp files) (boundp files))
  6551. (setq files (symbol-value files))))
  6552. (if (stringp files) (setq files (list files)))
  6553. (cond
  6554. ((eq (car desc) :tag)
  6555. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  6556. ((eq (car desc) :todo)
  6557. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  6558. ((eq (car desc) :regexp)
  6559. (setq descre (cdr desc)))
  6560. ((eq (car desc) :level)
  6561. (setq descre (concat "^\\*\\{" (number-to-string
  6562. (if org-odd-levels-only
  6563. (1- (* 2 (cdr desc)))
  6564. (cdr desc)))
  6565. "\\}[ \t]")))
  6566. ((eq (car desc) :maxlevel)
  6567. (setq descre (concat "^\\*\\{1," (number-to-string
  6568. (if org-odd-levels-only
  6569. (1- (* 2 (cdr desc)))
  6570. (cdr desc)))
  6571. "\\}[ \t]")))
  6572. (t (error "Bad refiling target description %s" desc)))
  6573. (while (setq f (pop files))
  6574. (save-excursion
  6575. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  6576. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  6577. (save-excursion
  6578. (save-restriction
  6579. (widen)
  6580. (goto-char (point-min))
  6581. (while (re-search-forward descre nil t)
  6582. (goto-char (point-at-bol))
  6583. (when (looking-at org-complex-heading-regexp)
  6584. (setq txt (match-string 4)
  6585. re (concat "^" (regexp-quote
  6586. (buffer-substring (match-beginning 1)
  6587. (match-end 4)))))
  6588. (if (match-end 5) (setq re (concat re "[ \t]+"
  6589. (regexp-quote
  6590. (match-string 5)))))
  6591. (setq re (concat re "[ \t]*$"))
  6592. (when org-refile-use-outline-path
  6593. (setq txt (mapconcat 'identity
  6594. (append
  6595. (if (eq org-refile-use-outline-path 'file)
  6596. (list (file-name-nondirectory
  6597. (buffer-file-name (buffer-base-buffer))))
  6598. (if (eq org-refile-use-outline-path 'full-file-path)
  6599. (list (buffer-file-name (buffer-base-buffer)))))
  6600. (org-get-outline-path)
  6601. (list txt))
  6602. "/")))
  6603. (push (list txt f re (point)) targets))
  6604. (goto-char (point-at-eol))))))))
  6605. (nreverse targets))))
  6606. (defun org-get-outline-path ()
  6607. "Return the outline path to the current entry, as a list."
  6608. (let (rtn)
  6609. (save-excursion
  6610. (while (org-up-heading-safe)
  6611. (when (looking-at org-complex-heading-regexp)
  6612. (push (org-match-string-no-properties 4) rtn)))
  6613. rtn)))
  6614. (defvar org-refile-history nil
  6615. "History for refiling operations.")
  6616. (defun org-refile (&optional goto default-buffer)
  6617. "Move the entry at point to another heading.
  6618. The list of target headings is compiled using the information in
  6619. `org-refile-targets', which see. This list is created before each use
  6620. and will therefore always be up-to-date.
  6621. At the target location, the entry is filed as a subitem of the target heading.
  6622. Depending on `org-reverse-note-order', the new subitem will either be the
  6623. first of the last subitem.
  6624. With prefix arg GOTO, the command will only visit the target location,
  6625. not actually move anything.
  6626. With a double prefix `C-c C-c', go to the location where the last refiling
  6627. operation has put the subtree."
  6628. (interactive "P")
  6629. (let* ((cbuf (current-buffer))
  6630. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  6631. pos it nbuf file re level reversed)
  6632. (if (equal goto '(16))
  6633. (org-refile-goto-last-stored)
  6634. (when (setq it (org-refile-get-location
  6635. (if goto "Goto: " "Refile to: ") default-buffer))
  6636. (setq file (nth 1 it)
  6637. re (nth 2 it)
  6638. pos (nth 3 it))
  6639. (setq nbuf (or (find-buffer-visiting file)
  6640. (find-file-noselect file)))
  6641. (if goto
  6642. (progn
  6643. (switch-to-buffer nbuf)
  6644. (goto-char pos)
  6645. (org-show-context 'org-goto))
  6646. (org-copy-special)
  6647. (save-excursion
  6648. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  6649. (find-file-noselect file))))
  6650. (setq reversed (org-notes-order-reversed-p))
  6651. (save-excursion
  6652. (save-restriction
  6653. (widen)
  6654. (goto-char pos)
  6655. (looking-at outline-regexp)
  6656. (setq level (org-get-valid-level (funcall outline-level) 1))
  6657. (goto-char
  6658. (if reversed
  6659. (outline-next-heading)
  6660. (or (save-excursion (outline-get-next-sibling))
  6661. (org-end-of-subtree t t)
  6662. (point-max))))
  6663. (bookmark-set "org-refile-last-stored")
  6664. (org-paste-subtree level))))
  6665. (org-cut-special)
  6666. (message "Entry refiled to \"%s\"" (car it)))))))
  6667. (defun org-refile-goto-last-stored ()
  6668. "Go to the location where the last refile was stored."
  6669. (interactive)
  6670. (bookmark-jump "org-refile-last-stored")
  6671. (message "This is the location of the last refile"))
  6672. (defun org-refile-get-location (&optional prompt default-buffer)
  6673. "Prompt the user for a refile location, using PROMPT."
  6674. (let ((org-refile-targets org-refile-targets)
  6675. (org-refile-use-outline-path org-refile-use-outline-path))
  6676. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  6677. (unless org-refile-target-table
  6678. (error "No refile targets"))
  6679. (let* ((cbuf (current-buffer))
  6680. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  6681. (fname (and filename (file-truename filename)))
  6682. (tbl (mapcar
  6683. (lambda (x)
  6684. (if (not (equal fname (file-truename (nth 1 x))))
  6685. (cons (concat (car x) " (" (file-name-nondirectory
  6686. (nth 1 x)) ")")
  6687. (cdr x))
  6688. x))
  6689. org-refile-target-table))
  6690. (completion-ignore-case t))
  6691. (assoc (completing-read prompt tbl nil t nil 'org-refile-history)
  6692. tbl)))
  6693. ;;;; Dynamic blocks
  6694. (defun org-find-dblock (name)
  6695. "Find the first dynamic block with name NAME in the buffer.
  6696. If not found, stay at current position and return nil."
  6697. (let (pos)
  6698. (save-excursion
  6699. (goto-char (point-min))
  6700. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  6701. nil t)
  6702. (match-beginning 0))))
  6703. (if pos (goto-char pos))
  6704. pos))
  6705. (defconst org-dblock-start-re
  6706. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  6707. "Matches the startline of a dynamic block, with parameters.")
  6708. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  6709. "Matches the end of a dyhamic block.")
  6710. (defun org-create-dblock (plist)
  6711. "Create a dynamic block section, with parameters taken from PLIST.
  6712. PLIST must containe a :name entry which is used as name of the block."
  6713. (unless (bolp) (newline))
  6714. (let ((name (plist-get plist :name)))
  6715. (insert "#+BEGIN: " name)
  6716. (while plist
  6717. (if (eq (car plist) :name)
  6718. (setq plist (cddr plist))
  6719. (insert " " (prin1-to-string (pop plist)))))
  6720. (insert "\n\n#+END:\n")
  6721. (beginning-of-line -2)))
  6722. (defun org-prepare-dblock ()
  6723. "Prepare dynamic block for refresh.
  6724. This empties the block, puts the cursor at the insert position and returns
  6725. the property list including an extra property :name with the block name."
  6726. (unless (looking-at org-dblock-start-re)
  6727. (error "Not at a dynamic block"))
  6728. (let* ((begdel (1+ (match-end 0)))
  6729. (name (org-no-properties (match-string 1)))
  6730. (params (append (list :name name)
  6731. (read (concat "(" (match-string 3) ")")))))
  6732. (unless (re-search-forward org-dblock-end-re nil t)
  6733. (error "Dynamic block not terminated"))
  6734. (setq params
  6735. (append params
  6736. (list :content (buffer-substring
  6737. begdel (match-beginning 0)))))
  6738. (delete-region begdel (match-beginning 0))
  6739. (goto-char begdel)
  6740. (open-line 1)
  6741. params))
  6742. (defun org-map-dblocks (&optional command)
  6743. "Apply COMMAND to all dynamic blocks in the current buffer.
  6744. If COMMAND is not given, use `org-update-dblock'."
  6745. (let ((cmd (or command 'org-update-dblock))
  6746. pos)
  6747. (save-excursion
  6748. (goto-char (point-min))
  6749. (while (re-search-forward org-dblock-start-re nil t)
  6750. (goto-char (setq pos (match-beginning 0)))
  6751. (condition-case nil
  6752. (funcall cmd)
  6753. (error (message "Error during update of dynamic block")))
  6754. (goto-char pos)
  6755. (unless (re-search-forward org-dblock-end-re nil t)
  6756. (error "Dynamic block not terminated"))))))
  6757. (defun org-dblock-update (&optional arg)
  6758. "User command for updating dynamic blocks.
  6759. Update the dynamic block at point. With prefix ARG, update all dynamic
  6760. blocks in the buffer."
  6761. (interactive "P")
  6762. (if arg
  6763. (org-update-all-dblocks)
  6764. (or (looking-at org-dblock-start-re)
  6765. (org-beginning-of-dblock))
  6766. (org-update-dblock)))
  6767. (defun org-update-dblock ()
  6768. "Update the dynamic block at point
  6769. This means to empty the block, parse for parameters and then call
  6770. the correct writing function."
  6771. (save-window-excursion
  6772. (let* ((pos (point))
  6773. (line (org-current-line))
  6774. (params (org-prepare-dblock))
  6775. (name (plist-get params :name))
  6776. (cmd (intern (concat "org-dblock-write:" name))))
  6777. (message "Updating dynamic block `%s' at line %d..." name line)
  6778. (funcall cmd params)
  6779. (message "Updating dynamic block `%s' at line %d...done" name line)
  6780. (goto-char pos))))
  6781. (defun org-beginning-of-dblock ()
  6782. "Find the beginning of the dynamic block at point.
  6783. Error if there is no scuh block at point."
  6784. (let ((pos (point))
  6785. beg)
  6786. (end-of-line 1)
  6787. (if (and (re-search-backward org-dblock-start-re nil t)
  6788. (setq beg (match-beginning 0))
  6789. (re-search-forward org-dblock-end-re nil t)
  6790. (> (match-end 0) pos))
  6791. (goto-char beg)
  6792. (goto-char pos)
  6793. (error "Not in a dynamic block"))))
  6794. (defun org-update-all-dblocks ()
  6795. "Update all dynamic blocks in the buffer.
  6796. This function can be used in a hook."
  6797. (when (org-mode-p)
  6798. (org-map-dblocks 'org-update-dblock)))
  6799. ;;;; Completion
  6800. (defconst org-additional-option-like-keywords
  6801. '("BEGIN_HTML" "BEGIN_LaTeX" "END_HTML" "END_LaTeX"
  6802. "ORGTBL" "HTML:" "LaTeX:" "BEGIN:" "END:" "TBLFM"
  6803. "BEGIN_EXAMPLE" "END_EXAMPLE"))
  6804. (defun org-complete (&optional arg)
  6805. "Perform completion on word at point.
  6806. At the beginning of a headline, this completes TODO keywords as given in
  6807. `org-todo-keywords'.
  6808. If the current word is preceded by a backslash, completes the TeX symbols
  6809. that are supported for HTML support.
  6810. If the current word is preceded by \"#+\", completes special words for
  6811. setting file options.
  6812. In the line after \"#+STARTUP:, complete valid keywords.\"
  6813. At all other locations, this simply calls the value of
  6814. `org-completion-fallback-command'."
  6815. (interactive "P")
  6816. (org-without-partial-completion
  6817. (catch 'exit
  6818. (let* ((end (point))
  6819. (beg1 (save-excursion
  6820. (skip-chars-backward (org-re "[:alnum:]_@"))
  6821. (point)))
  6822. (beg (save-excursion
  6823. (skip-chars-backward "a-zA-Z0-9_:$")
  6824. (point)))
  6825. (confirm (lambda (x) (stringp (car x))))
  6826. (searchhead (equal (char-before beg) ?*))
  6827. (tag (and (equal (char-before beg1) ?:)
  6828. (equal (char-after (point-at-bol)) ?*)))
  6829. (prop (and (equal (char-before beg1) ?:)
  6830. (not (equal (char-after (point-at-bol)) ?*))))
  6831. (texp (equal (char-before beg) ?\\))
  6832. (link (equal (char-before beg) ?\[))
  6833. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  6834. beg)
  6835. "#+"))
  6836. (startup (string-match "^#\\+STARTUP:.*"
  6837. (buffer-substring (point-at-bol) (point))))
  6838. (completion-ignore-case opt)
  6839. (type nil)
  6840. (tbl nil)
  6841. (table (cond
  6842. (opt
  6843. (setq type :opt)
  6844. (require 'org-exp)
  6845. (append
  6846. (mapcar
  6847. (lambda (x)
  6848. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  6849. (cons (match-string 2 x) (match-string 1 x)))
  6850. (org-split-string (org-get-current-options) "\n"))
  6851. (mapcar 'list org-additional-option-like-keywords)))
  6852. (startup
  6853. (setq type :startup)
  6854. org-startup-options)
  6855. (link (append org-link-abbrev-alist-local
  6856. org-link-abbrev-alist))
  6857. (texp
  6858. (setq type :tex)
  6859. org-html-entities)
  6860. ((string-match "\\`\\*+[ \t]+\\'"
  6861. (buffer-substring (point-at-bol) beg))
  6862. (setq type :todo)
  6863. (mapcar 'list org-todo-keywords-1))
  6864. (searchhead
  6865. (setq type :searchhead)
  6866. (save-excursion
  6867. (goto-char (point-min))
  6868. (while (re-search-forward org-todo-line-regexp nil t)
  6869. (push (list
  6870. (org-make-org-heading-search-string
  6871. (match-string 3) t))
  6872. tbl)))
  6873. tbl)
  6874. (tag (setq type :tag beg beg1)
  6875. (or org-tag-alist (org-get-buffer-tags)))
  6876. (prop (setq type :prop beg beg1)
  6877. (mapcar 'list (org-buffer-property-keys nil t t)))
  6878. (t (progn
  6879. (call-interactively org-completion-fallback-command)
  6880. (throw 'exit nil)))))
  6881. (pattern (buffer-substring-no-properties beg end))
  6882. (completion (try-completion pattern table confirm)))
  6883. (cond ((eq completion t)
  6884. (if (not (assoc (upcase pattern) table))
  6885. (message "Already complete")
  6886. (if (and (equal type :opt)
  6887. (not (member (car (assoc (upcase pattern) table))
  6888. org-additional-option-like-keywords)))
  6889. (insert (substring (cdr (assoc (upcase pattern) table))
  6890. (length pattern)))
  6891. (if (memq type '(:tag :prop)) (insert ":")))))
  6892. ((null completion)
  6893. (message "Can't find completion for \"%s\"" pattern)
  6894. (ding))
  6895. ((not (string= pattern completion))
  6896. (delete-region beg end)
  6897. (if (string-match " +$" completion)
  6898. (setq completion (replace-match "" t t completion)))
  6899. (insert completion)
  6900. (if (get-buffer-window "*Completions*")
  6901. (delete-window (get-buffer-window "*Completions*")))
  6902. (if (assoc completion table)
  6903. (if (eq type :todo) (insert " ")
  6904. (if (memq type '(:tag :prop)) (insert ":"))))
  6905. (if (and (equal type :opt) (assoc completion table))
  6906. (message "%s" (substitute-command-keys
  6907. "Press \\[org-complete] again to insert example settings"))))
  6908. (t
  6909. (message "Making completion list...")
  6910. (let ((list (sort (all-completions pattern table confirm)
  6911. 'string<)))
  6912. (with-output-to-temp-buffer "*Completions*"
  6913. (condition-case nil
  6914. ;; Protection needed for XEmacs and emacs 21
  6915. (display-completion-list list pattern)
  6916. (error (display-completion-list list)))))
  6917. (message "Making completion list...%s" "done")))))))
  6918. ;;;; TODO, DEADLINE, Comments
  6919. (defun org-toggle-comment ()
  6920. "Change the COMMENT state of an entry."
  6921. (interactive)
  6922. (save-excursion
  6923. (org-back-to-heading)
  6924. (let (case-fold-search)
  6925. (if (looking-at (concat outline-regexp
  6926. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  6927. (replace-match "" t t nil 1)
  6928. (if (looking-at outline-regexp)
  6929. (progn
  6930. (goto-char (match-end 0))
  6931. (insert org-comment-string " ")))))))
  6932. (defvar org-last-todo-state-is-todo nil
  6933. "This is non-nil when the last TODO state change led to a TODO state.
  6934. If the last change removed the TODO tag or switched to DONE, then
  6935. this is nil.")
  6936. (defvar org-setting-tags nil) ; dynamically skiped
  6937. (defun org-parse-local-options (string var)
  6938. "Parse STRING for startup setting relevant for variable VAR."
  6939. (let ((rtn (symbol-value var))
  6940. e opts)
  6941. (save-match-data
  6942. (if (or (not string) (not (string-match "\\S-" string)))
  6943. rtn
  6944. (setq opts (delq nil (mapcar (lambda (x)
  6945. (setq e (assoc x org-startup-options))
  6946. (if (eq (nth 1 e) var) e nil))
  6947. (org-split-string string "[ \t]+"))))
  6948. (if (not opts)
  6949. rtn
  6950. (setq rtn nil)
  6951. (while (setq e (pop opts))
  6952. (if (not (nth 3 e))
  6953. (setq rtn (nth 2 e))
  6954. (if (not (listp rtn)) (setq rtn nil))
  6955. (push (nth 2 e) rtn)))
  6956. rtn)))))
  6957. (defvar org-blocker-hook nil
  6958. "Hook for functions that are allowed to block a state change.
  6959. Each function gets as its single argument a property list, see
  6960. `org-trigger-hook' for more information about this list.
  6961. If any of the functions in this hook returns nil, the state change
  6962. is blocked.")
  6963. (defvar org-trigger-hook nil
  6964. "Hook for functions that are triggered by a state change.
  6965. Each function gets as its single argument a property list with at least
  6966. the following elements:
  6967. (:type type-of-change :position pos-at-entry-start
  6968. :from old-state :to new-state)
  6969. Depending on the type, more properties may be present.
  6970. This mechanism is currently implemented for:
  6971. TODO state changes
  6972. ------------------
  6973. :type todo-state-change
  6974. :from previous state (keyword as a string), or nil
  6975. :to new state (keyword as a string), or nil")
  6976. (defun org-todo (&optional arg)
  6977. "Change the TODO state of an item.
  6978. The state of an item is given by a keyword at the start of the heading,
  6979. like
  6980. *** TODO Write paper
  6981. *** DONE Call mom
  6982. The different keywords are specified in the variable `org-todo-keywords'.
  6983. By default the available states are \"TODO\" and \"DONE\".
  6984. So for this example: when the item starts with TODO, it is changed to DONE.
  6985. When it starts with DONE, the DONE is removed. And when neither TODO nor
  6986. DONE are present, add TODO at the beginning of the heading.
  6987. With C-u prefix arg, use completion to determine the new state.
  6988. With numeric prefix arg, switch to that state.
  6989. For calling through lisp, arg is also interpreted in the following way:
  6990. 'none -> empty state
  6991. \"\"(empty string) -> switch to empty state
  6992. 'done -> switch to DONE
  6993. 'nextset -> switch to the next set of keywords
  6994. 'previousset -> switch to the previous set of keywords
  6995. \"WAITING\" -> switch to the specified keyword, but only if it
  6996. really is a member of `org-todo-keywords'."
  6997. (interactive "P")
  6998. (save-excursion
  6999. (catch 'exit
  7000. (org-back-to-heading)
  7001. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  7002. (or (looking-at (concat " +" org-todo-regexp " *"))
  7003. (looking-at " *"))
  7004. (let* ((match-data (match-data))
  7005. (startpos (point-at-bol))
  7006. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  7007. (org-log-done org-log-done)
  7008. (org-log-repeat org-log-repeat)
  7009. (org-todo-log-states org-todo-log-states)
  7010. (this (match-string 1))
  7011. (hl-pos (match-beginning 0))
  7012. (head (org-get-todo-sequence-head this))
  7013. (ass (assoc head org-todo-kwd-alist))
  7014. (interpret (nth 1 ass))
  7015. (done-word (nth 3 ass))
  7016. (final-done-word (nth 4 ass))
  7017. (last-state (or this ""))
  7018. (completion-ignore-case t)
  7019. (member (member this org-todo-keywords-1))
  7020. (tail (cdr member))
  7021. (state (cond
  7022. ((and org-todo-key-trigger
  7023. (or (and (equal arg '(4)) (eq org-use-fast-todo-selection 'prefix))
  7024. (and (not arg) org-use-fast-todo-selection
  7025. (not (eq org-use-fast-todo-selection 'prefix)))))
  7026. ;; Use fast selection
  7027. (org-fast-todo-selection))
  7028. ((and (equal arg '(4))
  7029. (or (not org-use-fast-todo-selection)
  7030. (not org-todo-key-trigger)))
  7031. ;; Read a state with completion
  7032. (completing-read "State: " (mapcar (lambda(x) (list x))
  7033. org-todo-keywords-1)
  7034. nil t))
  7035. ((eq arg 'right)
  7036. (if this
  7037. (if tail (car tail) nil)
  7038. (car org-todo-keywords-1)))
  7039. ((eq arg 'left)
  7040. (if (equal member org-todo-keywords-1)
  7041. nil
  7042. (if this
  7043. (nth (- (length org-todo-keywords-1) (length tail) 2)
  7044. org-todo-keywords-1)
  7045. (org-last org-todo-keywords-1))))
  7046. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  7047. (setq arg nil))) ; hack to fall back to cycling
  7048. (arg
  7049. ;; user or caller requests a specific state
  7050. (cond
  7051. ((equal arg "") nil)
  7052. ((eq arg 'none) nil)
  7053. ((eq arg 'done) (or done-word (car org-done-keywords)))
  7054. ((eq arg 'nextset)
  7055. (or (car (cdr (member head org-todo-heads)))
  7056. (car org-todo-heads)))
  7057. ((eq arg 'previousset)
  7058. (let ((org-todo-heads (reverse org-todo-heads)))
  7059. (or (car (cdr (member head org-todo-heads)))
  7060. (car org-todo-heads))))
  7061. ((car (member arg org-todo-keywords-1)))
  7062. ((nth (1- (prefix-numeric-value arg))
  7063. org-todo-keywords-1))))
  7064. ((null member) (or head (car org-todo-keywords-1)))
  7065. ((equal this final-done-word) nil) ;; -> make empty
  7066. ((null tail) nil) ;; -> first entry
  7067. ((eq interpret 'sequence)
  7068. (car tail))
  7069. ((memq interpret '(type priority))
  7070. (if (eq this-command last-command)
  7071. (car tail)
  7072. (if (> (length tail) 0)
  7073. (or done-word (car org-done-keywords))
  7074. nil)))
  7075. (t nil)))
  7076. (next (if state (concat " " state " ") " "))
  7077. (change-plist (list :type 'todo-state-change :from this :to state
  7078. :position startpos))
  7079. dolog now-done-p)
  7080. (when org-blocker-hook
  7081. (unless (save-excursion
  7082. (save-match-data
  7083. (run-hook-with-args-until-failure
  7084. 'org-blocker-hook change-plist)))
  7085. (if (interactive-p)
  7086. (error "TODO state change from %s to %s blocked" this state)
  7087. ;; fail silently
  7088. (message "TODO state change from %s to %s blocked" this state)
  7089. (throw 'exit nil))))
  7090. (store-match-data match-data)
  7091. (replace-match next t t)
  7092. (unless (pos-visible-in-window-p hl-pos)
  7093. (message "TODO state changed to %s" (org-trim next)))
  7094. (unless head
  7095. (setq head (org-get-todo-sequence-head state)
  7096. ass (assoc head org-todo-kwd-alist)
  7097. interpret (nth 1 ass)
  7098. done-word (nth 3 ass)
  7099. final-done-word (nth 4 ass)))
  7100. (when (memq arg '(nextset previousset))
  7101. (message "Keyword-Set %d/%d: %s"
  7102. (- (length org-todo-sets) -1
  7103. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  7104. (length org-todo-sets)
  7105. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  7106. (setq org-last-todo-state-is-todo
  7107. (not (member state org-done-keywords)))
  7108. (setq now-done-p (and (member state org-done-keywords)
  7109. (not (member this org-done-keywords))))
  7110. (and logging (org-local-logging logging))
  7111. (when (and (or org-todo-log-states org-log-done)
  7112. (not (memq arg '(nextset previousset))))
  7113. ;; we need to look at recording a time and note
  7114. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  7115. (nth 2 (assoc this org-todo-log-states))))
  7116. (when (and state
  7117. (member state org-not-done-keywords)
  7118. (not (member this org-not-done-keywords)))
  7119. ;; This is now a todo state and was not one before
  7120. ;; If there was a CLOSED time stamp, get rid of it.
  7121. (org-add-planning-info nil nil 'closed))
  7122. (when (and now-done-p org-log-done)
  7123. ;; It is now done, and it was not done before
  7124. (org-add-planning-info 'closed (org-current-time))
  7125. (if (and (not dolog) (eq 'note org-log-done))
  7126. (org-add-log-setup 'done state 'findpos 'note)))
  7127. (when (and state dolog)
  7128. ;; This is a non-nil state, and we need to log it
  7129. (org-add-log-setup 'state state 'findpos dolog)))
  7130. ;; Fixup tag positioning
  7131. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  7132. (run-hooks 'org-after-todo-state-change-hook)
  7133. (if (and arg (not (member state org-done-keywords)))
  7134. (setq head (org-get-todo-sequence-head state)))
  7135. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  7136. ;; Do we need to trigger a repeat?
  7137. (when now-done-p (org-auto-repeat-maybe state))
  7138. ;; Fixup cursor location if close to the keyword
  7139. (if (and (outline-on-heading-p)
  7140. (not (bolp))
  7141. (save-excursion (beginning-of-line 1)
  7142. (looking-at org-todo-line-regexp))
  7143. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  7144. (progn
  7145. (goto-char (or (match-end 2) (match-end 1)))
  7146. (just-one-space)))
  7147. (when org-trigger-hook
  7148. (save-excursion
  7149. (run-hook-with-args 'org-trigger-hook change-plist)))))))
  7150. (defun org-local-logging (value)
  7151. "Get logging settings from a property VALUE."
  7152. (let* (words w a)
  7153. ;; directly set the variables, they are already local.
  7154. (setq org-log-done nil
  7155. org-log-repeat nil
  7156. org-todo-log-states nil)
  7157. (setq words (org-split-string value))
  7158. (while (setq w (pop words))
  7159. (cond
  7160. ((setq a (assoc w org-startup-options))
  7161. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  7162. (set (nth 1 a) (nth 2 a))))
  7163. ((setq a (org-extract-log-state-settings w))
  7164. (and (member (car a) org-todo-keywords-1)
  7165. (push a org-todo-log-states)))))))
  7166. (defun org-get-todo-sequence-head (kwd)
  7167. "Return the head of the TODO sequence to which KWD belongs.
  7168. If KWD is not set, check if there is a text property remembering the
  7169. right sequence."
  7170. (let (p)
  7171. (cond
  7172. ((not kwd)
  7173. (or (get-text-property (point-at-bol) 'org-todo-head)
  7174. (progn
  7175. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  7176. nil (point-at-eol)))
  7177. (get-text-property p 'org-todo-head))))
  7178. ((not (member kwd org-todo-keywords-1))
  7179. (car org-todo-keywords-1))
  7180. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  7181. (defun org-fast-todo-selection ()
  7182. "Fast TODO keyword selection with single keys.
  7183. Returns the new TODO keyword, or nil if no state change should occur."
  7184. (let* ((fulltable org-todo-key-alist)
  7185. (done-keywords org-done-keywords) ;; needed for the faces.
  7186. (maxlen (apply 'max (mapcar
  7187. (lambda (x)
  7188. (if (stringp (car x)) (string-width (car x)) 0))
  7189. fulltable)))
  7190. (expert nil)
  7191. (fwidth (+ maxlen 3 1 3))
  7192. (ncol (/ (- (window-width) 4) fwidth))
  7193. tg cnt e c tbl
  7194. groups ingroup)
  7195. (save-window-excursion
  7196. (if expert
  7197. (set-buffer (get-buffer-create " *Org todo*"))
  7198. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  7199. (erase-buffer)
  7200. (org-set-local 'org-done-keywords done-keywords)
  7201. (setq tbl fulltable cnt 0)
  7202. (while (setq e (pop tbl))
  7203. (cond
  7204. ((equal e '(:startgroup))
  7205. (push '() groups) (setq ingroup t)
  7206. (when (not (= cnt 0))
  7207. (setq cnt 0)
  7208. (insert "\n"))
  7209. (insert "{ "))
  7210. ((equal e '(:endgroup))
  7211. (setq ingroup nil cnt 0)
  7212. (insert "}\n"))
  7213. (t
  7214. (setq tg (car e) c (cdr e))
  7215. (if ingroup (push tg (car groups)))
  7216. (setq tg (org-add-props tg nil 'face
  7217. (org-get-todo-face tg)))
  7218. (if (and (= cnt 0) (not ingroup)) (insert " "))
  7219. (insert "[" c "] " tg (make-string
  7220. (- fwidth 4 (length tg)) ?\ ))
  7221. (when (= (setq cnt (1+ cnt)) ncol)
  7222. (insert "\n")
  7223. (if ingroup (insert " "))
  7224. (setq cnt 0)))))
  7225. (insert "\n")
  7226. (goto-char (point-min))
  7227. (if (and (not expert) (fboundp 'fit-window-to-buffer))
  7228. (fit-window-to-buffer))
  7229. (message "[a-z..]:Set [SPC]:clear")
  7230. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  7231. (cond
  7232. ((or (= c ?\C-g)
  7233. (and (= c ?q) (not (rassoc c fulltable))))
  7234. (setq quit-flag t))
  7235. ((= c ?\ ) nil)
  7236. ((setq e (rassoc c fulltable) tg (car e))
  7237. tg)
  7238. (t (setq quit-flag t))))))
  7239. (defun org-entry-is-todo-p ()
  7240. (member (org-get-todo-state) org-not-done-keywords))
  7241. (defun org-entry-is-done-p ()
  7242. (member (org-get-todo-state) org-done-keywords))
  7243. (defun org-get-todo-state ()
  7244. (save-excursion
  7245. (org-back-to-heading t)
  7246. (and (looking-at org-todo-line-regexp)
  7247. (match-end 2)
  7248. (match-string 2))))
  7249. (defun org-at-date-range-p (&optional inactive-ok)
  7250. "Is the cursor inside a date range?"
  7251. (interactive)
  7252. (save-excursion
  7253. (catch 'exit
  7254. (let ((pos (point)))
  7255. (skip-chars-backward "^[<\r\n")
  7256. (skip-chars-backward "<[")
  7257. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  7258. (>= (match-end 0) pos)
  7259. (throw 'exit t))
  7260. (skip-chars-backward "^<[\r\n")
  7261. (skip-chars-backward "<[")
  7262. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  7263. (>= (match-end 0) pos)
  7264. (throw 'exit t)))
  7265. nil)))
  7266. (defun org-get-repeat ()
  7267. "Check if tere is a deadline/schedule with repeater in this entry."
  7268. (save-match-data
  7269. (save-excursion
  7270. (org-back-to-heading t)
  7271. (if (re-search-forward
  7272. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  7273. (match-string 1)))))
  7274. (defvar org-last-changed-timestamp)
  7275. (defvar org-log-post-message)
  7276. (defvar org-log-note-purpose)
  7277. (defvar org-log-note-how)
  7278. (defun org-auto-repeat-maybe (done-word)
  7279. "Check if the current headline contains a repeated deadline/schedule.
  7280. If yes, set TODO state back to what it was and change the base date
  7281. of repeating deadline/scheduled time stamps to new date.
  7282. This function is run automatically after each state change to a DONE state."
  7283. ;; last-state is dynamically scoped into this function
  7284. (let* ((repeat (org-get-repeat))
  7285. (aa (assoc last-state org-todo-kwd-alist))
  7286. (interpret (nth 1 aa))
  7287. (head (nth 2 aa))
  7288. (whata '(("d" . day) ("m" . month) ("y" . year)))
  7289. (msg "Entry repeats: ")
  7290. (org-log-done nil)
  7291. (org-todo-log-states nil)
  7292. (nshiftmax 10) (nshift 0)
  7293. re type n what ts mb0 time)
  7294. (when repeat
  7295. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  7296. (org-todo (if (eq interpret 'type) last-state head))
  7297. (when org-log-repeat
  7298. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  7299. (memq 'org-add-log-note post-command-hook))
  7300. ;; OK, we are already setup for some record
  7301. (if (eq org-log-repeat 'note)
  7302. ;; make sure we take a note, not only a time stamp
  7303. (setq org-log-note-how 'note))
  7304. ;; Set up for taking a record
  7305. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  7306. 'findpos org-log-repeat)))
  7307. (org-back-to-heading t)
  7308. (org-add-planning-info nil nil 'closed)
  7309. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  7310. org-deadline-time-regexp "\\)\\|\\("
  7311. org-ts-regexp "\\)"))
  7312. (while (re-search-forward
  7313. re (save-excursion (outline-next-heading) (point)) t)
  7314. (setq type (if (match-end 1) org-scheduled-string
  7315. (if (match-end 3) org-deadline-string "Plain:"))
  7316. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0)))
  7317. mb0 (match-beginning 0))
  7318. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  7319. (setq n (string-to-number (match-string 2 ts))
  7320. what (match-string 3 ts))
  7321. (if (equal what "w") (setq n (* n 7) what "d"))
  7322. ;; Preparation, see if we need to modify the start date for the change
  7323. (when (match-end 1)
  7324. (setq time (save-match-data (org-time-string-to-time ts)))
  7325. (cond
  7326. ((equal (match-string 1 ts) ".")
  7327. ;; Shift starting date to today
  7328. (org-timestamp-change
  7329. (- (time-to-days (current-time)) (time-to-days time))
  7330. 'day))
  7331. ((equal (match-string 1 ts) "+")
  7332. (while (or (= nshift 0)
  7333. (<= (time-to-days time) (time-to-days (current-time))))
  7334. (when (= (incf nshift) nshiftmax)
  7335. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  7336. (error "Abort")))
  7337. (org-timestamp-change n (cdr (assoc what whata)))
  7338. (org-at-timestamp-p t)
  7339. (setq ts (match-string 1))
  7340. (setq time (save-match-data (org-time-string-to-time ts))))
  7341. (org-timestamp-change (- n) (cdr (assoc what whata)))
  7342. ;; rematch, so that we have everything in place for the real shift
  7343. (org-at-timestamp-p t)
  7344. (setq ts (match-string 1))
  7345. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  7346. (org-timestamp-change n (cdr (assoc what whata)))
  7347. (setq msg (concat msg type org-last-changed-timestamp " "))))
  7348. (setq org-log-post-message msg)
  7349. (message "%s" msg))))
  7350. (defun org-show-todo-tree (arg)
  7351. "Make a compact tree which shows all headlines marked with TODO.
  7352. The tree will show the lines where the regexp matches, and all higher
  7353. headlines above the match.
  7354. With a \\[universal-argument] prefix, also show the DONE entries.
  7355. With a numeric prefix N, construct a sparse tree for the Nth element
  7356. of `org-todo-keywords-1'."
  7357. (interactive "P")
  7358. (let ((case-fold-search nil)
  7359. (kwd-re
  7360. (cond ((null arg) org-not-done-regexp)
  7361. ((equal arg '(4))
  7362. (let ((kwd (completing-read "Keyword (or KWD1|KWD2|...): "
  7363. (mapcar 'list org-todo-keywords-1))))
  7364. (concat "\\("
  7365. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  7366. "\\)\\>")))
  7367. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  7368. (regexp-quote (nth (1- (prefix-numeric-value arg))
  7369. org-todo-keywords-1)))
  7370. (t (error "Invalid prefix argument: %s" arg)))))
  7371. (message "%d TODO entries found"
  7372. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  7373. (defun org-deadline (&optional remove)
  7374. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  7375. With argument REMOVE, remove any deadline from the item."
  7376. (interactive "P")
  7377. (if remove
  7378. (progn
  7379. (org-remove-timestamp-with-keyword org-deadline-string)
  7380. (message "Item no longer has a deadline."))
  7381. (org-add-planning-info 'deadline nil 'closed)))
  7382. (defun org-schedule (&optional remove)
  7383. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  7384. With argument REMOVE, remove any scheduling date from the item."
  7385. (interactive "P")
  7386. (if remove
  7387. (progn
  7388. (org-remove-timestamp-with-keyword org-scheduled-string)
  7389. (message "Item is no longer scheduled."))
  7390. (org-add-planning-info 'scheduled nil 'closed)))
  7391. (defun org-remove-timestamp-with-keyword (keyword)
  7392. "Remove all time stamps with KEYWORD in the current entry."
  7393. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  7394. beg)
  7395. (save-excursion
  7396. (org-back-to-heading t)
  7397. (setq beg (point))
  7398. (org-end-of-subtree t t)
  7399. (while (re-search-backward re beg t)
  7400. (replace-match "")
  7401. (unless (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  7402. (delete-region (point-at-bol) (min (1+ (point)) (point-max))))))))
  7403. (defun org-add-planning-info (what &optional time &rest remove)
  7404. "Insert new timestamp with keyword in the line directly after the headline.
  7405. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  7406. If non is given, the user is prompted for a date.
  7407. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  7408. be removed."
  7409. (interactive)
  7410. (let (org-time-was-given org-end-time-was-given ts
  7411. end default-time default-input)
  7412. (when (and (not time) (memq what '(scheduled deadline)))
  7413. ;; Try to get a default date/time from existing timestamp
  7414. (save-excursion
  7415. (org-back-to-heading t)
  7416. (setq end (save-excursion (outline-next-heading) (point)))
  7417. (when (re-search-forward (if (eq what 'scheduled)
  7418. org-scheduled-time-regexp
  7419. org-deadline-time-regexp)
  7420. end t)
  7421. (setq ts (match-string 1)
  7422. default-time
  7423. (apply 'encode-time (org-parse-time-string ts))
  7424. default-input (and ts (org-get-compact-tod ts))))))
  7425. (when what
  7426. ;; If necessary, get the time from the user
  7427. (setq time (or time (org-read-date nil 'to-time nil nil
  7428. default-time default-input))))
  7429. (when (and org-insert-labeled-timestamps-at-point
  7430. (member what '(scheduled deadline)))
  7431. (insert
  7432. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  7433. (org-insert-time-stamp time org-time-was-given
  7434. nil nil nil (list org-end-time-was-given))
  7435. (setq what nil))
  7436. (save-excursion
  7437. (save-restriction
  7438. (let (col list elt ts buffer-invisibility-spec)
  7439. (org-back-to-heading t)
  7440. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  7441. (goto-char (match-end 1))
  7442. (setq col (current-column))
  7443. (goto-char (match-end 0))
  7444. (if (eobp) (insert "\n") (forward-char 1))
  7445. (if (and (not (looking-at outline-regexp))
  7446. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  7447. "[^\r\n]*"))
  7448. (not (equal (match-string 1) org-clock-string)))
  7449. (narrow-to-region (match-beginning 0) (match-end 0))
  7450. (insert-before-markers "\n")
  7451. (backward-char 1)
  7452. (narrow-to-region (point) (point))
  7453. (org-indent-to-column col))
  7454. ;; Check if we have to remove something.
  7455. (setq list (cons what remove))
  7456. (while list
  7457. (setq elt (pop list))
  7458. (goto-char (point-min))
  7459. (when (or (and (eq elt 'scheduled)
  7460. (re-search-forward org-scheduled-time-regexp nil t))
  7461. (and (eq elt 'deadline)
  7462. (re-search-forward org-deadline-time-regexp nil t))
  7463. (and (eq elt 'closed)
  7464. (re-search-forward org-closed-time-regexp nil t)))
  7465. (replace-match "")
  7466. (if (looking-at "--+<[^>]+>") (replace-match ""))
  7467. (if (looking-at " +") (replace-match ""))))
  7468. (goto-char (point-max))
  7469. (when what
  7470. (insert
  7471. (if (not (equal (char-before) ?\ )) " " "")
  7472. (cond ((eq what 'scheduled) org-scheduled-string)
  7473. ((eq what 'deadline) org-deadline-string)
  7474. ((eq what 'closed) org-closed-string))
  7475. " ")
  7476. (setq ts (org-insert-time-stamp
  7477. time
  7478. (or org-time-was-given
  7479. (and (eq what 'closed) org-log-done-with-time))
  7480. (eq what 'closed)
  7481. nil nil (list org-end-time-was-given)))
  7482. (end-of-line 1))
  7483. (goto-char (point-min))
  7484. (widen)
  7485. (if (and (looking-at "[ \t]+\n")
  7486. (equal (char-before) ?\n))
  7487. (backward-delete-char 1))
  7488. ts)))))
  7489. (defvar org-log-note-marker (make-marker))
  7490. (defvar org-log-note-purpose nil)
  7491. (defvar org-log-note-state nil)
  7492. (defvar org-log-note-how nil)
  7493. (defvar org-log-note-window-configuration nil)
  7494. (defvar org-log-note-return-to (make-marker))
  7495. (defvar org-log-post-message nil
  7496. "Message to be displayed after a log note has been stored.
  7497. The auto-repeater uses this.")
  7498. (defun org-add-note ()
  7499. "Add a note to the current entry.
  7500. This is done in the same way as adding a state change note."
  7501. (interactive)
  7502. (org-add-log-setup 'note nil t nil))
  7503. (defun org-add-log-setup (&optional purpose state findpos how)
  7504. "Set up the post command hook to take a note.
  7505. If this is about to TODO state change, the new state is expected in STATE.
  7506. When FINDPOS is non-nil, find the correct position for the note in
  7507. the current entry. If not, assume that it can be inserted at point."
  7508. (save-excursion
  7509. (when findpos
  7510. (org-back-to-heading t)
  7511. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  7512. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  7513. "[^\r\n]*\\)?"))
  7514. (goto-char (match-end 0))
  7515. (unless org-log-states-order-reversed
  7516. (and (= (char-after) ?\n) (forward-char 1))
  7517. (org-skip-over-state-notes)
  7518. (skip-chars-backward " \t\n\r")))
  7519. (move-marker org-log-note-marker (point))
  7520. (setq org-log-note-purpose purpose
  7521. org-log-note-state state
  7522. org-log-note-how how)
  7523. (add-hook 'post-command-hook 'org-add-log-note 'append)))
  7524. (defun org-skip-over-state-notes ()
  7525. "Skip past the list of State notes in an entry."
  7526. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  7527. (while (looking-at "[ \t]*- State")
  7528. (condition-case nil
  7529. (org-next-item)
  7530. (error (org-end-of-item)))))
  7531. (defun org-add-log-note (&optional purpose)
  7532. "Pop up a window for taking a note, and add this note later at point."
  7533. (remove-hook 'post-command-hook 'org-add-log-note)
  7534. (setq org-log-note-window-configuration (current-window-configuration))
  7535. (delete-other-windows)
  7536. (move-marker org-log-note-return-to (point))
  7537. (switch-to-buffer (marker-buffer org-log-note-marker))
  7538. (goto-char org-log-note-marker)
  7539. (org-switch-to-buffer-other-window "*Org Note*")
  7540. (erase-buffer)
  7541. (if (memq org-log-note-how '(time state))
  7542. (org-store-log-note)
  7543. (let ((org-inhibit-startup t)) (org-mode))
  7544. (insert (format "# Insert note for %s.
  7545. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  7546. (cond
  7547. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  7548. ((eq org-log-note-purpose 'done) "closed todo item")
  7549. ((eq org-log-note-purpose 'state)
  7550. (format "state change to \"%s\"" org-log-note-state))
  7551. ((eq org-log-note-purpose 'note)
  7552. "this entry")
  7553. (t (error "This should not happen")))))
  7554. (org-set-local 'org-finish-function 'org-store-log-note)))
  7555. (defvar org-note-abort nil) ; dynamically scoped
  7556. (defun org-store-log-note ()
  7557. "Finish taking a log note, and insert it to where it belongs."
  7558. (let ((txt (buffer-string))
  7559. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  7560. lines ind)
  7561. (kill-buffer (current-buffer))
  7562. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  7563. (setq txt (replace-match "" t t txt)))
  7564. (if (string-match "\\s-+\\'" txt)
  7565. (setq txt (replace-match "" t t txt)))
  7566. (setq lines (org-split-string txt "\n"))
  7567. (when (and note (string-match "\\S-" note))
  7568. (setq note
  7569. (org-replace-escapes
  7570. note
  7571. (list (cons "%u" (user-login-name))
  7572. (cons "%U" user-full-name)
  7573. (cons "%t" (format-time-string
  7574. (org-time-stamp-format 'long 'inactive)
  7575. (current-time)))
  7576. (cons "%s" (if org-log-note-state
  7577. (concat "\"" org-log-note-state "\"")
  7578. "")))))
  7579. (if lines (setq note (concat note " \\\\")))
  7580. (push note lines))
  7581. (when (or current-prefix-arg org-note-abort) (setq lines nil))
  7582. (when lines
  7583. (save-excursion
  7584. (set-buffer (marker-buffer org-log-note-marker))
  7585. (save-excursion
  7586. (goto-char org-log-note-marker)
  7587. (move-marker org-log-note-marker nil)
  7588. (end-of-line 1)
  7589. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  7590. (indent-relative nil)
  7591. (insert "- " (pop lines))
  7592. (org-indent-line-function)
  7593. (beginning-of-line 1)
  7594. (looking-at "[ \t]*")
  7595. (setq ind (concat (match-string 0) " "))
  7596. (end-of-line 1)
  7597. (while lines (insert "\n" ind (pop lines)))))))
  7598. (set-window-configuration org-log-note-window-configuration)
  7599. (with-current-buffer (marker-buffer org-log-note-return-to)
  7600. (goto-char org-log-note-return-to))
  7601. (move-marker org-log-note-return-to nil)
  7602. (and org-log-post-message (message "%s" org-log-post-message)))
  7603. (defun org-sparse-tree (&optional arg)
  7604. "Create a sparse tree, prompt for the details.
  7605. This command can create sparse trees. You first need to select the type
  7606. of match used to create the tree:
  7607. t Show entries with a specific TODO keyword.
  7608. T Show entries selected by a tags match.
  7609. p Enter a property name and its value (both with completion on existing
  7610. names/values) and show entries with that property.
  7611. r Show entries matching a regular expression
  7612. d Show deadlines due within `org-deadline-warning-days'."
  7613. (interactive "P")
  7614. (let (ans kwd value)
  7615. (message "Sparse tree: [/]regexp [t]odo-kwd [T]ag [p]roperty [d]eadlines [b]efore-date")
  7616. (setq ans (read-char-exclusive))
  7617. (cond
  7618. ((equal ans ?d)
  7619. (call-interactively 'org-check-deadlines))
  7620. ((equal ans ?b)
  7621. (call-interactively 'org-check-before-date))
  7622. ((equal ans ?t)
  7623. (org-show-todo-tree '(4)))
  7624. ((equal ans ?T)
  7625. (call-interactively 'org-tags-sparse-tree))
  7626. ((member ans '(?p ?P))
  7627. (setq kwd (completing-read "Property: "
  7628. (mapcar 'list (org-buffer-property-keys))))
  7629. (setq value (completing-read "Value: "
  7630. (mapcar 'list (org-property-values kwd))))
  7631. (unless (string-match "\\`{.*}\\'" value)
  7632. (setq value (concat "\"" value "\"")))
  7633. (org-tags-sparse-tree arg (concat kwd "=" value)))
  7634. ((member ans '(?r ?R ?/))
  7635. (call-interactively 'org-occur))
  7636. (t (error "No such sparse tree command \"%c\"" ans)))))
  7637. (defvar org-occur-highlights nil
  7638. "List of overlays used for occur matches.")
  7639. (make-variable-buffer-local 'org-occur-highlights)
  7640. (defvar org-occur-parameters nil
  7641. "Parameters of the active org-occur calls.
  7642. This is a list, each call to org-occur pushes as cons cell,
  7643. containing the regular expression and the callback, onto the list.
  7644. The list can contain several entries if `org-occur' has been called
  7645. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  7646. will only contain one set of parameters. When the highlights are
  7647. removed (for example with `C-c C-c', or with the next edit (depending
  7648. on `org-remove-highlights-with-change'), this variable is emptied
  7649. as well.")
  7650. (make-variable-buffer-local 'org-occur-parameters)
  7651. (defun org-occur (regexp &optional keep-previous callback)
  7652. "Make a compact tree which shows all matches of REGEXP.
  7653. The tree will show the lines where the regexp matches, and all higher
  7654. headlines above the match. It will also show the heading after the match,
  7655. to make sure editing the matching entry is easy.
  7656. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  7657. call to `org-occur' will be kept, to allow stacking of calls to this
  7658. command.
  7659. If CALLBACK is non-nil, it is a function which is called to confirm
  7660. that the match should indeed be shown."
  7661. (interactive "sRegexp: \nP")
  7662. (unless keep-previous
  7663. (org-remove-occur-highlights nil nil t))
  7664. (push (cons regexp callback) org-occur-parameters)
  7665. (let ((cnt 0))
  7666. (save-excursion
  7667. (goto-char (point-min))
  7668. (if (or (not keep-previous) ; do not want to keep
  7669. (not org-occur-highlights)) ; no previous matches
  7670. ;; hide everything
  7671. (org-overview))
  7672. (while (re-search-forward regexp nil t)
  7673. (when (or (not callback)
  7674. (save-match-data (funcall callback)))
  7675. (setq cnt (1+ cnt))
  7676. (when org-highlight-sparse-tree-matches
  7677. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  7678. (org-show-context 'occur-tree))))
  7679. (when org-remove-highlights-with-change
  7680. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  7681. nil 'local))
  7682. (unless org-sparse-tree-open-archived-trees
  7683. (org-hide-archived-subtrees (point-min) (point-max)))
  7684. (run-hooks 'org-occur-hook)
  7685. (if (interactive-p)
  7686. (message "%d match(es) for regexp %s" cnt regexp))
  7687. cnt))
  7688. (defun org-show-context (&optional key)
  7689. "Make sure point and context and visible.
  7690. How much context is shown depends upon the variables
  7691. `org-show-hierarchy-above', `org-show-following-heading'. and
  7692. `org-show-siblings'."
  7693. (let ((heading-p (org-on-heading-p t))
  7694. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  7695. (following-p (org-get-alist-option org-show-following-heading key))
  7696. (entry-p (org-get-alist-option org-show-entry-below key))
  7697. (siblings-p (org-get-alist-option org-show-siblings key)))
  7698. (catch 'exit
  7699. ;; Show heading or entry text
  7700. (if (and heading-p (not entry-p))
  7701. (org-flag-heading nil) ; only show the heading
  7702. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  7703. (org-show-hidden-entry))) ; show entire entry
  7704. (when following-p
  7705. ;; Show next sibling, or heading below text
  7706. (save-excursion
  7707. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  7708. (org-flag-heading nil))))
  7709. (when siblings-p (org-show-siblings))
  7710. (when hierarchy-p
  7711. ;; show all higher headings, possibly with siblings
  7712. (save-excursion
  7713. (while (and (condition-case nil
  7714. (progn (org-up-heading-all 1) t)
  7715. (error nil))
  7716. (not (bobp)))
  7717. (org-flag-heading nil)
  7718. (when siblings-p (org-show-siblings))))))))
  7719. (defun org-reveal (&optional siblings)
  7720. "Show current entry, hierarchy above it, and the following headline.
  7721. This can be used to show a consistent set of context around locations
  7722. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  7723. not t for the search context.
  7724. With optional argument SIBLINGS, on each level of the hierarchy all
  7725. siblings are shown. This repairs the tree structure to what it would
  7726. look like when opened with hierarchical calls to `org-cycle'."
  7727. (interactive "P")
  7728. (let ((org-show-hierarchy-above t)
  7729. (org-show-following-heading t)
  7730. (org-show-siblings (if siblings t org-show-siblings)))
  7731. (org-show-context nil)))
  7732. (defun org-highlight-new-match (beg end)
  7733. "Highlight from BEG to END and mark the highlight is an occur headline."
  7734. (let ((ov (org-make-overlay beg end)))
  7735. (org-overlay-put ov 'face 'secondary-selection)
  7736. (push ov org-occur-highlights)))
  7737. (defun org-remove-occur-highlights (&optional beg end noremove)
  7738. "Remove the occur highlights from the buffer.
  7739. BEG and END are ignored. If NOREMOVE is nil, remove this function
  7740. from the `before-change-functions' in the current buffer."
  7741. (interactive)
  7742. (unless org-inhibit-highlight-removal
  7743. (mapc 'org-delete-overlay org-occur-highlights)
  7744. (setq org-occur-highlights nil)
  7745. (setq org-occur-parameters nil)
  7746. (unless noremove
  7747. (remove-hook 'before-change-functions
  7748. 'org-remove-occur-highlights 'local))))
  7749. ;;;; Priorities
  7750. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  7751. "Regular expression matching the priority indicator.")
  7752. (defvar org-remove-priority-next-time nil)
  7753. (defun org-priority-up ()
  7754. "Increase the priority of the current item."
  7755. (interactive)
  7756. (org-priority 'up))
  7757. (defun org-priority-down ()
  7758. "Decrease the priority of the current item."
  7759. (interactive)
  7760. (org-priority 'down))
  7761. (defun org-priority (&optional action)
  7762. "Change the priority of an item by ARG.
  7763. ACTION can be `set', `up', `down', or a character."
  7764. (interactive)
  7765. (setq action (or action 'set))
  7766. (let (current new news have remove)
  7767. (save-excursion
  7768. (org-back-to-heading)
  7769. (if (looking-at org-priority-regexp)
  7770. (setq current (string-to-char (match-string 2))
  7771. have t)
  7772. (setq current org-default-priority))
  7773. (cond
  7774. ((or (eq action 'set)
  7775. (if (featurep 'xemacs) (characterp action) (integerp action)))
  7776. (if (not (eq action 'set))
  7777. (setq new action)
  7778. (message "Priority %c-%c, SPC to remove: "
  7779. org-highest-priority org-lowest-priority)
  7780. (setq new (read-char-exclusive)))
  7781. (if (and (= (upcase org-highest-priority) org-highest-priority)
  7782. (= (upcase org-lowest-priority) org-lowest-priority))
  7783. (setq new (upcase new)))
  7784. (cond ((equal new ?\ ) (setq remove t))
  7785. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  7786. (error "Priority must be between `%c' and `%c'"
  7787. org-highest-priority org-lowest-priority))))
  7788. ((eq action 'up)
  7789. (if (and (not have) (eq last-command this-command))
  7790. (setq new org-lowest-priority)
  7791. (setq new (if (and org-priority-start-cycle-with-default (not have))
  7792. org-default-priority (1- current)))))
  7793. ((eq action 'down)
  7794. (if (and (not have) (eq last-command this-command))
  7795. (setq new org-highest-priority)
  7796. (setq new (if (and org-priority-start-cycle-with-default (not have))
  7797. org-default-priority (1+ current)))))
  7798. (t (error "Invalid action")))
  7799. (if (or (< (upcase new) org-highest-priority)
  7800. (> (upcase new) org-lowest-priority))
  7801. (setq remove t))
  7802. (setq news (format "%c" new))
  7803. (if have
  7804. (if remove
  7805. (replace-match "" t t nil 1)
  7806. (replace-match news t t nil 2))
  7807. (if remove
  7808. (error "No priority cookie found in line")
  7809. (looking-at org-todo-line-regexp)
  7810. (if (match-end 2)
  7811. (progn
  7812. (goto-char (match-end 2))
  7813. (insert " [#" news "]"))
  7814. (goto-char (match-beginning 3))
  7815. (insert "[#" news "] ")))))
  7816. (org-preserve-lc (org-set-tags nil 'align))
  7817. (if remove
  7818. (message "Priority removed")
  7819. (message "Priority of current item set to %s" news))))
  7820. (defun org-get-priority (s)
  7821. "Find priority cookie and return priority."
  7822. (save-match-data
  7823. (if (not (string-match org-priority-regexp s))
  7824. (* 1000 (- org-lowest-priority org-default-priority))
  7825. (* 1000 (- org-lowest-priority
  7826. (string-to-char (match-string 2 s)))))))
  7827. ;;;; Tags
  7828. (defun org-scan-tags (action matcher &optional todo-only)
  7829. "Scan headline tags with inheritance and produce output ACTION.
  7830. ACTION can be `sparse-tree' or `agenda'. MATCHER is a Lisp form to be
  7831. evaluated, testing if a given set of tags qualifies a headline for
  7832. inclusion. When TODO-ONLY is non-nil, only lines with a TODO keyword
  7833. are included in the output."
  7834. (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
  7835. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  7836. (org-re
  7837. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  7838. (props (list 'face nil
  7839. 'done-face 'org-done
  7840. 'undone-face nil
  7841. 'mouse-face 'highlight
  7842. 'org-not-done-regexp org-not-done-regexp
  7843. 'org-todo-regexp org-todo-regexp
  7844. 'keymap org-agenda-keymap
  7845. 'help-echo
  7846. (format "mouse-2 or RET jump to org file %s"
  7847. (abbreviate-file-name
  7848. (or (buffer-file-name (buffer-base-buffer))
  7849. (buffer-name (buffer-base-buffer)))))))
  7850. (case-fold-search nil)
  7851. lspos
  7852. tags tags-list tags-alist (llast 0) rtn level category i txt
  7853. todo marker entry priority)
  7854. (save-excursion
  7855. (goto-char (point-min))
  7856. (when (eq action 'sparse-tree)
  7857. (org-overview)
  7858. (org-remove-occur-highlights))
  7859. (while (re-search-forward re nil t)
  7860. (catch :skip
  7861. (setq todo (if (match-end 1) (match-string 2))
  7862. tags (if (match-end 4) (match-string 4)))
  7863. (goto-char (setq lspos (1+ (match-beginning 0))))
  7864. (setq level (org-reduced-level (funcall outline-level))
  7865. category (org-get-category))
  7866. (setq i llast llast level)
  7867. ;; remove tag lists from same and sublevels
  7868. (while (>= i level)
  7869. (when (setq entry (assoc i tags-alist))
  7870. (setq tags-alist (delete entry tags-alist)))
  7871. (setq i (1- i)))
  7872. ;; add the next tags
  7873. (when tags
  7874. (setq tags (mapcar 'downcase (org-split-string tags ":"))
  7875. tags-alist
  7876. (cons (cons level tags) tags-alist)))
  7877. ;; compile tags for current headline
  7878. (setq tags-list
  7879. (if org-use-tag-inheritance
  7880. (apply 'append (mapcar 'cdr tags-alist))
  7881. tags))
  7882. (when (and tags org-use-tag-inheritance
  7883. (not (eq t org-use-tag-inheritance)))
  7884. ;; selective inheritance, remove uninherited ones
  7885. (setcdr (car tags-alist)
  7886. (org-remove-uniherited-tags (cdar tags-alist))))
  7887. (when (and (or (not todo-only) (member todo org-not-done-keywords))
  7888. (eval matcher)
  7889. (or (not org-agenda-skip-archived-trees)
  7890. (not (member org-archive-tag tags-list))))
  7891. (and (eq action 'agenda) (org-agenda-skip))
  7892. ;; list this headline
  7893. (if (eq action 'sparse-tree)
  7894. (progn
  7895. (and org-highlight-sparse-tree-matches
  7896. (org-get-heading) (match-end 0)
  7897. (org-highlight-new-match
  7898. (match-beginning 0) (match-beginning 1)))
  7899. (org-show-context 'tags-tree))
  7900. (setq txt (org-format-agenda-item
  7901. ""
  7902. (concat
  7903. (if org-tags-match-list-sublevels
  7904. (make-string (1- level) ?.) "")
  7905. (org-get-heading))
  7906. category tags-list)
  7907. priority (org-get-priority txt))
  7908. (goto-char lspos)
  7909. (setq marker (org-agenda-new-marker))
  7910. (org-add-props txt props
  7911. 'org-marker marker 'org-hd-marker marker 'org-category category
  7912. 'priority priority 'type "tagsmatch")
  7913. (push txt rtn))
  7914. ;; if we are to skip sublevels, jump to end of subtree
  7915. (or org-tags-match-list-sublevels (org-end-of-subtree t))))))
  7916. (when (and (eq action 'sparse-tree)
  7917. (not org-sparse-tree-open-archived-trees))
  7918. (org-hide-archived-subtrees (point-min) (point-max)))
  7919. (nreverse rtn)))
  7920. (defun org-remove-uniherited-tags (tags)
  7921. "Remove all tags that are not inherited from the list TAGS."
  7922. (cond
  7923. ((eq org-use-tag-inheritance t) tags)
  7924. ((not org-use-tag-inheritance) nil)
  7925. ((stringp org-use-tag-inheritance)
  7926. (delq nil (mapcar
  7927. (lambda (x) (if (string-match org-use-tag-inheritance x) x nil))
  7928. tags)))
  7929. ((listp org-use-tag-inheritance)
  7930. (org-delete-all org-use-tag-inheritance tags))))
  7931. (defvar todo-only) ;; dynamically scoped
  7932. (defun org-tags-sparse-tree (&optional todo-only match)
  7933. "Create a sparse tree according to tags string MATCH.
  7934. MATCH can contain positive and negative selection of tags, like
  7935. \"+WORK+URGENT-WITHBOSS\".
  7936. If optional argument TODO_ONLY is non-nil, only select lines that are
  7937. also TODO lines."
  7938. (interactive "P")
  7939. (org-prepare-agenda-buffers (list (current-buffer)))
  7940. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  7941. (defvar org-cached-props nil)
  7942. (defun org-cached-entry-get (pom property)
  7943. (if (or (eq t org-use-property-inheritance)
  7944. (and (stringp org-use-property-inheritance)
  7945. (string-match org-use-property-inheritance property))
  7946. (and (listp org-use-property-inheritance)
  7947. (member property org-use-property-inheritance)))
  7948. ;; Caching is not possible, check it directly
  7949. (org-entry-get pom property 'inherit)
  7950. ;; Get all properties, so that we can do complicated checks easily
  7951. (cdr (assoc property (or org-cached-props
  7952. (setq org-cached-props
  7953. (org-entry-properties pom)))))))
  7954. (defun org-global-tags-completion-table (&optional files)
  7955. "Return the list of all tags in all agenda buffer/files."
  7956. (save-excursion
  7957. (org-uniquify
  7958. (delq nil
  7959. (apply 'append
  7960. (mapcar
  7961. (lambda (file)
  7962. (set-buffer (find-file-noselect file))
  7963. (append (org-get-buffer-tags)
  7964. (mapcar (lambda (x) (if (stringp (car-safe x))
  7965. (list (car-safe x)) nil))
  7966. org-tag-alist)))
  7967. (if (and files (car files))
  7968. files
  7969. (org-agenda-files))))))))
  7970. (defun org-make-tags-matcher (match)
  7971. "Create the TAGS//TODO matcher form for the selection string MATCH."
  7972. ;; todo-only is scoped dynamically into this function, and the function
  7973. ;; may change it it the matcher asksk for it.
  7974. (unless match
  7975. ;; Get a new match request, with completion
  7976. (let ((org-last-tags-completion-table
  7977. (org-global-tags-completion-table)))
  7978. (setq match (completing-read
  7979. "Match: " 'org-tags-completion-function nil nil nil
  7980. 'org-tags-history))))
  7981. ;; Parse the string and create a lisp form
  7982. (let ((match0 match)
  7983. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
  7984. minus tag mm
  7985. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  7986. orterms term orlist re-p str-p level-p level-op
  7987. prop-p pn pv po cat-p gv)
  7988. (if (string-match "/+" match)
  7989. ;; match contains also a todo-matching request
  7990. (progn
  7991. (setq tagsmatch (substring match 0 (match-beginning 0))
  7992. todomatch (substring match (match-end 0)))
  7993. (if (string-match "^!" todomatch)
  7994. (setq todo-only t todomatch (substring todomatch 1)))
  7995. (if (string-match "^\\s-*$" todomatch)
  7996. (setq todomatch nil)))
  7997. ;; only matching tags
  7998. (setq tagsmatch match todomatch nil))
  7999. ;; Make the tags matcher
  8000. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  8001. (setq tagsmatcher t)
  8002. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  8003. (while (setq term (pop orterms))
  8004. (while (and (equal (substring term -1) "\\") orterms)
  8005. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  8006. (while (string-match re term)
  8007. (setq minus (and (match-end 1)
  8008. (equal (match-string 1 term) "-"))
  8009. tag (match-string 2 term)
  8010. re-p (equal (string-to-char tag) ?{)
  8011. level-p (match-end 4)
  8012. prop-p (match-end 5)
  8013. mm (cond
  8014. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  8015. (level-p
  8016. (setq level-op (org-op-to-function (match-string 3 term)))
  8017. `(,level-op level ,(string-to-number
  8018. (match-string 4 term))))
  8019. (prop-p
  8020. (setq pn (match-string 5 term)
  8021. po (match-string 6 term)
  8022. pv (match-string 7 term)
  8023. cat-p (equal pn "CATEGORY")
  8024. re-p (equal (string-to-char pv) ?{)
  8025. str-p (equal (string-to-char pv) ?\")
  8026. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  8027. (setq po (org-op-to-function po str-p))
  8028. (if (equal pn "CATEGORY")
  8029. (setq gv '(get-text-property (point) 'org-category))
  8030. (setq gv `(org-cached-entry-get nil ,pn)))
  8031. (if re-p
  8032. (if (eq po 'org<>)
  8033. `(not (string-match ,pv (or ,gv "")))
  8034. `(string-match ,pv (or ,gv "")))
  8035. (if str-p
  8036. `(,po (or ,gv "") ,pv)
  8037. `(,po (string-to-number (or ,gv ""))
  8038. ,(string-to-number pv) ))))
  8039. (t `(member ,(downcase tag) tags-list)))
  8040. mm (if minus (list 'not mm) mm)
  8041. term (substring term (match-end 0)))
  8042. (push mm tagsmatcher))
  8043. (push (if (> (length tagsmatcher) 1)
  8044. (cons 'and tagsmatcher)
  8045. (car tagsmatcher))
  8046. orlist)
  8047. (setq tagsmatcher nil))
  8048. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  8049. (setq tagsmatcher
  8050. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  8051. ;; Make the todo matcher
  8052. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  8053. (setq todomatcher t)
  8054. (setq orterms (org-split-string todomatch "|") orlist nil)
  8055. (while (setq term (pop orterms))
  8056. (while (string-match re term)
  8057. (setq minus (and (match-end 1)
  8058. (equal (match-string 1 term) "-"))
  8059. kwd (match-string 2 term)
  8060. re-p (equal (string-to-char kwd) ?{)
  8061. term (substring term (match-end 0))
  8062. mm (if re-p
  8063. `(string-match ,(substring kwd 1 -1) todo)
  8064. (list 'equal 'todo kwd))
  8065. mm (if minus (list 'not mm) mm))
  8066. (push mm todomatcher))
  8067. (push (if (> (length todomatcher) 1)
  8068. (cons 'and todomatcher)
  8069. (car todomatcher))
  8070. orlist)
  8071. (setq todomatcher nil))
  8072. (setq todomatcher (if (> (length orlist) 1)
  8073. (cons 'or orlist) (car orlist))))
  8074. ;; Return the string and lisp forms of the matcher
  8075. (setq matcher (if todomatcher
  8076. (list 'and tagsmatcher todomatcher)
  8077. tagsmatcher))
  8078. (cons match0 matcher)))
  8079. (defun org-op-to-function (op &optional stringp)
  8080. (setq op
  8081. (cond
  8082. ((equal op "<" ) '(< string< ))
  8083. ((equal op ">" ) '(> org-string> ))
  8084. ((member op '("<=" "=<")) '(<= org-string<= ))
  8085. ((member op '(">=" "=>")) '(>= org-string>= ))
  8086. ((member op '("=" "==")) '(= string= ))
  8087. ((member op '("<>" "!=")) '(org<> org-string<> ))))
  8088. (nth (if stringp 1 0) op))
  8089. (defun org<> (a b) (not (= a b)))
  8090. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  8091. (defun org-string>= (a b) (not (string< a b)))
  8092. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  8093. (defun org-string<> (a b) (not (string= a b)))
  8094. (defun org-match-any-p (re list)
  8095. "Does re match any element of list?"
  8096. (setq list (mapcar (lambda (x) (string-match re x)) list))
  8097. (delq nil list))
  8098. (defvar org-add-colon-after-tag-completion nil) ;; dynamically skoped param
  8099. (defvar org-tags-overlay (org-make-overlay 1 1))
  8100. (org-detach-overlay org-tags-overlay)
  8101. (defun org-get-tags-at (&optional pos)
  8102. "Get a list of all headline tags applicable at POS.
  8103. POS defaults to point. If tags are inherited, the list contains
  8104. the targets in the same sequence as the headlines appear, i.e.
  8105. sthe tags of the current headline come last."
  8106. (interactive)
  8107. (let (tags ltags lastpos parent)
  8108. (save-excursion
  8109. (save-restriction
  8110. (widen)
  8111. (goto-char (or pos (point)))
  8112. (save-match-data
  8113. (condition-case nil
  8114. (progn
  8115. (org-back-to-heading t)
  8116. (while (not (equal lastpos (point)))
  8117. (setq lastpos (point))
  8118. (when (looking-at (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  8119. (setq ltags (org-split-string
  8120. (org-match-string-no-properties 1) ":"))
  8121. (setq tags (append (org-remove-uniherited-tags ltags)
  8122. tags)))
  8123. (or org-use-tag-inheritance (error ""))
  8124. (org-up-heading-all 1)
  8125. (setq parent t)))
  8126. (error nil))))
  8127. tags)))
  8128. (defun org-toggle-tag (tag &optional onoff)
  8129. "Toggle the tag TAG for the current line.
  8130. If ONOFF is `on' or `off', don't toggle but set to this state."
  8131. (unless (org-on-heading-p t) (error "Not on headling"))
  8132. (let (res current)
  8133. (save-excursion
  8134. (beginning-of-line)
  8135. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  8136. (point-at-eol) t)
  8137. (progn
  8138. (setq current (match-string 1))
  8139. (replace-match ""))
  8140. (setq current ""))
  8141. (setq current (nreverse (org-split-string current ":")))
  8142. (cond
  8143. ((eq onoff 'on)
  8144. (setq res t)
  8145. (or (member tag current) (push tag current)))
  8146. ((eq onoff 'off)
  8147. (or (not (member tag current)) (setq current (delete tag current))))
  8148. (t (if (member tag current)
  8149. (setq current (delete tag current))
  8150. (setq res t)
  8151. (push tag current))))
  8152. (end-of-line 1)
  8153. (if current
  8154. (progn
  8155. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  8156. (org-set-tags nil t))
  8157. (delete-horizontal-space))
  8158. (run-hooks 'org-after-tags-change-hook))
  8159. res))
  8160. (defun org-align-tags-here (to-col)
  8161. ;; Assumes that this is a headline
  8162. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  8163. (beginning-of-line 1)
  8164. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  8165. (< pos (match-beginning 2)))
  8166. (progn
  8167. (setq tags-l (- (match-end 2) (match-beginning 2)))
  8168. (goto-char (match-beginning 1))
  8169. (insert " ")
  8170. (delete-region (point) (1+ (match-beginning 2)))
  8171. (setq ncol (max (1+ (current-column))
  8172. (1+ col)
  8173. (if (> to-col 0)
  8174. to-col
  8175. (- (abs to-col) tags-l))))
  8176. (setq p (point))
  8177. (insert (make-string (- ncol (current-column)) ?\ ))
  8178. (setq ncol (current-column))
  8179. (tabify p (point-at-eol))
  8180. (org-move-to-column (min ncol col) t))
  8181. (goto-char pos))))
  8182. (defun org-set-tags (&optional arg just-align)
  8183. "Set the tags for the current headline.
  8184. With prefix ARG, realign all tags in headings in the current buffer."
  8185. (interactive "P")
  8186. (let* ((re (concat "^" outline-regexp))
  8187. (current (org-get-tags-string))
  8188. (col (current-column))
  8189. (org-setting-tags t)
  8190. table current-tags inherited-tags ; computed below when needed
  8191. tags p0 c0 c1 rpl)
  8192. (if arg
  8193. (save-excursion
  8194. (goto-char (point-min))
  8195. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  8196. (while (re-search-forward re nil t)
  8197. (org-set-tags nil t)
  8198. (end-of-line 1)))
  8199. (message "All tags realigned to column %d" org-tags-column))
  8200. (if just-align
  8201. (setq tags current)
  8202. ;; Get a new set of tags from the user
  8203. (save-excursion
  8204. (setq table (or org-tag-alist (org-get-buffer-tags))
  8205. org-last-tags-completion-table table
  8206. current-tags (org-split-string current ":")
  8207. inherited-tags (nreverse
  8208. (nthcdr (length current-tags)
  8209. (nreverse (org-get-tags-at))))
  8210. tags
  8211. (if (or (eq t org-use-fast-tag-selection)
  8212. (and org-use-fast-tag-selection
  8213. (delq nil (mapcar 'cdr table))))
  8214. (org-fast-tag-selection
  8215. current-tags inherited-tags table
  8216. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  8217. (let ((org-add-colon-after-tag-completion t))
  8218. (org-trim
  8219. (org-without-partial-completion
  8220. (completing-read "Tags: " 'org-tags-completion-function
  8221. nil nil current 'org-tags-history)))))))
  8222. (while (string-match "[-+&]+" tags)
  8223. ;; No boolean logic, just a list
  8224. (setq tags (replace-match ":" t t tags))))
  8225. (if (string-match "\\`[\t ]*\\'" tags)
  8226. (setq tags "")
  8227. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  8228. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  8229. ;; Insert new tags at the correct column
  8230. (beginning-of-line 1)
  8231. (cond
  8232. ((and (equal current "") (equal tags "")))
  8233. ((re-search-forward
  8234. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  8235. (point-at-eol) t)
  8236. (if (equal tags "")
  8237. (setq rpl "")
  8238. (goto-char (match-beginning 0))
  8239. (setq c0 (current-column) p0 (point)
  8240. c1 (max (1+ c0) (if (> org-tags-column 0)
  8241. org-tags-column
  8242. (- (- org-tags-column) (length tags))))
  8243. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  8244. (replace-match rpl t t)
  8245. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  8246. tags)
  8247. (t (error "Tags alignment failed")))
  8248. (org-move-to-column col)
  8249. (unless just-align
  8250. (run-hooks 'org-after-tags-change-hook)))))
  8251. (defun org-change-tag-in-region (beg end tag off)
  8252. "Add or remove TAG for each entry in the region.
  8253. This works in the agenda, and also in an org-mode buffer."
  8254. (interactive
  8255. (list (region-beginning) (region-end)
  8256. (let ((org-last-tags-completion-table
  8257. (if (org-mode-p)
  8258. (org-get-buffer-tags)
  8259. (org-global-tags-completion-table))))
  8260. (completing-read
  8261. "Tag: " 'org-tags-completion-function nil nil nil
  8262. 'org-tags-history))
  8263. (progn
  8264. (message "[s]et or [r]emove? ")
  8265. (equal (read-char-exclusive) ?r))))
  8266. (if (fboundp 'deactivate-mark) (deactivate-mark))
  8267. (let ((agendap (equal major-mode 'org-agenda-mode))
  8268. l1 l2 m buf pos newhead (cnt 0))
  8269. (goto-char end)
  8270. (setq l2 (1- (org-current-line)))
  8271. (goto-char beg)
  8272. (setq l1 (org-current-line))
  8273. (loop for l from l1 to l2 do
  8274. (goto-line l)
  8275. (setq m (get-text-property (point) 'org-hd-marker))
  8276. (when (or (and (org-mode-p) (org-on-heading-p))
  8277. (and agendap m))
  8278. (setq buf (if agendap (marker-buffer m) (current-buffer))
  8279. pos (if agendap m (point)))
  8280. (with-current-buffer buf
  8281. (save-excursion
  8282. (save-restriction
  8283. (goto-char pos)
  8284. (setq cnt (1+ cnt))
  8285. (org-toggle-tag tag (if off 'off 'on))
  8286. (setq newhead (org-get-heading)))))
  8287. (and agendap (org-agenda-change-all-lines newhead m))))
  8288. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  8289. (defun org-tags-completion-function (string predicate &optional flag)
  8290. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  8291. (confirm (lambda (x) (stringp (car x)))))
  8292. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  8293. (setq s1 (match-string 1 string)
  8294. s2 (match-string 2 string))
  8295. (setq s1 "" s2 string))
  8296. (cond
  8297. ((eq flag nil)
  8298. ;; try completion
  8299. (setq rtn (try-completion s2 ctable confirm))
  8300. (if (stringp rtn)
  8301. (setq rtn
  8302. (concat s1 s2 (substring rtn (length s2))
  8303. (if (and org-add-colon-after-tag-completion
  8304. (assoc rtn ctable))
  8305. ":" ""))))
  8306. rtn)
  8307. ((eq flag t)
  8308. ;; all-completions
  8309. (all-completions s2 ctable confirm)
  8310. )
  8311. ((eq flag 'lambda)
  8312. ;; exact match?
  8313. (assoc s2 ctable)))
  8314. ))
  8315. (defun org-fast-tag-insert (kwd tags face &optional end)
  8316. "Insert KDW, and the TAGS, the latter with face FACE. Also inser END."
  8317. (insert (format "%-12s" (concat kwd ":"))
  8318. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  8319. (or end "")))
  8320. (defun org-fast-tag-show-exit (flag)
  8321. (save-excursion
  8322. (goto-line 3)
  8323. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  8324. (replace-match ""))
  8325. (when flag
  8326. (end-of-line 1)
  8327. (org-move-to-column (- (window-width) 19) t)
  8328. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  8329. (defun org-set-current-tags-overlay (current prefix)
  8330. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  8331. (if (featurep 'xemacs)
  8332. (org-overlay-display org-tags-overlay (concat prefix s)
  8333. 'secondary-selection)
  8334. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  8335. (org-overlay-display org-tags-overlay (concat prefix s)))))
  8336. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  8337. "Fast tag selection with single keys.
  8338. CURRENT is the current list of tags in the headline, INHERITED is the
  8339. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  8340. possibly with grouping information. TODO-TABLE is a similar table with
  8341. TODO keywords, should these have keys assigned to them.
  8342. If the keys are nil, a-z are automatically assigned.
  8343. Returns the new tags string, or nil to not change the current settings."
  8344. (let* ((fulltable (append table todo-table))
  8345. (maxlen (apply 'max (mapcar
  8346. (lambda (x)
  8347. (if (stringp (car x)) (string-width (car x)) 0))
  8348. fulltable)))
  8349. (buf (current-buffer))
  8350. (expert (eq org-fast-tag-selection-single-key 'expert))
  8351. (buffer-tags nil)
  8352. (fwidth (+ maxlen 3 1 3))
  8353. (ncol (/ (- (window-width) 4) fwidth))
  8354. (i-face 'org-done)
  8355. (c-face 'org-todo)
  8356. tg cnt e c char c1 c2 ntable tbl rtn
  8357. ov-start ov-end ov-prefix
  8358. (exit-after-next org-fast-tag-selection-single-key)
  8359. (done-keywords org-done-keywords)
  8360. groups ingroup)
  8361. (save-excursion
  8362. (beginning-of-line 1)
  8363. (if (looking-at
  8364. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  8365. (setq ov-start (match-beginning 1)
  8366. ov-end (match-end 1)
  8367. ov-prefix "")
  8368. (setq ov-start (1- (point-at-eol))
  8369. ov-end (1+ ov-start))
  8370. (skip-chars-forward "^\n\r")
  8371. (setq ov-prefix
  8372. (concat
  8373. (buffer-substring (1- (point)) (point))
  8374. (if (> (current-column) org-tags-column)
  8375. " "
  8376. (make-string (- org-tags-column (current-column)) ?\ ))))))
  8377. (org-move-overlay org-tags-overlay ov-start ov-end)
  8378. (save-window-excursion
  8379. (if expert
  8380. (set-buffer (get-buffer-create " *Org tags*"))
  8381. (delete-other-windows)
  8382. (split-window-vertically)
  8383. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  8384. (erase-buffer)
  8385. (org-set-local 'org-done-keywords done-keywords)
  8386. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  8387. (org-fast-tag-insert "Current" current c-face "\n\n")
  8388. (org-fast-tag-show-exit exit-after-next)
  8389. (org-set-current-tags-overlay current ov-prefix)
  8390. (setq tbl fulltable char ?a cnt 0)
  8391. (while (setq e (pop tbl))
  8392. (cond
  8393. ((equal e '(:startgroup))
  8394. (push '() groups) (setq ingroup t)
  8395. (when (not (= cnt 0))
  8396. (setq cnt 0)
  8397. (insert "\n"))
  8398. (insert "{ "))
  8399. ((equal e '(:endgroup))
  8400. (setq ingroup nil cnt 0)
  8401. (insert "}\n"))
  8402. (t
  8403. (setq tg (car e) c2 nil)
  8404. (if (cdr e)
  8405. (setq c (cdr e))
  8406. ;; automatically assign a character.
  8407. (setq c1 (string-to-char
  8408. (downcase (substring
  8409. tg (if (= (string-to-char tg) ?@) 1 0)))))
  8410. (if (or (rassoc c1 ntable) (rassoc c1 table))
  8411. (while (or (rassoc char ntable) (rassoc char table))
  8412. (setq char (1+ char)))
  8413. (setq c2 c1))
  8414. (setq c (or c2 char)))
  8415. (if ingroup (push tg (car groups)))
  8416. (setq tg (org-add-props tg nil 'face
  8417. (cond
  8418. ((not (assoc tg table))
  8419. (org-get-todo-face tg))
  8420. ((member tg current) c-face)
  8421. ((member tg inherited) i-face)
  8422. (t nil))))
  8423. (if (and (= cnt 0) (not ingroup)) (insert " "))
  8424. (insert "[" c "] " tg (make-string
  8425. (- fwidth 4 (length tg)) ?\ ))
  8426. (push (cons tg c) ntable)
  8427. (when (= (setq cnt (1+ cnt)) ncol)
  8428. (insert "\n")
  8429. (if ingroup (insert " "))
  8430. (setq cnt 0)))))
  8431. (setq ntable (nreverse ntable))
  8432. (insert "\n")
  8433. (goto-char (point-min))
  8434. (if (and (not expert) (fboundp 'fit-window-to-buffer))
  8435. (fit-window-to-buffer))
  8436. (setq rtn
  8437. (catch 'exit
  8438. (while t
  8439. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  8440. (if groups " [!] no groups" " [!]groups")
  8441. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  8442. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  8443. (cond
  8444. ((= c ?\r) (throw 'exit t))
  8445. ((= c ?!)
  8446. (setq groups (not groups))
  8447. (goto-char (point-min))
  8448. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  8449. ((= c ?\C-c)
  8450. (if (not expert)
  8451. (org-fast-tag-show-exit
  8452. (setq exit-after-next (not exit-after-next)))
  8453. (setq expert nil)
  8454. (delete-other-windows)
  8455. (split-window-vertically)
  8456. (org-switch-to-buffer-other-window " *Org tags*")
  8457. (and (fboundp 'fit-window-to-buffer)
  8458. (fit-window-to-buffer))))
  8459. ((or (= c ?\C-g)
  8460. (and (= c ?q) (not (rassoc c ntable))))
  8461. (org-detach-overlay org-tags-overlay)
  8462. (setq quit-flag t))
  8463. ((= c ?\ )
  8464. (setq current nil)
  8465. (if exit-after-next (setq exit-after-next 'now)))
  8466. ((= c ?\t)
  8467. (condition-case nil
  8468. (setq tg (completing-read
  8469. "Tag: "
  8470. (or buffer-tags
  8471. (with-current-buffer buf
  8472. (org-get-buffer-tags)))))
  8473. (quit (setq tg "")))
  8474. (when (string-match "\\S-" tg)
  8475. (add-to-list 'buffer-tags (list tg))
  8476. (if (member tg current)
  8477. (setq current (delete tg current))
  8478. (push tg current)))
  8479. (if exit-after-next (setq exit-after-next 'now)))
  8480. ((setq e (rassoc c todo-table) tg (car e))
  8481. (with-current-buffer buf
  8482. (save-excursion (org-todo tg)))
  8483. (if exit-after-next (setq exit-after-next 'now)))
  8484. ((setq e (rassoc c ntable) tg (car e))
  8485. (if (member tg current)
  8486. (setq current (delete tg current))
  8487. (loop for g in groups do
  8488. (if (member tg g)
  8489. (mapc (lambda (x)
  8490. (setq current (delete x current)))
  8491. g)))
  8492. (push tg current))
  8493. (if exit-after-next (setq exit-after-next 'now))))
  8494. ;; Create a sorted list
  8495. (setq current
  8496. (sort current
  8497. (lambda (a b)
  8498. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  8499. (if (eq exit-after-next 'now) (throw 'exit t))
  8500. (goto-char (point-min))
  8501. (beginning-of-line 2)
  8502. (delete-region (point) (point-at-eol))
  8503. (org-fast-tag-insert "Current" current c-face)
  8504. (org-set-current-tags-overlay current ov-prefix)
  8505. (while (re-search-forward
  8506. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  8507. (setq tg (match-string 1))
  8508. (add-text-properties
  8509. (match-beginning 1) (match-end 1)
  8510. (list 'face
  8511. (cond
  8512. ((member tg current) c-face)
  8513. ((member tg inherited) i-face)
  8514. (t (get-text-property (match-beginning 1) 'face))))))
  8515. (goto-char (point-min)))))
  8516. (org-detach-overlay org-tags-overlay)
  8517. (if rtn
  8518. (mapconcat 'identity current ":")
  8519. nil))))
  8520. (defun org-get-tags-string ()
  8521. "Get the TAGS string in the current headline."
  8522. (unless (org-on-heading-p t)
  8523. (error "Not on a heading"))
  8524. (save-excursion
  8525. (beginning-of-line 1)
  8526. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  8527. (org-match-string-no-properties 1)
  8528. "")))
  8529. (defun org-get-tags ()
  8530. "Get the list of tags specified in the current headline."
  8531. (org-split-string (org-get-tags-string) ":"))
  8532. (defun org-get-buffer-tags ()
  8533. "Get a table of all tags used in the buffer, for completion."
  8534. (let (tags)
  8535. (save-excursion
  8536. (goto-char (point-min))
  8537. (while (re-search-forward
  8538. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  8539. (when (equal (char-after (point-at-bol 0)) ?*)
  8540. (mapc (lambda (x) (add-to-list 'tags x))
  8541. (org-split-string (org-match-string-no-properties 1) ":")))))
  8542. (mapcar 'list tags)))
  8543. ;;;; Properties
  8544. ;;; Setting and retrieving properties
  8545. (defconst org-special-properties
  8546. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "PRIORITY"
  8547. "TIMESTAMP" "TIMESTAMP_IA")
  8548. "The special properties valid in Org-mode.
  8549. These are properties that are not defined in the property drawer,
  8550. but in some other way.")
  8551. (defconst org-default-properties
  8552. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION"
  8553. "LOCATION" "LOGGING" "COLUMNS")
  8554. "Some properties that are used by Org-mode for various purposes.
  8555. Being in this list makes sure that they are offered for completion.")
  8556. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  8557. "Regular expression matching the first line of a property drawer.")
  8558. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  8559. "Regular expression matching the first line of a property drawer.")
  8560. (defun org-property-action ()
  8561. "Do an action on properties."
  8562. (interactive)
  8563. (let (c)
  8564. (org-at-property-p)
  8565. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  8566. (setq c (read-char-exclusive))
  8567. (cond
  8568. ((equal c ?s)
  8569. (call-interactively 'org-set-property))
  8570. ((equal c ?d)
  8571. (call-interactively 'org-delete-property))
  8572. ((equal c ?D)
  8573. (call-interactively 'org-delete-property-globally))
  8574. ((equal c ?c)
  8575. (call-interactively 'org-compute-property-at-point))
  8576. (t (error "No such property action %c" c)))))
  8577. (defun org-at-property-p ()
  8578. "Is the cursor in a property line?"
  8579. ;; FIXME: Does not check if we are actually in the drawer.
  8580. ;; FIXME: also returns true on any drawers.....
  8581. ;; This is used by C-c C-c for property action.
  8582. (save-excursion
  8583. (beginning-of-line 1)
  8584. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  8585. (defun org-get-property-block (&optional beg end force)
  8586. "Return the (beg . end) range of the body of the property drawer.
  8587. BEG and END can be beginning and end of subtree, if not given
  8588. they will be found.
  8589. If the drawer does not exist and FORCE is non-nil, create the drawer."
  8590. (catch 'exit
  8591. (save-excursion
  8592. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  8593. (end (or end (progn (outline-next-heading) (point)))))
  8594. (goto-char beg)
  8595. (if (re-search-forward org-property-start-re end t)
  8596. (setq beg (1+ (match-end 0)))
  8597. (if force
  8598. (save-excursion
  8599. (org-insert-property-drawer)
  8600. (setq end (progn (outline-next-heading) (point))))
  8601. (throw 'exit nil))
  8602. (goto-char beg)
  8603. (if (re-search-forward org-property-start-re end t)
  8604. (setq beg (1+ (match-end 0)))))
  8605. (if (re-search-forward org-property-end-re end t)
  8606. (setq end (match-beginning 0))
  8607. (or force (throw 'exit nil))
  8608. (goto-char beg)
  8609. (setq end beg)
  8610. (org-indent-line-function)
  8611. (insert ":END:\n"))
  8612. (cons beg end)))))
  8613. (defun org-entry-properties (&optional pom which)
  8614. "Get all properties of the entry at point-or-marker POM.
  8615. This includes the TODO keyword, the tags, time strings for deadline,
  8616. scheduled, and clocking, and any additional properties defined in the
  8617. entry. The return value is an alist, keys may occur multiple times
  8618. if the property key was used several times.
  8619. POM may also be nil, in which case the current entry is used.
  8620. If WHICH is nil or `all', get all properties. If WHICH is
  8621. `special' or `standard', only get that subclass."
  8622. (setq which (or which 'all))
  8623. (org-with-point-at pom
  8624. (let ((clockstr (substring org-clock-string 0 -1))
  8625. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  8626. beg end range props sum-props key value string clocksum)
  8627. (save-excursion
  8628. (when (condition-case nil (org-back-to-heading t) (error nil))
  8629. (setq beg (point))
  8630. (setq sum-props (get-text-property (point) 'org-summaries))
  8631. (setq clocksum (get-text-property (point) :org-clock-minutes))
  8632. (outline-next-heading)
  8633. (setq end (point))
  8634. (when (memq which '(all special))
  8635. ;; Get the special properties, like TODO and tags
  8636. (goto-char beg)
  8637. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  8638. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  8639. (when (looking-at org-priority-regexp)
  8640. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  8641. (when (and (setq value (org-get-tags-string))
  8642. (string-match "\\S-" value))
  8643. (push (cons "TAGS" value) props))
  8644. (when (setq value (org-get-tags-at))
  8645. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  8646. props))
  8647. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  8648. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  8649. string (if (equal key clockstr)
  8650. (org-no-properties
  8651. (org-trim
  8652. (buffer-substring
  8653. (match-beginning 3) (goto-char (point-at-eol)))))
  8654. (substring (org-match-string-no-properties 3) 1 -1)))
  8655. (unless key
  8656. (if (= (char-after (match-beginning 3)) ?\[)
  8657. (setq key "TIMESTAMP_IA")
  8658. (setq key "TIMESTAMP")))
  8659. (when (or (equal key clockstr) (not (assoc key props)))
  8660. (push (cons key string) props)))
  8661. )
  8662. (when (memq which '(all standard))
  8663. ;; Get the standard properties, like :PORP: ...
  8664. (setq range (org-get-property-block beg end))
  8665. (when range
  8666. (goto-char (car range))
  8667. (while (re-search-forward
  8668. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  8669. (cdr range) t)
  8670. (setq key (org-match-string-no-properties 1)
  8671. value (org-trim (or (org-match-string-no-properties 2) "")))
  8672. (unless (member key excluded)
  8673. (push (cons key (or value "")) props)))))
  8674. (if clocksum
  8675. (push (cons "CLOCKSUM"
  8676. (org-columns-number-to-string (/ (float clocksum) 60.)
  8677. 'add_times))
  8678. props))
  8679. (append sum-props (nreverse props)))))))
  8680. (defun org-entry-get (pom property &optional inherit)
  8681. "Get value of PROPERTY for entry at point-or-marker POM.
  8682. If INHERIT is non-nil and the entry does not have the property,
  8683. then also check higher levels of the hierarchy.
  8684. If INHERIT is the symbol `selective', use inheritance only if the setting
  8685. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  8686. If the property is present but empty, the return value is the empty string.
  8687. If the property is not present at all, nil is returned."
  8688. (org-with-point-at pom
  8689. (if (and inherit (if (eq inherit 'selective)
  8690. (org-property-inherit-p property)
  8691. t))
  8692. (org-entry-get-with-inheritance property)
  8693. (if (member property org-special-properties)
  8694. ;; We need a special property. Use brute force, get all properties.
  8695. (cdr (assoc property (org-entry-properties nil 'special)))
  8696. (let ((range (org-get-property-block)))
  8697. (if (and range
  8698. (goto-char (car range))
  8699. (re-search-forward
  8700. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)?")
  8701. (cdr range) t))
  8702. ;; Found the property, return it.
  8703. (if (match-end 1)
  8704. (org-match-string-no-properties 1)
  8705. "")))))))
  8706. (defun org-property-or-variable-value (var &optional inherit)
  8707. "Check if there is a property fixing the value of VAR.
  8708. If yes, return this value. If not, return the current value of the variable."
  8709. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  8710. (if (and prop (stringp prop) (string-match "\\S-" prop))
  8711. (read prop)
  8712. (symbol-value var))))
  8713. (defun org-entry-delete (pom property)
  8714. "Delete the property PROPERTY from entry at point-or-marker POM."
  8715. (org-with-point-at pom
  8716. (if (member property org-special-properties)
  8717. nil ; cannot delete these properties.
  8718. (let ((range (org-get-property-block)))
  8719. (if (and range
  8720. (goto-char (car range))
  8721. (re-search-forward
  8722. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)")
  8723. (cdr range) t))
  8724. (progn
  8725. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  8726. t)
  8727. nil)))))
  8728. ;; Multi-values properties are properties that contain multiple values
  8729. ;; These values are assumed to be single words, separated by whitespace.
  8730. (defun org-entry-add-to-multivalued-property (pom property value)
  8731. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  8732. (let* ((old (org-entry-get pom property))
  8733. (values (and old (org-split-string old "[ \t]"))))
  8734. (unless (member value values)
  8735. (setq values (cons value values))
  8736. (org-entry-put pom property
  8737. (mapconcat 'identity values " ")))))
  8738. (defun org-entry-remove-from-multivalued-property (pom property value)
  8739. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  8740. (let* ((old (org-entry-get pom property))
  8741. (values (and old (org-split-string old "[ \t]"))))
  8742. (when (member value values)
  8743. (setq values (delete value values))
  8744. (org-entry-put pom property
  8745. (mapconcat 'identity values " ")))))
  8746. (defun org-entry-member-in-multivalued-property (pom property value)
  8747. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  8748. (let* ((old (org-entry-get pom property))
  8749. (values (and old (org-split-string old "[ \t]"))))
  8750. (member value values)))
  8751. (defvar org-entry-property-inherited-from (make-marker))
  8752. (defun org-entry-get-with-inheritance (property)
  8753. "Get entry property, and search higher levels if not present."
  8754. (let (tmp)
  8755. (save-excursion
  8756. (save-restriction
  8757. (widen)
  8758. (catch 'ex
  8759. (while t
  8760. (when (setq tmp (org-entry-get nil property))
  8761. (org-back-to-heading t)
  8762. (move-marker org-entry-property-inherited-from (point))
  8763. (throw 'ex tmp))
  8764. (or (org-up-heading-safe) (throw 'ex nil)))))
  8765. (or tmp (cdr (assoc property org-local-properties))
  8766. (cdr (assoc property org-global-properties))))))
  8767. (defun org-entry-put (pom property value)
  8768. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  8769. (org-with-point-at pom
  8770. (org-back-to-heading t)
  8771. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  8772. range)
  8773. (cond
  8774. ((equal property "TODO")
  8775. (when (and (stringp value) (string-match "\\S-" value)
  8776. (not (member value org-todo-keywords-1)))
  8777. (error "\"%s\" is not a valid TODO state" value))
  8778. (if (or (not value)
  8779. (not (string-match "\\S-" value)))
  8780. (setq value 'none))
  8781. (org-todo value)
  8782. (org-set-tags nil 'align))
  8783. ((equal property "PRIORITY")
  8784. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  8785. (string-to-char value) ?\ ))
  8786. (org-set-tags nil 'align))
  8787. ((equal property "SCHEDULED")
  8788. (if (re-search-forward org-scheduled-time-regexp end t)
  8789. (cond
  8790. ((eq value 'earlier) (org-timestamp-change -1 'day))
  8791. ((eq value 'later) (org-timestamp-change 1 'day))
  8792. (t (call-interactively 'org-schedule)))
  8793. (call-interactively 'org-schedule)))
  8794. ((equal property "DEADLINE")
  8795. (if (re-search-forward org-deadline-time-regexp end t)
  8796. (cond
  8797. ((eq value 'earlier) (org-timestamp-change -1 'day))
  8798. ((eq value 'later) (org-timestamp-change 1 'day))
  8799. (t (call-interactively 'org-deadline)))
  8800. (call-interactively 'org-deadline)))
  8801. ((member property org-special-properties)
  8802. (error "The %s property can not yet be set with `org-entry-put'"
  8803. property))
  8804. (t ; a non-special property
  8805. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  8806. (setq range (org-get-property-block beg end 'force))
  8807. (goto-char (car range))
  8808. (if (re-search-forward
  8809. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  8810. (progn
  8811. (delete-region (match-beginning 1) (match-end 1))
  8812. (goto-char (match-beginning 1)))
  8813. (goto-char (cdr range))
  8814. (insert "\n")
  8815. (backward-char 1)
  8816. (org-indent-line-function)
  8817. (insert ":" property ":"))
  8818. (and value (insert " " value))
  8819. (org-indent-line-function)))))))
  8820. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  8821. "Get all property keys in the current buffer.
  8822. With INCLUDE-SPECIALS, also list the special properties that relect things
  8823. like tags and TODO state.
  8824. With INCLUDE-DEFAULTS, also include properties that has special meaning
  8825. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  8826. With INCLUDE-COLUMNS, also include property names given in COLUMN
  8827. formats in the current buffer."
  8828. (let (rtn range cfmt cols s p)
  8829. (save-excursion
  8830. (save-restriction
  8831. (widen)
  8832. (goto-char (point-min))
  8833. (while (re-search-forward org-property-start-re nil t)
  8834. (setq range (org-get-property-block))
  8835. (goto-char (car range))
  8836. (while (re-search-forward
  8837. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  8838. (cdr range) t)
  8839. (add-to-list 'rtn (org-match-string-no-properties 1)))
  8840. (outline-next-heading))))
  8841. (when include-specials
  8842. (setq rtn (append org-special-properties rtn)))
  8843. (when include-defaults
  8844. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties))
  8845. (when include-columns
  8846. (save-excursion
  8847. (save-restriction
  8848. (widen)
  8849. (goto-char (point-min))
  8850. (while (re-search-forward
  8851. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  8852. nil t)
  8853. (setq cfmt (match-string 2) s 0)
  8854. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  8855. cfmt s)
  8856. (setq s (match-end 0)
  8857. p (match-string 1 cfmt))
  8858. (unless (or (equal p "ITEM")
  8859. (member p org-special-properties))
  8860. (add-to-list 'rtn (match-string 1 cfmt))))))))
  8861. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  8862. (defun org-property-values (key)
  8863. "Return a list of all values of property KEY."
  8864. (save-excursion
  8865. (save-restriction
  8866. (widen)
  8867. (goto-char (point-min))
  8868. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  8869. values)
  8870. (while (re-search-forward re nil t)
  8871. (add-to-list 'values (org-trim (match-string 1))))
  8872. (delete "" values)))))
  8873. (defun org-insert-property-drawer ()
  8874. "Insert a property drawer into the current entry."
  8875. (interactive)
  8876. (org-back-to-heading t)
  8877. (looking-at outline-regexp)
  8878. (let ((indent (- (match-end 0)(match-beginning 0)))
  8879. (beg (point))
  8880. (re (concat "^[ \t]*" org-keyword-time-regexp))
  8881. end hiddenp)
  8882. (outline-next-heading)
  8883. (setq end (point))
  8884. (goto-char beg)
  8885. (while (re-search-forward re end t))
  8886. (setq hiddenp (org-invisible-p))
  8887. (end-of-line 1)
  8888. (and (equal (char-after) ?\n) (forward-char 1))
  8889. (while (looking-at "^[ \t]*\\(:CLOCK:\\|CLOCK\\|:END:\\)")
  8890. (beginning-of-line 2))
  8891. (org-skip-over-state-notes)
  8892. (skip-chars-backward " \t\n\r")
  8893. (if (eq (char-before) ?*) (forward-char 1))
  8894. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  8895. (beginning-of-line 0)
  8896. (org-indent-to-column indent)
  8897. (beginning-of-line 2)
  8898. (org-indent-to-column indent)
  8899. (beginning-of-line 0)
  8900. (if hiddenp
  8901. (save-excursion
  8902. (org-back-to-heading t)
  8903. (hide-entry))
  8904. (org-flag-drawer t))))
  8905. (defun org-set-property (property value)
  8906. "In the current entry, set PROPERTY to VALUE.
  8907. When called interactively, this will prompt for a property name, offering
  8908. completion on existing and default properties. And then it will prompt
  8909. for a value, offering competion either on allowed values (via an inherited
  8910. xxx_ALL property) or on existing values in other instances of this property
  8911. in the current file."
  8912. (interactive
  8913. (let* ((prop (completing-read
  8914. "Property: " (mapcar 'list (org-buffer-property-keys nil t t))))
  8915. (cur (org-entry-get nil prop))
  8916. (allowed (org-property-get-allowed-values nil prop 'table))
  8917. (existing (mapcar 'list (org-property-values prop)))
  8918. (val (if allowed
  8919. (completing-read "Value: " allowed nil 'req-match)
  8920. (completing-read
  8921. (concat "Value" (if (and cur (string-match "\\S-" cur))
  8922. (concat "[" cur "]") "")
  8923. ": ")
  8924. existing nil nil "" nil cur))))
  8925. (list prop (if (equal val "") cur val))))
  8926. (unless (equal (org-entry-get nil property) value)
  8927. (org-entry-put nil property value)))
  8928. (defun org-delete-property (property)
  8929. "In the current entry, delete PROPERTY."
  8930. (interactive
  8931. (let* ((prop (completing-read
  8932. "Property: " (org-entry-properties nil 'standard))))
  8933. (list prop)))
  8934. (message "Property %s %s" property
  8935. (if (org-entry-delete nil property)
  8936. "deleted"
  8937. "was not present in the entry")))
  8938. (defun org-delete-property-globally (property)
  8939. "Remove PROPERTY globally, from all entries."
  8940. (interactive
  8941. (let* ((prop (completing-read
  8942. "Globally remove property: "
  8943. (mapcar 'list (org-buffer-property-keys)))))
  8944. (list prop)))
  8945. (save-excursion
  8946. (save-restriction
  8947. (widen)
  8948. (goto-char (point-min))
  8949. (let ((cnt 0))
  8950. (while (re-search-forward
  8951. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  8952. nil t)
  8953. (setq cnt (1+ cnt))
  8954. (replace-match ""))
  8955. (message "Property \"%s\" removed from %d entries" property cnt)))))
  8956. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  8957. (defun org-compute-property-at-point ()
  8958. "Compute the property at point.
  8959. This looks for an enclosing column format, extracts the operator and
  8960. then applies it to the proerty in the column format's scope."
  8961. (interactive)
  8962. (unless (org-at-property-p)
  8963. (error "Not at a property"))
  8964. (let ((prop (org-match-string-no-properties 2)))
  8965. (org-columns-get-format-and-top-level)
  8966. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  8967. (error "No operator defined for property %s" prop))
  8968. (org-columns-compute prop)))
  8969. (defun org-property-get-allowed-values (pom property &optional table)
  8970. "Get allowed values for the property PROPERTY.
  8971. When TABLE is non-nil, return an alist that can directly be used for
  8972. completion."
  8973. (let (vals)
  8974. (cond
  8975. ((equal property "TODO")
  8976. (setq vals (org-with-point-at pom
  8977. (append org-todo-keywords-1 '("")))))
  8978. ((equal property "PRIORITY")
  8979. (let ((n org-lowest-priority))
  8980. (while (>= n org-highest-priority)
  8981. (push (char-to-string n) vals)
  8982. (setq n (1- n)))))
  8983. ((member property org-special-properties))
  8984. (t
  8985. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  8986. (when (and vals (string-match "\\S-" vals))
  8987. (setq vals (car (read-from-string (concat "(" vals ")"))))
  8988. (setq vals (mapcar (lambda (x)
  8989. (cond ((stringp x) x)
  8990. ((numberp x) (number-to-string x))
  8991. ((symbolp x) (symbol-name x))
  8992. (t "???")))
  8993. vals)))))
  8994. (if table (mapcar 'list vals) vals)))
  8995. (defun org-property-previous-allowed-value (&optional previous)
  8996. "Switch to the next allowed value for this property."
  8997. (interactive)
  8998. (org-property-next-allowed-value t))
  8999. (defun org-property-next-allowed-value (&optional previous)
  9000. "Switch to the next allowed value for this property."
  9001. (interactive)
  9002. (unless (org-at-property-p)
  9003. (error "Not at a property"))
  9004. (let* ((key (match-string 2))
  9005. (value (match-string 3))
  9006. (allowed (or (org-property-get-allowed-values (point) key)
  9007. (and (member value '("[ ]" "[-]" "[X]"))
  9008. '("[ ]" "[X]"))))
  9009. nval)
  9010. (unless allowed
  9011. (error "Allowed values for this property have not been defined"))
  9012. (if previous (setq allowed (reverse allowed)))
  9013. (if (member value allowed)
  9014. (setq nval (car (cdr (member value allowed)))))
  9015. (setq nval (or nval (car allowed)))
  9016. (if (equal nval value)
  9017. (error "Only one allowed value for this property"))
  9018. (org-at-property-p)
  9019. (replace-match (concat " :" key ": " nval) t t)
  9020. (org-indent-line-function)
  9021. (beginning-of-line 1)
  9022. (skip-chars-forward " \t")))
  9023. (defun org-find-entry-with-id (ident)
  9024. "Locate the entry that contains the ID property with exact value IDENT.
  9025. IDENT can be a string, a symbol or a number, this function will search for
  9026. the string representation of it.
  9027. Return the position where this entry starts, or nil if there is no such entry."
  9028. (let ((id (cond
  9029. ((stringp ident) ident)
  9030. ((symbol-name ident) (symbol-name ident))
  9031. ((numberp ident) (number-to-string ident))
  9032. (t (error "IDENT %s must be a string, symbol or number" ident))))
  9033. (case-fold-search nil))
  9034. (save-excursion
  9035. (save-restriction
  9036. (widen)
  9037. (goto-char (point-min))
  9038. (when (re-search-forward
  9039. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  9040. nil t)
  9041. (org-back-to-heading)
  9042. (point))))))
  9043. ;;;; Timestamps
  9044. (defvar org-last-changed-timestamp nil)
  9045. (defvar org-time-was-given) ; dynamically scoped parameter
  9046. (defvar org-end-time-was-given) ; dynamically scoped parameter
  9047. (defvar org-ts-what) ; dynamically scoped parameter
  9048. (defun org-time-stamp (arg)
  9049. "Prompt for a date/time and insert a time stamp.
  9050. If the user specifies a time like HH:MM, or if this command is called
  9051. with a prefix argument, the time stamp will contain date and time.
  9052. Otherwise, only the date will be included. All parts of a date not
  9053. specified by the user will be filled in from the current date/time.
  9054. So if you press just return without typing anything, the time stamp
  9055. will represent the current date/time. If there is already a timestamp
  9056. at the cursor, it will be modified."
  9057. (interactive "P")
  9058. (let* ((ts nil)
  9059. (default-time
  9060. ;; Default time is either today, or, when entering a range,
  9061. ;; the range start.
  9062. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  9063. (save-excursion
  9064. (re-search-backward
  9065. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  9066. (- (point) 20) t)))
  9067. (apply 'encode-time (org-parse-time-string (match-string 1)))
  9068. (current-time)))
  9069. (default-input (and ts (org-get-compact-tod ts)))
  9070. org-time-was-given org-end-time-was-given time)
  9071. (cond
  9072. ((and (org-at-timestamp-p)
  9073. (eq last-command 'org-time-stamp)
  9074. (eq this-command 'org-time-stamp))
  9075. (insert "--")
  9076. (setq time (let ((this-command this-command))
  9077. (org-read-date arg 'totime nil nil default-time default-input)))
  9078. (org-insert-time-stamp time (or org-time-was-given arg)))
  9079. ((org-at-timestamp-p)
  9080. (setq time (let ((this-command this-command))
  9081. (org-read-date arg 'totime nil nil default-time default-input)))
  9082. (when (org-at-timestamp-p) ; just to get the match data
  9083. (replace-match "")
  9084. (setq org-last-changed-timestamp
  9085. (org-insert-time-stamp
  9086. time (or org-time-was-given arg)
  9087. nil nil nil (list org-end-time-was-given))))
  9088. (message "Timestamp updated"))
  9089. (t
  9090. (setq time (let ((this-command this-command))
  9091. (org-read-date arg 'totime nil nil default-time default-input)))
  9092. (org-insert-time-stamp time (or org-time-was-given arg)
  9093. nil nil nil (list org-end-time-was-given))))))
  9094. ;; FIXME: can we use this for something else, like computing time differences?
  9095. (defun org-get-compact-tod (s)
  9096. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  9097. (let* ((t1 (match-string 1 s))
  9098. (h1 (string-to-number (match-string 2 s)))
  9099. (m1 (string-to-number (match-string 3 s)))
  9100. (t2 (and (match-end 4) (match-string 5 s)))
  9101. (h2 (and t2 (string-to-number (match-string 6 s))))
  9102. (m2 (and t2 (string-to-number (match-string 7 s))))
  9103. dh dm)
  9104. (if (not t2)
  9105. t1
  9106. (setq dh (- h2 h1) dm (- m2 m1))
  9107. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  9108. (concat t1 "+" (number-to-string dh)
  9109. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  9110. (defun org-time-stamp-inactive (&optional arg)
  9111. "Insert an inactive time stamp.
  9112. An inactive time stamp is enclosed in square brackets instead of angle
  9113. brackets. It is inactive in the sense that it does not trigger agenda entries,
  9114. does not link to the calendar and cannot be changed with the S-cursor keys.
  9115. So these are more for recording a certain time/date."
  9116. (interactive "P")
  9117. (let (org-time-was-given org-end-time-was-given time)
  9118. (setq time (org-read-date arg 'totime))
  9119. (org-insert-time-stamp time (or org-time-was-given arg) 'inactive
  9120. nil nil (list org-end-time-was-given))))
  9121. (defvar org-date-ovl (org-make-overlay 1 1))
  9122. (org-overlay-put org-date-ovl 'face 'org-warning)
  9123. (org-detach-overlay org-date-ovl)
  9124. (defvar org-ans1) ; dynamically scoped parameter
  9125. (defvar org-ans2) ; dynamically scoped parameter
  9126. (defvar org-plain-time-of-day-regexp) ; defined below
  9127. (defvar org-read-date-overlay nil)
  9128. (defvar org-dcst nil) ; dynamically scoped
  9129. (defun org-read-date (&optional with-time to-time from-string prompt
  9130. default-time default-input)
  9131. "Read a date, possibly a time, and make things smooth for the user.
  9132. The prompt will suggest to enter an ISO date, but you can also enter anything
  9133. which will at least partially be understood by `parse-time-string'.
  9134. Unrecognized parts of the date will default to the current day, month, year,
  9135. hour and minute. If this command is called to replace a timestamp at point,
  9136. of to enter the second timestamp of a range, the default time is taken from the
  9137. existing stamp. For example,
  9138. 3-2-5 --> 2003-02-05
  9139. feb 15 --> currentyear-02-15
  9140. sep 12 9 --> 2009-09-12
  9141. 12:45 --> today 12:45
  9142. 22 sept 0:34 --> currentyear-09-22 0:34
  9143. 12 --> currentyear-currentmonth-12
  9144. Fri --> nearest Friday (today or later)
  9145. etc.
  9146. Furthermore you can specify a relative date by giving, as the *first* thing
  9147. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  9148. change in days weeks, months, years.
  9149. With a single plus or minus, the date is relative to today. With a double
  9150. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  9151. +4d --> four days from today
  9152. +4 --> same as above
  9153. +2w --> two weeks from today
  9154. ++5 --> five days from default date
  9155. The function understands only English month and weekday abbreviations,
  9156. but this can be configured with the variables `parse-time-months' and
  9157. `parse-time-weekdays'.
  9158. While prompting, a calendar is popped up - you can also select the
  9159. date with the mouse (button 1). The calendar shows a period of three
  9160. months. To scroll it to other months, use the keys `>' and `<'.
  9161. If you don't like the calendar, turn it off with
  9162. \(setq org-read-date-popup-calendar nil)
  9163. With optional argument TO-TIME, the date will immediately be converted
  9164. to an internal time.
  9165. With an optional argument WITH-TIME, the prompt will suggest to also
  9166. insert a time. Note that when WITH-TIME is not set, you can still
  9167. enter a time, and this function will inform the calling routine about
  9168. this change. The calling routine may then choose to change the format
  9169. used to insert the time stamp into the buffer to include the time.
  9170. With optional argument FROM-STRING, read from this string instead from
  9171. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  9172. the time/date that is used for everything that is not specified by the
  9173. user."
  9174. (require 'parse-time)
  9175. (let* ((org-time-stamp-rounding-minutes
  9176. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  9177. (org-dcst org-display-custom-times)
  9178. (ct (org-current-time))
  9179. (def (or default-time ct))
  9180. (defdecode (decode-time def))
  9181. (dummy (progn
  9182. (when (< (nth 2 defdecode) org-extend-today-until)
  9183. (setcar (nthcdr 2 defdecode) -1)
  9184. (setcar (nthcdr 1 defdecode) 59)
  9185. (setq def (apply 'encode-time defdecode)
  9186. defdecode (decode-time def)))))
  9187. (calendar-move-hook nil)
  9188. (calendar-view-diary-initially-flag nil)
  9189. (view-diary-entries-initially nil)
  9190. (calendar-view-holidays-initially-flag nil)
  9191. (view-calendar-holidays-initially nil)
  9192. (timestr (format-time-string
  9193. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  9194. (prompt (concat (if prompt (concat prompt " ") "")
  9195. (format "Date+time [%s]: " timestr)))
  9196. ans (org-ans0 "") org-ans1 org-ans2 final)
  9197. (cond
  9198. (from-string (setq ans from-string))
  9199. (org-read-date-popup-calendar
  9200. (save-excursion
  9201. (save-window-excursion
  9202. (calendar)
  9203. (calendar-forward-day (- (time-to-days def)
  9204. (calendar-absolute-from-gregorian
  9205. (calendar-current-date))))
  9206. (org-eval-in-calendar nil t)
  9207. (let* ((old-map (current-local-map))
  9208. (map (copy-keymap calendar-mode-map))
  9209. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  9210. (org-defkey map (kbd "RET") 'org-calendar-select)
  9211. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  9212. 'org-calendar-select-mouse)
  9213. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  9214. 'org-calendar-select-mouse)
  9215. (org-defkey minibuffer-local-map [(meta shift left)]
  9216. (lambda () (interactive)
  9217. (org-eval-in-calendar '(calendar-backward-month 1))))
  9218. (org-defkey minibuffer-local-map [(meta shift right)]
  9219. (lambda () (interactive)
  9220. (org-eval-in-calendar '(calendar-forward-month 1))))
  9221. (org-defkey minibuffer-local-map [(meta shift up)]
  9222. (lambda () (interactive)
  9223. (org-eval-in-calendar '(calendar-backward-year 1))))
  9224. (org-defkey minibuffer-local-map [(meta shift down)]
  9225. (lambda () (interactive)
  9226. (org-eval-in-calendar '(calendar-forward-year 1))))
  9227. (org-defkey minibuffer-local-map [(shift up)]
  9228. (lambda () (interactive)
  9229. (org-eval-in-calendar '(calendar-backward-week 1))))
  9230. (org-defkey minibuffer-local-map [(shift down)]
  9231. (lambda () (interactive)
  9232. (org-eval-in-calendar '(calendar-forward-week 1))))
  9233. (org-defkey minibuffer-local-map [(shift left)]
  9234. (lambda () (interactive)
  9235. (org-eval-in-calendar '(calendar-backward-day 1))))
  9236. (org-defkey minibuffer-local-map [(shift right)]
  9237. (lambda () (interactive)
  9238. (org-eval-in-calendar '(calendar-forward-day 1))))
  9239. (org-defkey minibuffer-local-map ">"
  9240. (lambda () (interactive)
  9241. (org-eval-in-calendar '(scroll-calendar-left 1))))
  9242. (org-defkey minibuffer-local-map "<"
  9243. (lambda () (interactive)
  9244. (org-eval-in-calendar '(scroll-calendar-right 1))))
  9245. (unwind-protect
  9246. (progn
  9247. (use-local-map map)
  9248. (add-hook 'post-command-hook 'org-read-date-display)
  9249. (setq org-ans0 (read-string prompt default-input nil nil))
  9250. ;; org-ans0: from prompt
  9251. ;; org-ans1: from mouse click
  9252. ;; org-ans2: from calendar motion
  9253. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  9254. (remove-hook 'post-command-hook 'org-read-date-display)
  9255. (use-local-map old-map)
  9256. (when org-read-date-overlay
  9257. (org-delete-overlay org-read-date-overlay)
  9258. (setq org-read-date-overlay nil)))))))
  9259. (t ; Naked prompt only
  9260. (unwind-protect
  9261. (setq ans (read-string prompt default-input nil timestr))
  9262. (when org-read-date-overlay
  9263. (org-delete-overlay org-read-date-overlay)
  9264. (setq org-read-date-overlay nil)))))
  9265. (setq final (org-read-date-analyze ans def defdecode))
  9266. (if to-time
  9267. (apply 'encode-time final)
  9268. (if (and (boundp 'org-time-was-given) org-time-was-given)
  9269. (format "%04d-%02d-%02d %02d:%02d"
  9270. (nth 5 final) (nth 4 final) (nth 3 final)
  9271. (nth 2 final) (nth 1 final))
  9272. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  9273. (defvar def)
  9274. (defvar defdecode)
  9275. (defvar with-time)
  9276. (defun org-read-date-display ()
  9277. "Display the currrent date prompt interpretation in the minibuffer."
  9278. (when org-read-date-display-live
  9279. (when org-read-date-overlay
  9280. (org-delete-overlay org-read-date-overlay))
  9281. (let ((p (point)))
  9282. (end-of-line 1)
  9283. (while (not (equal (buffer-substring
  9284. (max (point-min) (- (point) 4)) (point))
  9285. " "))
  9286. (insert " "))
  9287. (goto-char p))
  9288. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  9289. " " (or org-ans1 org-ans2)))
  9290. (org-end-time-was-given nil)
  9291. (f (org-read-date-analyze ans def defdecode))
  9292. (fmts (if org-dcst
  9293. org-time-stamp-custom-formats
  9294. org-time-stamp-formats))
  9295. (fmt (if (or with-time
  9296. (and (boundp 'org-time-was-given) org-time-was-given))
  9297. (cdr fmts)
  9298. (car fmts)))
  9299. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  9300. (when (and org-end-time-was-given
  9301. (string-match org-plain-time-of-day-regexp txt))
  9302. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  9303. org-end-time-was-given
  9304. (substring txt (match-end 0)))))
  9305. (setq org-read-date-overlay
  9306. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  9307. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  9308. (defun org-read-date-analyze (ans def defdecode)
  9309. "Analyze the combined answer of the date prompt."
  9310. ;; FIXME: cleanup and comment
  9311. (let (delta deltan deltaw deltadef year month day
  9312. hour minute second wday pm h2 m2 tl wday1
  9313. iso-year iso-weekday iso-week iso-year iso-date)
  9314. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  9315. (setq ans (replace-match "" t t ans)
  9316. deltan (car delta)
  9317. deltaw (nth 1 delta)
  9318. deltadef (nth 2 delta)))
  9319. ;; Check if there is an iso week date in there
  9320. ;; If yes, sore the info and ostpone interpreting it until the rest
  9321. ;; of the parsing is done
  9322. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  9323. (setq iso-year (if (match-end 1) (org-small-year-to-year (string-to-number (match-string 1 ans))))
  9324. iso-weekday (if (match-end 3) (string-to-number (match-string 3 ans)))
  9325. iso-week (string-to-number (match-string 2 ans)))
  9326. (setq ans (replace-match "" t t ans)))
  9327. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  9328. (when (string-match
  9329. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  9330. (setq year (if (match-end 2)
  9331. (string-to-number (match-string 2 ans))
  9332. (string-to-number (format-time-string "%Y")))
  9333. month (string-to-number (match-string 3 ans))
  9334. day (string-to-number (match-string 4 ans)))
  9335. (if (< year 100) (setq year (+ 2000 year)))
  9336. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  9337. t nil ans)))
  9338. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  9339. ;; If there is a time with am/pm, and *no* time without it, we convert
  9340. ;; so that matching will be successful.
  9341. (loop for i from 1 to 2 do ; twice, for end time as well
  9342. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  9343. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  9344. (setq hour (string-to-number (match-string 1 ans))
  9345. minute (if (match-end 3)
  9346. (string-to-number (match-string 3 ans))
  9347. 0)
  9348. pm (equal ?p
  9349. (string-to-char (downcase (match-string 4 ans)))))
  9350. (if (and (= hour 12) (not pm))
  9351. (setq hour 0)
  9352. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  9353. (setq ans (replace-match (format "%02d:%02d" hour minute)
  9354. t t ans))))
  9355. ;; Check if a time range is given as a duration
  9356. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  9357. (setq hour (string-to-number (match-string 1 ans))
  9358. h2 (+ hour (string-to-number (match-string 3 ans)))
  9359. minute (string-to-number (match-string 2 ans))
  9360. m2 (+ minute (if (match-end 5) (string-to-number
  9361. (match-string 5 ans))0)))
  9362. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  9363. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  9364. t t ans)))
  9365. ;; Check if there is a time range
  9366. (when (boundp 'org-end-time-was-given)
  9367. (setq org-time-was-given nil)
  9368. (when (and (string-match org-plain-time-of-day-regexp ans)
  9369. (match-end 8))
  9370. (setq org-end-time-was-given (match-string 8 ans))
  9371. (setq ans (concat (substring ans 0 (match-beginning 7))
  9372. (substring ans (match-end 7))))))
  9373. (setq tl (parse-time-string ans)
  9374. day (or (nth 3 tl) (nth 3 defdecode))
  9375. month (or (nth 4 tl)
  9376. (if (and org-read-date-prefer-future
  9377. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  9378. (1+ (nth 4 defdecode))
  9379. (nth 4 defdecode)))
  9380. year (or (nth 5 tl)
  9381. (if (and org-read-date-prefer-future
  9382. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  9383. (1+ (nth 5 defdecode))
  9384. (nth 5 defdecode)))
  9385. hour (or (nth 2 tl) (nth 2 defdecode))
  9386. minute (or (nth 1 tl) (nth 1 defdecode))
  9387. second (or (nth 0 tl) 0)
  9388. wday (nth 6 tl))
  9389. ;; Special date definitions below
  9390. (cond
  9391. (iso-week
  9392. ;; There was an iso week
  9393. (setq year (or iso-year year)
  9394. day (or iso-weekday wday 1)
  9395. wday nil ; to make sure that the trigger below does not match
  9396. iso-date (calendar-gregorian-from-absolute
  9397. (calendar-absolute-from-iso
  9398. (list iso-week day year))))
  9399. ; FIXME: Should we also push ISO weeks into the future?
  9400. ; (when (and org-read-date-prefer-future
  9401. ; (not iso-year)
  9402. ; (< (calendar-absolute-from-gregorian iso-date)
  9403. ; (time-to-days (current-time))))
  9404. ; (setq year (1+ year)
  9405. ; iso-date (calendar-gregorian-from-absolute
  9406. ; (calendar-absolute-from-iso
  9407. ; (list iso-week day year)))))
  9408. (setq month (car iso-date)
  9409. year (nth 2 iso-date)
  9410. day (nth 1 iso-date)))
  9411. (deltan
  9412. (unless deltadef
  9413. (let ((now (decode-time (current-time))))
  9414. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  9415. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  9416. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  9417. ((equal deltaw "m") (setq month (+ month deltan)))
  9418. ((equal deltaw "y") (setq year (+ year deltan)))))
  9419. ((and wday (not (nth 3 tl)))
  9420. ;; Weekday was given, but no day, so pick that day in the week
  9421. ;; on or after the derived date.
  9422. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  9423. (unless (equal wday wday1)
  9424. (setq day (+ day (% (- wday wday1 -7) 7))))))
  9425. (if (and (boundp 'org-time-was-given)
  9426. (nth 2 tl))
  9427. (setq org-time-was-given t))
  9428. (if (< year 100) (setq year (+ 2000 year)))
  9429. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  9430. (list second minute hour day month year)))
  9431. (defvar parse-time-weekdays)
  9432. (defun org-read-date-get-relative (s today default)
  9433. "Check string S for special relative date string.
  9434. TODAY and DEFAULT are internal times, for today and for a default.
  9435. Return shift list (N what def-flag)
  9436. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  9437. N is the number of WHATs to shift.
  9438. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  9439. the DEFAULT date rather than TODAY."
  9440. (when (string-match
  9441. (concat
  9442. "\\`[ \t]*\\([-+]\\{1,2\\}\\)"
  9443. "\\([0-9]+\\)?"
  9444. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  9445. "\\([ \t]\\|$\\)") s)
  9446. (let* ((dir (if (match-end 1)
  9447. (string-to-char (substring (match-string 1 s) -1))
  9448. ?+))
  9449. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  9450. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  9451. (what (if (match-end 3) (match-string 3 s) "d"))
  9452. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  9453. (date (if rel default today))
  9454. (wday (nth 6 (decode-time date)))
  9455. delta)
  9456. (if wday1
  9457. (progn
  9458. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  9459. (if (= dir ?-) (setq delta (- delta 7)))
  9460. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  9461. (list delta "d" rel))
  9462. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  9463. (defun org-eval-in-calendar (form &optional keepdate)
  9464. "Eval FORM in the calendar window and return to current window.
  9465. Also, store the cursor date in variable org-ans2."
  9466. (let ((sw (selected-window)))
  9467. (select-window (get-buffer-window "*Calendar*"))
  9468. (eval form)
  9469. (when (and (not keepdate) (calendar-cursor-to-date))
  9470. (let* ((date (calendar-cursor-to-date))
  9471. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  9472. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  9473. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  9474. (select-window sw)))
  9475. ; ;; Update the prompt to show new default date
  9476. ; (save-excursion
  9477. ; (goto-char (point-min))
  9478. ; (when (and org-ans2
  9479. ; (re-search-forward "\\[[-0-9]+\\]" nil t)
  9480. ; (get-text-property (match-end 0) 'field))
  9481. ; (let ((inhibit-read-only t))
  9482. ; (replace-match (concat "[" org-ans2 "]") t t)
  9483. ; (add-text-properties (point-min) (1+ (match-end 0))
  9484. ; (text-properties-at (1+ (point-min)))))))))
  9485. (defun org-calendar-select ()
  9486. "Return to `org-read-date' with the date currently selected.
  9487. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  9488. (interactive)
  9489. (when (calendar-cursor-to-date)
  9490. (let* ((date (calendar-cursor-to-date))
  9491. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  9492. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  9493. (if (active-minibuffer-window) (exit-minibuffer))))
  9494. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  9495. "Insert a date stamp for the date given by the internal TIME.
  9496. WITH-HM means, use the stamp format that includes the time of the day.
  9497. INACTIVE means use square brackets instead of angular ones, so that the
  9498. stamp will not contribute to the agenda.
  9499. PRE and POST are optional strings to be inserted before and after the
  9500. stamp.
  9501. The command returns the inserted time stamp."
  9502. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  9503. stamp)
  9504. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  9505. (insert-before-markers (or pre ""))
  9506. (insert-before-markers (setq stamp (format-time-string fmt time)))
  9507. (when (listp extra)
  9508. (setq extra (car extra))
  9509. (if (and (stringp extra)
  9510. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  9511. (setq extra (format "-%02d:%02d"
  9512. (string-to-number (match-string 1 extra))
  9513. (string-to-number (match-string 2 extra))))
  9514. (setq extra nil)))
  9515. (when extra
  9516. (backward-char 1)
  9517. (insert-before-markers extra)
  9518. (forward-char 1))
  9519. (insert-before-markers (or post ""))
  9520. stamp))
  9521. (defun org-toggle-time-stamp-overlays ()
  9522. "Toggle the use of custom time stamp formats."
  9523. (interactive)
  9524. (setq org-display-custom-times (not org-display-custom-times))
  9525. (unless org-display-custom-times
  9526. (let ((p (point-min)) (bmp (buffer-modified-p)))
  9527. (while (setq p (next-single-property-change p 'display))
  9528. (if (and (get-text-property p 'display)
  9529. (eq (get-text-property p 'face) 'org-date))
  9530. (remove-text-properties
  9531. p (setq p (next-single-property-change p 'display))
  9532. '(display t))))
  9533. (set-buffer-modified-p bmp)))
  9534. (if (featurep 'xemacs)
  9535. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  9536. (org-restart-font-lock)
  9537. (setq org-table-may-need-update t)
  9538. (if org-display-custom-times
  9539. (message "Time stamps are overlayed with custom format")
  9540. (message "Time stamp overlays removed")))
  9541. (defun org-display-custom-time (beg end)
  9542. "Overlay modified time stamp format over timestamp between BED and END."
  9543. (let* ((ts (buffer-substring beg end))
  9544. t1 w1 with-hm tf time str w2 (off 0))
  9545. (save-match-data
  9546. (setq t1 (org-parse-time-string ts t))
  9547. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\)?\\'" ts)
  9548. (setq off (- (match-end 0) (match-beginning 0)))))
  9549. (setq end (- end off))
  9550. (setq w1 (- end beg)
  9551. with-hm (and (nth 1 t1) (nth 2 t1))
  9552. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  9553. time (org-fix-decoded-time t1)
  9554. str (org-add-props
  9555. (format-time-string
  9556. (substring tf 1 -1) (apply 'encode-time time))
  9557. nil 'mouse-face 'highlight)
  9558. w2 (length str))
  9559. (if (not (= w2 w1))
  9560. (add-text-properties (1+ beg) (+ 2 beg)
  9561. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  9562. (if (featurep 'xemacs)
  9563. (progn
  9564. (put-text-property beg end 'invisible t)
  9565. (put-text-property beg end 'end-glyph (make-glyph str)))
  9566. (put-text-property beg end 'display str))))
  9567. (defun org-translate-time (string)
  9568. "Translate all timestamps in STRING to custom format.
  9569. But do this only if the variable `org-display-custom-times' is set."
  9570. (when org-display-custom-times
  9571. (save-match-data
  9572. (let* ((start 0)
  9573. (re org-ts-regexp-both)
  9574. t1 with-hm inactive tf time str beg end)
  9575. (while (setq start (string-match re string start))
  9576. (setq beg (match-beginning 0)
  9577. end (match-end 0)
  9578. t1 (save-match-data
  9579. (org-parse-time-string (substring string beg end) t))
  9580. with-hm (and (nth 1 t1) (nth 2 t1))
  9581. inactive (equal (substring string beg (1+ beg)) "[")
  9582. tf (funcall (if with-hm 'cdr 'car)
  9583. org-time-stamp-custom-formats)
  9584. time (org-fix-decoded-time t1)
  9585. str (format-time-string
  9586. (concat
  9587. (if inactive "[" "<") (substring tf 1 -1)
  9588. (if inactive "]" ">"))
  9589. (apply 'encode-time time))
  9590. string (replace-match str t t string)
  9591. start (+ start (length str)))))))
  9592. string)
  9593. (defun org-fix-decoded-time (time)
  9594. "Set 0 instead of nil for the first 6 elements of time.
  9595. Don't touch the rest."
  9596. (let ((n 0))
  9597. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  9598. (defun org-days-to-time (timestamp-string)
  9599. "Difference between TIMESTAMP-STRING and now in days."
  9600. (- (time-to-days (org-time-string-to-time timestamp-string))
  9601. (time-to-days (current-time))))
  9602. (defun org-deadline-close (timestamp-string &optional ndays)
  9603. "Is the time in TIMESTAMP-STRING close to the current date?"
  9604. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  9605. (and (< (org-days-to-time timestamp-string) ndays)
  9606. (not (org-entry-is-done-p))))
  9607. (defun org-get-wdays (ts)
  9608. "Get the deadline lead time appropriate for timestring TS."
  9609. (cond
  9610. ((<= org-deadline-warning-days 0)
  9611. ;; 0 or negative, enforce this value no matter what
  9612. (- org-deadline-warning-days))
  9613. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\)" ts)
  9614. ;; lead time is specified.
  9615. (floor (* (string-to-number (match-string 1 ts))
  9616. (cdr (assoc (match-string 2 ts)
  9617. '(("d" . 1) ("w" . 7)
  9618. ("m" . 30.4) ("y" . 365.25)))))))
  9619. ;; go for the default.
  9620. (t org-deadline-warning-days)))
  9621. (defun org-calendar-select-mouse (ev)
  9622. "Return to `org-read-date' with the date currently selected.
  9623. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  9624. (interactive "e")
  9625. (mouse-set-point ev)
  9626. (when (calendar-cursor-to-date)
  9627. (let* ((date (calendar-cursor-to-date))
  9628. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  9629. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  9630. (if (active-minibuffer-window) (exit-minibuffer))))
  9631. (defun org-check-deadlines (ndays)
  9632. "Check if there are any deadlines due or past due.
  9633. A deadline is considered due if it happens within `org-deadline-warning-days'
  9634. days from today's date. If the deadline appears in an entry marked DONE,
  9635. it is not shown. The prefix arg NDAYS can be used to test that many
  9636. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  9637. (interactive "P")
  9638. (let* ((org-warn-days
  9639. (cond
  9640. ((equal ndays '(4)) 100000)
  9641. (ndays (prefix-numeric-value ndays))
  9642. (t (abs org-deadline-warning-days))))
  9643. (case-fold-search nil)
  9644. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  9645. (callback
  9646. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  9647. (message "%d deadlines past-due or due within %d days"
  9648. (org-occur regexp nil callback)
  9649. org-warn-days)))
  9650. (defun org-check-before-date (date)
  9651. "Check if there are deadlines or scheduled entries before DATE."
  9652. (interactive (list (org-read-date)))
  9653. (let ((case-fold-search nil)
  9654. (regexp (concat "\\<\\(" org-deadline-string
  9655. "\\|" org-scheduled-string
  9656. "\\) *<\\([^>]+\\)>"))
  9657. (callback
  9658. (lambda () (time-less-p
  9659. (org-time-string-to-time (match-string 2))
  9660. (org-time-string-to-time date)))))
  9661. (message "%d entries before %s"
  9662. (org-occur regexp nil callback) date)))
  9663. (defun org-evaluate-time-range (&optional to-buffer)
  9664. "Evaluate a time range by computing the difference between start and end.
  9665. Normally the result is just printed in the echo area, but with prefix arg
  9666. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  9667. If the time range is actually in a table, the result is inserted into the
  9668. next column.
  9669. For time difference computation, a year is assumed to be exactly 365
  9670. days in order to avoid rounding problems."
  9671. (interactive "P")
  9672. (or
  9673. (org-clock-update-time-maybe)
  9674. (save-excursion
  9675. (unless (org-at-date-range-p t)
  9676. (goto-char (point-at-bol))
  9677. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  9678. (if (not (org-at-date-range-p t))
  9679. (error "Not at a time-stamp range, and none found in current line")))
  9680. (let* ((ts1 (match-string 1))
  9681. (ts2 (match-string 2))
  9682. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  9683. (match-end (match-end 0))
  9684. (time1 (org-time-string-to-time ts1))
  9685. (time2 (org-time-string-to-time ts2))
  9686. (t1 (time-to-seconds time1))
  9687. (t2 (time-to-seconds time2))
  9688. (diff (abs (- t2 t1)))
  9689. (negative (< (- t2 t1) 0))
  9690. ;; (ys (floor (* 365 24 60 60)))
  9691. (ds (* 24 60 60))
  9692. (hs (* 60 60))
  9693. (fy "%dy %dd %02d:%02d")
  9694. (fy1 "%dy %dd")
  9695. (fd "%dd %02d:%02d")
  9696. (fd1 "%dd")
  9697. (fh "%02d:%02d")
  9698. y d h m align)
  9699. (if havetime
  9700. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  9701. y 0
  9702. d (floor (/ diff ds)) diff (mod diff ds)
  9703. h (floor (/ diff hs)) diff (mod diff hs)
  9704. m (floor (/ diff 60)))
  9705. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  9706. y 0
  9707. d (floor (+ (/ diff ds) 0.5))
  9708. h 0 m 0))
  9709. (if (not to-buffer)
  9710. (message "%s" (org-make-tdiff-string y d h m))
  9711. (if (org-at-table-p)
  9712. (progn
  9713. (goto-char match-end)
  9714. (setq align t)
  9715. (and (looking-at " *|") (goto-char (match-end 0))))
  9716. (goto-char match-end))
  9717. (if (looking-at
  9718. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  9719. (replace-match ""))
  9720. (if negative (insert " -"))
  9721. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  9722. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  9723. (insert " " (format fh h m))))
  9724. (if align (org-table-align))
  9725. (message "Time difference inserted")))))
  9726. (defun org-make-tdiff-string (y d h m)
  9727. (let ((fmt "")
  9728. (l nil))
  9729. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  9730. l (push y l)))
  9731. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  9732. l (push d l)))
  9733. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  9734. l (push h l)))
  9735. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  9736. l (push m l)))
  9737. (apply 'format fmt (nreverse l))))
  9738. (defun org-time-string-to-time (s)
  9739. (apply 'encode-time (org-parse-time-string s)))
  9740. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  9741. "Convert a time stamp to an absolute day number.
  9742. If there is a specifyer for a cyclic time stamp, get the closest date to
  9743. DAYNR.
  9744. PREFER and SHOW_ALL are passed through to `org-closest-date'."
  9745. (cond
  9746. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  9747. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  9748. daynr
  9749. (+ daynr 1000)))
  9750. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  9751. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  9752. (time-to-days (current-time))) (match-string 0 s)
  9753. prefer show-all))
  9754. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  9755. (defun org-days-to-iso-week (days)
  9756. "Return the iso week number."
  9757. (require 'cal-iso)
  9758. (car (calendar-iso-from-absolute days)))
  9759. (defun org-small-year-to-year (year)
  9760. "Convert 2-digit years into 4-digit years.
  9761. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  9762. The year 2000 cannot be abbreviated. Any year lager than 99
  9763. is retrned unchanged."
  9764. (if (< year 38)
  9765. (setq year (+ 2000 year))
  9766. (if (< year 100)
  9767. (setq year (+ 1900 year))))
  9768. year)
  9769. (defun org-time-from-absolute (d)
  9770. "Return the time corresponding to date D.
  9771. D may be an absolute day number, or a calendar-type list (month day year)."
  9772. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  9773. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  9774. (defun org-calendar-holiday ()
  9775. "List of holidays, for Diary display in Org-mode."
  9776. (require 'holidays)
  9777. (let ((hl (funcall
  9778. (if (fboundp 'calendar-check-holidays)
  9779. 'calendar-check-holidays 'check-calendar-holidays) date)))
  9780. (if hl (mapconcat 'identity hl "; "))))
  9781. (defun org-diary-sexp-entry (sexp entry date)
  9782. "Process a SEXP diary ENTRY for DATE."
  9783. (require 'diary-lib)
  9784. (let ((result (if calendar-debug-sexp
  9785. (let ((stack-trace-on-error t))
  9786. (eval (car (read-from-string sexp))))
  9787. (condition-case nil
  9788. (eval (car (read-from-string sexp)))
  9789. (error
  9790. (beep)
  9791. (message "Bad sexp at line %d in %s: %s"
  9792. (org-current-line)
  9793. (buffer-file-name) sexp)
  9794. (sleep-for 2))))))
  9795. (cond ((stringp result) result)
  9796. ((and (consp result)
  9797. (stringp (cdr result))) (cdr result))
  9798. (result entry)
  9799. (t nil))))
  9800. (defun org-diary-to-ical-string (frombuf)
  9801. "Get iCalendar entries from diary entries in buffer FROMBUF.
  9802. This uses the icalendar.el library."
  9803. (let* ((tmpdir (if (featurep 'xemacs)
  9804. (temp-directory)
  9805. temporary-file-directory))
  9806. (tmpfile (make-temp-name
  9807. (expand-file-name "orgics" tmpdir)))
  9808. buf rtn b e)
  9809. (save-excursion
  9810. (set-buffer frombuf)
  9811. (icalendar-export-region (point-min) (point-max) tmpfile)
  9812. (setq buf (find-buffer-visiting tmpfile))
  9813. (set-buffer buf)
  9814. (goto-char (point-min))
  9815. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  9816. (setq b (match-beginning 0)))
  9817. (goto-char (point-max))
  9818. (if (re-search-backward "^END:VEVENT" nil t)
  9819. (setq e (match-end 0)))
  9820. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  9821. (kill-buffer buf)
  9822. (kill-buffer frombuf)
  9823. (delete-file tmpfile)
  9824. rtn))
  9825. (defun org-closest-date (start current change prefer show-all)
  9826. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  9827. When PREFER is `past' return a date that is either CURRENT or past.
  9828. When PREFER is `future', return a date that is either CURRENT or future.
  9829. When SHOW-ALL is nil, only return the current occurence of a time stamp."
  9830. ;; Make the proper lists from the dates
  9831. (catch 'exit
  9832. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  9833. dn dw sday cday n1 n2
  9834. d m y y1 y2 date1 date2 nmonths nm ny m2)
  9835. (setq start (org-date-to-gregorian start)
  9836. current (org-date-to-gregorian
  9837. (if show-all
  9838. current
  9839. (time-to-days (current-time))))
  9840. sday (calendar-absolute-from-gregorian start)
  9841. cday (calendar-absolute-from-gregorian current))
  9842. (if (<= cday sday) (throw 'exit sday))
  9843. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  9844. (setq dn (string-to-number (match-string 1 change))
  9845. dw (cdr (assoc (match-string 2 change) a1)))
  9846. (error "Invalid change specifyer: %s" change))
  9847. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  9848. (cond
  9849. ((eq dw 'day)
  9850. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  9851. n2 (+ n1 dn)))
  9852. ((eq dw 'year)
  9853. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  9854. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  9855. (setq date1 (list m d y1)
  9856. n1 (calendar-absolute-from-gregorian date1)
  9857. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  9858. n2 (calendar-absolute-from-gregorian date2)))
  9859. ((eq dw 'month)
  9860. ;; approx number of month between the tow dates
  9861. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  9862. ;; How often does dn fit in there?
  9863. (setq d (nth 1 start) m (car start) y (nth 2 start)
  9864. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  9865. m (+ m nm)
  9866. ny (floor (/ m 12))
  9867. y (+ y ny)
  9868. m (- m (* ny 12)))
  9869. (while (> m 12) (setq m (- m 12) y (1+ y)))
  9870. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  9871. (setq m2 (+ m dn) y2 y)
  9872. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  9873. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  9874. (while (< n2 cday)
  9875. (setq n1 n2 m m2 y y2)
  9876. (setq m2 (+ m dn) y2 y)
  9877. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  9878. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  9879. (if show-all
  9880. (cond
  9881. ((eq prefer 'past) n1)
  9882. ((eq prefer 'future) (if (= cday n1) n1 n2))
  9883. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  9884. (cond
  9885. ((eq prefer 'past) n1)
  9886. ((eq prefer 'future) (if (= cday n1) n1 n2))
  9887. (t (if (= cday n1) n1 n2)))))))
  9888. (defun org-date-to-gregorian (date)
  9889. "Turn any specification of DATE into a gregorian date for the calendar."
  9890. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  9891. ((and (listp date) (= (length date) 3)) date)
  9892. ((stringp date)
  9893. (setq date (org-parse-time-string date))
  9894. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  9895. ((listp date)
  9896. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  9897. (defun org-parse-time-string (s &optional nodefault)
  9898. "Parse the standard Org-mode time string.
  9899. This should be a lot faster than the normal `parse-time-string'.
  9900. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  9901. hour and minute fields will be nil if not given."
  9902. (if (string-match org-ts-regexp0 s)
  9903. (list 0
  9904. (if (or (match-beginning 8) (not nodefault))
  9905. (string-to-number (or (match-string 8 s) "0")))
  9906. (if (or (match-beginning 7) (not nodefault))
  9907. (string-to-number (or (match-string 7 s) "0")))
  9908. (string-to-number (match-string 4 s))
  9909. (string-to-number (match-string 3 s))
  9910. (string-to-number (match-string 2 s))
  9911. nil nil nil)
  9912. (make-list 9 0)))
  9913. (defun org-timestamp-up (&optional arg)
  9914. "Increase the date item at the cursor by one.
  9915. If the cursor is on the year, change the year. If it is on the month or
  9916. the day, change that.
  9917. With prefix ARG, change by that many units."
  9918. (interactive "p")
  9919. (org-timestamp-change (prefix-numeric-value arg)))
  9920. (defun org-timestamp-down (&optional arg)
  9921. "Decrease the date item at the cursor by one.
  9922. If the cursor is on the year, change the year. If it is on the month or
  9923. the day, change that.
  9924. With prefix ARG, change by that many units."
  9925. (interactive "p")
  9926. (org-timestamp-change (- (prefix-numeric-value arg))))
  9927. (defun org-timestamp-up-day (&optional arg)
  9928. "Increase the date in the time stamp by one day.
  9929. With prefix ARG, change that many days."
  9930. (interactive "p")
  9931. (if (and (not (org-at-timestamp-p t))
  9932. (org-on-heading-p))
  9933. (org-todo 'up)
  9934. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  9935. (defun org-timestamp-down-day (&optional arg)
  9936. "Decrease the date in the time stamp by one day.
  9937. With prefix ARG, change that many days."
  9938. (interactive "p")
  9939. (if (and (not (org-at-timestamp-p t))
  9940. (org-on-heading-p))
  9941. (org-todo 'down)
  9942. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  9943. (defun org-at-timestamp-p (&optional inactive-ok)
  9944. "Determine if the cursor is in or at a timestamp."
  9945. (interactive)
  9946. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  9947. (pos (point))
  9948. (ans (or (looking-at tsr)
  9949. (save-excursion
  9950. (skip-chars-backward "^[<\n\r\t")
  9951. (if (> (point) (point-min)) (backward-char 1))
  9952. (and (looking-at tsr)
  9953. (> (- (match-end 0) pos) -1))))))
  9954. (and ans
  9955. (boundp 'org-ts-what)
  9956. (setq org-ts-what
  9957. (cond
  9958. ((= pos (match-beginning 0)) 'bracket)
  9959. ((= pos (1- (match-end 0))) 'bracket)
  9960. ((org-pos-in-match-range pos 2) 'year)
  9961. ((org-pos-in-match-range pos 3) 'month)
  9962. ((org-pos-in-match-range pos 7) 'hour)
  9963. ((org-pos-in-match-range pos 8) 'minute)
  9964. ((or (org-pos-in-match-range pos 4)
  9965. (org-pos-in-match-range pos 5)) 'day)
  9966. ((and (> pos (or (match-end 8) (match-end 5)))
  9967. (< pos (match-end 0)))
  9968. (- pos (or (match-end 8) (match-end 5))))
  9969. (t 'day))))
  9970. ans))
  9971. (defun org-toggle-timestamp-type ()
  9972. "Toggle the type (<active> or [inactive]) of a time stamp."
  9973. (interactive)
  9974. (when (org-at-timestamp-p t)
  9975. (save-excursion
  9976. (goto-char (match-beginning 0))
  9977. (insert (if (equal (char-after) ?<) "[" "<")) (delete-char 1)
  9978. (goto-char (1- (match-end 0)))
  9979. (insert (if (equal (char-after) ?>) "]" ">")) (delete-char 1))
  9980. (message "Timestamp is now %sactive"
  9981. (if (equal (char-before) ?>) "in" ""))))
  9982. (defun org-timestamp-change (n &optional what)
  9983. "Change the date in the time stamp at point.
  9984. The date will be changed by N times WHAT. WHAT can be `day', `month',
  9985. `year', `minute', `second'. If WHAT is not given, the cursor position
  9986. in the timestamp determines what will be changed."
  9987. (let ((pos (point))
  9988. with-hm inactive
  9989. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  9990. org-ts-what
  9991. extra rem
  9992. ts time time0)
  9993. (if (not (org-at-timestamp-p t))
  9994. (error "Not at a timestamp"))
  9995. (if (and (not what) (eq org-ts-what 'bracket))
  9996. (org-toggle-timestamp-type)
  9997. (if (and (not what) (not (eq org-ts-what 'day))
  9998. org-display-custom-times
  9999. (get-text-property (point) 'display)
  10000. (not (get-text-property (1- (point)) 'display)))
  10001. (setq org-ts-what 'day))
  10002. (setq org-ts-what (or what org-ts-what)
  10003. inactive (= (char-after (match-beginning 0)) ?\[)
  10004. ts (match-string 0))
  10005. (replace-match "")
  10006. (if (string-match
  10007. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\)*\\)[]>]"
  10008. ts)
  10009. (setq extra (match-string 1 ts)))
  10010. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  10011. (setq with-hm t))
  10012. (setq time0 (org-parse-time-string ts))
  10013. (when (and (eq org-ts-what 'minute)
  10014. (eq current-prefix-arg nil))
  10015. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  10016. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  10017. (setcar (cdr time0) (+ (nth 1 time0)
  10018. (if (> n 0) (- rem) (- dm rem))))))
  10019. (setq time
  10020. (encode-time (or (car time0) 0)
  10021. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  10022. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  10023. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  10024. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  10025. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  10026. (nthcdr 6 time0)))
  10027. (when (integerp org-ts-what)
  10028. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  10029. (if (eq what 'calendar)
  10030. (let ((cal-date (org-get-date-from-calendar)))
  10031. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  10032. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  10033. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  10034. (setcar time0 (or (car time0) 0))
  10035. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  10036. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  10037. (setq time (apply 'encode-time time0))))
  10038. (setq org-last-changed-timestamp
  10039. (org-insert-time-stamp time with-hm inactive nil nil extra))
  10040. (org-clock-update-time-maybe)
  10041. (goto-char pos)
  10042. ;; Try to recenter the calendar window, if any
  10043. (if (and org-calendar-follow-timestamp-change
  10044. (get-buffer-window "*Calendar*" t)
  10045. (memq org-ts-what '(day month year)))
  10046. (org-recenter-calendar (time-to-days time))))))
  10047. (defun org-modify-ts-extra (s pos n dm)
  10048. "Change the different parts of the lead-time and repeat fields in timestamp."
  10049. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  10050. ng h m new rem)
  10051. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  10052. (cond
  10053. ((or (org-pos-in-match-range pos 2)
  10054. (org-pos-in-match-range pos 3))
  10055. (setq m (string-to-number (match-string 3 s))
  10056. h (string-to-number (match-string 2 s)))
  10057. (if (org-pos-in-match-range pos 2)
  10058. (setq h (+ h n))
  10059. (setq n (* dm (org-no-warnings (signum n))))
  10060. (when (not (= 0 (setq rem (% m dm))))
  10061. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  10062. (setq m (+ m n)))
  10063. (if (< m 0) (setq m (+ m 60) h (1- h)))
  10064. (if (> m 59) (setq m (- m 60) h (1+ h)))
  10065. (setq h (min 24 (max 0 h)))
  10066. (setq ng 1 new (format "-%02d:%02d" h m)))
  10067. ((org-pos-in-match-range pos 6)
  10068. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  10069. ((org-pos-in-match-range pos 5)
  10070. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  10071. ((org-pos-in-match-range pos 9)
  10072. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  10073. ((org-pos-in-match-range pos 8)
  10074. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  10075. (when ng
  10076. (setq s (concat
  10077. (substring s 0 (match-beginning ng))
  10078. new
  10079. (substring s (match-end ng))))))
  10080. s))
  10081. (defun org-recenter-calendar (date)
  10082. "If the calendar is visible, recenter it to DATE."
  10083. (let* ((win (selected-window))
  10084. (cwin (get-buffer-window "*Calendar*" t))
  10085. (calendar-move-hook nil))
  10086. (when cwin
  10087. (select-window cwin)
  10088. (calendar-goto-date (if (listp date) date
  10089. (calendar-gregorian-from-absolute date)))
  10090. (select-window win))))
  10091. (defun org-goto-calendar (&optional arg)
  10092. "Go to the Emacs calendar at the current date.
  10093. If there is a time stamp in the current line, go to that date.
  10094. A prefix ARG can be used to force the current date."
  10095. (interactive "P")
  10096. (let ((tsr org-ts-regexp) diff
  10097. (calendar-move-hook nil)
  10098. (calendar-view-holidays-initially-flag nil)
  10099. (view-calendar-holidays-initially nil)
  10100. (calendar-view-diary-initially-flag nil)
  10101. (view-diary-entries-initially nil))
  10102. (if (or (org-at-timestamp-p)
  10103. (save-excursion
  10104. (beginning-of-line 1)
  10105. (looking-at (concat ".*" tsr))))
  10106. (let ((d1 (time-to-days (current-time)))
  10107. (d2 (time-to-days
  10108. (org-time-string-to-time (match-string 1)))))
  10109. (setq diff (- d2 d1))))
  10110. (calendar)
  10111. (calendar-goto-today)
  10112. (if (and diff (not arg)) (calendar-forward-day diff))))
  10113. (defun org-get-date-from-calendar ()
  10114. "Return a list (month day year) of date at point in calendar."
  10115. (with-current-buffer "*Calendar*"
  10116. (save-match-data
  10117. (calendar-cursor-to-date))))
  10118. (defun org-date-from-calendar ()
  10119. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  10120. If there is already a time stamp at the cursor position, update it."
  10121. (interactive)
  10122. (if (org-at-timestamp-p t)
  10123. (org-timestamp-change 0 'calendar)
  10124. (let ((cal-date (org-get-date-from-calendar)))
  10125. (org-insert-time-stamp
  10126. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  10127. (defun org-minutes-to-hh:mm-string (m)
  10128. "Compute H:MM from a number of minutes."
  10129. (let ((h (/ m 60)))
  10130. (setq m (- m (* 60 h)))
  10131. (format "%d:%02d" h m)))
  10132. (defun org-hh:mm-string-to-minutes (s)
  10133. "Convert a string H:MM to a number of minutes."
  10134. (if (string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  10135. (+ (* (string-to-number (match-string 1 s)) 60)
  10136. (string-to-number (match-string 2 s)))
  10137. 0))
  10138. ;;;; Agenda files
  10139. ;;;###autoload
  10140. (defun org-iswitchb (&optional arg)
  10141. "Use `iswitchb-read-buffer' to prompt for an Org buffer to switch to.
  10142. With a prefix argument, restrict available to files.
  10143. With two prefix arguments, restrict available buffers to agenda files.
  10144. Due to some yet unresolved reason, global function
  10145. `iswitchb-mode' needs to be active for this function to work."
  10146. (interactive "P")
  10147. (require 'iswitchb)
  10148. (let ((enabled iswitchb-mode) blist)
  10149. (or enabled (iswitchb-mode 1))
  10150. (setq blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  10151. ((equal arg '(16)) (org-buffer-list 'agenda))
  10152. (t (org-buffer-list))))
  10153. (unwind-protect
  10154. (let ((iswitchb-make-buflist-hook
  10155. (lambda ()
  10156. (setq iswitchb-temp-buflist
  10157. (mapcar 'buffer-name blist)))))
  10158. (switch-to-buffer
  10159. (iswitchb-read-buffer
  10160. "Switch-to: " nil t))
  10161. (or enabled (iswitchb-mode -1))))))
  10162. (defun org-buffer-list (&optional predicate tmp)
  10163. "Return a list of Org buffers.
  10164. PREDICATE can be either 'export, 'files or 'agenda.
  10165. 'export restrict the list to Export buffers.
  10166. 'files restrict the list to buffers visiting Org files.
  10167. 'agenda restrict the list to buffers visiting agenda files.
  10168. If TMP is non-nil, don't include temporary buffers."
  10169. (let (filter blist)
  10170. (setq filter
  10171. (cond ((eq predicate 'files) "\.org$")
  10172. ((eq predicate 'export) "\*Org .*Export")
  10173. (t "\*Org \\|\.org$")))
  10174. (setq blist
  10175. (mapcar
  10176. (lambda(b)
  10177. (let ((bname (buffer-name b))
  10178. (bfile (buffer-file-name b)))
  10179. (if (and (string-match filter bname)
  10180. (if (eq predicate 'agenda)
  10181. (member bfile
  10182. (mapcar (lambda(f) (file-truename f))
  10183. org-agenda-files)) t)
  10184. (if tmp (not (string-match "tmp" bname)) t)) b)))
  10185. (buffer-list)))
  10186. (delete nil blist)))
  10187. (defun org-agenda-files (&optional unrestricted ext)
  10188. "Get the list of agenda files.
  10189. Optional UNRESTRICTED means return the full list even if a restriction
  10190. is currently in place.
  10191. When EXT is non-nil, try to add all files that are created by adding EXT
  10192. to the file nemes. Basically, this is a way to add the archive files
  10193. to the list, by setting EXT to \"_archive\" If EXT is non-nil, but not
  10194. a string, \"_archive\" will be used."
  10195. (let ((files
  10196. (cond
  10197. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  10198. ((stringp org-agenda-files) (org-read-agenda-file-list))
  10199. ((listp org-agenda-files) org-agenda-files)
  10200. (t (error "Invalid value of `org-agenda-files'")))))
  10201. (setq files (apply 'append
  10202. (mapcar (lambda (f)
  10203. (if (file-directory-p f)
  10204. (directory-files
  10205. f t org-agenda-file-regexp)
  10206. (list f)))
  10207. files)))
  10208. (when org-agenda-skip-unavailable-files
  10209. (setq files (delq nil
  10210. (mapcar (function
  10211. (lambda (file)
  10212. (and (file-readable-p file) file)))
  10213. files))))
  10214. (when ext
  10215. (setq ext (if (and (stringp ext) (string-match "\\S-" ext))
  10216. ext "_archive"))
  10217. (setq files (apply 'append
  10218. (mapcar
  10219. (lambda (f)
  10220. (if (file-exists-p (concat f ext))
  10221. (list f (concat f ext))
  10222. (list f)))
  10223. files))))
  10224. files))
  10225. (defun org-edit-agenda-file-list ()
  10226. "Edit the list of agenda files.
  10227. Depending on setup, this either uses customize to edit the variable
  10228. `org-agenda-files', or it visits the file that is holding the list. In the
  10229. latter case, the buffer is set up in a way that saving it automatically kills
  10230. the buffer and restores the previous window configuration."
  10231. (interactive)
  10232. (if (stringp org-agenda-files)
  10233. (let ((cw (current-window-configuration)))
  10234. (find-file org-agenda-files)
  10235. (org-set-local 'org-window-configuration cw)
  10236. (org-add-hook 'after-save-hook
  10237. (lambda ()
  10238. (set-window-configuration
  10239. (prog1 org-window-configuration
  10240. (kill-buffer (current-buffer))))
  10241. (org-install-agenda-files-menu)
  10242. (message "New agenda file list installed"))
  10243. nil 'local)
  10244. (message "%s" (substitute-command-keys
  10245. "Edit list and finish with \\[save-buffer]")))
  10246. (customize-variable 'org-agenda-files)))
  10247. (defun org-store-new-agenda-file-list (list)
  10248. "Set new value for the agenda file list and save it correcly."
  10249. (if (stringp org-agenda-files)
  10250. (let ((f org-agenda-files) b)
  10251. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  10252. (with-temp-file f
  10253. (insert (mapconcat 'identity list "\n") "\n")))
  10254. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  10255. (setq org-agenda-files list)
  10256. (customize-save-variable 'org-agenda-files org-agenda-files))))
  10257. (defun org-read-agenda-file-list ()
  10258. "Read the list of agenda files from a file."
  10259. (when (file-directory-p org-agenda-files)
  10260. (error "`org-agenda-files' cannot be a single directory"))
  10261. (when (stringp org-agenda-files)
  10262. (with-temp-buffer
  10263. (insert-file-contents org-agenda-files)
  10264. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  10265. ;;;###autoload
  10266. (defun org-cycle-agenda-files ()
  10267. "Cycle through the files in `org-agenda-files'.
  10268. If the current buffer visits an agenda file, find the next one in the list.
  10269. If the current buffer does not, find the first agenda file."
  10270. (interactive)
  10271. (let* ((fs (org-agenda-files t))
  10272. (files (append fs (list (car fs))))
  10273. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  10274. file)
  10275. (unless files (error "No agenda files"))
  10276. (catch 'exit
  10277. (while (setq file (pop files))
  10278. (if (equal (file-truename file) tcf)
  10279. (when (car files)
  10280. (find-file (car files))
  10281. (throw 'exit t))))
  10282. (find-file (car fs)))
  10283. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  10284. (defun org-agenda-file-to-front (&optional to-end)
  10285. "Move/add the current file to the top of the agenda file list.
  10286. If the file is not present in the list, it is added to the front. If it is
  10287. present, it is moved there. With optional argument TO-END, add/move to the
  10288. end of the list."
  10289. (interactive "P")
  10290. (let ((org-agenda-skip-unavailable-files nil)
  10291. (file-alist (mapcar (lambda (x)
  10292. (cons (file-truename x) x))
  10293. (org-agenda-files t)))
  10294. (ctf (file-truename buffer-file-name))
  10295. x had)
  10296. (setq x (assoc ctf file-alist) had x)
  10297. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  10298. (if to-end
  10299. (setq file-alist (append (delq x file-alist) (list x)))
  10300. (setq file-alist (cons x (delq x file-alist))))
  10301. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  10302. (org-install-agenda-files-menu)
  10303. (message "File %s to %s of agenda file list"
  10304. (if had "moved" "added") (if to-end "end" "front"))))
  10305. (defun org-remove-file (&optional file)
  10306. "Remove current file from the list of files in variable `org-agenda-files'.
  10307. These are the files which are being checked for agenda entries.
  10308. Optional argument FILE means, use this file instead of the current."
  10309. (interactive)
  10310. (let* ((org-agenda-skip-unavailable-files nil)
  10311. (file (or file buffer-file-name))
  10312. (true-file (file-truename file))
  10313. (afile (abbreviate-file-name file))
  10314. (files (delq nil (mapcar
  10315. (lambda (x)
  10316. (if (equal true-file
  10317. (file-truename x))
  10318. nil x))
  10319. (org-agenda-files t)))))
  10320. (if (not (= (length files) (length (org-agenda-files t))))
  10321. (progn
  10322. (org-store-new-agenda-file-list files)
  10323. (org-install-agenda-files-menu)
  10324. (message "Removed file: %s" afile))
  10325. (message "File was not in list: %s (not removed)" afile))))
  10326. (defun org-file-menu-entry (file)
  10327. (vector file (list 'find-file file) t))
  10328. (defun org-check-agenda-file (file)
  10329. "Make sure FILE exists. If not, ask user what to do."
  10330. (when (not (file-exists-p file))
  10331. (message "non-existent file %s. [R]emove from list or [A]bort?"
  10332. (abbreviate-file-name file))
  10333. (let ((r (downcase (read-char-exclusive))))
  10334. (cond
  10335. ((equal r ?r)
  10336. (org-remove-file file)
  10337. (throw 'nextfile t))
  10338. (t (error "Abort"))))))
  10339. (defun org-get-agenda-file-buffer (file)
  10340. "Get a buffer visiting FILE. If the buffer needs to be created, add
  10341. it to the list of buffers which might be released later."
  10342. (let ((buf (org-find-base-buffer-visiting file)))
  10343. (if buf
  10344. buf ; just return it
  10345. ;; Make a new buffer and remember it
  10346. (setq buf (find-file-noselect file))
  10347. (if buf (push buf org-agenda-new-buffers))
  10348. buf)))
  10349. (defun org-release-buffers (blist)
  10350. "Release all buffers in list, asking the user for confirmation when needed.
  10351. When a buffer is unmodified, it is just killed. When modified, it is saved
  10352. \(if the user agrees) and then killed."
  10353. (let (buf file)
  10354. (while (setq buf (pop blist))
  10355. (setq file (buffer-file-name buf))
  10356. (when (and (buffer-modified-p buf)
  10357. file
  10358. (y-or-n-p (format "Save file %s? " file)))
  10359. (with-current-buffer buf (save-buffer)))
  10360. (kill-buffer buf))))
  10361. (defun org-prepare-agenda-buffers (files)
  10362. "Create buffers for all agenda files, protect archived trees and comments."
  10363. (interactive)
  10364. (let ((pa '(:org-archived t))
  10365. (pc '(:org-comment t))
  10366. (pall '(:org-archived t :org-comment t))
  10367. (inhibit-read-only t)
  10368. (rea (concat ":" org-archive-tag ":"))
  10369. bmp file re)
  10370. (save-excursion
  10371. (save-restriction
  10372. (while (setq file (pop files))
  10373. (if (bufferp file)
  10374. (set-buffer file)
  10375. (org-check-agenda-file file)
  10376. (set-buffer (org-get-agenda-file-buffer file)))
  10377. (widen)
  10378. (setq bmp (buffer-modified-p))
  10379. (org-refresh-category-properties)
  10380. (setq org-todo-keywords-for-agenda
  10381. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  10382. (setq org-done-keywords-for-agenda
  10383. (append org-done-keywords-for-agenda org-done-keywords))
  10384. (save-excursion
  10385. (remove-text-properties (point-min) (point-max) pall)
  10386. (when org-agenda-skip-archived-trees
  10387. (goto-char (point-min))
  10388. (while (re-search-forward rea nil t)
  10389. (if (org-on-heading-p t)
  10390. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  10391. (goto-char (point-min))
  10392. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  10393. (while (re-search-forward re nil t)
  10394. (add-text-properties
  10395. (match-beginning 0) (org-end-of-subtree t) pc)))
  10396. (set-buffer-modified-p bmp))))))
  10397. ;;;; Embedded LaTeX
  10398. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  10399. "Keymap for the minor `org-cdlatex-mode'.")
  10400. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  10401. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  10402. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  10403. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  10404. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  10405. (defvar org-cdlatex-texmathp-advice-is-done nil
  10406. "Flag remembering if we have applied the advice to texmathp already.")
  10407. (define-minor-mode org-cdlatex-mode
  10408. "Toggle the minor `org-cdlatex-mode'.
  10409. This mode supports entering LaTeX environment and math in LaTeX fragments
  10410. in Org-mode.
  10411. \\{org-cdlatex-mode-map}"
  10412. nil " OCDL" nil
  10413. (when org-cdlatex-mode (require 'cdlatex))
  10414. (unless org-cdlatex-texmathp-advice-is-done
  10415. (setq org-cdlatex-texmathp-advice-is-done t)
  10416. (defadvice texmathp (around org-math-always-on activate)
  10417. "Always return t in org-mode buffers.
  10418. This is because we want to insert math symbols without dollars even outside
  10419. the LaTeX math segments. If Orgmode thinks that point is actually inside
  10420. en embedded LaTeX fragement, let texmathp do its job.
  10421. \\[org-cdlatex-mode-map]"
  10422. (interactive)
  10423. (let (p)
  10424. (cond
  10425. ((not (org-mode-p)) ad-do-it)
  10426. ((eq this-command 'cdlatex-math-symbol)
  10427. (setq ad-return-value t
  10428. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  10429. (t
  10430. (let ((p (org-inside-LaTeX-fragment-p)))
  10431. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  10432. (setq ad-return-value t
  10433. texmathp-why '("Org-mode embedded math" . 0))
  10434. (if p ad-do-it)))))))))
  10435. (defun turn-on-org-cdlatex ()
  10436. "Unconditionally turn on `org-cdlatex-mode'."
  10437. (org-cdlatex-mode 1))
  10438. (defun org-inside-LaTeX-fragment-p ()
  10439. "Test if point is inside a LaTeX fragment.
  10440. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  10441. sequence appearing also before point.
  10442. Even though the matchers for math are configurable, this function assumes
  10443. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  10444. delimiters are skipped when they have been removed by customization.
  10445. The return value is nil, or a cons cell with the delimiter and
  10446. and the position of this delimiter.
  10447. This function does a reasonably good job, but can locally be fooled by
  10448. for example currency specifications. For example it will assume being in
  10449. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  10450. fragments that are properly closed, but during editing, we have to live
  10451. with the uncertainty caused by missing closing delimiters. This function
  10452. looks only before point, not after."
  10453. (catch 'exit
  10454. (let ((pos (point))
  10455. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  10456. (lim (progn
  10457. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  10458. (point)))
  10459. dd-on str (start 0) m re)
  10460. (goto-char pos)
  10461. (when dodollar
  10462. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  10463. re (nth 1 (assoc "$" org-latex-regexps)))
  10464. (while (string-match re str start)
  10465. (cond
  10466. ((= (match-end 0) (length str))
  10467. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  10468. ((= (match-end 0) (- (length str) 5))
  10469. (throw 'exit nil))
  10470. (t (setq start (match-end 0))))))
  10471. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  10472. (goto-char pos)
  10473. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  10474. (and (match-beginning 2) (throw 'exit nil))
  10475. ;; count $$
  10476. (while (re-search-backward "\\$\\$" lim t)
  10477. (setq dd-on (not dd-on)))
  10478. (goto-char pos)
  10479. (if dd-on (cons "$$" m))))))
  10480. (defun org-try-cdlatex-tab ()
  10481. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  10482. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  10483. - inside a LaTeX fragment, or
  10484. - after the first word in a line, where an abbreviation expansion could
  10485. insert a LaTeX environment."
  10486. (when org-cdlatex-mode
  10487. (cond
  10488. ((save-excursion
  10489. (skip-chars-backward "a-zA-Z0-9*")
  10490. (skip-chars-backward " \t")
  10491. (bolp))
  10492. (cdlatex-tab) t)
  10493. ((org-inside-LaTeX-fragment-p)
  10494. (cdlatex-tab) t)
  10495. (t nil))))
  10496. (defun org-cdlatex-underscore-caret (&optional arg)
  10497. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  10498. Revert to the normal definition outside of these fragments."
  10499. (interactive "P")
  10500. (if (org-inside-LaTeX-fragment-p)
  10501. (call-interactively 'cdlatex-sub-superscript)
  10502. (let (org-cdlatex-mode)
  10503. (call-interactively (key-binding (vector last-input-event))))))
  10504. (defun org-cdlatex-math-modify (&optional arg)
  10505. "Execute `cdlatex-math-modify' in LaTeX fragments.
  10506. Revert to the normal definition outside of these fragments."
  10507. (interactive "P")
  10508. (if (org-inside-LaTeX-fragment-p)
  10509. (call-interactively 'cdlatex-math-modify)
  10510. (let (org-cdlatex-mode)
  10511. (call-interactively (key-binding (vector last-input-event))))))
  10512. (defvar org-latex-fragment-image-overlays nil
  10513. "List of overlays carrying the images of latex fragments.")
  10514. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  10515. (defun org-remove-latex-fragment-image-overlays ()
  10516. "Remove all overlays with LaTeX fragment images in current buffer."
  10517. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  10518. (setq org-latex-fragment-image-overlays nil))
  10519. (defun org-preview-latex-fragment (&optional subtree)
  10520. "Preview the LaTeX fragment at point, or all locally or globally.
  10521. If the cursor is in a LaTeX fragment, create the image and overlay
  10522. it over the source code. If there is no fragment at point, display
  10523. all fragments in the current text, from one headline to the next. With
  10524. prefix SUBTREE, display all fragments in the current subtree. With a
  10525. double prefix `C-u C-u', or when the cursor is before the first headline,
  10526. display all fragments in the buffer.
  10527. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  10528. (interactive "P")
  10529. (org-remove-latex-fragment-image-overlays)
  10530. (save-excursion
  10531. (save-restriction
  10532. (let (beg end at msg)
  10533. (cond
  10534. ((or (equal subtree '(16))
  10535. (not (save-excursion
  10536. (re-search-backward (concat "^" outline-regexp) nil t))))
  10537. (setq beg (point-min) end (point-max)
  10538. msg "Creating images for buffer...%s"))
  10539. ((equal subtree '(4))
  10540. (org-back-to-heading)
  10541. (setq beg (point) end (org-end-of-subtree t)
  10542. msg "Creating images for subtree...%s"))
  10543. (t
  10544. (if (setq at (org-inside-LaTeX-fragment-p))
  10545. (goto-char (max (point-min) (- (cdr at) 2)))
  10546. (org-back-to-heading))
  10547. (setq beg (point) end (progn (outline-next-heading) (point))
  10548. msg (if at "Creating image...%s"
  10549. "Creating images for entry...%s"))))
  10550. (message msg "")
  10551. (narrow-to-region beg end)
  10552. (goto-char beg)
  10553. (org-format-latex
  10554. (concat "ltxpng/" (file-name-sans-extension
  10555. (file-name-nondirectory
  10556. buffer-file-name)))
  10557. default-directory 'overlays msg at 'forbuffer)
  10558. (message msg "done. Use `C-c C-c' to remove images.")))))
  10559. (defvar org-latex-regexps
  10560. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  10561. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  10562. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  10563. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([ .,?;:'\")\000]\\|$\\)" 2 nil)
  10564. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  10565. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  10566. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  10567. "Regular expressions for matching embedded LaTeX.")
  10568. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  10569. "Replace LaTeX fragments with links to an image, and produce images."
  10570. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  10571. (let* ((prefixnodir (file-name-nondirectory prefix))
  10572. (absprefix (expand-file-name prefix dir))
  10573. (todir (file-name-directory absprefix))
  10574. (opt org-format-latex-options)
  10575. (matchers (plist-get opt :matchers))
  10576. (re-list org-latex-regexps)
  10577. (cnt 0) txt link beg end re e checkdir
  10578. m n block linkfile movefile ov)
  10579. ;; Check if there are old images files with this prefix, and remove them
  10580. (when (file-directory-p todir)
  10581. (mapc 'delete-file
  10582. (directory-files
  10583. todir 'full
  10584. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  10585. ;; Check the different regular expressions
  10586. (while (setq e (pop re-list))
  10587. (setq m (car e) re (nth 1 e) n (nth 2 e)
  10588. block (if (nth 3 e) "\n\n" ""))
  10589. (when (member m matchers)
  10590. (goto-char (point-min))
  10591. (while (re-search-forward re nil t)
  10592. (when (or (not at) (equal (cdr at) (match-beginning n)))
  10593. (setq txt (match-string n)
  10594. beg (match-beginning n) end (match-end n)
  10595. cnt (1+ cnt)
  10596. linkfile (format "%s_%04d.png" prefix cnt)
  10597. movefile (format "%s_%04d.png" absprefix cnt)
  10598. link (concat block "[[file:" linkfile "]]" block))
  10599. (if msg (message msg cnt))
  10600. (goto-char beg)
  10601. (unless checkdir ; make sure the directory exists
  10602. (setq checkdir t)
  10603. (or (file-directory-p todir) (make-directory todir)))
  10604. (org-create-formula-image
  10605. txt movefile opt forbuffer)
  10606. (if overlays
  10607. (progn
  10608. (setq ov (org-make-overlay beg end))
  10609. (if (featurep 'xemacs)
  10610. (progn
  10611. (org-overlay-put ov 'invisible t)
  10612. (org-overlay-put
  10613. ov 'end-glyph
  10614. (make-glyph (vector 'png :file movefile))))
  10615. (org-overlay-put
  10616. ov 'display
  10617. (list 'image :type 'png :file movefile :ascent 'center)))
  10618. (push ov org-latex-fragment-image-overlays)
  10619. (goto-char end))
  10620. (delete-region beg end)
  10621. (insert link))))))))
  10622. ;; This function borrows from Ganesh Swami's latex2png.el
  10623. (defun org-create-formula-image (string tofile options buffer)
  10624. (let* ((tmpdir (if (featurep 'xemacs)
  10625. (temp-directory)
  10626. temporary-file-directory))
  10627. (texfilebase (make-temp-name
  10628. (expand-file-name "orgtex" tmpdir)))
  10629. (texfile (concat texfilebase ".tex"))
  10630. (dvifile (concat texfilebase ".dvi"))
  10631. (pngfile (concat texfilebase ".png"))
  10632. (fnh (if (featurep 'xemacs)
  10633. (font-height (get-face-font 'default))
  10634. (face-attribute 'default :height nil)))
  10635. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  10636. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  10637. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  10638. "Black"))
  10639. (bg (or (plist-get options (if buffer :background :html-background))
  10640. "Transparent")))
  10641. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  10642. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  10643. (with-temp-file texfile
  10644. (insert org-format-latex-header
  10645. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  10646. (let ((dir default-directory))
  10647. (condition-case nil
  10648. (progn
  10649. (cd tmpdir)
  10650. (call-process "latex" nil nil nil texfile))
  10651. (error nil))
  10652. (cd dir))
  10653. (if (not (file-exists-p dvifile))
  10654. (progn (message "Failed to create dvi file from %s" texfile) nil)
  10655. (call-process "dvipng" nil nil nil
  10656. "-E" "-fg" fg "-bg" bg
  10657. "-D" dpi
  10658. ;;"-x" scale "-y" scale
  10659. "-T" "tight"
  10660. "-o" pngfile
  10661. dvifile)
  10662. (if (not (file-exists-p pngfile))
  10663. (progn (message "Failed to create png file from %s" texfile) nil)
  10664. ;; Use the requested file name and clean up
  10665. (copy-file pngfile tofile 'replace)
  10666. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  10667. (delete-file (concat texfilebase e)))
  10668. pngfile))))
  10669. (defun org-dvipng-color (attr)
  10670. "Return an rgb color specification for dvipng."
  10671. (apply 'format "rgb %s %s %s"
  10672. (mapcar 'org-normalize-color
  10673. (color-values (face-attribute 'default attr nil)))))
  10674. (defun org-normalize-color (value)
  10675. "Return string to be used as color value for an RGB component."
  10676. (format "%g" (/ value 65535.0)))
  10677. ;;;; Key bindings
  10678. ;; Make `C-c C-x' a prefix key
  10679. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  10680. ;; TAB key with modifiers
  10681. (org-defkey org-mode-map "\C-i" 'org-cycle)
  10682. (org-defkey org-mode-map [(tab)] 'org-cycle)
  10683. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  10684. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  10685. (org-defkey org-mode-map "\M-\t" 'org-complete)
  10686. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  10687. ;; The following line is necessary under Suse GNU/Linux
  10688. (unless (featurep 'xemacs)
  10689. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  10690. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  10691. (define-key org-mode-map [backtab] 'org-shifttab)
  10692. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  10693. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  10694. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  10695. ;; Cursor keys with modifiers
  10696. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  10697. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  10698. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  10699. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  10700. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  10701. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  10702. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  10703. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  10704. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  10705. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  10706. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  10707. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  10708. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  10709. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  10710. ;;; Extra keys for tty access.
  10711. ;; We only set them when really needed because otherwise the
  10712. ;; menus don't show the simple keys
  10713. (when (or (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  10714. (not window-system))
  10715. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  10716. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  10717. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  10718. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  10719. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  10720. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  10721. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  10722. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  10723. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  10724. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  10725. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  10726. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  10727. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  10728. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  10729. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  10730. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  10731. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  10732. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  10733. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  10734. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  10735. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  10736. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
  10737. ;; All the other keys
  10738. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  10739. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  10740. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree)
  10741. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  10742. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  10743. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  10744. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  10745. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  10746. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  10747. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  10748. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  10749. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  10750. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  10751. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  10752. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  10753. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  10754. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  10755. (org-defkey org-mode-map "\C-c\\" 'org-tags-sparse-tree) ; Minor-mode res.
  10756. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  10757. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  10758. (org-defkey org-mode-map [(control return)] 'org-insert-heading-after-current)
  10759. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  10760. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  10761. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  10762. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  10763. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  10764. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  10765. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  10766. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  10767. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  10768. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  10769. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  10770. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  10771. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  10772. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  10773. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  10774. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  10775. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  10776. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  10777. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  10778. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  10779. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  10780. (org-defkey org-mode-map "\C-c^" 'org-sort)
  10781. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  10782. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  10783. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  10784. (org-defkey org-mode-map "\C-m" 'org-return)
  10785. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  10786. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  10787. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  10788. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  10789. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  10790. (org-defkey org-mode-map "\C-c'" 'org-table-edit-formulas)
  10791. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  10792. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  10793. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  10794. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  10795. (org-defkey org-mode-map "\C-c\C-q" 'org-table-wrap-region)
  10796. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  10797. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  10798. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  10799. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  10800. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  10801. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-cut-special)
  10802. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  10803. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  10804. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  10805. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  10806. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  10807. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  10808. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  10809. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  10810. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  10811. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  10812. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  10813. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  10814. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  10815. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  10816. (org-defkey org-mode-map "\C-c\C-xr" 'org-insert-columns-dblock)
  10817. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  10818. (when (featurep 'xemacs)
  10819. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  10820. (defvar org-table-auto-blank-field) ; defined in org-table.el
  10821. (defun org-self-insert-command (N)
  10822. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  10823. If the cursor is in a table looking at whitespace, the whitespace is
  10824. overwritten, and the table is not marked as requiring realignment."
  10825. (interactive "p")
  10826. (if (and (org-table-p)
  10827. (progn
  10828. ;; check if we blank the field, and if that triggers align
  10829. (and (featurep 'org-table) org-table-auto-blank-field
  10830. (member last-command
  10831. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  10832. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  10833. ;; got extra space, this field does not determine column width
  10834. (let (org-table-may-need-update) (org-table-blank-field))
  10835. ;; no extra space, this field may determine column width
  10836. (org-table-blank-field)))
  10837. t)
  10838. (eq N 1)
  10839. (looking-at "[^|\n]* |"))
  10840. (let (org-table-may-need-update)
  10841. (goto-char (1- (match-end 0)))
  10842. (delete-backward-char 1)
  10843. (goto-char (match-beginning 0))
  10844. (self-insert-command N))
  10845. (setq org-table-may-need-update t)
  10846. (self-insert-command N)
  10847. (org-fix-tags-on-the-fly)))
  10848. (defun org-fix-tags-on-the-fly ()
  10849. (when (and (equal (char-after (point-at-bol)) ?*)
  10850. (org-on-heading-p))
  10851. (org-align-tags-here org-tags-column)))
  10852. (defun org-delete-backward-char (N)
  10853. "Like `delete-backward-char', insert whitespace at field end in tables.
  10854. When deleting backwards, in tables this function will insert whitespace in
  10855. front of the next \"|\" separator, to keep the table aligned. The table will
  10856. still be marked for re-alignment if the field did fill the entire column,
  10857. because, in this case the deletion might narrow the column."
  10858. (interactive "p")
  10859. (if (and (org-table-p)
  10860. (eq N 1)
  10861. (string-match "|" (buffer-substring (point-at-bol) (point)))
  10862. (looking-at ".*?|"))
  10863. (let ((pos (point))
  10864. (noalign (looking-at "[^|\n\r]* |"))
  10865. (c org-table-may-need-update))
  10866. (backward-delete-char N)
  10867. (skip-chars-forward "^|")
  10868. (insert " ")
  10869. (goto-char (1- pos))
  10870. ;; noalign: if there were two spaces at the end, this field
  10871. ;; does not determine the width of the column.
  10872. (if noalign (setq org-table-may-need-update c)))
  10873. (backward-delete-char N)
  10874. (org-fix-tags-on-the-fly)))
  10875. (defun org-delete-char (N)
  10876. "Like `delete-char', but insert whitespace at field end in tables.
  10877. When deleting characters, in tables this function will insert whitespace in
  10878. front of the next \"|\" separator, to keep the table aligned. The table will
  10879. still be marked for re-alignment if the field did fill the entire column,
  10880. because, in this case the deletion might narrow the column."
  10881. (interactive "p")
  10882. (if (and (org-table-p)
  10883. (not (bolp))
  10884. (not (= (char-after) ?|))
  10885. (eq N 1))
  10886. (if (looking-at ".*?|")
  10887. (let ((pos (point))
  10888. (noalign (looking-at "[^|\n\r]* |"))
  10889. (c org-table-may-need-update))
  10890. (replace-match (concat
  10891. (substring (match-string 0) 1 -1)
  10892. " |"))
  10893. (goto-char pos)
  10894. ;; noalign: if there were two spaces at the end, this field
  10895. ;; does not determine the width of the column.
  10896. (if noalign (setq org-table-may-need-update c)))
  10897. (delete-char N))
  10898. (delete-char N)
  10899. (org-fix-tags-on-the-fly)))
  10900. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  10901. (put 'org-self-insert-command 'delete-selection t)
  10902. (put 'orgtbl-self-insert-command 'delete-selection t)
  10903. (put 'org-delete-char 'delete-selection 'supersede)
  10904. (put 'org-delete-backward-char 'delete-selection 'supersede)
  10905. ;; Make `flyspell-mode' delay after some commands
  10906. (put 'org-self-insert-command 'flyspell-delayed t)
  10907. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  10908. (put 'org-delete-char 'flyspell-delayed t)
  10909. (put 'org-delete-backward-char 'flyspell-delayed t)
  10910. ;; Make pabbrev-mode expand after org-mode commands
  10911. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  10912. (put 'orgybl-self-insert-command 'pabbrev-expand-after-command t)
  10913. ;; How to do this: Measure non-white length of current string
  10914. ;; If equal to column width, we should realign.
  10915. (defun org-remap (map &rest commands)
  10916. "In MAP, remap the functions given in COMMANDS.
  10917. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  10918. (let (new old)
  10919. (while commands
  10920. (setq old (pop commands) new (pop commands))
  10921. (if (fboundp 'command-remapping)
  10922. (org-defkey map (vector 'remap old) new)
  10923. (substitute-key-definition old new map global-map)))))
  10924. (when (eq org-enable-table-editor 'optimized)
  10925. ;; If the user wants maximum table support, we need to hijack
  10926. ;; some standard editing functions
  10927. (org-remap org-mode-map
  10928. 'self-insert-command 'org-self-insert-command
  10929. 'delete-char 'org-delete-char
  10930. 'delete-backward-char 'org-delete-backward-char)
  10931. (org-defkey org-mode-map "|" 'org-force-self-insert))
  10932. (defun org-shiftcursor-error ()
  10933. "Throw an error because Shift-Cursor command was applied in wrong context."
  10934. (error "This command is active in special context like tables, headlines or timestamps"))
  10935. (defun org-shifttab (&optional arg)
  10936. "Global visibility cycling or move to previous table field.
  10937. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  10938. on context.
  10939. See the individual commands for more information."
  10940. (interactive "P")
  10941. (cond
  10942. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  10943. (arg (message "Content view to level: ")
  10944. (org-content (prefix-numeric-value arg))
  10945. (setq org-cycle-global-status 'overview))
  10946. (t (call-interactively 'org-global-cycle))))
  10947. (defun org-shiftmetaleft ()
  10948. "Promote subtree or delete table column.
  10949. Calls `org-promote-subtree', `org-outdent-item',
  10950. or `org-table-delete-column', depending on context.
  10951. See the individual commands for more information."
  10952. (interactive)
  10953. (cond
  10954. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  10955. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  10956. ((org-at-item-p) (call-interactively 'org-outdent-item))
  10957. (t (org-shiftcursor-error))))
  10958. (defun org-shiftmetaright ()
  10959. "Demote subtree or insert table column.
  10960. Calls `org-demote-subtree', `org-indent-item',
  10961. or `org-table-insert-column', depending on context.
  10962. See the individual commands for more information."
  10963. (interactive)
  10964. (cond
  10965. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  10966. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  10967. ((org-at-item-p) (call-interactively 'org-indent-item))
  10968. (t (org-shiftcursor-error))))
  10969. (defun org-shiftmetaup (&optional arg)
  10970. "Move subtree up or kill table row.
  10971. Calls `org-move-subtree-up' or `org-table-kill-row' or
  10972. `org-move-item-up' depending on context. See the individual commands
  10973. for more information."
  10974. (interactive "P")
  10975. (cond
  10976. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  10977. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  10978. ((org-at-item-p) (call-interactively 'org-move-item-up))
  10979. (t (org-shiftcursor-error))))
  10980. (defun org-shiftmetadown (&optional arg)
  10981. "Move subtree down or insert table row.
  10982. Calls `org-move-subtree-down' or `org-table-insert-row' or
  10983. `org-move-item-down', depending on context. See the individual
  10984. commands for more information."
  10985. (interactive "P")
  10986. (cond
  10987. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  10988. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  10989. ((org-at-item-p) (call-interactively 'org-move-item-down))
  10990. (t (org-shiftcursor-error))))
  10991. (defun org-metaleft (&optional arg)
  10992. "Promote heading or move table column to left.
  10993. Calls `org-do-promote' or `org-table-move-column', depending on context.
  10994. With no specific context, calls the Emacs default `backward-word'.
  10995. See the individual commands for more information."
  10996. (interactive "P")
  10997. (cond
  10998. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  10999. ((or (org-on-heading-p) (org-region-active-p))
  11000. (call-interactively 'org-do-promote))
  11001. ((org-at-item-p) (call-interactively 'org-outdent-item))
  11002. (t (call-interactively 'backward-word))))
  11003. (defun org-metaright (&optional arg)
  11004. "Demote subtree or move table column to right.
  11005. Calls `org-do-demote' or `org-table-move-column', depending on context.
  11006. With no specific context, calls the Emacs default `forward-word'.
  11007. See the individual commands for more information."
  11008. (interactive "P")
  11009. (cond
  11010. ((org-at-table-p) (call-interactively 'org-table-move-column))
  11011. ((or (org-on-heading-p) (org-region-active-p))
  11012. (call-interactively 'org-do-demote))
  11013. ((org-at-item-p) (call-interactively 'org-indent-item))
  11014. (t (call-interactively 'forward-word))))
  11015. (defun org-metaup (&optional arg)
  11016. "Move subtree up or move table row up.
  11017. Calls `org-move-subtree-up' or `org-table-move-row' or
  11018. `org-move-item-up', depending on context. See the individual commands
  11019. for more information."
  11020. (interactive "P")
  11021. (cond
  11022. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  11023. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  11024. ((org-at-item-p) (call-interactively 'org-move-item-up))
  11025. (t (transpose-lines 1) (beginning-of-line -1))))
  11026. (defun org-metadown (&optional arg)
  11027. "Move subtree down or move table row down.
  11028. Calls `org-move-subtree-down' or `org-table-move-row' or
  11029. `org-move-item-down', depending on context. See the individual
  11030. commands for more information."
  11031. (interactive "P")
  11032. (cond
  11033. ((org-at-table-p) (call-interactively 'org-table-move-row))
  11034. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  11035. ((org-at-item-p) (call-interactively 'org-move-item-down))
  11036. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  11037. (defun org-shiftup (&optional arg)
  11038. "Increase item in timestamp or increase priority of current headline.
  11039. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  11040. depending on context. See the individual commands for more information."
  11041. (interactive "P")
  11042. (cond
  11043. ((org-at-timestamp-p t)
  11044. (call-interactively (if org-edit-timestamp-down-means-later
  11045. 'org-timestamp-down 'org-timestamp-up)))
  11046. ((org-on-heading-p) (call-interactively 'org-priority-up))
  11047. ((org-at-item-p) (call-interactively 'org-previous-item))
  11048. ((org-clocktable-try-shift 'up arg))
  11049. (t (call-interactively 'org-beginning-of-item) (beginning-of-line 1))))
  11050. (defun org-shiftdown (&optional arg)
  11051. "Decrease item in timestamp or decrease priority of current headline.
  11052. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  11053. depending on context. See the individual commands for more information."
  11054. (interactive "P")
  11055. (cond
  11056. ((org-at-timestamp-p t)
  11057. (call-interactively (if org-edit-timestamp-down-means-later
  11058. 'org-timestamp-up 'org-timestamp-down)))
  11059. ((org-on-heading-p) (call-interactively 'org-priority-down))
  11060. ((org-clocktable-try-shift 'down arg))
  11061. (t (call-interactively 'org-next-item))))
  11062. (defun org-shiftright (&optional arg)
  11063. "Next TODO keyword or timestamp one day later, depending on context."
  11064. (interactive "P")
  11065. (cond
  11066. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  11067. ((org-on-heading-p) (org-call-with-arg 'org-todo 'right))
  11068. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet nil))
  11069. ((org-at-property-p) (call-interactively 'org-property-next-allowed-value))
  11070. ((org-clocktable-try-shift 'right arg))
  11071. (t (org-shiftcursor-error))))
  11072. (defun org-shiftleft (&optional arg)
  11073. "Previous TODO keyword or timestamp one day earlier, depending on context."
  11074. (interactive "P")
  11075. (cond
  11076. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  11077. ((org-on-heading-p) (org-call-with-arg 'org-todo 'left))
  11078. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet 'previous))
  11079. ((org-at-property-p)
  11080. (call-interactively 'org-property-previous-allowed-value))
  11081. ((org-clocktable-try-shift 'left arg))
  11082. (t (org-shiftcursor-error))))
  11083. (defun org-shiftcontrolright ()
  11084. "Switch to next TODO set."
  11085. (interactive)
  11086. (cond
  11087. ((org-on-heading-p) (org-call-with-arg 'org-todo 'nextset))
  11088. (t (org-shiftcursor-error))))
  11089. (defun org-shiftcontrolleft ()
  11090. "Switch to previous TODO set."
  11091. (interactive)
  11092. (cond
  11093. ((org-on-heading-p) (org-call-with-arg 'org-todo 'previousset))
  11094. (t (org-shiftcursor-error))))
  11095. (defun org-ctrl-c-ret ()
  11096. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  11097. (interactive)
  11098. (cond
  11099. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  11100. (t (call-interactively 'org-insert-heading))))
  11101. (defun org-copy-special ()
  11102. "Copy region in table or copy current subtree.
  11103. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  11104. See the individual commands for more information."
  11105. (interactive)
  11106. (call-interactively
  11107. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  11108. (defun org-cut-special ()
  11109. "Cut region in table or cut current subtree.
  11110. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  11111. See the individual commands for more information."
  11112. (interactive)
  11113. (call-interactively
  11114. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  11115. (defun org-paste-special (arg)
  11116. "Paste rectangular region into table, or past subtree relative to level.
  11117. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  11118. See the individual commands for more information."
  11119. (interactive "P")
  11120. (if (org-at-table-p)
  11121. (org-table-paste-rectangle)
  11122. (org-paste-subtree arg)))
  11123. (defun org-ctrl-c-ctrl-c (&optional arg)
  11124. "Set tags in headline, or update according to changed information at point.
  11125. This command does many different things, depending on context:
  11126. - If the cursor is in a headline, prompt for tags and insert them
  11127. into the current line, aligned to `org-tags-column'. When called
  11128. with prefix arg, realign all tags in the current buffer.
  11129. - If the cursor is in one of the special #+KEYWORD lines, this
  11130. triggers scanning the buffer for these lines and updating the
  11131. information.
  11132. - If the cursor is inside a table, realign the table. This command
  11133. works even if the automatic table editor has been turned off.
  11134. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  11135. the entire table.
  11136. - If the cursor is a the beginning of a dynamic block, update it.
  11137. - If the cursor is inside a table created by the table.el package,
  11138. activate that table.
  11139. - If the current buffer is a remember buffer, close note and file it.
  11140. with a prefix argument, file it without further interaction to the default
  11141. location.
  11142. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  11143. links in this buffer.
  11144. - If the cursor is on a numbered item in a plain list, renumber the
  11145. ordered list.
  11146. - If the cursor is on a checkbox, toggle it."
  11147. (interactive "P")
  11148. (let ((org-enable-table-editor t))
  11149. (cond
  11150. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  11151. org-occur-highlights
  11152. org-latex-fragment-image-overlays)
  11153. (and (boundp 'org-clock-overlays) (org-remove-clock-overlays))
  11154. (org-remove-occur-highlights)
  11155. (org-remove-latex-fragment-image-overlays)
  11156. (message "Temporary highlights/overlays removed from current buffer"))
  11157. ((and (local-variable-p 'org-finish-function (current-buffer))
  11158. (fboundp org-finish-function))
  11159. (funcall org-finish-function))
  11160. ((org-at-property-p)
  11161. (call-interactively 'org-property-action))
  11162. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  11163. ((org-on-heading-p) (call-interactively 'org-set-tags))
  11164. ((org-at-table.el-p)
  11165. (require 'table)
  11166. (beginning-of-line 1)
  11167. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  11168. (call-interactively 'table-recognize-table))
  11169. ((org-at-table-p)
  11170. (org-table-maybe-eval-formula)
  11171. (if arg
  11172. (call-interactively 'org-table-recalculate)
  11173. (org-table-maybe-recalculate-line))
  11174. (call-interactively 'org-table-align))
  11175. ((org-at-item-checkbox-p)
  11176. (call-interactively 'org-toggle-checkbox))
  11177. ((org-at-item-p)
  11178. (call-interactively 'org-maybe-renumber-ordered-list))
  11179. ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
  11180. ;; Dynamic block
  11181. (beginning-of-line 1)
  11182. (org-update-dblock))
  11183. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  11184. (cond
  11185. ((equal (match-string 1) "TBLFM")
  11186. ;; Recalculate the table before this line
  11187. (save-excursion
  11188. (beginning-of-line 1)
  11189. (skip-chars-backward " \r\n\t")
  11190. (if (org-at-table-p)
  11191. (org-call-with-arg 'org-table-recalculate t))))
  11192. (t
  11193. (call-interactively 'org-mode-restart))))
  11194. (t (error "C-c C-c can do nothing useful at this location.")))))
  11195. (defun org-mode-restart ()
  11196. "Restart Org-mode, to scan again for special lines.
  11197. Also updates the keyword regular expressions."
  11198. (interactive)
  11199. (let ((org-inhibit-startup t)) (org-mode))
  11200. (message "Org-mode restarted to refresh keyword and special line setup"))
  11201. (defun org-kill-note-or-show-branches ()
  11202. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  11203. (interactive)
  11204. (if (not org-finish-function)
  11205. (call-interactively 'show-branches)
  11206. (let ((org-note-abort t))
  11207. (funcall org-finish-function))))
  11208. (defun org-return (&optional indent)
  11209. "Goto next table row or insert a newline.
  11210. Calls `org-table-next-row' or `newline', depending on context.
  11211. See the individual commands for more information."
  11212. (interactive)
  11213. (cond
  11214. ((bobp) (if indent (newline-and-indent) (newline)))
  11215. ((and (org-at-heading-p)
  11216. (looking-at
  11217. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  11218. (org-show-entry)
  11219. (end-of-line 1)
  11220. (newline))
  11221. ((org-at-table-p)
  11222. (org-table-justify-field-maybe)
  11223. (call-interactively 'org-table-next-row))
  11224. (t (if indent (newline-and-indent) (newline)))))
  11225. (defun org-return-indent ()
  11226. "Goto next table row or insert a newline and indent.
  11227. Calls `org-table-next-row' or `newline-and-indent', depending on
  11228. context. See the individual commands for more information."
  11229. (interactive)
  11230. (org-return t))
  11231. (defun org-ctrl-c-star ()
  11232. "Compute table, or change heading status of lines.
  11233. Calls `org-table-recalculate' or `org-toggle-region-headlines',
  11234. depending on context. This will also turn a plain list item or a normal
  11235. line into a subheading."
  11236. (interactive)
  11237. (cond
  11238. ((org-at-table-p)
  11239. (call-interactively 'org-table-recalculate))
  11240. ((org-region-active-p)
  11241. ;; Convert all lines in region to list items
  11242. (call-interactively 'org-toggle-region-headings))
  11243. ((org-on-heading-p)
  11244. (org-toggle-region-headings (point-at-bol)
  11245. (min (1+ (point-at-eol)) (point-max))))
  11246. ((org-at-item-p)
  11247. ;; Convert to heading
  11248. (let ((level (save-match-data
  11249. (save-excursion
  11250. (condition-case nil
  11251. (progn
  11252. (org-back-to-heading t)
  11253. (funcall outline-level))
  11254. (error 0))))))
  11255. (replace-match
  11256. (concat (make-string (org-get-valid-level level 1) ?*) " ") t t)))
  11257. (t (org-toggle-region-headings (point-at-bol)
  11258. (min (1+ (point-at-eol)) (point-max))))))
  11259. (defun org-ctrl-c-minus ()
  11260. "Insert separator line in table or modify bullet status of line.
  11261. Also turns a plain line or a region of lines into list items.
  11262. Calls `org-table-insert-hline', `org-toggle-region-items', or
  11263. `org-cycle-list-bullet', depending on context."
  11264. (interactive)
  11265. (cond
  11266. ((org-at-table-p)
  11267. (call-interactively 'org-table-insert-hline))
  11268. ((org-on-heading-p)
  11269. ;; Convert to item
  11270. (save-excursion
  11271. (beginning-of-line 1)
  11272. (if (looking-at "\\*+ ")
  11273. (replace-match (concat (make-string (- (match-end 0) (point) 1) ?\ ) "- ")))))
  11274. ((org-region-active-p)
  11275. ;; Convert all lines in region to list items
  11276. (call-interactively 'org-toggle-region-items))
  11277. ((org-in-item-p)
  11278. (call-interactively 'org-cycle-list-bullet))
  11279. (t (org-toggle-region-items (point-at-bol)
  11280. (min (1+ (point-at-eol)) (point-max))))))
  11281. (defun org-toggle-region-items (beg end)
  11282. "Convert all lines in region to list items.
  11283. If the first line is already an item, convert all list items in the region
  11284. to normal lines."
  11285. (interactive "r")
  11286. (let (l2 l)
  11287. (save-excursion
  11288. (goto-char end)
  11289. (setq l2 (org-current-line))
  11290. (goto-char beg)
  11291. (beginning-of-line 1)
  11292. (setq l (1- (org-current-line)))
  11293. (if (org-at-item-p)
  11294. ;; We already have items, de-itemize
  11295. (while (< (setq l (1+ l)) l2)
  11296. (when (org-at-item-p)
  11297. (goto-char (match-beginning 2))
  11298. (delete-region (match-beginning 2) (match-end 2))
  11299. (and (looking-at "[ \t]+") (replace-match "")))
  11300. (beginning-of-line 2))
  11301. (while (< (setq l (1+ l)) l2)
  11302. (unless (org-at-item-p)
  11303. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  11304. (replace-match "\\1- \\2")))
  11305. (beginning-of-line 2))))))
  11306. (defun org-toggle-region-headings (beg end)
  11307. "Convert all lines in region to list items.
  11308. If the first line is already an item, convert all list items in the region
  11309. to normal lines."
  11310. (interactive "r")
  11311. (let (l2 l)
  11312. (save-excursion
  11313. (goto-char end)
  11314. (setq l2 (org-current-line))
  11315. (goto-char beg)
  11316. (beginning-of-line 1)
  11317. (setq l (1- (org-current-line)))
  11318. (if (org-on-heading-p)
  11319. ;; We already have headlines, de-star them
  11320. (while (< (setq l (1+ l)) l2)
  11321. (when (org-on-heading-p t)
  11322. (and (looking-at outline-regexp) (replace-match "")))
  11323. (beginning-of-line 2))
  11324. (let* ((stars (save-excursion
  11325. (re-search-backward org-complex-heading-regexp nil t)
  11326. (or (match-string 1) "*")))
  11327. (add-stars (if org-odd-levels-only "**" "*"))
  11328. (rpl (concat stars add-stars " \\2")))
  11329. (while (< (setq l (1+ l)) l2)
  11330. (unless (org-on-heading-p)
  11331. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  11332. (replace-match rpl)))
  11333. (beginning-of-line 2)))))))
  11334. (defun org-meta-return (&optional arg)
  11335. "Insert a new heading or wrap a region in a table.
  11336. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  11337. See the individual commands for more information."
  11338. (interactive "P")
  11339. (cond
  11340. ((org-at-table-p)
  11341. (call-interactively 'org-table-wrap-region))
  11342. (t (call-interactively 'org-insert-heading))))
  11343. ;;; Menu entries
  11344. ;; Define the Org-mode menus
  11345. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  11346. '("Tbl"
  11347. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  11348. ["Next Field" org-cycle (org-at-table-p)]
  11349. ["Previous Field" org-shifttab (org-at-table-p)]
  11350. ["Next Row" org-return (org-at-table-p)]
  11351. "--"
  11352. ["Blank Field" org-table-blank-field (org-at-table-p)]
  11353. ["Edit Field" org-table-edit-field (org-at-table-p)]
  11354. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  11355. "--"
  11356. ("Column"
  11357. ["Move Column Left" org-metaleft (org-at-table-p)]
  11358. ["Move Column Right" org-metaright (org-at-table-p)]
  11359. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  11360. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  11361. ("Row"
  11362. ["Move Row Up" org-metaup (org-at-table-p)]
  11363. ["Move Row Down" org-metadown (org-at-table-p)]
  11364. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  11365. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  11366. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  11367. "--"
  11368. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  11369. ("Rectangle"
  11370. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  11371. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  11372. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  11373. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  11374. "--"
  11375. ("Calculate"
  11376. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  11377. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  11378. ["Edit Formulas" org-table-edit-formulas (org-at-table-p)]
  11379. "--"
  11380. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  11381. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  11382. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  11383. "--"
  11384. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  11385. "--"
  11386. ["Sum Column/Rectangle" org-table-sum
  11387. (or (org-at-table-p) (org-region-active-p))]
  11388. ["Which Column?" org-table-current-column (org-at-table-p)])
  11389. ["Debug Formulas"
  11390. org-table-toggle-formula-debugger
  11391. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  11392. ["Show Col/Row Numbers"
  11393. org-table-toggle-coordinate-overlays
  11394. :style toggle
  11395. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  11396. "--"
  11397. ["Create" org-table-create (and (not (org-at-table-p))
  11398. org-enable-table-editor)]
  11399. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  11400. ["Import from File" org-table-import (not (org-at-table-p))]
  11401. ["Export to File" org-table-export (org-at-table-p)]
  11402. "--"
  11403. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  11404. (easy-menu-define org-org-menu org-mode-map "Org menu"
  11405. '("Org"
  11406. ("Show/Hide"
  11407. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  11408. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  11409. ["Sparse Tree..." org-sparse-tree t]
  11410. ["Reveal Context" org-reveal t]
  11411. ["Show All" show-all t]
  11412. "--"
  11413. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  11414. "--"
  11415. ["New Heading" org-insert-heading t]
  11416. ("Navigate Headings"
  11417. ["Up" outline-up-heading t]
  11418. ["Next" outline-next-visible-heading t]
  11419. ["Previous" outline-previous-visible-heading t]
  11420. ["Next Same Level" outline-forward-same-level t]
  11421. ["Previous Same Level" outline-backward-same-level t]
  11422. "--"
  11423. ["Jump" org-goto t])
  11424. ("Edit Structure"
  11425. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  11426. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  11427. "--"
  11428. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  11429. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  11430. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  11431. "--"
  11432. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  11433. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  11434. ["Demote Heading" org-metaright (not (org-at-table-p))]
  11435. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  11436. "--"
  11437. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  11438. "--"
  11439. ["Convert to odd levels" org-convert-to-odd-levels t]
  11440. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  11441. ("Editing"
  11442. ["Emphasis..." org-emphasize t])
  11443. ("Archive"
  11444. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  11445. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  11446. ; :active t :keys "C-u C-c C-x C-a"]
  11447. ["Sparse trees open ARCHIVE trees"
  11448. (setq org-sparse-tree-open-archived-trees
  11449. (not org-sparse-tree-open-archived-trees))
  11450. :style toggle :selected org-sparse-tree-open-archived-trees]
  11451. ["Cycling opens ARCHIVE trees"
  11452. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  11453. :style toggle :selected org-cycle-open-archived-trees]
  11454. ["Agenda includes ARCHIVE trees"
  11455. (setq org-agenda-skip-archived-trees (not org-agenda-skip-archived-trees))
  11456. :style toggle :selected (not org-agenda-skip-archived-trees)]
  11457. "--"
  11458. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  11459. ; ["Check and Move Children" (org-archive-subtree '(4))
  11460. ; :active t :keys "C-u C-c C-x C-s"]
  11461. )
  11462. "--"
  11463. ("TODO Lists"
  11464. ["TODO/DONE/-" org-todo t]
  11465. ("Select keyword"
  11466. ["Next keyword" org-shiftright (org-on-heading-p)]
  11467. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  11468. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  11469. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  11470. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  11471. ["Show TODO Tree" org-show-todo-tree t]
  11472. ["Global TODO list" org-todo-list t]
  11473. "--"
  11474. ["Set Priority" org-priority t]
  11475. ["Priority Up" org-shiftup t]
  11476. ["Priority Down" org-shiftdown t])
  11477. ("TAGS and Properties"
  11478. ["Set Tags" 'org-ctrl-c-ctrl-c (org-at-heading-p)]
  11479. ["Change tag in region" 'org-change-tag-in-region (org-region-active-p)]
  11480. "--"
  11481. ["Set property" 'org-set-property t]
  11482. ["Column view of properties" org-columns t]
  11483. ["Insert Column View DBlock" org-insert-columns-dblock t])
  11484. ("Dates and Scheduling"
  11485. ["Timestamp" org-time-stamp t]
  11486. ["Timestamp (inactive)" org-time-stamp-inactive t]
  11487. ("Change Date"
  11488. ["1 Day Later" org-shiftright t]
  11489. ["1 Day Earlier" org-shiftleft t]
  11490. ["1 ... Later" org-shiftup t]
  11491. ["1 ... Earlier" org-shiftdown t])
  11492. ["Compute Time Range" org-evaluate-time-range t]
  11493. ["Schedule Item" org-schedule t]
  11494. ["Deadline" org-deadline t]
  11495. "--"
  11496. ["Custom time format" org-toggle-time-stamp-overlays
  11497. :style radio :selected org-display-custom-times]
  11498. "--"
  11499. ["Goto Calendar" org-goto-calendar t]
  11500. ["Date from Calendar" org-date-from-calendar t])
  11501. ("Logging work"
  11502. ["Clock in" org-clock-in t]
  11503. ["Clock out" org-clock-out t]
  11504. ["Clock cancel" org-clock-cancel t]
  11505. ["Goto running clock" org-clock-goto t]
  11506. ["Display times" org-clock-display t]
  11507. ["Create clock table" org-clock-report t]
  11508. "--"
  11509. ["Record DONE time"
  11510. (progn (setq org-log-done (not org-log-done))
  11511. (message "Switching to %s will %s record a timestamp"
  11512. (car org-done-keywords)
  11513. (if org-log-done "automatically" "not")))
  11514. :style toggle :selected org-log-done])
  11515. "--"
  11516. ["Agenda Command..." org-agenda t]
  11517. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  11518. ("File List for Agenda")
  11519. ("Special views current file"
  11520. ["TODO Tree" org-show-todo-tree t]
  11521. ["Check Deadlines" org-check-deadlines t]
  11522. ["Timeline" org-timeline t]
  11523. ["Tags Tree" org-tags-sparse-tree t])
  11524. "--"
  11525. ("Hyperlinks"
  11526. ["Store Link (Global)" org-store-link t]
  11527. ["Insert Link" org-insert-link t]
  11528. ["Follow Link" org-open-at-point t]
  11529. "--"
  11530. ["Next link" org-next-link t]
  11531. ["Previous link" org-previous-link t]
  11532. "--"
  11533. ["Descriptive Links"
  11534. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  11535. :style radio
  11536. :selected (member '(org-link) buffer-invisibility-spec)]
  11537. ["Literal Links"
  11538. (progn
  11539. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  11540. :style radio
  11541. :selected (not (member '(org-link) buffer-invisibility-spec))])
  11542. "--"
  11543. ["Export/Publish..." org-export t]
  11544. ("LaTeX"
  11545. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  11546. :selected org-cdlatex-mode]
  11547. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  11548. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  11549. ["Modify math symbol" org-cdlatex-math-modify
  11550. (org-inside-LaTeX-fragment-p)]
  11551. ["Export LaTeX fragments as images"
  11552. (if (featurep 'org-exp)
  11553. (setq org-export-with-LaTeX-fragments
  11554. (not org-export-with-LaTeX-fragments))
  11555. (require 'org-exp))
  11556. :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
  11557. org-export-with-LaTeX-fragments)])
  11558. "--"
  11559. ("Documentation"
  11560. ["Show Version" org-version t]
  11561. ["Info Documentation" org-info t])
  11562. ("Customize"
  11563. ["Browse Org Group" org-customize t]
  11564. "--"
  11565. ["Expand This Menu" org-create-customize-menu
  11566. (fboundp 'customize-menu-create)])
  11567. "--"
  11568. ["Refresh setup" org-mode-restart t]
  11569. ))
  11570. (defun org-info (&optional node)
  11571. "Read documentation for Org-mode in the info system.
  11572. With optional NODE, go directly to that node."
  11573. (interactive)
  11574. (info (format "(org)%s" (or node ""))))
  11575. (defun org-install-agenda-files-menu ()
  11576. (let ((bl (buffer-list)))
  11577. (save-excursion
  11578. (while bl
  11579. (set-buffer (pop bl))
  11580. (if (org-mode-p) (setq bl nil)))
  11581. (when (org-mode-p)
  11582. (easy-menu-change
  11583. '("Org") "File List for Agenda"
  11584. (append
  11585. (list
  11586. ["Edit File List" (org-edit-agenda-file-list) t]
  11587. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  11588. ["Remove Current File from List" org-remove-file t]
  11589. ["Cycle through agenda files" org-cycle-agenda-files t]
  11590. ["Occur in all agenda files" org-occur-in-agenda-files t]
  11591. "--")
  11592. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  11593. ;;;; Documentation
  11594. (defun org-require-autoloaded-modules ()
  11595. (interactive)
  11596. (mapc 'require
  11597. '(org-agenda org-archive org-clock org-colview
  11598. org-exp org-export-latex org-publish
  11599. org-remember org-table)))
  11600. (defun org-customize ()
  11601. "Call the customize function with org as argument."
  11602. (interactive)
  11603. (org-load-modules-maybe)
  11604. (org-require-autoloaded-modules)
  11605. (customize-browse 'org))
  11606. (defun org-create-customize-menu ()
  11607. "Create a full customization menu for Org-mode, insert it into the menu."
  11608. (interactive)
  11609. (org-load-modules-maybe)
  11610. (org-require-autoloaded-modules)
  11611. (if (fboundp 'customize-menu-create)
  11612. (progn
  11613. (easy-menu-change
  11614. '("Org") "Customize"
  11615. `(["Browse Org group" org-customize t]
  11616. "--"
  11617. ,(customize-menu-create 'org)
  11618. ["Set" Custom-set t]
  11619. ["Save" Custom-save t]
  11620. ["Reset to Current" Custom-reset-current t]
  11621. ["Reset to Saved" Custom-reset-saved t]
  11622. ["Reset to Standard Settings" Custom-reset-standard t]))
  11623. (message "\"Org\"-menu now contains full customization menu"))
  11624. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  11625. ;;;; Miscellaneous stuff
  11626. ;;; Generally useful functions
  11627. (defun org-plist-delete (plist property)
  11628. "Delete PROPERTY from PLIST.
  11629. This is in contrast to merely setting it to 0."
  11630. (let (p)
  11631. (while plist
  11632. (if (not (eq property (car plist)))
  11633. (setq p (plist-put p (car plist) (nth 1 plist))))
  11634. (setq plist (cddr plist)))
  11635. p))
  11636. (defun org-force-self-insert (N)
  11637. "Needed to enforce self-insert under remapping."
  11638. (interactive "p")
  11639. (self-insert-command N))
  11640. (defun org-string-width (s)
  11641. "Compute width of string, ignoring invisible characters.
  11642. This ignores character with invisibility property `org-link', and also
  11643. characters with property `org-cwidth', because these will become invisible
  11644. upon the next fontification round."
  11645. (let (b l)
  11646. (when (or (eq t buffer-invisibility-spec)
  11647. (assq 'org-link buffer-invisibility-spec))
  11648. (while (setq b (text-property-any 0 (length s)
  11649. 'invisible 'org-link s))
  11650. (setq s (concat (substring s 0 b)
  11651. (substring s (or (next-single-property-change
  11652. b 'invisible s) (length s)))))))
  11653. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  11654. (setq s (concat (substring s 0 b)
  11655. (substring s (or (next-single-property-change
  11656. b 'org-cwidth s) (length s))))))
  11657. (setq l (string-width s) b -1)
  11658. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  11659. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  11660. l))
  11661. (defun org-trim (s)
  11662. "Remove whitespace at beginning and end of string."
  11663. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  11664. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  11665. s)
  11666. (defun org-wrap (string &optional width lines)
  11667. "Wrap string to either a number of lines, or a width in characters.
  11668. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  11669. that costs. If there is a word longer than WIDTH, the text is actually
  11670. wrapped to the length of that word.
  11671. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  11672. many lines, whatever width that takes.
  11673. The return value is a list of lines, without newlines at the end."
  11674. (let* ((words (org-split-string string "[ \t\n]+"))
  11675. (maxword (apply 'max (mapcar 'org-string-width words)))
  11676. w ll)
  11677. (cond (width
  11678. (org-do-wrap words (max maxword width)))
  11679. (lines
  11680. (setq w maxword)
  11681. (setq ll (org-do-wrap words maxword))
  11682. (if (<= (length ll) lines)
  11683. ll
  11684. (setq ll words)
  11685. (while (> (length ll) lines)
  11686. (setq w (1+ w))
  11687. (setq ll (org-do-wrap words w)))
  11688. ll))
  11689. (t (error "Cannot wrap this")))))
  11690. (defun org-do-wrap (words width)
  11691. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  11692. (let (lines line)
  11693. (while words
  11694. (setq line (pop words))
  11695. (while (and words (< (+ (length line) (length (car words))) width))
  11696. (setq line (concat line " " (pop words))))
  11697. (setq lines (push line lines)))
  11698. (nreverse lines)))
  11699. (defun org-split-string (string &optional separators)
  11700. "Splits STRING into substrings at SEPARATORS.
  11701. No empty strings are returned if there are matches at the beginning
  11702. and end of string."
  11703. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  11704. (start 0)
  11705. notfirst
  11706. (list nil))
  11707. (while (and (string-match rexp string
  11708. (if (and notfirst
  11709. (= start (match-beginning 0))
  11710. (< start (length string)))
  11711. (1+ start) start))
  11712. (< (match-beginning 0) (length string)))
  11713. (setq notfirst t)
  11714. (or (eq (match-beginning 0) 0)
  11715. (and (eq (match-beginning 0) (match-end 0))
  11716. (eq (match-beginning 0) start))
  11717. (setq list
  11718. (cons (substring string start (match-beginning 0))
  11719. list)))
  11720. (setq start (match-end 0)))
  11721. (or (eq start (length string))
  11722. (setq list
  11723. (cons (substring string start)
  11724. list)))
  11725. (nreverse list)))
  11726. (defun org-context ()
  11727. "Return a list of contexts of the current cursor position.
  11728. If several contexts apply, all are returned.
  11729. Each context entry is a list with a symbol naming the context, and
  11730. two positions indicating start and end of the context. Possible
  11731. contexts are:
  11732. :headline anywhere in a headline
  11733. :headline-stars on the leading stars in a headline
  11734. :todo-keyword on a TODO keyword (including DONE) in a headline
  11735. :tags on the TAGS in a headline
  11736. :priority on the priority cookie in a headline
  11737. :item on the first line of a plain list item
  11738. :item-bullet on the bullet/number of a plain list item
  11739. :checkbox on the checkbox in a plain list item
  11740. :table in an org-mode table
  11741. :table-special on a special filed in a table
  11742. :table-table in a table.el table
  11743. :link on a hyperlink
  11744. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  11745. :target on a <<target>>
  11746. :radio-target on a <<<radio-target>>>
  11747. :latex-fragment on a LaTeX fragment
  11748. :latex-preview on a LaTeX fragment with overlayed preview image
  11749. This function expects the position to be visible because it uses font-lock
  11750. faces as a help to recognize the following contexts: :table-special, :link,
  11751. and :keyword."
  11752. (let* ((f (get-text-property (point) 'face))
  11753. (faces (if (listp f) f (list f)))
  11754. (p (point)) clist o)
  11755. ;; First the large context
  11756. (cond
  11757. ((org-on-heading-p t)
  11758. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  11759. (when (progn
  11760. (beginning-of-line 1)
  11761. (looking-at org-todo-line-tags-regexp))
  11762. (push (org-point-in-group p 1 :headline-stars) clist)
  11763. (push (org-point-in-group p 2 :todo-keyword) clist)
  11764. (push (org-point-in-group p 4 :tags) clist))
  11765. (goto-char p)
  11766. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  11767. (if (looking-at "\\[#[A-Z0-9]\\]")
  11768. (push (org-point-in-group p 0 :priority) clist)))
  11769. ((org-at-item-p)
  11770. (push (org-point-in-group p 2 :item-bullet) clist)
  11771. (push (list :item (point-at-bol)
  11772. (save-excursion (org-end-of-item) (point)))
  11773. clist)
  11774. (and (org-at-item-checkbox-p)
  11775. (push (org-point-in-group p 0 :checkbox) clist)))
  11776. ((org-at-table-p)
  11777. (push (list :table (org-table-begin) (org-table-end)) clist)
  11778. (if (memq 'org-formula faces)
  11779. (push (list :table-special
  11780. (previous-single-property-change p 'face)
  11781. (next-single-property-change p 'face)) clist)))
  11782. ((org-at-table-p 'any)
  11783. (push (list :table-table) clist)))
  11784. (goto-char p)
  11785. ;; Now the small context
  11786. (cond
  11787. ((org-at-timestamp-p)
  11788. (push (org-point-in-group p 0 :timestamp) clist))
  11789. ((memq 'org-link faces)
  11790. (push (list :link
  11791. (previous-single-property-change p 'face)
  11792. (next-single-property-change p 'face)) clist))
  11793. ((memq 'org-special-keyword faces)
  11794. (push (list :keyword
  11795. (previous-single-property-change p 'face)
  11796. (next-single-property-change p 'face)) clist))
  11797. ((org-on-target-p)
  11798. (push (org-point-in-group p 0 :target) clist)
  11799. (goto-char (1- (match-beginning 0)))
  11800. (if (looking-at org-radio-target-regexp)
  11801. (push (org-point-in-group p 0 :radio-target) clist))
  11802. (goto-char p))
  11803. ((setq o (car (delq nil
  11804. (mapcar
  11805. (lambda (x)
  11806. (if (memq x org-latex-fragment-image-overlays) x))
  11807. (org-overlays-at (point))))))
  11808. (push (list :latex-fragment
  11809. (org-overlay-start o) (org-overlay-end o)) clist)
  11810. (push (list :latex-preview
  11811. (org-overlay-start o) (org-overlay-end o)) clist))
  11812. ((org-inside-LaTeX-fragment-p)
  11813. ;; FIXME: positions wrong.
  11814. (push (list :latex-fragment (point) (point)) clist)))
  11815. (setq clist (nreverse (delq nil clist)))
  11816. clist))
  11817. ;; FIXME: Compare with at-regexp-p Do we need both?
  11818. (defun org-in-regexp (re &optional nlines visually)
  11819. "Check if point is inside a match of regexp.
  11820. Normally only the current line is checked, but you can include NLINES extra
  11821. lines both before and after point into the search.
  11822. If VISUALLY is set, require that the cursor is not after the match but
  11823. really on, so that the block visually is on the match."
  11824. (catch 'exit
  11825. (let ((pos (point))
  11826. (eol (point-at-eol (+ 1 (or nlines 0))))
  11827. (inc (if visually 1 0)))
  11828. (save-excursion
  11829. (beginning-of-line (- 1 (or nlines 0)))
  11830. (while (re-search-forward re eol t)
  11831. (if (and (<= (match-beginning 0) pos)
  11832. (>= (+ inc (match-end 0)) pos))
  11833. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  11834. (defun org-at-regexp-p (regexp)
  11835. "Is point inside a match of REGEXP in the current line?"
  11836. (catch 'exit
  11837. (save-excursion
  11838. (let ((pos (point)) (end (point-at-eol)))
  11839. (beginning-of-line 1)
  11840. (while (re-search-forward regexp end t)
  11841. (if (and (<= (match-beginning 0) pos)
  11842. (>= (match-end 0) pos))
  11843. (throw 'exit t)))
  11844. nil))))
  11845. (defun org-occur-in-agenda-files (regexp &optional nlines)
  11846. "Call `multi-occur' with buffers for all agenda files."
  11847. (interactive "sOrg-files matching: \np")
  11848. (let* ((files (org-agenda-files))
  11849. (tnames (mapcar 'file-truename files))
  11850. (extra org-agenda-text-search-extra-files)
  11851. f)
  11852. (when (eq (car extra) 'agenda-archives)
  11853. (setq extra (cdr extra))
  11854. (setq files (org-add-archive-files files)))
  11855. (while (setq f (pop extra))
  11856. (unless (member (file-truename f) tnames)
  11857. (add-to-list 'files f 'append)
  11858. (add-to-list 'tnames (file-truename f) 'append)))
  11859. (multi-occur
  11860. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  11861. regexp)))
  11862. (if (boundp 'occur-mode-find-occurrence-hook)
  11863. ;; Emacs 23
  11864. (add-hook 'occur-mode-find-occurrence-hook
  11865. (lambda ()
  11866. (when (org-mode-p)
  11867. (org-reveal))))
  11868. ;; Emacs 22
  11869. (defadvice occur-mode-goto-occurrence
  11870. (after org-occur-reveal activate)
  11871. (and (org-mode-p) (org-reveal)))
  11872. (defadvice occur-mode-goto-occurrence-other-window
  11873. (after org-occur-reveal activate)
  11874. (and (org-mode-p) (org-reveal)))
  11875. (defadvice occur-mode-display-occurrence
  11876. (after org-occur-reveal activate)
  11877. (when (org-mode-p)
  11878. (let ((pos (occur-mode-find-occurrence)))
  11879. (with-current-buffer (marker-buffer pos)
  11880. (save-excursion
  11881. (goto-char pos)
  11882. (org-reveal)))))))
  11883. (defun org-uniquify (list)
  11884. "Remove duplicate elements from LIST."
  11885. (let (res)
  11886. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  11887. res))
  11888. (defun org-delete-all (elts list)
  11889. "Remove all elements in ELTS from LIST."
  11890. (while elts
  11891. (setq list (delete (pop elts) list)))
  11892. list)
  11893. (defun org-back-over-empty-lines ()
  11894. "Move backwards over witespace, to the beginning of the first empty line.
  11895. Returns the number of empty lines passed."
  11896. (let ((pos (point)))
  11897. (skip-chars-backward " \t\n\r")
  11898. (beginning-of-line 2)
  11899. (goto-char (min (point) pos))
  11900. (count-lines (point) pos)))
  11901. (defun org-skip-whitespace ()
  11902. (skip-chars-forward " \t\n\r"))
  11903. (defun org-point-in-group (point group &optional context)
  11904. "Check if POINT is in match-group GROUP.
  11905. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  11906. match. If the match group does ot exist or point is not inside it,
  11907. return nil."
  11908. (and (match-beginning group)
  11909. (>= point (match-beginning group))
  11910. (<= point (match-end group))
  11911. (if context
  11912. (list context (match-beginning group) (match-end group))
  11913. t)))
  11914. (defun org-switch-to-buffer-other-window (&rest args)
  11915. "Switch to buffer in a second window on the current frame.
  11916. In particular, do not allow pop-up frames."
  11917. (let (pop-up-frames special-display-buffer-names special-display-regexps
  11918. special-display-function)
  11919. (apply 'switch-to-buffer-other-window args)))
  11920. (defun org-combine-plists (&rest plists)
  11921. "Create a single property list from all plists in PLISTS.
  11922. The process starts by copying the first list, and then setting properties
  11923. from the other lists. Settings in the last list are the most significant
  11924. ones and overrule settings in the other lists."
  11925. (let ((rtn (copy-sequence (pop plists)))
  11926. p v ls)
  11927. (while plists
  11928. (setq ls (pop plists))
  11929. (while ls
  11930. (setq p (pop ls) v (pop ls))
  11931. (setq rtn (plist-put rtn p v))))
  11932. rtn))
  11933. (defun org-move-line-down (arg)
  11934. "Move the current line down. With prefix argument, move it past ARG lines."
  11935. (interactive "p")
  11936. (let ((col (current-column))
  11937. beg end pos)
  11938. (beginning-of-line 1) (setq beg (point))
  11939. (beginning-of-line 2) (setq end (point))
  11940. (beginning-of-line (+ 1 arg))
  11941. (setq pos (move-marker (make-marker) (point)))
  11942. (insert (delete-and-extract-region beg end))
  11943. (goto-char pos)
  11944. (org-move-to-column col)))
  11945. (defun org-move-line-up (arg)
  11946. "Move the current line up. With prefix argument, move it past ARG lines."
  11947. (interactive "p")
  11948. (let ((col (current-column))
  11949. beg end pos)
  11950. (beginning-of-line 1) (setq beg (point))
  11951. (beginning-of-line 2) (setq end (point))
  11952. (beginning-of-line (- arg))
  11953. (setq pos (move-marker (make-marker) (point)))
  11954. (insert (delete-and-extract-region beg end))
  11955. (goto-char pos)
  11956. (org-move-to-column col)))
  11957. (defun org-replace-escapes (string table)
  11958. "Replace %-escapes in STRING with values in TABLE.
  11959. TABLE is an association list with keys like \"%a\" and string values.
  11960. The sequences in STRING may contain normal field width and padding information,
  11961. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  11962. so values can contain further %-escapes if they are define later in TABLE."
  11963. (let ((case-fold-search nil)
  11964. e re rpl)
  11965. (while (setq e (pop table))
  11966. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  11967. (while (string-match re string)
  11968. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  11969. (cdr e)))
  11970. (setq string (replace-match rpl t t string))))
  11971. string))
  11972. (defun org-sublist (list start end)
  11973. "Return a section of LIST, from START to END.
  11974. Counting starts at 1."
  11975. (let (rtn (c start))
  11976. (setq list (nthcdr (1- start) list))
  11977. (while (and list (<= c end))
  11978. (push (pop list) rtn)
  11979. (setq c (1+ c)))
  11980. (nreverse rtn)))
  11981. (defun org-find-base-buffer-visiting (file)
  11982. "Like `find-buffer-visiting' but alway return the base buffer and
  11983. not an indirect buffer."
  11984. (let ((buf (find-buffer-visiting file)))
  11985. (if buf
  11986. (or (buffer-base-buffer buf) buf)
  11987. nil)))
  11988. (defun org-image-file-name-regexp ()
  11989. "Return regexp matching the file names of images."
  11990. (if (fboundp 'image-file-name-regexp)
  11991. (image-file-name-regexp)
  11992. (let ((image-file-name-extensions
  11993. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  11994. "xbm" "xpm" "pbm" "pgm" "ppm")))
  11995. (concat "\\."
  11996. (regexp-opt (nconc (mapcar 'upcase
  11997. image-file-name-extensions)
  11998. image-file-name-extensions)
  11999. t)
  12000. "\\'"))))
  12001. (defun org-file-image-p (file)
  12002. "Return non-nil if FILE is an image."
  12003. (save-match-data
  12004. (string-match (org-image-file-name-regexp) file)))
  12005. ;;; Paragraph filling stuff.
  12006. ;; We want this to be just right, so use the full arsenal.
  12007. (defun org-indent-line-function ()
  12008. "Indent line like previous, but further if previous was headline or item."
  12009. (interactive)
  12010. (let* ((pos (point))
  12011. (itemp (org-at-item-p))
  12012. column bpos bcol tpos tcol bullet btype bullet-type)
  12013. ;; Find the previous relevant line
  12014. (beginning-of-line 1)
  12015. (cond
  12016. ((looking-at "#") (setq column 0))
  12017. ((looking-at "\\*+ ") (setq column 0))
  12018. (t
  12019. (beginning-of-line 0)
  12020. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]"))
  12021. (beginning-of-line 0))
  12022. (cond
  12023. ((looking-at "\\*+[ \t]+")
  12024. (goto-char (match-end 0))
  12025. (setq column (current-column)))
  12026. ((org-in-item-p)
  12027. (org-beginning-of-item)
  12028. ; (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  12029. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\)?")
  12030. (setq bpos (match-beginning 1) tpos (match-end 0)
  12031. bcol (progn (goto-char bpos) (current-column))
  12032. tcol (progn (goto-char tpos) (current-column))
  12033. bullet (match-string 1)
  12034. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  12035. (if (not itemp)
  12036. (setq column tcol)
  12037. (goto-char pos)
  12038. (beginning-of-line 1)
  12039. (if (looking-at "\\S-")
  12040. (progn
  12041. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  12042. (setq bullet (match-string 1)
  12043. btype (if (string-match "[0-9]" bullet) "n" bullet))
  12044. (setq column (if (equal btype bullet-type) bcol tcol)))
  12045. (setq column (org-get-indentation)))))
  12046. (t (setq column (org-get-indentation))))))
  12047. (goto-char pos)
  12048. (if (<= (current-column) (current-indentation))
  12049. (org-indent-line-to column)
  12050. (save-excursion (org-indent-line-to column)))
  12051. (setq column (current-column))
  12052. (beginning-of-line 1)
  12053. (if (looking-at
  12054. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  12055. (replace-match (concat "\\1" (format org-property-format
  12056. (match-string 2) (match-string 3)))
  12057. t nil))
  12058. (org-move-to-column column)))
  12059. (defun org-set-autofill-regexps ()
  12060. (interactive)
  12061. ;; In the paragraph separator we include headlines, because filling
  12062. ;; text in a line directly attached to a headline would otherwise
  12063. ;; fill the headline as well.
  12064. (org-set-local 'comment-start-skip "^#+[ \t]*")
  12065. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  12066. ;; The paragraph starter includes hand-formatted lists.
  12067. (org-set-local 'paragraph-start
  12068. "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  12069. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  12070. ;; But only if the user has not turned off tables or fixed-width regions
  12071. (org-set-local
  12072. 'auto-fill-inhibit-regexp
  12073. (concat "\\*+ \\|#\\+"
  12074. "\\|[ \t]*" org-keyword-time-regexp
  12075. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  12076. (concat
  12077. "\\|[ \t]*["
  12078. (if org-enable-table-editor "|" "")
  12079. (if org-enable-fixed-width-editor ":" "")
  12080. "]"))))
  12081. ;; We use our own fill-paragraph function, to make sure that tables
  12082. ;; and fixed-width regions are not wrapped. That function will pass
  12083. ;; through to `fill-paragraph' when appropriate.
  12084. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  12085. ; Adaptive filling: To get full control, first make sure that
  12086. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  12087. (org-set-local 'adaptive-fill-regexp "\000")
  12088. (org-set-local 'adaptive-fill-function
  12089. 'org-adaptive-fill-function)
  12090. (org-set-local
  12091. 'align-mode-rules-list
  12092. '((org-in-buffer-settings
  12093. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  12094. (modes . '(org-mode))))))
  12095. (defun org-fill-paragraph (&optional justify)
  12096. "Re-align a table, pass through to fill-paragraph if no table."
  12097. (let ((table-p (org-at-table-p))
  12098. (table.el-p (org-at-table.el-p)))
  12099. (cond ((and (equal (char-after (point-at-bol)) ?*)
  12100. (save-excursion (goto-char (point-at-bol))
  12101. (looking-at outline-regexp)))
  12102. t) ; skip headlines
  12103. (table.el-p t) ; skip table.el tables
  12104. (table-p (org-table-align) t) ; align org-mode tables
  12105. (t nil)))) ; call paragraph-fill
  12106. ;; For reference, this is the default value of adaptive-fill-regexp
  12107. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  12108. (defun org-adaptive-fill-function ()
  12109. "Return a fill prefix for org-mode files.
  12110. In particular, this makes sure hanging paragraphs for hand-formatted lists
  12111. work correctly."
  12112. (cond ((looking-at "#[ \t]+")
  12113. (match-string 0))
  12114. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] \\)?")
  12115. (save-excursion
  12116. (goto-char (match-end 0))
  12117. (make-string (current-column) ?\ )))
  12118. (t nil)))
  12119. ;;; Other stuff.
  12120. (defun org-toggle-fixed-width-section (arg)
  12121. "Toggle the fixed-width export.
  12122. If there is no active region, the QUOTE keyword at the current headline is
  12123. inserted or removed. When present, it causes the text between this headline
  12124. and the next to be exported as fixed-width text, and unmodified.
  12125. If there is an active region, this command adds or removes a colon as the
  12126. first character of this line. If the first character of a line is a colon,
  12127. this line is also exported in fixed-width font."
  12128. (interactive "P")
  12129. (let* ((cc 0)
  12130. (regionp (org-region-active-p))
  12131. (beg (if regionp (region-beginning) (point)))
  12132. (end (if regionp (region-end)))
  12133. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  12134. (case-fold-search nil)
  12135. (re "[ \t]*\\(:\\)")
  12136. off)
  12137. (if regionp
  12138. (save-excursion
  12139. (goto-char beg)
  12140. (setq cc (current-column))
  12141. (beginning-of-line 1)
  12142. (setq off (looking-at re))
  12143. (while (> nlines 0)
  12144. (setq nlines (1- nlines))
  12145. (beginning-of-line 1)
  12146. (cond
  12147. (arg
  12148. (org-move-to-column cc t)
  12149. (insert ":\n")
  12150. (forward-line -1))
  12151. ((and off (looking-at re))
  12152. (replace-match "" t t nil 1))
  12153. ((not off) (org-move-to-column cc t) (insert ":")))
  12154. (forward-line 1)))
  12155. (save-excursion
  12156. (org-back-to-heading)
  12157. (if (looking-at (concat outline-regexp
  12158. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  12159. (replace-match "" t t nil 1)
  12160. (if (looking-at outline-regexp)
  12161. (progn
  12162. (goto-char (match-end 0))
  12163. (insert org-quote-string " "))))))))
  12164. ;;;; Functions extending outline functionality
  12165. (defun org-beginning-of-line (&optional arg)
  12166. "Go to the beginning of the current line. If that is invisible, continue
  12167. to a visible line beginning. This makes the function of C-a more intuitive.
  12168. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  12169. first attempt, and only move to after the tags when the cursor is already
  12170. beyond the end of the headline."
  12171. (interactive "P")
  12172. (let ((pos (point)))
  12173. (beginning-of-line 1)
  12174. (if (bobp)
  12175. nil
  12176. (backward-char 1)
  12177. (if (org-invisible-p)
  12178. (while (and (not (bobp)) (org-invisible-p))
  12179. (backward-char 1)
  12180. (beginning-of-line 1))
  12181. (forward-char 1)))
  12182. (when org-special-ctrl-a/e
  12183. (cond
  12184. ((and (looking-at org-todo-line-regexp)
  12185. (= (char-after (match-end 1)) ?\ ))
  12186. (goto-char
  12187. (if (eq org-special-ctrl-a/e t)
  12188. (cond ((> pos (match-beginning 3)) (match-beginning 3))
  12189. ((= pos (point)) (match-beginning 3))
  12190. (t (point)))
  12191. (cond ((> pos (point)) (point))
  12192. ((not (eq last-command this-command)) (point))
  12193. (t (match-beginning 3))))))
  12194. ((org-at-item-p)
  12195. (goto-char
  12196. (if (eq org-special-ctrl-a/e t)
  12197. (cond ((> pos (match-end 4)) (match-end 4))
  12198. ((= pos (point)) (match-end 4))
  12199. (t (point)))
  12200. (cond ((> pos (point)) (point))
  12201. ((not (eq last-command this-command)) (point))
  12202. (t (match-end 4))))))))))
  12203. (defun org-end-of-line (&optional arg)
  12204. "Go to the end of the line.
  12205. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  12206. first attempt, and only move to after the tags when the cursor is already
  12207. beyond the end of the headline."
  12208. (interactive "P")
  12209. (if (or (not org-special-ctrl-a/e)
  12210. (not (org-on-heading-p)))
  12211. (end-of-line arg)
  12212. (let ((pos (point)))
  12213. (beginning-of-line 1)
  12214. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  12215. (if (eq org-special-ctrl-a/e t)
  12216. (if (or (< pos (match-beginning 1))
  12217. (= pos (match-end 0)))
  12218. (goto-char (match-beginning 1))
  12219. (goto-char (match-end 0)))
  12220. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  12221. (goto-char (match-end 0))
  12222. (goto-char (match-beginning 1))))
  12223. (end-of-line arg)))))
  12224. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  12225. (define-key org-mode-map "\C-e" 'org-end-of-line)
  12226. (defun org-kill-line (&optional arg)
  12227. "Kill line, to tags or end of line."
  12228. (interactive "P")
  12229. (cond
  12230. ((or (not org-special-ctrl-k)
  12231. (bolp)
  12232. (not (org-on-heading-p)))
  12233. (call-interactively 'kill-line))
  12234. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  12235. (kill-region (point) (match-beginning 1))
  12236. (org-set-tags nil t))
  12237. (t (kill-region (point) (point-at-eol)))))
  12238. (define-key org-mode-map "\C-k" 'org-kill-line)
  12239. (defun org-invisible-p ()
  12240. "Check if point is at a character currently not visible."
  12241. ;; Early versions of noutline don't have `outline-invisible-p'.
  12242. (if (fboundp 'outline-invisible-p)
  12243. (outline-invisible-p)
  12244. (get-char-property (point) 'invisible)))
  12245. (defun org-invisible-p2 ()
  12246. "Check if point is at a character currently not visible."
  12247. (save-excursion
  12248. (if (and (eolp) (not (bobp))) (backward-char 1))
  12249. ;; Early versions of noutline don't have `outline-invisible-p'.
  12250. (if (fboundp 'outline-invisible-p)
  12251. (outline-invisible-p)
  12252. (get-char-property (point) 'invisible))))
  12253. (defalias 'org-back-to-heading 'outline-back-to-heading)
  12254. (defalias 'org-on-heading-p 'outline-on-heading-p)
  12255. (defalias 'org-at-heading-p 'outline-on-heading-p)
  12256. (defun org-at-heading-or-item-p ()
  12257. (or (org-on-heading-p) (org-at-item-p)))
  12258. (defun org-on-target-p ()
  12259. (or (org-in-regexp org-radio-target-regexp)
  12260. (org-in-regexp org-target-regexp)))
  12261. (defun org-up-heading-all (arg)
  12262. "Move to the heading line of which the present line is a subheading.
  12263. This function considers both visible and invisible heading lines.
  12264. With argument, move up ARG levels."
  12265. (if (fboundp 'outline-up-heading-all)
  12266. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  12267. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  12268. (defun org-up-heading-safe ()
  12269. "Move to the heading line of which the present line is a subheading.
  12270. This version will not throw an error. It will return the level of the
  12271. headline found, or nil if no higher level is found."
  12272. (let ((pos (point)) start-level level
  12273. (re (concat "^" outline-regexp)))
  12274. (catch 'exit
  12275. (outline-back-to-heading t)
  12276. (setq start-level (funcall outline-level))
  12277. (if (equal start-level 1) (throw 'exit nil))
  12278. (while (re-search-backward re nil t)
  12279. (setq level (funcall outline-level))
  12280. (if (< level start-level) (throw 'exit level)))
  12281. nil)))
  12282. (defun org-first-sibling-p ()
  12283. "Is this heading the first child of its parents?"
  12284. (interactive)
  12285. (let ((re (concat "^" outline-regexp))
  12286. level l)
  12287. (unless (org-at-heading-p t)
  12288. (error "Not at a heading"))
  12289. (setq level (funcall outline-level))
  12290. (save-excursion
  12291. (if (not (re-search-backward re nil t))
  12292. t
  12293. (setq l (funcall outline-level))
  12294. (< l level)))))
  12295. (defun org-goto-sibling (&optional previous)
  12296. "Goto the next sibling, even if it is invisible.
  12297. When PREVIOUS is set, go to the previous sibling instead. Returns t
  12298. when a sibling was found. When none is found, return nil and don't
  12299. move point."
  12300. (let ((fun (if previous 're-search-backward 're-search-forward))
  12301. (pos (point))
  12302. (re (concat "^" outline-regexp))
  12303. level l)
  12304. (when (condition-case nil (org-back-to-heading t) (error nil))
  12305. (setq level (funcall outline-level))
  12306. (catch 'exit
  12307. (or previous (forward-char 1))
  12308. (while (funcall fun re nil t)
  12309. (setq l (funcall outline-level))
  12310. (when (< l level) (goto-char pos) (throw 'exit nil))
  12311. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  12312. (goto-char pos)
  12313. nil))))
  12314. (defun org-show-siblings ()
  12315. "Show all siblings of the current headline."
  12316. (save-excursion
  12317. (while (org-goto-sibling) (org-flag-heading nil)))
  12318. (save-excursion
  12319. (while (org-goto-sibling 'previous)
  12320. (org-flag-heading nil))))
  12321. (defun org-show-hidden-entry ()
  12322. "Show an entry where even the heading is hidden."
  12323. (save-excursion
  12324. (org-show-entry)))
  12325. (defun org-flag-heading (flag &optional entry)
  12326. "Flag the current heading. FLAG non-nil means make invisible.
  12327. When ENTRY is non-nil, show the entire entry."
  12328. (save-excursion
  12329. (org-back-to-heading t)
  12330. ;; Check if we should show the entire entry
  12331. (if entry
  12332. (progn
  12333. (org-show-entry)
  12334. (save-excursion
  12335. (and (outline-next-heading)
  12336. (org-flag-heading nil))))
  12337. (outline-flag-region (max (point-min) (1- (point)))
  12338. (save-excursion (outline-end-of-heading) (point))
  12339. flag))))
  12340. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  12341. ;; This is an exact copy of the original function, but it uses
  12342. ;; `org-back-to-heading', to make it work also in invisible
  12343. ;; trees. And is uses an invisible-OK argument.
  12344. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  12345. (org-back-to-heading invisible-OK)
  12346. (let ((first t)
  12347. (level (funcall outline-level)))
  12348. (while (and (not (eobp))
  12349. (or first (> (funcall outline-level) level)))
  12350. (setq first nil)
  12351. (outline-next-heading))
  12352. (unless to-heading
  12353. (if (memq (preceding-char) '(?\n ?\^M))
  12354. (progn
  12355. ;; Go to end of line before heading
  12356. (forward-char -1)
  12357. (if (memq (preceding-char) '(?\n ?\^M))
  12358. ;; leave blank line before heading
  12359. (forward-char -1))))))
  12360. (point))
  12361. (defun org-show-subtree ()
  12362. "Show everything after this heading at deeper levels."
  12363. (outline-flag-region
  12364. (point)
  12365. (save-excursion
  12366. (outline-end-of-subtree) (outline-next-heading) (point))
  12367. nil))
  12368. (defun org-show-entry ()
  12369. "Show the body directly following this heading.
  12370. Show the heading too, if it is currently invisible."
  12371. (interactive)
  12372. (save-excursion
  12373. (condition-case nil
  12374. (progn
  12375. (org-back-to-heading t)
  12376. (outline-flag-region
  12377. (max (point-min) (1- (point)))
  12378. (save-excursion
  12379. (re-search-forward
  12380. (concat "[\r\n]\\(" outline-regexp "\\)") nil 'move)
  12381. (or (match-beginning 1) (point-max)))
  12382. nil))
  12383. (error nil))))
  12384. (defun org-make-options-regexp (kwds)
  12385. "Make a regular expression for keyword lines."
  12386. (concat
  12387. "^"
  12388. "#?[ \t]*\\+\\("
  12389. (mapconcat 'regexp-quote kwds "\\|")
  12390. "\\):[ \t]*"
  12391. "\\(.+\\)"))
  12392. ;; Make isearch reveal the necessary context
  12393. (defun org-isearch-end ()
  12394. "Reveal context after isearch exits."
  12395. (when isearch-success ; only if search was successful
  12396. (if (featurep 'xemacs)
  12397. ;; Under XEmacs, the hook is run in the correct place,
  12398. ;; we directly show the context.
  12399. (org-show-context 'isearch)
  12400. ;; In Emacs the hook runs *before* restoring the overlays.
  12401. ;; So we have to use a one-time post-command-hook to do this.
  12402. ;; (Emacs 22 has a special variable, see function `org-mode')
  12403. (unless (and (boundp 'isearch-mode-end-hook-quit)
  12404. isearch-mode-end-hook-quit)
  12405. ;; Only when the isearch was not quitted.
  12406. (org-add-hook 'post-command-hook 'org-isearch-post-command
  12407. 'append 'local)))))
  12408. (defun org-isearch-post-command ()
  12409. "Remove self from hook, and show context."
  12410. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  12411. (org-show-context 'isearch))
  12412. ;;;; Integration with and fixes for other packages
  12413. ;;; Imenu support
  12414. (defvar org-imenu-markers nil
  12415. "All markers currently used by Imenu.")
  12416. (make-variable-buffer-local 'org-imenu-markers)
  12417. (defun org-imenu-new-marker (&optional pos)
  12418. "Return a new marker for use by Imenu, and remember the marker."
  12419. (let ((m (make-marker)))
  12420. (move-marker m (or pos (point)))
  12421. (push m org-imenu-markers)
  12422. m))
  12423. (defun org-imenu-get-tree ()
  12424. "Produce the index for Imenu."
  12425. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  12426. (setq org-imenu-markers nil)
  12427. (let* ((n org-imenu-depth)
  12428. (re (concat "^" outline-regexp))
  12429. (subs (make-vector (1+ n) nil))
  12430. (last-level 0)
  12431. m tree level head)
  12432. (save-excursion
  12433. (save-restriction
  12434. (widen)
  12435. (goto-char (point-max))
  12436. (while (re-search-backward re nil t)
  12437. (setq level (org-reduced-level (funcall outline-level)))
  12438. (when (<= level n)
  12439. (looking-at org-complex-heading-regexp)
  12440. (setq head (org-match-string-no-properties 4)
  12441. m (org-imenu-new-marker))
  12442. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  12443. (if (>= level last-level)
  12444. (push (cons head m) (aref subs level))
  12445. (push (cons head (aref subs (1+ level))) (aref subs level))
  12446. (loop for i from (1+ level) to n do (aset subs i nil)))
  12447. (setq last-level level)))))
  12448. (aref subs 1)))
  12449. (eval-after-load "imenu"
  12450. '(progn
  12451. (add-hook 'imenu-after-jump-hook
  12452. (lambda () (org-show-context 'org-goto)))))
  12453. ;; Speedbar support
  12454. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  12455. "Overlay marking the agenda restriction line in speedbar.")
  12456. (org-overlay-put org-speedbar-restriction-lock-overlay
  12457. 'face 'org-agenda-restriction-lock)
  12458. (org-overlay-put org-speedbar-restriction-lock-overlay
  12459. 'help-echo "Agendas are currently limited to this item.")
  12460. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  12461. (defun org-speedbar-set-agenda-restriction ()
  12462. "Restrict future agenda commands to the location at point in speedbar.
  12463. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  12464. (interactive)
  12465. (require 'org-agenda)
  12466. (let (p m tp np dir txt w)
  12467. (cond
  12468. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  12469. 'org-imenu t))
  12470. (setq m (get-text-property p 'org-imenu-marker))
  12471. (save-excursion
  12472. (save-restriction
  12473. (set-buffer (marker-buffer m))
  12474. (goto-char m)
  12475. (org-agenda-set-restriction-lock 'subtree))))
  12476. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  12477. 'speedbar-function 'speedbar-find-file))
  12478. (setq tp (previous-single-property-change
  12479. (1+ p) 'speedbar-function)
  12480. np (next-single-property-change
  12481. tp 'speedbar-function)
  12482. dir (speedbar-line-directory)
  12483. txt (buffer-substring-no-properties (or tp (point-min))
  12484. (or np (point-max))))
  12485. (save-excursion
  12486. (save-restriction
  12487. (set-buffer (find-file-noselect
  12488. (let ((default-directory dir))
  12489. (expand-file-name txt))))
  12490. (unless (org-mode-p)
  12491. (error "Cannot restrict to non-Org-mode file"))
  12492. (org-agenda-set-restriction-lock 'file))))
  12493. (t (error "Don't know how to restrict Org-mode's agenda")))
  12494. (org-move-overlay org-speedbar-restriction-lock-overlay
  12495. (point-at-bol) (point-at-eol))
  12496. (setq current-prefix-arg nil)
  12497. (org-agenda-maybe-redo)))
  12498. (eval-after-load "speedbar"
  12499. '(progn
  12500. (speedbar-add-supported-extension ".org")
  12501. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  12502. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  12503. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  12504. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  12505. (add-hook 'speedbar-visiting-tag-hook
  12506. (lambda () (org-show-context 'org-goto)))))
  12507. ;;; Fixes and Hacks for problems with other packages
  12508. ;; Make flyspell not check words in links, to not mess up our keymap
  12509. (defun org-mode-flyspell-verify ()
  12510. "Don't let flyspell put overlays at active buttons."
  12511. (not (get-text-property (point) 'keymap)))
  12512. ;; Make `bookmark-jump' show the jump location if it was hidden.
  12513. (eval-after-load "bookmark"
  12514. '(if (boundp 'bookmark-after-jump-hook)
  12515. ;; We can use the hook
  12516. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  12517. ;; Hook not available, use advice
  12518. (defadvice bookmark-jump (after org-make-visible activate)
  12519. "Make the position visible."
  12520. (org-bookmark-jump-unhide))))
  12521. (defun org-bookmark-jump-unhide ()
  12522. "Unhide the current position, to show the bookmark location."
  12523. (and (org-mode-p)
  12524. (or (org-invisible-p)
  12525. (save-excursion (goto-char (max (point-min) (1- (point))))
  12526. (org-invisible-p)))
  12527. (org-show-context 'bookmark-jump)))
  12528. ;; Make session.el ignore our circular variable
  12529. (eval-after-load "session"
  12530. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  12531. ;;;; Experimental code
  12532. (defun org-closed-in-range ()
  12533. "Sparse tree of items closed in a certain time range.
  12534. Still experimental, may disappear in the future."
  12535. (interactive)
  12536. ;; Get the time interval from the user.
  12537. (let* ((time1 (time-to-seconds
  12538. (org-read-date nil 'to-time nil "Starting date: ")))
  12539. (time2 (time-to-seconds
  12540. (org-read-date nil 'to-time nil "End date:")))
  12541. ;; callback function
  12542. (callback (lambda ()
  12543. (let ((time
  12544. (time-to-seconds
  12545. (apply 'encode-time
  12546. (org-parse-time-string
  12547. (match-string 1))))))
  12548. ;; check if time in interval
  12549. (and (>= time time1) (<= time time2))))))
  12550. ;; make tree, check each match with the callback
  12551. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  12552. ;;;; Finish up
  12553. (provide 'org)
  12554. (run-hooks 'org-load-hook)
  12555. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  12556. ;;; org.el ends here