cc-cmds.el 169 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777
  1. ;;; cc-cmds.el --- user level commands for CC Mode
  2. ;; Copyright (C) 1985, 1987, 1992-2015 Free Software Foundation, Inc.
  3. ;; Authors: 2003- Alan Mackenzie
  4. ;; 1998- Martin Stjernholm
  5. ;; 1992-1999 Barry A. Warsaw
  6. ;; 1987 Dave Detlefs
  7. ;; 1987 Stewart Clamen
  8. ;; 1985 Richard M. Stallman
  9. ;; Maintainer: bug-cc-mode@gnu.org
  10. ;; Created: 22-Apr-1997 (split from cc-mode.el)
  11. ;; Keywords: c languages
  12. ;; Package: cc-mode
  13. ;; This file is part of GNU Emacs.
  14. ;; GNU Emacs is free software: you can redistribute it and/or modify
  15. ;; it under the terms of the GNU General Public License as published by
  16. ;; the Free Software Foundation, either version 3 of the License, or
  17. ;; (at your option) any later version.
  18. ;; GNU Emacs is distributed in the hope that it will be useful,
  19. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. ;; GNU General Public License for more details.
  22. ;; You should have received a copy of the GNU General Public License
  23. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  24. ;;; Commentary:
  25. ;;; Code:
  26. (eval-when-compile
  27. (let ((load-path
  28. (if (and (boundp 'byte-compile-dest-file)
  29. (stringp byte-compile-dest-file))
  30. (cons (file-name-directory byte-compile-dest-file) load-path)
  31. load-path)))
  32. (load "cc-bytecomp" nil t)))
  33. (cc-require 'cc-defs)
  34. (cc-require 'cc-vars)
  35. (cc-require 'cc-engine)
  36. ;; Silence the compiler.
  37. (cc-bytecomp-defvar filladapt-mode) ; c-fill-paragraph contains a kludge
  38. ; which looks at this.
  39. ;; Indentation / Display syntax functions
  40. (defvar c-fix-backslashes t)
  41. (defun c-indent-line (&optional syntax quiet ignore-point-pos)
  42. "Indent the current line according to the syntactic context,
  43. if `c-syntactic-indentation' is non-nil. Optional SYNTAX is the
  44. syntactic information for the current line. Be silent about syntactic
  45. errors if the optional argument QUIET is non-nil, even if
  46. `c-report-syntactic-errors' is non-nil. Normally the position of
  47. point is used to decide where the old indentation is on a lines that
  48. is otherwise empty \(ignoring any line continuation backslash), but
  49. that's not done if IGNORE-POINT-POS is non-nil. Returns the amount of
  50. indentation change \(in columns)."
  51. (let ((line-cont-backslash (save-excursion
  52. (end-of-line)
  53. (eq (char-before) ?\\)))
  54. (c-fix-backslashes c-fix-backslashes)
  55. bs-col
  56. shift-amt)
  57. (when (and (not ignore-point-pos)
  58. (save-excursion
  59. (beginning-of-line)
  60. (looking-at (if line-cont-backslash
  61. ;; Don't use "\\s " - ^L doesn't count as WS
  62. ;; here
  63. "\\([ \t]*\\)\\\\$"
  64. "\\([ \t]*\\)$")))
  65. (<= (point) (match-end 1)))
  66. ;; Delete all whitespace after point if there's only whitespace
  67. ;; on the line, so that any code that does back-to-indentation
  68. ;; or similar gets the current column in this case. If this
  69. ;; removes a line continuation backslash it'll be restored
  70. ;; at the end.
  71. (unless c-auto-align-backslashes
  72. ;; Should try to keep the backslash alignment
  73. ;; in this case.
  74. (save-excursion
  75. (goto-char (match-end 0))
  76. (setq bs-col (1- (current-column)))))
  77. (delete-region (point) (match-end 0))
  78. (setq c-fix-backslashes t))
  79. (if c-syntactic-indentation
  80. (setq c-parsing-error
  81. (or (let ((c-parsing-error nil)
  82. (c-syntactic-context
  83. (or syntax
  84. (and (boundp 'c-syntactic-context)
  85. c-syntactic-context))))
  86. (c-save-buffer-state (indent)
  87. (unless c-syntactic-context
  88. (setq c-syntactic-context (c-guess-basic-syntax)))
  89. (setq indent (c-get-syntactic-indentation
  90. c-syntactic-context))
  91. (and (not (c-echo-parsing-error quiet))
  92. c-echo-syntactic-information-p
  93. (message "syntax: %s, indent: %d"
  94. c-syntactic-context indent))
  95. (setq shift-amt (- indent (current-indentation))))
  96. (c-shift-line-indentation shift-amt)
  97. (run-hooks 'c-special-indent-hook)
  98. c-parsing-error)
  99. c-parsing-error))
  100. (let ((indent 0))
  101. (save-excursion
  102. (while (and (= (forward-line -1) 0)
  103. (if (looking-at "\\s *\\\\?$")
  104. t
  105. (setq indent (current-indentation))
  106. nil))))
  107. (setq shift-amt (- indent (current-indentation)))
  108. (c-shift-line-indentation shift-amt)))
  109. (when (and c-fix-backslashes line-cont-backslash)
  110. (if bs-col
  111. (save-excursion
  112. (indent-to bs-col)
  113. (insert ?\\))
  114. (when c-auto-align-backslashes
  115. ;; Realign the line continuation backslash.
  116. (c-backslash-region (point) (point) nil t))))
  117. shift-amt))
  118. (defun c-newline-and-indent (&optional newline-arg)
  119. "Insert a newline and indent the new line.
  120. This function fixes line continuation backslashes if inside a macro,
  121. and takes care to set the indentation before calling
  122. `indent-according-to-mode', so that lineup functions like
  123. `c-lineup-dont-change' works better."
  124. ;; TODO: Backslashes before eol in comments and literals aren't
  125. ;; kept intact.
  126. (let ((c-macro-start (c-query-macro-start))
  127. ;; Avoid calling c-backslash-region from c-indent-line if it's
  128. ;; called during the newline call, which can happen due to
  129. ;; c-electric-continued-statement, for example. We also don't
  130. ;; want any backslash alignment from indent-according-to-mode.
  131. (c-fix-backslashes nil)
  132. has-backslash insert-backslash
  133. start col)
  134. (save-excursion
  135. (beginning-of-line)
  136. (setq start (point))
  137. (while (and (looking-at "[ \t]*\\\\?$")
  138. (= (forward-line -1) 0)))
  139. (setq col (current-indentation)))
  140. (when c-macro-start
  141. (if (and (eolp) (eq (char-before) ?\\))
  142. (setq insert-backslash t
  143. has-backslash t)
  144. (setq has-backslash (eq (char-before (c-point 'eol)) ?\\))))
  145. (newline newline-arg)
  146. (indent-to col)
  147. (when c-macro-start
  148. (if insert-backslash
  149. (progn
  150. ;; The backslash stayed on the previous line. Insert one
  151. ;; before calling c-backslash-region, so that
  152. ;; bs-col-after-end in it works better. Fixup the
  153. ;; backslashes on the newly inserted line.
  154. (insert ?\\)
  155. (backward-char)
  156. (c-backslash-region (point) (point) nil t))
  157. ;; The backslash moved to the new line, if there was any. Let
  158. ;; c-backslash-region fix a backslash on the previous line,
  159. ;; and the one that might be on the new line.
  160. ;; c-auto-align-backslashes is intentionally ignored here;
  161. ;; maybe the moved backslash should be left alone if it's set,
  162. ;; but we fix both lines on the grounds that the old backslash
  163. ;; has been moved anyway and is now in a different context.
  164. (c-backslash-region start (if has-backslash (point) start) nil t)))
  165. (when c-syntactic-indentation
  166. ;; Reindent syntactically. The indentation done above is not
  167. ;; wasted, since c-indent-line might look at the current
  168. ;; indentation.
  169. (let ((c-syntactic-context (c-save-buffer-state nil
  170. (c-guess-basic-syntax))))
  171. ;; We temporarily insert another line break, so that the
  172. ;; lineup functions will see the line as empty. That makes
  173. ;; e.g. c-lineup-cpp-define more intuitive since it then
  174. ;; proceeds to the preceding line in this case.
  175. (insert ?\n)
  176. (delete-horizontal-space)
  177. (setq start (- (point-max) (point)))
  178. (unwind-protect
  179. (progn
  180. (backward-char)
  181. (indent-according-to-mode))
  182. (goto-char (- (point-max) start))
  183. (delete-char -1)))
  184. (when has-backslash
  185. ;; Must align the backslash again after reindentation. The
  186. ;; c-backslash-region call above can't be optimized to ignore
  187. ;; this line, since it then won't align correctly with the
  188. ;; lines below if the first line in the macro is broken.
  189. (c-backslash-region (point) (point) nil t)))))
  190. (defun c-show-syntactic-information (arg)
  191. "Show syntactic information for current line.
  192. With universal argument, inserts the analysis as a comment on that line."
  193. (interactive "P")
  194. (let* ((c-parsing-error nil)
  195. (syntax (if (boundp 'c-syntactic-context)
  196. ;; Use `c-syntactic-context' in the same way as
  197. ;; `c-indent-line', to be consistent.
  198. c-syntactic-context
  199. (c-save-buffer-state nil
  200. (c-guess-basic-syntax)))))
  201. (if (not (consp arg))
  202. (let (elem pos ols)
  203. (message "Syntactic analysis: %s" syntax)
  204. (unwind-protect
  205. (progn
  206. (while syntax
  207. (setq elem (pop syntax))
  208. (when (setq pos (c-langelem-pos elem))
  209. (push (c-put-overlay pos (1+ pos)
  210. 'face 'highlight)
  211. ols))
  212. (when (setq pos (c-langelem-2nd-pos elem))
  213. (push (c-put-overlay pos (1+ pos)
  214. 'face 'secondary-selection)
  215. ols)))
  216. (sit-for 10))
  217. (while ols
  218. (c-delete-overlay (pop ols)))))
  219. (indent-for-comment)
  220. (insert-and-inherit (format "%s" syntax))
  221. ))
  222. (c-keep-region-active))
  223. (defun c-syntactic-information-on-region (from to)
  224. "Insert a comment with the syntactic analysis on every line in the region."
  225. (interactive "*r")
  226. (save-excursion
  227. (save-restriction
  228. (narrow-to-region from to)
  229. (goto-char (point-min))
  230. (while (not (eobp))
  231. (c-show-syntactic-information '(0))
  232. (forward-line)))))
  233. ;; Minor mode functions.
  234. (defun c-update-modeline ()
  235. (let ((fmt (format "/%s%s%s%s"
  236. (if c-electric-flag "l" "")
  237. (if (and c-electric-flag c-auto-newline)
  238. "a" "")
  239. (if c-hungry-delete-key "h" "")
  240. (if (and
  241. ;; (cc-)subword might not be loaded.
  242. (boundp 'c-subword-mode)
  243. (symbol-value 'c-subword-mode))
  244. ;; FIXME: subword-mode already comes with its
  245. ;; own lighter!
  246. "w"
  247. "")))
  248. ;; FIXME: Derived modes might want to use something else
  249. ;; than a string for `mode-name'.
  250. (bare-mode-name (if (string-match "\\(^[^/]*\\)/" mode-name)
  251. (match-string 1 mode-name)
  252. mode-name)))
  253. ;; (setq c-submode-indicators
  254. ;; (if (> (length fmt) 1)
  255. ;; fmt))
  256. (setq mode-name
  257. (if (> (length fmt) 1)
  258. (concat bare-mode-name fmt)
  259. bare-mode-name))
  260. (force-mode-line-update)))
  261. (defun c-toggle-syntactic-indentation (&optional arg)
  262. "Toggle syntactic indentation.
  263. Optional numeric ARG, if supplied, turns on syntactic indentation when
  264. positive, turns it off when negative, and just toggles it when zero or
  265. left out.
  266. When syntactic indentation is turned on (the default), the indentation
  267. functions and the electric keys indent according to the syntactic
  268. context keys, when applicable.
  269. When it's turned off, the electric keys don't reindent, the indentation
  270. functions indents every new line to the same level as the previous
  271. nonempty line, and \\[c-indent-command] adjusts the indentation in steps
  272. specified by `c-basic-offset'. The indentation style has no effect in
  273. this mode, nor any of the indentation associated variables,
  274. e.g. `c-special-indent-hook'.
  275. This command sets the variable `c-syntactic-indentation'."
  276. (interactive "P")
  277. (setq c-syntactic-indentation
  278. (c-calculate-state arg c-syntactic-indentation))
  279. (c-keep-region-active))
  280. (defun c-toggle-auto-newline (&optional arg)
  281. "Toggle auto-newline feature.
  282. Optional numeric ARG, if supplied, turns on auto-newline when
  283. positive, turns it off when negative, and just toggles it when zero or
  284. left out.
  285. Turning on auto-newline automatically enables electric indentation.
  286. When the auto-newline feature is enabled (indicated by \"/la\" on the
  287. mode line after the mode name) newlines are automatically inserted
  288. after special characters such as brace, comma, semi-colon, and colon."
  289. (interactive "P")
  290. (setq c-auto-newline
  291. (c-calculate-state arg (and c-auto-newline c-electric-flag)))
  292. (if c-auto-newline (setq c-electric-flag t))
  293. (c-update-modeline)
  294. (c-keep-region-active))
  295. (defalias 'c-toggle-auto-state 'c-toggle-auto-newline)
  296. (make-obsolete 'c-toggle-auto-state 'c-toggle-auto-newline "22.1")
  297. (defun c-toggle-hungry-state (&optional arg)
  298. "Toggle hungry-delete-key feature.
  299. Optional numeric ARG, if supplied, turns on hungry-delete when
  300. positive, turns it off when negative, and just toggles it when zero or
  301. left out.
  302. When the hungry-delete-key feature is enabled (indicated by \"/h\" on
  303. the mode line after the mode name) the delete key gobbles all preceding
  304. whitespace in one fell swoop."
  305. (interactive "P")
  306. (setq c-hungry-delete-key (c-calculate-state arg c-hungry-delete-key))
  307. (c-update-modeline)
  308. (c-keep-region-active))
  309. (defun c-toggle-auto-hungry-state (&optional arg)
  310. "Toggle auto-newline and hungry-delete-key features.
  311. Optional numeric ARG, if supplied, turns on auto-newline and
  312. hungry-delete when positive, turns them off when negative, and just
  313. toggles them when zero or left out.
  314. See `c-toggle-auto-newline' and `c-toggle-hungry-state' for details."
  315. (interactive "P")
  316. (setq c-auto-newline (c-calculate-state arg c-auto-newline))
  317. (setq c-hungry-delete-key (c-calculate-state arg c-hungry-delete-key))
  318. (c-update-modeline)
  319. (c-keep-region-active))
  320. (defun c-toggle-electric-state (&optional arg)
  321. "Toggle the electric indentation feature.
  322. Optional numeric ARG, if supplied, turns on electric indentation when
  323. positive, turns it off when negative, and just toggles it when zero or
  324. left out."
  325. (interactive "P")
  326. (setq c-electric-flag (c-calculate-state arg c-electric-flag))
  327. (c-update-modeline)
  328. (when (fboundp 'electric-indent-local-mode) ; Emacs 24.4 or later.
  329. (electric-indent-local-mode (if c-electric-flag 1 0)))
  330. (c-keep-region-active))
  331. ;; Electric keys
  332. (defun c-electric-backspace (arg)
  333. "Delete the preceding character or whitespace.
  334. If `c-hungry-delete-key' is non-nil (indicated by \"/h\" on the mode
  335. line) then all preceding whitespace is consumed. If however a prefix
  336. argument is supplied, or `c-hungry-delete-key' is nil, or point is
  337. inside a literal then the function in the variable
  338. `c-backspace-function' is called."
  339. (interactive "*P")
  340. (if (c-save-buffer-state ()
  341. (or (not c-hungry-delete-key)
  342. arg
  343. (c-in-literal)))
  344. (funcall c-backspace-function (prefix-numeric-value arg))
  345. (c-hungry-delete-backwards)))
  346. (defun c-hungry-delete-backwards ()
  347. "Delete the preceding character or all preceding whitespace
  348. back to the previous non-whitespace character.
  349. See also \\[c-hungry-delete-forward]."
  350. (interactive)
  351. (let ((here (point)))
  352. (c-skip-ws-backward)
  353. (if (/= (point) here)
  354. (delete-region (point) here)
  355. (funcall c-backspace-function 1))))
  356. (defalias 'c-hungry-backspace 'c-hungry-delete-backwards)
  357. (defun c-electric-delete-forward (arg)
  358. "Delete the following character or whitespace.
  359. If `c-hungry-delete-key' is non-nil (indicated by \"/h\" on the mode
  360. line) then all following whitespace is consumed. If however a prefix
  361. argument is supplied, or `c-hungry-delete-key' is nil, or point is
  362. inside a literal then the function in the variable `c-delete-function'
  363. is called."
  364. (interactive "*P")
  365. (if (c-save-buffer-state ()
  366. (or (not c-hungry-delete-key)
  367. arg
  368. (c-in-literal)))
  369. (funcall c-delete-function (prefix-numeric-value arg))
  370. (c-hungry-delete-forward)))
  371. (defun c-hungry-delete-forward ()
  372. "Delete the following character or all following whitespace
  373. up to the next non-whitespace character.
  374. See also \\[c-hungry-delete-backwards]."
  375. (interactive)
  376. (let ((here (point)))
  377. (c-skip-ws-forward)
  378. (if (/= (point) here)
  379. (delete-region (point) here)
  380. (funcall c-delete-function 1))))
  381. ;; This function is only used in XEmacs.
  382. (defun c-electric-delete (arg)
  383. "Deletes preceding or following character or whitespace.
  384. This function either deletes forward as `c-electric-delete-forward' or
  385. backward as `c-electric-backspace', depending on the configuration: If
  386. the function `delete-forward-p' is defined and returns non-nil, it
  387. deletes forward. Otherwise it deletes backward.
  388. Note: This is the way in XEmacs to choose the correct action for the
  389. \[delete] key, whichever key that means. Other flavors don't use this
  390. function to control that."
  391. (interactive "*P")
  392. (if (and (fboundp 'delete-forward-p)
  393. (delete-forward-p))
  394. (c-electric-delete-forward arg)
  395. (c-electric-backspace arg)))
  396. ;; This function is only used in XEmacs.
  397. (defun c-hungry-delete ()
  398. "Delete a non-whitespace char, or all whitespace up to the next non-whitespace char.
  399. The direction of deletion depends on the configuration: If the
  400. function `delete-forward-p' is defined and returns non-nil, it deletes
  401. forward using `c-hungry-delete-forward'. Otherwise it deletes
  402. backward using `c-hungry-backspace'.
  403. Note: This is the way in XEmacs to choose the correct action for the
  404. \[delete] key, whichever key that means. Other flavors don't use this
  405. function to control that."
  406. (interactive)
  407. (if (and (fboundp 'delete-forward-p)
  408. (delete-forward-p))
  409. (c-hungry-delete-forward)
  410. (c-hungry-delete-backwards)))
  411. (defun c-electric-pound (arg)
  412. "Insert a \"#\".
  413. If `c-electric-flag' is set, handle it specially according to the variable
  414. `c-electric-pound-behavior'. If a numeric ARG is supplied, or if point is
  415. inside a literal or a macro, nothing special happens."
  416. (interactive "*P")
  417. (if (c-save-buffer-state ()
  418. (or arg
  419. (not c-electric-flag)
  420. (not (memq 'alignleft c-electric-pound-behavior))
  421. (save-excursion
  422. (skip-chars-backward " \t")
  423. (not (bolp)))
  424. (save-excursion
  425. (and (= (forward-line -1) 0)
  426. (progn (end-of-line)
  427. (eq (char-before) ?\\))))
  428. (c-in-literal)))
  429. ;; do nothing special
  430. (self-insert-command (prefix-numeric-value arg))
  431. ;; place the pound character at the left edge
  432. (let ((pos (- (point-max) (point)))
  433. (bolp (bolp)))
  434. (beginning-of-line)
  435. (delete-horizontal-space)
  436. (insert (c-last-command-char))
  437. (and (not bolp)
  438. (goto-char (- (point-max) pos)))
  439. )))
  440. (defun c-point-syntax ()
  441. ;; Return the syntactic context of the construct at point. (This is NOT
  442. ;; nec. the same as the s.c. of the line point is on). N.B. This won't work
  443. ;; between the `#' of a cpp thing and what follows (see c-opt-cpp-prefix).
  444. (c-save-buffer-state (;; shut this up too
  445. (c-echo-syntactic-information-p nil)
  446. syntax)
  447. (c-tentative-buffer-changes
  448. ;; insert a newline to isolate the construct at point for syntactic
  449. ;; analysis.
  450. (insert-char ?\n 1)
  451. ;; In AWK (etc.) or in a macro, make sure this CR hasn't changed
  452. ;; the syntax. (There might already be an escaped NL there.)
  453. (when (or
  454. (save-excursion
  455. (c-skip-ws-backward (c-point 'bopl))
  456. (c-at-vsemi-p))
  457. (let ((pt (point)))
  458. (save-excursion
  459. (backward-char)
  460. (and (c-beginning-of-macro)
  461. (progn (c-end-of-macro)
  462. (< (point) pt))))))
  463. (backward-char)
  464. (insert-char ?\\ 1)
  465. (forward-char))
  466. (let ((c-syntactic-indentation-in-macros t)
  467. (c-auto-newline-analysis t))
  468. ;; Turn on syntactic macro analysis to help with auto
  469. ;; newlines only.
  470. (setq syntax (c-guess-basic-syntax))
  471. nil))
  472. syntax))
  473. (defun c-brace-newlines (syntax)
  474. ;; A brace stands at point. SYNTAX is the syntactic context of this brace
  475. ;; (not necessarily the same as the S.C. of the line it is on). Return
  476. ;; NEWLINES, the list containing some combination of the symbols `before'
  477. ;; and `after' saying where newlines should be inserted.
  478. (c-save-buffer-state
  479. ((syms
  480. ;; This is the list of brace syntactic symbols that can hang.
  481. ;; If any new ones are added to c-offsets-alist, they should be
  482. ;; added here as well.
  483. ;;
  484. ;; The order of this list is important; if SYNTAX has several
  485. ;; elements, the element that "wins" is the earliest in SYMS.
  486. '(arglist-cont-nonempty ; e.g. an array literal.
  487. class-open class-close defun-open defun-close
  488. inline-open inline-close
  489. brace-list-open brace-list-close
  490. brace-list-intro brace-entry-open
  491. block-open block-close
  492. substatement-open statement-case-open
  493. extern-lang-open extern-lang-close
  494. namespace-open namespace-close
  495. module-open module-close
  496. composition-open composition-close
  497. inexpr-class-open inexpr-class-close
  498. ;; `statement-cont' is here for the case with a brace
  499. ;; list opener inside a statement. C.f. CASE B.2 in
  500. ;; `c-guess-continued-construct'.
  501. statement-cont))
  502. ;; shut this up too
  503. (c-echo-syntactic-information-p nil)
  504. symb-newlines) ; e.g. (substatement-open . (after))
  505. (setq symb-newlines
  506. ;; Do not try to insert newlines around a special
  507. ;; (Pike-style) brace list.
  508. (if (and c-special-brace-lists
  509. (save-excursion
  510. (c-safe (if (= (char-before) ?{)
  511. (forward-char -1)
  512. (c-forward-sexp -1))
  513. (c-looking-at-special-brace-list))))
  514. nil
  515. ;; Seek the matching entry in c-hanging-braces-alist.
  516. (or (c-lookup-lists
  517. syms
  518. ;; Substitute inexpr-class and class-open or
  519. ;; class-close with inexpr-class-open or
  520. ;; inexpr-class-close.
  521. (if (assq 'inexpr-class syntax)
  522. (cond ((assq 'class-open syntax)
  523. '((inexpr-class-open)))
  524. ((assq 'class-close syntax)
  525. '((inexpr-class-close)))
  526. (t syntax))
  527. syntax)
  528. c-hanging-braces-alist)
  529. '(ignore before after)))) ; Default, when not in c-h-b-l.
  530. ;; If syntax is a function symbol, then call it using the
  531. ;; defined semantics.
  532. (if (and (not (consp (cdr symb-newlines)))
  533. (functionp (cdr symb-newlines)))
  534. (let ((c-syntactic-context syntax))
  535. (funcall (cdr symb-newlines)
  536. (car symb-newlines)
  537. (point)))
  538. (cdr symb-newlines))))
  539. (defun c-try-one-liner ()
  540. ;; Point is just after a newly inserted }. If the non-whitespace
  541. ;; content of the braces is a single line of code, compact the whole
  542. ;; construct to a single line, if this line isn't too long. The Right
  543. ;; Thing is done with comments.
  544. ;;
  545. ;; Point will be left after the }, regardless of whether the clean-up is
  546. ;; done. Return NON-NIL if the clean-up happened, NIL if it didn't.
  547. (let ((here (point))
  548. (pos (- (point-max) (point)))
  549. mbeg1 mend1 mbeg4 mend4
  550. eol-col cmnt-pos cmnt-col cmnt-gap)
  551. (when
  552. (save-excursion
  553. (save-restriction
  554. ;; Avoid backtracking over a very large block. The one we
  555. ;; deal with here can never be more than three lines.
  556. (narrow-to-region (save-excursion
  557. (forward-line -2)
  558. (point))
  559. (point))
  560. (and (c-safe (c-backward-sexp))
  561. (progn
  562. (forward-char)
  563. (narrow-to-region (point) (1- here)) ; innards of {.}
  564. (looking-at
  565. (cc-eval-when-compile
  566. (concat
  567. "\\(" ; (match-beginning 1)
  568. "[ \t]*\\([\r\n][ \t]*\\)?" ; WS with opt. NL
  569. "\\)" ; (match-end 1)
  570. "[^ \t\r\n]+\\([ \t]+[^ \t\r\n]+\\)*" ; non-WS
  571. "\\(" ; (match-beginning 4)
  572. "[ \t]*\\([\r\n][ \t]*\\)?" ; WS with opt. NL
  573. "\\)\\'"))))))) ; (match-end 4) at EOB.
  574. (if (c-tentative-buffer-changes
  575. (setq mbeg1 (match-beginning 1) mend1 (match-end 1)
  576. mbeg4 (match-beginning 4) mend4 (match-end 4))
  577. (backward-char) ; back over the `}'
  578. (save-excursion
  579. (setq cmnt-pos (and (c-backward-single-comment)
  580. (- (point) (- mend1 mbeg1)))))
  581. (delete-region mbeg4 mend4)
  582. (delete-region mbeg1 mend1)
  583. (setq eol-col (save-excursion (end-of-line) (current-column)))
  584. ;; Necessary to put the closing brace before any line
  585. ;; oriented comment to keep it syntactically significant.
  586. ;; This isn't necessary for block comments, but the result
  587. ;; looks nicer anyway.
  588. (when cmnt-pos
  589. (delete-char 1) ; the `}' has blundered into a comment
  590. (goto-char cmnt-pos)
  591. (setq cmnt-col (1+ (current-column)))
  592. (setq cmnt-pos (1+ cmnt-pos)) ; we're inserting a `}'
  593. (c-skip-ws-backward)
  594. (insert-char ?\} 1) ; reinsert the `}' before the comment.
  595. (setq cmnt-gap (- cmnt-col (current-column)))
  596. (when (zerop cmnt-gap)
  597. (insert-char ?\ 1) ; Put a space before a bare comment.
  598. (setq cmnt-gap 1)))
  599. (or (null c-max-one-liner-length)
  600. (zerop c-max-one-liner-length)
  601. (<= eol-col c-max-one-liner-length)
  602. ;; Can we trim space before comment to make the line fit?
  603. (and cmnt-gap
  604. (< (- eol-col cmnt-gap) c-max-one-liner-length)
  605. (progn (goto-char cmnt-pos)
  606. (backward-delete-char-untabify
  607. (- eol-col c-max-one-liner-length))
  608. t))))
  609. (goto-char (- (point-max) pos))))))
  610. (defun c-electric-brace (arg)
  611. "Insert a brace.
  612. If `c-electric-flag' is non-nil, the brace is not inside a literal and a
  613. numeric ARG hasn't been supplied, the command performs several electric
  614. actions:
  615. \(a) If the auto-newline feature is turned on (indicated by \"/la\" on
  616. the mode line) newlines are inserted before and after the brace as
  617. directed by the settings in `c-hanging-braces-alist'.
  618. \(b) Any auto-newlines are indented. The original line is also
  619. reindented unless `c-syntactic-indentation' is nil.
  620. \(c) If auto-newline is turned on, various newline cleanups based on the
  621. settings of `c-cleanup-list' are done."
  622. (interactive "*P")
  623. (let (safepos literal
  624. ;; We want to inhibit blinking the paren since this would be
  625. ;; most disruptive. We'll blink it ourselves later on.
  626. (old-blink-paren blink-paren-function)
  627. blink-paren-function case-fold-search)
  628. (c-save-buffer-state ()
  629. (setq safepos (c-safe-position (point) (c-parse-state))
  630. literal (c-in-literal safepos)))
  631. ;; Insert the brace. Note that expand-abbrev might reindent
  632. ;; the line here if there's a preceding "else" or something.
  633. (self-insert-command (prefix-numeric-value arg))
  634. (when (and c-electric-flag (not literal) (not arg))
  635. (if (not (looking-at "[ \t]*\\\\?$"))
  636. (if c-syntactic-indentation
  637. (indent-according-to-mode))
  638. (let ( ;; shut this up too
  639. (c-echo-syntactic-information-p nil)
  640. newlines
  641. ln-syntax br-syntax syntax) ; Syntactic context of the original line,
  642. ; of the brace itself, of the line the brace ends up on.
  643. (c-save-buffer-state ((c-syntactic-indentation-in-macros t)
  644. (c-auto-newline-analysis t))
  645. (setq ln-syntax (c-guess-basic-syntax)))
  646. (if c-syntactic-indentation
  647. (c-indent-line ln-syntax))
  648. (when c-auto-newline
  649. (backward-char)
  650. (setq br-syntax (c-point-syntax)
  651. newlines (c-brace-newlines br-syntax))
  652. ;; Insert the BEFORE newline, if wanted, and reindent the newline.
  653. (if (and (memq 'before newlines)
  654. (> (current-column) (current-indentation)))
  655. (if c-syntactic-indentation
  656. ;; Only a plain newline for now - it's indented
  657. ;; after the cleanups when the line has its final
  658. ;; appearance.
  659. (newline)
  660. (c-newline-and-indent)))
  661. (forward-char)
  662. ;; `syntax' is the syntactic context of the line which ends up
  663. ;; with the brace on it.
  664. (setq syntax (if (memq 'before newlines) br-syntax ln-syntax))
  665. ;; Do all appropriate clean ups
  666. (let ((here (point))
  667. (pos (- (point-max) (point)))
  668. mbeg mend
  669. )
  670. ;; `}': clean up empty defun braces
  671. (when (c-save-buffer-state ()
  672. (and (memq 'empty-defun-braces c-cleanup-list)
  673. (eq (c-last-command-char) ?\})
  674. (c-intersect-lists '(defun-close class-close inline-close)
  675. syntax)
  676. (progn
  677. (forward-char -1)
  678. (c-skip-ws-backward)
  679. (eq (char-before) ?\{))
  680. ;; make sure matching open brace isn't in a comment
  681. (not (c-in-literal))))
  682. (delete-region (point) (1- here))
  683. (setq here (- (point-max) pos)))
  684. (goto-char here)
  685. ;; `}': compact to a one-liner defun?
  686. (save-match-data
  687. (when
  688. (and (eq (c-last-command-char) ?\})
  689. (memq 'one-liner-defun c-cleanup-list)
  690. (c-intersect-lists '(defun-close) syntax)
  691. (c-try-one-liner))
  692. (setq here (- (point-max) pos))))
  693. ;; `{': clean up brace-else-brace and brace-elseif-brace
  694. (when (eq (c-last-command-char) ?\{)
  695. (cond
  696. ((and (memq 'brace-else-brace c-cleanup-list)
  697. (re-search-backward
  698. (concat "}"
  699. "\\([ \t\n]\\|\\\\\n\\)*"
  700. "else"
  701. "\\([ \t\n]\\|\\\\\n\\)*"
  702. "{"
  703. "\\=")
  704. nil t))
  705. (delete-region (match-beginning 0) (match-end 0))
  706. (insert-and-inherit "} else {"))
  707. ((and (memq 'brace-elseif-brace c-cleanup-list)
  708. (progn
  709. (goto-char (1- here))
  710. (setq mend (point))
  711. (c-skip-ws-backward)
  712. (setq mbeg (point))
  713. (eq (char-before) ?\)))
  714. (zerop (c-save-buffer-state nil (c-backward-token-2 1 t)))
  715. (eq (char-after) ?\()
  716. ; (progn
  717. ; (setq tmp (point))
  718. (re-search-backward
  719. (concat "}"
  720. "\\([ \t\n]\\|\\\\\n\\)*"
  721. "else"
  722. "\\([ \t\n]\\|\\\\\n\\)+"
  723. "if"
  724. "\\([ \t\n]\\|\\\\\n\\)*"
  725. "\\=")
  726. nil t);)
  727. ;(eq (match-end 0) tmp);
  728. )
  729. (delete-region mbeg mend)
  730. (goto-char mbeg)
  731. (insert ?\ ))))
  732. (goto-char (- (point-max) pos))
  733. ;; Indent the line after the cleanups since it might
  734. ;; very well indent differently due to them, e.g. if
  735. ;; c-indent-one-line-block is used together with the
  736. ;; one-liner-defun cleanup.
  737. (when c-syntactic-indentation
  738. (c-indent-line)))
  739. ;; does a newline go after the brace?
  740. (if (memq 'after newlines)
  741. (c-newline-and-indent))
  742. ))))
  743. ;; blink the paren
  744. (and (eq (c-last-command-char) ?\})
  745. (not executing-kbd-macro)
  746. old-blink-paren
  747. (save-excursion
  748. (c-save-buffer-state nil
  749. (c-backward-syntactic-ws safepos))
  750. (funcall old-blink-paren)))))
  751. (defun c-electric-slash (arg)
  752. "Insert a slash character.
  753. If the slash is inserted immediately after the comment prefix in a c-style
  754. comment, the comment might get closed by removing whitespace and possibly
  755. inserting a \"*\". See the variable `c-cleanup-list'.
  756. Indent the line as a comment, if:
  757. 1. The slash is second of a \"//\" line oriented comment introducing
  758. token and we are on a comment-only-line, or
  759. 2. The slash is part of a \"*/\" token that closes a block oriented
  760. comment.
  761. If a numeric ARG is supplied, point is inside a literal, or
  762. `c-syntactic-indentation' is nil or `c-electric-flag' is nil, indentation
  763. is inhibited."
  764. (interactive "*P")
  765. (let ((literal (c-save-buffer-state () (c-in-literal)))
  766. indentp
  767. ;; shut this up
  768. (c-echo-syntactic-information-p nil))
  769. ;; comment-close-slash cleanup? This DOESN'T need `c-electric-flag' or
  770. ;; `c-syntactic-indentation' set.
  771. (when (and (not arg)
  772. (eq literal 'c)
  773. (memq 'comment-close-slash c-cleanup-list)
  774. (eq (c-last-command-char) ?/)
  775. (looking-at (concat "[ \t]*\\("
  776. (regexp-quote comment-end) "\\)?$"))
  777. ; (eq c-block-comment-ender "*/") ; C-style comments ALWAYS end in */
  778. (save-excursion
  779. (save-restriction
  780. (narrow-to-region (point-min) (point))
  781. (back-to-indentation)
  782. (looking-at (concat c-current-comment-prefix "[ \t]*$")))))
  783. (delete-region (progn (forward-line 0) (point))
  784. (progn (end-of-line) (point)))
  785. (insert-char ?* 1)) ; the / comes later. ; Do I need a t (retain sticky properties) here?
  786. (setq indentp (and (not arg)
  787. c-syntactic-indentation
  788. c-electric-flag
  789. (eq (c-last-command-char) ?/)
  790. (eq (char-before) (if literal ?* ?/))))
  791. (self-insert-command (prefix-numeric-value arg))
  792. (if indentp
  793. (indent-according-to-mode))))
  794. (defun c-electric-star (arg)
  795. "Insert a star character.
  796. If `c-electric-flag' and `c-syntactic-indentation' are both non-nil, and
  797. the star is the second character of a C style comment starter on a
  798. comment-only-line, indent the line as a comment. If a numeric ARG is
  799. supplied, point is inside a literal, or `c-syntactic-indentation' is nil,
  800. this indentation is inhibited."
  801. (interactive "*P")
  802. (self-insert-command (prefix-numeric-value arg))
  803. ;; if we are in a literal, or if arg is given do not reindent the
  804. ;; current line, unless this star introduces a comment-only line.
  805. (if (c-save-buffer-state ()
  806. (and c-syntactic-indentation
  807. c-electric-flag
  808. (not arg)
  809. (eq (c-in-literal) 'c)
  810. (eq (char-before) ?*)
  811. (save-excursion
  812. (forward-char -1)
  813. (skip-chars-backward "*")
  814. (if (eq (char-before) ?/)
  815. (forward-char -1))
  816. (skip-chars-backward " \t")
  817. (bolp))))
  818. (let (c-echo-syntactic-information-p) ; shut this up
  819. (indent-according-to-mode))
  820. ))
  821. (defun c-electric-semi&comma (arg)
  822. "Insert a comma or semicolon.
  823. If `c-electric-flag' is non-nil, point isn't inside a literal and a
  824. numeric ARG hasn't been supplied, the command performs several electric
  825. actions:
  826. \(a) When the auto-newline feature is turned on (indicated by \"/la\" on
  827. the mode line) a newline might be inserted. See the variable
  828. `c-hanging-semi&comma-criteria' for how newline insertion is determined.
  829. \(b) Any auto-newlines are indented. The original line is also
  830. reindented unless `c-syntactic-indentation' is nil.
  831. \(c) If auto-newline is turned on, a comma following a brace list or a
  832. semicolon following a defun might be cleaned up, depending on the
  833. settings of `c-cleanup-list'."
  834. (interactive "*P")
  835. (let* (lim literal c-syntactic-context
  836. (here (point))
  837. ;; shut this up
  838. (c-echo-syntactic-information-p nil))
  839. (c-save-buffer-state ()
  840. (setq lim (c-most-enclosing-brace (c-parse-state))
  841. literal (c-in-literal lim)))
  842. (self-insert-command (prefix-numeric-value arg))
  843. (if (and c-electric-flag (not literal) (not arg))
  844. ;; do all cleanups and newline insertions if c-auto-newline is on.
  845. (if (or (not c-auto-newline)
  846. (not (looking-at "[ \t]*\\\\?$")))
  847. (if c-syntactic-indentation
  848. (c-indent-line))
  849. ;; clean ups: list-close-comma or defun-close-semi
  850. (let ((pos (- (point-max) (point))))
  851. (if (c-save-buffer-state ()
  852. (and (or (and
  853. (eq (c-last-command-char) ?,)
  854. (memq 'list-close-comma c-cleanup-list))
  855. (and
  856. (eq (c-last-command-char) ?\;)
  857. (memq 'defun-close-semi c-cleanup-list)))
  858. (progn
  859. (forward-char -1)
  860. (c-skip-ws-backward)
  861. (eq (char-before) ?}))
  862. ;; make sure matching open brace isn't in a comment
  863. (not (c-in-literal lim))))
  864. (delete-region (point) here))
  865. (goto-char (- (point-max) pos)))
  866. ;; reindent line
  867. (when c-syntactic-indentation
  868. (setq c-syntactic-context (c-guess-basic-syntax))
  869. (c-indent-line c-syntactic-context))
  870. ;; check to see if a newline should be added
  871. (let ((criteria c-hanging-semi&comma-criteria)
  872. answer add-newline-p)
  873. (while criteria
  874. (setq answer (funcall (car criteria)))
  875. ;; only nil value means continue checking
  876. (if (not answer)
  877. (setq criteria (cdr criteria))
  878. (setq criteria nil)
  879. ;; only 'stop specifically says do not add a newline
  880. (setq add-newline-p (not (eq answer 'stop)))
  881. ))
  882. (if add-newline-p
  883. (c-newline-and-indent))
  884. )))))
  885. (defun c-electric-colon (arg)
  886. "Insert a colon.
  887. If `c-electric-flag' is non-nil, the colon is not inside a literal and a
  888. numeric ARG hasn't been supplied, the command performs several electric
  889. actions:
  890. \(a) If the auto-newline feature is turned on (indicated by \"/la\" on
  891. the mode line) newlines are inserted before and after the colon based on
  892. the settings in `c-hanging-colons-alist'.
  893. \(b) Any auto-newlines are indented. The original line is also
  894. reindented unless `c-syntactic-indentation' is nil.
  895. \(c) If auto-newline is turned on, whitespace between two colons will be
  896. \"cleaned up\" leaving a scope operator, if this action is set in
  897. `c-cleanup-list'."
  898. (interactive "*P")
  899. (let* ((bod (c-point 'bod))
  900. (literal (c-save-buffer-state () (c-in-literal bod)))
  901. newlines is-scope-op
  902. ;; shut this up
  903. (c-echo-syntactic-information-p nil))
  904. (self-insert-command (prefix-numeric-value arg))
  905. ;; Any electric action?
  906. (if (and c-electric-flag (not literal) (not arg))
  907. ;; Unless we're at EOL, only re-indentation happens.
  908. (if (not (looking-at "[ \t]*\\\\?$"))
  909. (if c-syntactic-indentation
  910. (indent-according-to-mode))
  911. ;; scope-operator clean-up?
  912. (let ((pos (- (point-max) (point)))
  913. (here (point)))
  914. (if (c-save-buffer-state () ; Why do we need this? [ACM, 2003-03-12]
  915. (and c-auto-newline
  916. (memq 'scope-operator c-cleanup-list)
  917. (eq (char-before) ?:)
  918. (progn
  919. (forward-char -1)
  920. (c-skip-ws-backward)
  921. (eq (char-before) ?:))
  922. (not (c-in-literal))
  923. (not (eq (char-after (- (point) 2)) ?:))))
  924. (progn
  925. (delete-region (point) (1- here))
  926. (setq is-scope-op t)))
  927. (goto-char (- (point-max) pos)))
  928. ;; indent the current line if it's done syntactically.
  929. (if c-syntactic-indentation
  930. ;; Cannot use the same syntax analysis as we find below,
  931. ;; since that's made with c-syntactic-indentation-in-macros
  932. ;; always set to t.
  933. (indent-according-to-mode))
  934. ;; Calculate where, if anywhere, we want newlines.
  935. (c-save-buffer-state
  936. ((c-syntactic-indentation-in-macros t)
  937. (c-auto-newline-analysis t)
  938. ;; Turn on syntactic macro analysis to help with auto newlines
  939. ;; only.
  940. (syntax (c-guess-basic-syntax))
  941. (elem syntax))
  942. ;; Translate substatement-label to label for this operation.
  943. (while elem
  944. (if (eq (car (car elem)) 'substatement-label)
  945. (setcar (car elem) 'label))
  946. (setq elem (cdr elem)))
  947. ;; some language elements can only be determined by checking
  948. ;; the following line. Let's first look for ones that can be
  949. ;; found when looking on the line with the colon
  950. (setq newlines
  951. (and c-auto-newline
  952. (or (c-lookup-lists '(case-label label access-label)
  953. syntax c-hanging-colons-alist)
  954. (c-lookup-lists '(member-init-intro inher-intro)
  955. (progn
  956. (insert ?\n)
  957. (unwind-protect
  958. (c-guess-basic-syntax)
  959. (delete-char -1)))
  960. c-hanging-colons-alist)))))
  961. ;; does a newline go before the colon? Watch out for already
  962. ;; non-hung colons. However, we don't unhang them because that
  963. ;; would be a cleanup (and anti-social).
  964. (if (and (memq 'before newlines)
  965. (not is-scope-op)
  966. (save-excursion
  967. (skip-chars-backward ": \t")
  968. (not (bolp))))
  969. (let ((pos (- (point-max) (point))))
  970. (forward-char -1)
  971. (c-newline-and-indent)
  972. (goto-char (- (point-max) pos))))
  973. ;; does a newline go after the colon?
  974. (if (and (memq 'after (cdr-safe newlines))
  975. (not is-scope-op))
  976. (c-newline-and-indent))
  977. ))))
  978. (defun c-electric-lt-gt (arg)
  979. "Insert a \"<\" or \">\" character.
  980. If the current language uses angle bracket parens (e.g. template
  981. arguments in C++), try to find out if the inserted character is a
  982. paren and give it paren syntax if appropriate.
  983. If `c-electric-flag' and `c-syntactic-indentation' are both non-nil, the
  984. line will be reindented if the inserted character is a paren or if it
  985. finishes a C++ style stream operator in C++ mode. Exceptions are when a
  986. numeric argument is supplied, or the point is inside a literal."
  987. (interactive "*P")
  988. (let ((c-echo-syntactic-information-p nil)
  989. final-pos found-delim case-fold-search)
  990. (self-insert-command (prefix-numeric-value arg))
  991. (setq final-pos (point))
  992. ;;;; 2010-01-31: There used to be code here to put a syntax-table text
  993. ;;;; property on the new < or > and its mate (if any) when they are template
  994. ;;;; parens. This is now done in an after-change function.
  995. ;; Indent the line if appropriate.
  996. (when (and c-electric-flag c-syntactic-indentation c-recognize-<>-arglists)
  997. (setq found-delim
  998. (if (eq (c-last-command-char) ?<)
  999. ;; If a <, basically see if it's got "template" before it .....
  1000. (or (and (progn
  1001. (backward-char)
  1002. (= (point)
  1003. (progn (c-beginning-of-current-token) (point))))
  1004. (progn
  1005. (c-backward-token-2)
  1006. (looking-at c-opt-<>-sexp-key)))
  1007. ;; ..... or is a C++ << operator.
  1008. (and (c-major-mode-is 'c++-mode)
  1009. (progn
  1010. (goto-char (1- final-pos))
  1011. (c-beginning-of-current-token)
  1012. (looking-at "<<"))
  1013. (>= (match-end 0) final-pos)))
  1014. ;; It's a >. Either a C++ >> operator. ......
  1015. (or (and (c-major-mode-is 'c++-mode)
  1016. (progn
  1017. (goto-char (1- final-pos))
  1018. (c-beginning-of-current-token)
  1019. (looking-at ">>"))
  1020. (>= (match-end 0) final-pos))
  1021. ;; ...., or search back for a < which isn't already marked as an
  1022. ;; opening template delimiter.
  1023. (save-restriction
  1024. (widen)
  1025. ;; Narrow to avoid `c-forward-<>-arglist' below searching past
  1026. ;; our position.
  1027. (narrow-to-region (point-min) final-pos)
  1028. (goto-char final-pos)
  1029. (while
  1030. (and
  1031. (progn
  1032. (c-syntactic-skip-backward "^<;}" nil t)
  1033. (eq (char-before) ?<))
  1034. (progn
  1035. (backward-char)
  1036. (looking-at "\\s\("))))
  1037. (and (eq (char-after) ?<)
  1038. (not (looking-at "\\s\("))
  1039. (progn (c-backward-syntactic-ws)
  1040. (c-simple-skip-symbol-backward))
  1041. (or (looking-at c-opt-<>-sexp-key)
  1042. (not (looking-at c-keywords-regexp)))))))))
  1043. (goto-char final-pos)
  1044. (when found-delim
  1045. (indent-according-to-mode)
  1046. (when (and (eq (char-before) ?>)
  1047. (not executing-kbd-macro)
  1048. blink-paren-function)
  1049. ;; Currently (2014-10-19), the syntax-table text properties on < and >
  1050. ;; are only applied in code called during Emacs redisplay. We thus
  1051. ;; explicitly cause a redisplay so that these properties have been
  1052. ;; applied when `blink-paren-function' gets called.
  1053. (sit-for 0)
  1054. (funcall blink-paren-function)))))
  1055. (defun c-electric-paren (arg)
  1056. "Insert a parenthesis.
  1057. If `c-syntactic-indentation' and `c-electric-flag' are both non-nil, the
  1058. line is reindented unless a numeric ARG is supplied, or the parenthesis
  1059. is inserted inside a literal.
  1060. Whitespace between a function name and the parenthesis may get added or
  1061. removed; see the variable `c-cleanup-list'.
  1062. Also, if `c-electric-flag' and `c-auto-newline' are both non-nil, some
  1063. newline cleanups are done if appropriate; see the variable `c-cleanup-list'."
  1064. (interactive "*P")
  1065. (let ((literal (c-save-buffer-state () (c-in-literal)))
  1066. ;; shut this up
  1067. (c-echo-syntactic-information-p nil)
  1068. case-fold-search)
  1069. (self-insert-command (prefix-numeric-value arg))
  1070. (if (and (not arg) (not literal))
  1071. (let* ( ;; We want to inhibit blinking the paren since this will
  1072. ;; be most disruptive. We'll blink it ourselves
  1073. ;; afterwards.
  1074. (old-blink-paren blink-paren-function)
  1075. blink-paren-function)
  1076. (if (and c-syntactic-indentation c-electric-flag)
  1077. (indent-according-to-mode))
  1078. ;; If we're at EOL, check for new-line clean-ups.
  1079. (when (and c-electric-flag c-auto-newline
  1080. (looking-at "[ \t]*\\\\?$"))
  1081. ;; clean up brace-elseif-brace
  1082. (when
  1083. (and (memq 'brace-elseif-brace c-cleanup-list)
  1084. (eq (c-last-command-char) ?\()
  1085. (re-search-backward
  1086. (concat "}"
  1087. "\\([ \t\n]\\|\\\\\n\\)*"
  1088. "else"
  1089. "\\([ \t\n]\\|\\\\\n\\)+"
  1090. "if"
  1091. "\\([ \t\n]\\|\\\\\n\\)*"
  1092. "("
  1093. "\\=")
  1094. nil t)
  1095. (not (c-save-buffer-state () (c-in-literal))))
  1096. (delete-region (match-beginning 0) (match-end 0))
  1097. (insert-and-inherit "} else if ("))
  1098. ;; clean up brace-catch-brace
  1099. (when
  1100. (and (memq 'brace-catch-brace c-cleanup-list)
  1101. (eq (c-last-command-char) ?\()
  1102. (re-search-backward
  1103. (concat "}"
  1104. "\\([ \t\n]\\|\\\\\n\\)*"
  1105. "catch"
  1106. "\\([ \t\n]\\|\\\\\n\\)*"
  1107. "("
  1108. "\\=")
  1109. nil t)
  1110. (not (c-save-buffer-state () (c-in-literal))))
  1111. (delete-region (match-beginning 0) (match-end 0))
  1112. (insert-and-inherit "} catch (")))
  1113. ;; Check for clean-ups at function calls. These two DON'T need
  1114. ;; `c-electric-flag' or `c-syntactic-indentation' set.
  1115. ;; Point is currently just after the inserted paren.
  1116. (let (beg (end (1- (point))))
  1117. (cond
  1118. ;; space-before-funcall clean-up?
  1119. ((and (memq 'space-before-funcall c-cleanup-list)
  1120. (eq (c-last-command-char) ?\()
  1121. (save-excursion
  1122. (backward-char)
  1123. (skip-chars-backward " \t")
  1124. (setq beg (point))
  1125. (and (c-save-buffer-state () (c-on-identifier))
  1126. ;; Don't add a space into #define FOO()....
  1127. (not (and (c-beginning-of-macro)
  1128. (c-forward-over-cpp-define-id)
  1129. (eq (point) beg))))))
  1130. (save-excursion
  1131. (delete-region beg end)
  1132. (goto-char beg)
  1133. (insert ?\ )))
  1134. ;; compact-empty-funcall clean-up?
  1135. ((c-save-buffer-state ()
  1136. (and (memq 'compact-empty-funcall c-cleanup-list)
  1137. (eq (c-last-command-char) ?\))
  1138. (save-excursion
  1139. (c-safe (backward-char 2))
  1140. (when (looking-at "()")
  1141. (setq end (point))
  1142. (skip-chars-backward " \t")
  1143. (setq beg (point))
  1144. (c-on-identifier)))))
  1145. (delete-region beg end))))
  1146. (and (eq last-input-event ?\))
  1147. (not executing-kbd-macro)
  1148. old-blink-paren
  1149. (funcall old-blink-paren))))))
  1150. (defun c-electric-continued-statement ()
  1151. "Reindent the current line if appropriate.
  1152. This function is used to reindent the line after a keyword which
  1153. continues an earlier statement is typed, e.g. an \"else\" or the
  1154. \"while\" in a do-while block.
  1155. The line is reindented if there is nothing but whitespace before the
  1156. keyword on the line, the keyword is not inserted inside a literal, and
  1157. `c-electric-flag' and `c-syntactic-indentation' are both non-nil."
  1158. (let (;; shut this up
  1159. (c-echo-syntactic-information-p nil))
  1160. (when (c-save-buffer-state ()
  1161. (and c-electric-flag
  1162. c-syntactic-indentation
  1163. (not (eq (c-last-command-char) ?_))
  1164. (= (save-excursion
  1165. (skip-syntax-backward "w")
  1166. (point))
  1167. (c-point 'boi))
  1168. (not (c-in-literal (c-point 'bod)))))
  1169. ;; Have to temporarily insert a space so that
  1170. ;; c-guess-basic-syntax recognizes the keyword. Follow the
  1171. ;; space with a nonspace to avoid messing up any whitespace
  1172. ;; sensitive meddling that might be done, e.g. by
  1173. ;; `c-backslash-region'.
  1174. (insert-and-inherit " x")
  1175. (unwind-protect
  1176. (indent-according-to-mode)
  1177. (delete-char -2)))))
  1178. (declare-function subword-forward "subword" (&optional arg))
  1179. (declare-function subword-backward "subword" (&optional arg))
  1180. (cond
  1181. ((and (fboundp 'subword-mode) (not (fboundp 'c-subword-mode)))
  1182. ;; Recent Emacsen come with their own subword support. Use that.
  1183. (define-obsolete-function-alias 'c-subword-mode 'subword-mode "24.3")
  1184. (define-obsolete-variable-alias 'c-subword-mode 'subword-mode "24.3"))
  1185. (t
  1186. ;; Autoload directive for emacsen that doesn't have an older CC Mode
  1187. ;; version in the dist.
  1188. (autoload 'c-subword-mode "cc-subword"
  1189. "Mode enabling subword movement and editing keys." t)))
  1190. (declare-function c-forward-subword "ext:cc-subword" (&optional arg))
  1191. (declare-function c-backward-subword "ext:cc-subword" (&optional arg))
  1192. ;; "nomenclature" functions + c-scope-operator.
  1193. (defun c-forward-into-nomenclature (&optional arg)
  1194. "Compatibility alias for `c-forward-subword'."
  1195. (interactive "p")
  1196. (if (fboundp 'subword-mode)
  1197. (progn
  1198. (require 'subword)
  1199. (subword-forward arg))
  1200. (require 'cc-subword)
  1201. (c-forward-subword arg)))
  1202. (make-obsolete 'c-forward-into-nomenclature
  1203. (if (fboundp 'subword-mode) 'subword-forward 'c-forward-subword)
  1204. "23.2")
  1205. (defun c-backward-into-nomenclature (&optional arg)
  1206. "Compatibility alias for `c-backward-subword'."
  1207. (interactive "p")
  1208. (if (fboundp 'subword-mode)
  1209. (progn
  1210. (require 'subword)
  1211. (subword-backward arg))
  1212. (require 'cc-subword)
  1213. (c-backward-subword arg)))
  1214. (make-obsolete
  1215. 'c-backward-into-nomenclature
  1216. (if (fboundp 'subword-mode) 'subword-backward 'c-backward-subword) "23.2")
  1217. (defun c-scope-operator ()
  1218. "Insert a double colon scope operator at point.
  1219. No indentation or other \"electric\" behavior is performed."
  1220. (interactive "*")
  1221. (insert-and-inherit "::"))
  1222. ;; Movement (etc.) by defuns.
  1223. (defun c-in-function-trailer-p (&optional lim)
  1224. ;; Return non-nil if point is between the closing brace and the semicolon of
  1225. ;; a brace construct which needs a semicolon, e.g. within the "variables"
  1226. ;; portion of a declaration like "struct foo {...} bar ;".
  1227. ;;
  1228. ;; Return the position of the main declaration. Otherwise, return nil.
  1229. ;; Point is assumed to be at the top level and outside of any macro or
  1230. ;; literal.
  1231. ;;
  1232. ;; If LIM is non-nil, it is the bound on a the backward search for the
  1233. ;; beginning of the declaration.
  1234. ;;
  1235. ;; This function might do hidden buffer changes.
  1236. (and c-opt-block-decls-with-vars-key
  1237. (save-excursion
  1238. (c-syntactic-skip-backward "^;}" lim)
  1239. (let ((eo-block (point))
  1240. bod)
  1241. (and (eq (char-before) ?\})
  1242. (eq (car (c-beginning-of-decl-1 lim)) 'previous)
  1243. (setq bod (point))
  1244. ;; Look for struct or union or ... If we find one, it might
  1245. ;; be the return type of a function, or the like. Exclude
  1246. ;; this case.
  1247. (c-syntactic-re-search-forward
  1248. (concat "[;=\(\[{]\\|\\("
  1249. c-opt-block-decls-with-vars-key
  1250. "\\)")
  1251. eo-block t t t)
  1252. (match-beginning 1) ; Is there a "struct" etc., somewhere?
  1253. (not (eq (char-before) ?_))
  1254. (c-syntactic-re-search-forward "[;=\(\[{]" eo-block t t t)
  1255. (eq (char-before) ?\{)
  1256. bod)))))
  1257. (defun c-where-wrt-brace-construct ()
  1258. ;; Determine where we are with respect to functions (or other brace
  1259. ;; constructs, included in the term "function" in the rest of this comment).
  1260. ;; Point is assumed to be outside any macro or literal.
  1261. ;; This is used by c-\(beginning\|end\)-of-defun.
  1262. ;;
  1263. ;; Return one of these symbols:
  1264. ;; at-header : we're at the start of a function's header.
  1265. ;; in-header : we're inside a function's header, this extending right
  1266. ;; up to the brace. This bit includes any k&r declarations.
  1267. ;; in-block : we're inside a function's brace block.
  1268. ;; in-trailer : we're in the area between the "}" and ";" of something
  1269. ;; like "struct foo {...} bar, baz;".
  1270. ;; at-function-end : we're just after the closing brace (or semicolon) that
  1271. ;; terminates the function.
  1272. ;; outwith-function: we're not at or in any function. Being inside a
  1273. ;; non-brace construct also counts as 'outwith-function'.
  1274. ;;
  1275. ;; This function might do hidden buffer changes.
  1276. (save-excursion
  1277. (let* (kluge-start
  1278. decl-result brace-decl-p
  1279. (start (point))
  1280. (paren-state (c-parse-state))
  1281. (least-enclosing (c-least-enclosing-brace paren-state)))
  1282. (cond
  1283. ((and least-enclosing
  1284. (eq (char-after least-enclosing) ?\{))
  1285. 'in-block)
  1286. ((c-in-function-trailer-p)
  1287. 'in-trailer)
  1288. ((and (not least-enclosing)
  1289. (consp paren-state)
  1290. (consp (car paren-state))
  1291. (eq start (cdar paren-state)))
  1292. 'at-function-end)
  1293. (t
  1294. ;; Find the start of the current declaration. NOTE: If we're in the
  1295. ;; variables after a "struct/eval" type block, we don't get to the
  1296. ;; real declaration here - we detect and correct for this later.
  1297. ;;If we're in the parameters' parens, move back out of them.
  1298. (if least-enclosing (goto-char least-enclosing))
  1299. ;; Kluge so that c-beginning-of-decl-1 won't go back if we're already
  1300. ;; at a declaration.
  1301. (if (or (and (eolp) (not (eobp))) ; EOL is matched by "\\s>"
  1302. (not (looking-at
  1303. "\\([;#]\\|\\'\\|\\s(\\|\\s)\\|\\s\"\\|\\s\\\\|\\s$\\|\\s<\\|\\s>\\|\\s!\\)")))
  1304. (forward-char))
  1305. (setq kluge-start (point))
  1306. (setq decl-result
  1307. (car (c-beginning-of-decl-1
  1308. ;; NOTE: If we're in a K&R region, this might be the start
  1309. ;; of a parameter declaration, not the actual function.
  1310. ;; It might also leave us at a label or "label" like
  1311. ;; "private:".
  1312. (and least-enclosing ; LIMIT for c-b-of-decl-1
  1313. (c-safe-position least-enclosing paren-state)))))
  1314. ;; Has the declaration we've gone back to got braces?
  1315. (or (eq decl-result 'label)
  1316. (setq brace-decl-p
  1317. (save-excursion
  1318. (and (c-syntactic-re-search-forward "[;{]" nil t t)
  1319. (or (eq (char-before) ?\{)
  1320. (and c-recognize-knr-p
  1321. ;; Might have stopped on the
  1322. ;; ';' in a K&R argdecl. In
  1323. ;; that case the declaration
  1324. ;; should contain a block.
  1325. (c-in-knr-argdecl)))))))
  1326. (cond
  1327. ((or (eq decl-result 'label) ; e.g. "private:" or invalid syntax.
  1328. (= (point) kluge-start)) ; might be BOB or unbalanced parens.
  1329. 'outwith-function)
  1330. ((eq decl-result 'same)
  1331. (if brace-decl-p
  1332. (if (eq (point) start)
  1333. 'at-header
  1334. 'in-header)
  1335. 'outwith-function))
  1336. ((eq decl-result 'previous)
  1337. (if (and (not brace-decl-p)
  1338. (c-in-function-trailer-p))
  1339. 'at-function-end
  1340. 'outwith-function))
  1341. (t (error
  1342. "c-where-wrt-brace-construct: c-beginning-of-decl-1 returned %s"
  1343. decl-result))))))))
  1344. (defun c-backward-to-nth-BOF-{ (n where)
  1345. ;; Skip to the opening brace of the Nth function before point. If
  1346. ;; point is inside a function, this counts as the first. Point must be
  1347. ;; outside any comment/string or macro.
  1348. ;;
  1349. ;; N must be strictly positive.
  1350. ;; WHERE describes the position of point, one of the symbols `at-header',
  1351. ;; `in-header', `in-block', `in-trailer', `at-function-end',
  1352. ;; `outwith-function' as returned by c-where-wrt-brace-construct.
  1353. ;;
  1354. ;; If we run out of functions, leave point at BOB. Return zero on success,
  1355. ;; otherwise the number of {s still to go.
  1356. ;;
  1357. ;; This function may do hidden buffer changes
  1358. (cond
  1359. ;; What we do to go back the first defun depends on where we start.
  1360. ((bobp))
  1361. ((eq where 'in-block)
  1362. (goto-char (c-least-enclosing-brace (c-parse-state)))
  1363. (setq n (1- n)))
  1364. ((eq where 'in-header)
  1365. (c-syntactic-re-search-forward "{")
  1366. (backward-char)
  1367. (setq n (1- n)))
  1368. ((memq where '(at-header outwith-function at-function-end in-trailer))
  1369. (c-syntactic-skip-backward "^}")
  1370. (when (eq (char-before) ?\})
  1371. (backward-sexp)
  1372. (setq n (1- n))))
  1373. (t (error "Unknown `where' %s in c-backward-to-nth-EOF-{" where)))
  1374. ;; Each time round the loop, go back to a "{" at the outermost level.
  1375. (while (and (> n 0) (not (bobp)))
  1376. (c-parse-state) ; This call speeds up the following one
  1377. ; by a factor of ~6. Hmmm. 2006/4/5.
  1378. (c-syntactic-skip-backward "^}")
  1379. (when (eq (char-before) ?\})
  1380. (backward-sexp)
  1381. (setq n (1- n))))
  1382. n)
  1383. (defun c-narrow-to-most-enclosing-decl-block (&optional inclusive)
  1384. ;; If we are inside a decl-block (in the sense of c-looking-at-decl-block),
  1385. ;; i.e. something like namespace{} or extern{}, narrow to the insides of
  1386. ;; that block (NOT including the enclosing braces) if INCLUSIVE is nil,
  1387. ;; otherwise include the braces. If the closing brace is missing,
  1388. ;; (point-max) is used instead.
  1389. (let ((paren-state (c-parse-state))
  1390. encl-decl)
  1391. (setq encl-decl (and paren-state (c-most-enclosing-decl-block paren-state)))
  1392. (if encl-decl
  1393. (save-excursion
  1394. (narrow-to-region
  1395. (if inclusive
  1396. (progn (goto-char encl-decl)
  1397. (c-beginning-of-decl-1)
  1398. (point))
  1399. (1+ encl-decl))
  1400. (progn
  1401. (goto-char encl-decl)
  1402. (or (c-safe (forward-list)
  1403. (if inclusive
  1404. (point)
  1405. (1- (point))))
  1406. (point-max))))))))
  1407. (defun c-widen-to-enclosing-decl-scope (paren-state orig-point-min orig-point-max)
  1408. ;; Narrow the buffer to the innermost declaration scope (e.g. a class, a
  1409. ;; namespace or the "whole buffer") recorded in PAREN-STATE, the bounding
  1410. ;; braces NOT being included in the resulting region. On no account may the
  1411. ;; final region exceed that bounded by ORIG-POINT-MIN, ORIG-POINT-MAX.
  1412. ;; PAREN-STATE is a list of buffer positions in the style of
  1413. ;; (c-parse-state), one of which will be that of the desired opening brace,
  1414. ;; if there is one.
  1415. ;;
  1416. ;; Return the position of the enclosing opening brace, or nil
  1417. (let (encl-decl) ; putative position of decl-scope's opening brace.
  1418. (save-restriction
  1419. (narrow-to-region orig-point-min orig-point-max)
  1420. (setq encl-decl (and paren-state
  1421. (c-most-enclosing-decl-block paren-state))))
  1422. (if encl-decl
  1423. (progn
  1424. (widen)
  1425. (narrow-to-region (1+ encl-decl)
  1426. (save-excursion
  1427. (goto-char encl-decl)
  1428. (or (c-safe (forward-list)
  1429. (1- (point)))
  1430. orig-point-max)))
  1431. encl-decl)
  1432. (narrow-to-region orig-point-min orig-point-max)
  1433. nil)))
  1434. (eval-and-compile
  1435. (defmacro c-while-widening-to-decl-block (condition)
  1436. ;; Repeatedly evaluate CONDITION until it returns nil. After each
  1437. ;; evaluation, if `c-defun-tactic' is set appropriately, widen to innards
  1438. ;; of the next enclosing declaration block (e.g. namespace, class), or the
  1439. ;; buffer's original restriction.
  1440. ;;
  1441. ;; This is a very special purpose macro, which assumes the existence of
  1442. ;; several variables. It is for use only in c-beginning-of-defun and
  1443. ;; c-end-of-defun.
  1444. `(while
  1445. (and ,condition
  1446. (eq c-defun-tactic 'go-outward)
  1447. lim)
  1448. (setq paren-state (c-whack-state-after lim paren-state))
  1449. (setq lim (c-widen-to-enclosing-decl-scope
  1450. paren-state orig-point-min orig-point-max))
  1451. (setq where 'in-block))))
  1452. (defun c-beginning-of-defun (&optional arg)
  1453. "Move backward to the beginning of a defun.
  1454. Every top level declaration that contains a brace paren block is
  1455. considered to be a defun.
  1456. With a positive argument, move backward that many defuns. A negative
  1457. argument -N means move forward to the Nth following beginning. Return
  1458. t unless search stops due to beginning or end of buffer.
  1459. Unlike the built-in `beginning-of-defun' this tries to be smarter
  1460. about finding the char with open-parenthesis syntax that starts the
  1461. defun."
  1462. (interactive "p")
  1463. (or arg (setq arg 1))
  1464. (or (not (eq this-command 'c-beginning-of-defun))
  1465. (eq last-command 'c-beginning-of-defun)
  1466. (c-region-is-active-p)
  1467. (push-mark))
  1468. (c-save-buffer-state
  1469. (beginning-of-defun-function end-of-defun-function
  1470. (start (point))
  1471. (paren-state (copy-tree (c-parse-state))) ; This must not share list
  1472. ; structure with other users of c-state-cache.
  1473. (orig-point-min (point-min)) (orig-point-max (point-max))
  1474. lim ; Position of { which has been widened to.
  1475. where pos case-fold-search)
  1476. (save-restriction
  1477. (if (eq c-defun-tactic 'go-outward)
  1478. (setq lim (c-widen-to-enclosing-decl-scope ; e.g. class, namespace.
  1479. paren-state orig-point-min orig-point-max)))
  1480. ;; Move back out of any macro/comment/string we happen to be in.
  1481. (c-beginning-of-macro)
  1482. (setq pos (c-literal-limits))
  1483. (if pos (goto-char (car pos)))
  1484. (setq where (c-where-wrt-brace-construct))
  1485. (if (< arg 0)
  1486. ;; Move forward to the closing brace of a function.
  1487. (progn
  1488. (if (memq where '(at-function-end outwith-function))
  1489. (setq arg (1+ arg)))
  1490. (if (< arg 0)
  1491. (c-while-widening-to-decl-block
  1492. (< (setq arg (- (c-forward-to-nth-EOF-} (- arg) where))) 0)))
  1493. ;; Move forward to the next opening brace....
  1494. (when (and (= arg 0)
  1495. (progn
  1496. (c-while-widening-to-decl-block
  1497. (not (c-syntactic-re-search-forward "{" nil 'eob)))
  1498. (eq (char-before) ?{)))
  1499. (backward-char)
  1500. ;; ... and backward to the function header.
  1501. (c-beginning-of-decl-1)
  1502. t))
  1503. ;; Move backward to the opening brace of a function, making successively
  1504. ;; larger portions of the buffer visible as necessary.
  1505. (when (> arg 0)
  1506. (c-while-widening-to-decl-block
  1507. (> (setq arg (c-backward-to-nth-BOF-{ arg where)) 0)))
  1508. (when (eq arg 0)
  1509. ;; Go backward to this function's header.
  1510. (c-beginning-of-decl-1)
  1511. (setq pos (point))
  1512. ;; We're now there, modulo comments and whitespace.
  1513. ;; Try to be line oriented; position point at the closest
  1514. ;; preceding boi that isn't inside a comment, but if we hit
  1515. ;; the previous declaration then we use the current point
  1516. ;; instead.
  1517. (while (and (/= (point) (c-point 'boi))
  1518. (c-backward-single-comment)))
  1519. (if (/= (point) (c-point 'boi))
  1520. (goto-char pos)))
  1521. (c-keep-region-active)
  1522. (= arg 0)))))
  1523. (defun c-forward-to-nth-EOF-} (n where)
  1524. ;; Skip to the closing brace of the Nth function after point. If
  1525. ;; point is inside a function, this counts as the first. Point must be
  1526. ;; outside any comment/string or macro.
  1527. ;;
  1528. ;; N must be strictly positive.
  1529. ;; WHERE describes the position of point, one of the symbols `at-header',
  1530. ;; `in-header', `in-block', `in-trailer', `at-function-end',
  1531. ;; `outwith-function' as returned by c-where-wrt-brace-construct.
  1532. ;;
  1533. ;; If we run out of functions, leave point at EOB. Return zero on success,
  1534. ;; otherwise the number of }s still to go.
  1535. ;;
  1536. ;; This function may do hidden buffer changes.
  1537. (cond
  1538. ;; What we do to go forward over the first defun depends on where we
  1539. ;; start. We go to the closing brace of that defun, even when we go
  1540. ;; backwards to it (in a "struct foo {...} bar ;").
  1541. ((eobp))
  1542. ((eq where 'in-block)
  1543. (goto-char (c-least-enclosing-brace (c-parse-state)))
  1544. (forward-sexp)
  1545. (setq n (1- n)))
  1546. ((eq where 'in-trailer)
  1547. (c-syntactic-skip-backward "^}")
  1548. (setq n (1- n)))
  1549. ((memq where '(at-function-end outwith-function at-header in-header))
  1550. (when (c-syntactic-re-search-forward "{" nil 'eob)
  1551. (backward-char)
  1552. (forward-sexp)
  1553. (setq n (1- n))))
  1554. (t (error "c-forward-to-nth-EOF-}: `where' is %s" where)))
  1555. ;; Each time round the loop, go forward to a "}" at the outermost level.
  1556. (while (and (> n 0) (not (eobp)))
  1557. ;(c-parse-state) ; This call speeds up the following one by a factor
  1558. ; of ~6. Hmmm. 2006/4/5.
  1559. (when (c-syntactic-re-search-forward "{" nil 'eob)
  1560. (backward-char)
  1561. (forward-sexp))
  1562. (setq n (1- n)))
  1563. n)
  1564. (defun c-end-of-defun (&optional arg)
  1565. "Move forward to the end of a top level declaration.
  1566. With argument, do it that many times. Negative argument -N means move
  1567. back to Nth preceding end. Returns t unless search stops due to
  1568. beginning or end of buffer.
  1569. An end of a defun occurs right after the close-parenthesis that matches
  1570. the open-parenthesis that starts a defun; see `beginning-of-defun'."
  1571. (interactive "p")
  1572. (or arg (setq arg 1))
  1573. (or (not (eq this-command 'c-end-of-defun))
  1574. (eq last-command 'c-end-of-defun)
  1575. (c-region-is-active-p)
  1576. (push-mark))
  1577. (c-save-buffer-state
  1578. (beginning-of-defun-function end-of-defun-function
  1579. (start (point))
  1580. (paren-state (copy-tree (c-parse-state))) ; This must not share list
  1581. ; structure with other users of c-state-cache.
  1582. (orig-point-min (point-min)) (orig-point-max (point-max))
  1583. lim
  1584. where pos case-fold-search)
  1585. (save-restriction
  1586. (if (eq c-defun-tactic 'go-outward)
  1587. (setq lim (c-widen-to-enclosing-decl-scope ; e.g. class, namespace
  1588. paren-state orig-point-min orig-point-max)))
  1589. ;; Move back out of any macro/comment/string we happen to be in.
  1590. (c-beginning-of-macro)
  1591. (setq pos (c-literal-limits))
  1592. (if pos (goto-char (car pos)))
  1593. (setq where (c-where-wrt-brace-construct))
  1594. (if (< arg 0)
  1595. ;; Move backwards to the } of a function
  1596. (progn
  1597. (if (memq where '(at-header outwith-function))
  1598. (setq arg (1+ arg)))
  1599. (if (< arg 0)
  1600. (c-while-widening-to-decl-block
  1601. (< (setq arg (- (c-backward-to-nth-BOF-{ (- arg) where))) 0)))
  1602. (if (= arg 0)
  1603. (c-while-widening-to-decl-block
  1604. (progn (c-syntactic-skip-backward "^}")
  1605. (not (eq (char-before) ?}))))))
  1606. ;; Move forward to the } of a function
  1607. (if (> arg 0)
  1608. (c-while-widening-to-decl-block
  1609. (> (setq arg (c-forward-to-nth-EOF-} arg where)) 0))))
  1610. ;; Do we need to move forward from the brace to the semicolon?
  1611. (when (eq arg 0)
  1612. (if (c-in-function-trailer-p) ; after "}" of struct/enum, etc.
  1613. (c-syntactic-re-search-forward ";"))
  1614. (setq pos (point))
  1615. ;; We're there now, modulo comments and whitespace.
  1616. ;; Try to be line oriented; position point after the next
  1617. ;; newline that isn't inside a comment, but if we hit the
  1618. ;; next declaration then we use the current point instead.
  1619. (while (and (not (bolp))
  1620. (not (looking-at "\\s *$"))
  1621. (c-forward-single-comment)))
  1622. (cond ((bolp))
  1623. ((looking-at "\\s *$")
  1624. (forward-line 1))
  1625. (t
  1626. (goto-char pos))))
  1627. (c-keep-region-active)
  1628. (= arg 0))))
  1629. (defun c-defun-name ()
  1630. "Return the name of the current defun, or NIL if there isn't one.
  1631. \"Defun\" here means a function, or other top level construct
  1632. with a brace block."
  1633. (interactive)
  1634. (c-save-buffer-state
  1635. (beginning-of-defun-function end-of-defun-function
  1636. where pos name-end case-fold-search)
  1637. (save-restriction
  1638. (widen)
  1639. (save-excursion
  1640. ;; Move back out of any macro/comment/string we happen to be in.
  1641. (c-beginning-of-macro)
  1642. (setq pos (c-literal-limits))
  1643. (if pos (goto-char (car pos)))
  1644. (setq where (c-where-wrt-brace-construct))
  1645. ;; Move to the beginning of the current defun, if any, if we're not
  1646. ;; already there.
  1647. (if (eq where 'outwith-function)
  1648. nil
  1649. (unless (eq where 'at-header)
  1650. (c-backward-to-nth-BOF-{ 1 where)
  1651. (c-beginning-of-decl-1))
  1652. ;; Pick out the defun name, according to the type of defun.
  1653. (cond
  1654. ;; struct, union, enum, or similar:
  1655. ((and (looking-at c-type-prefix-key)
  1656. (progn (c-forward-token-2 2) ; over "struct foo "
  1657. (or (eq (char-after) ?\{)
  1658. (looking-at c-symbol-key)))) ; "struct foo bar ..."
  1659. (save-match-data (c-forward-token-2))
  1660. (when (eq (char-after) ?\{)
  1661. (c-backward-token-2)
  1662. (looking-at c-symbol-key))
  1663. (match-string-no-properties 0))
  1664. ((looking-at "DEFUN\\s-*(") ;"DEFUN\\_>") think of XEmacs!
  1665. ;; DEFUN ("file-name-directory", Ffile_name_directory, Sfile_name_directory, ...) ==> Ffile_name_directory
  1666. ;; DEFUN(POSIX::STREAM-LOCK, stream lockp &key BLOCK SHARED START LENGTH) ==> POSIX::STREAM-LOCK
  1667. (down-list 1)
  1668. (c-forward-syntactic-ws)
  1669. (when (eq (char-after) ?\")
  1670. (forward-sexp 1)
  1671. (c-forward-token-2)) ; over the comma and following WS.
  1672. (buffer-substring-no-properties
  1673. (point)
  1674. (progn
  1675. (c-forward-token-2)
  1676. (when (looking-at ":") ; CLISP: DEFUN(PACKAGE:LISP-SYMBOL,...)
  1677. (skip-chars-forward "^,"))
  1678. (c-backward-syntactic-ws)
  1679. (point))))
  1680. ((looking-at "DEF[a-zA-Z0-9_]* *( *\\([^, ]*\\) *,")
  1681. ;; DEFCHECKER(sysconf_arg,prefix=_SC,default=, ...) ==> sysconf_arg
  1682. ;; DEFFLAGSET(syslog_opt_flags,LOG_PID ...) ==> syslog_opt_flags
  1683. (match-string-no-properties 1))
  1684. ;; Objc selectors.
  1685. ((assq 'objc-method-intro (c-guess-basic-syntax))
  1686. (let ((bound (save-excursion (c-end-of-statement) (point)))
  1687. (kw-re (concat "\\(?:" c-symbol-key "\\)?:"))
  1688. (stretches))
  1689. (when (c-syntactic-re-search-forward c-symbol-key bound t t t)
  1690. (push (match-string-no-properties 0) stretches)
  1691. (while (c-syntactic-re-search-forward kw-re bound t t t)
  1692. (push (match-string-no-properties 0) stretches)))
  1693. (apply 'concat (nreverse stretches))))
  1694. (t
  1695. ;; Normal function or initializer.
  1696. (when (c-syntactic-re-search-forward "[{(]" nil t)
  1697. (backward-char)
  1698. (c-backward-syntactic-ws)
  1699. (when (eq (char-before) ?\=) ; struct foo bar = {0, 0} ;
  1700. (c-backward-token-2)
  1701. (c-backward-syntactic-ws))
  1702. (setq name-end (point))
  1703. (c-backward-token-2)
  1704. (buffer-substring-no-properties (point) name-end)))))))))
  1705. (defun c-declaration-limits (near)
  1706. ;; Return a cons of the beginning and end positions of the current
  1707. ;; top level declaration or macro. If point is not inside any then
  1708. ;; nil is returned, unless NEAR is non-nil in which case the closest
  1709. ;; following one is chosen instead (if there is any). The end
  1710. ;; position is at the next line, providing there is one before the
  1711. ;; declaration.
  1712. ;;
  1713. ;; This function might do hidden buffer changes.
  1714. (save-excursion
  1715. (save-restriction
  1716. (when (eq c-defun-tactic 'go-outward)
  1717. (c-narrow-to-most-enclosing-decl-block t) ; e.g. class, namespace
  1718. (or (save-restriction
  1719. (c-narrow-to-most-enclosing-decl-block nil)
  1720. ;; Note: Some code duplication in `c-beginning-of-defun' and
  1721. ;; `c-end-of-defun'.
  1722. (catch 'exit
  1723. (let ((start (point))
  1724. (paren-state (c-parse-state))
  1725. lim pos end-pos)
  1726. (unless (c-safe
  1727. (goto-char (c-least-enclosing-brace paren-state))
  1728. ;; If we moved to the outermost enclosing paren
  1729. ;; then we can use c-safe-position to set the
  1730. ;; limit. Can't do that otherwise since the
  1731. ;; earlier paren pair on paren-state might very
  1732. ;; well be part of the declaration we should go
  1733. ;; to.
  1734. (setq lim (c-safe-position (point) paren-state))
  1735. t)
  1736. ;; At top level. Make sure we aren't inside a literal.
  1737. (setq pos (c-literal-limits
  1738. (c-safe-position (point) paren-state)))
  1739. (if pos (goto-char (car pos))))
  1740. (when (c-beginning-of-macro)
  1741. (throw 'exit
  1742. (cons (point)
  1743. (save-excursion
  1744. (c-end-of-macro)
  1745. (forward-line 1)
  1746. (point)))))
  1747. (setq pos (point))
  1748. (when (or (eq (car (c-beginning-of-decl-1 lim)) 'previous)
  1749. (= pos (point)))
  1750. ;; We moved back over the previous defun. Skip to the next
  1751. ;; one. Not using c-forward-syntactic-ws here since we
  1752. ;; should not skip a macro. We can also be directly after
  1753. ;; the block in a `c-opt-block-decls-with-vars-key'
  1754. ;; declaration, but then we won't move significantly far
  1755. ;; here.
  1756. (goto-char pos)
  1757. (c-forward-comments)
  1758. (when (and near (c-beginning-of-macro))
  1759. (throw 'exit
  1760. (cons (point)
  1761. (save-excursion
  1762. (c-end-of-macro)
  1763. (forward-line 1)
  1764. (point))))))
  1765. (if (eobp) (throw 'exit nil))
  1766. ;; Check if `c-beginning-of-decl-1' put us after the block in a
  1767. ;; declaration that doesn't end there. We're searching back and
  1768. ;; forth over the block here, which can be expensive.
  1769. (setq pos (point))
  1770. (if (and c-opt-block-decls-with-vars-key
  1771. (progn
  1772. (c-backward-syntactic-ws)
  1773. (eq (char-before) ?}))
  1774. (eq (car (c-beginning-of-decl-1))
  1775. 'previous)
  1776. (save-excursion
  1777. (c-end-of-decl-1)
  1778. (and (> (point) pos)
  1779. (setq end-pos (point)))))
  1780. nil
  1781. (goto-char pos))
  1782. (if (and (not near) (> (point) start))
  1783. nil
  1784. ;; Try to be line oriented; position the limits at the
  1785. ;; closest preceding boi, and after the next newline, that
  1786. ;; isn't inside a comment, but if we hit a neighboring
  1787. ;; declaration then we instead use the exact declaration
  1788. ;; limit in that direction.
  1789. (cons (progn
  1790. (setq pos (point))
  1791. (while (and (/= (point) (c-point 'boi))
  1792. (c-backward-single-comment)))
  1793. (if (/= (point) (c-point 'boi))
  1794. pos
  1795. (point)))
  1796. (progn
  1797. (if end-pos
  1798. (goto-char end-pos)
  1799. (c-end-of-decl-1))
  1800. (setq pos (point))
  1801. (while (and (not (bolp))
  1802. (not (looking-at "\\s *$"))
  1803. (c-forward-single-comment)))
  1804. (cond ((bolp)
  1805. (point))
  1806. ((looking-at "\\s *$")
  1807. (forward-line 1)
  1808. (point))
  1809. (t
  1810. pos))))))))
  1811. (and (not near)
  1812. (goto-char (point-min))
  1813. (c-forward-decl-or-cast-1 -1 nil nil)
  1814. (eq (char-after) ?\{)
  1815. (cons (point-min) (point-max))))))))
  1816. (defun c-mark-function ()
  1817. "Put mark at end of the current top-level declaration or macro, point at beginning.
  1818. If point is not inside any then the closest following one is
  1819. chosen. Each successive call of this command extends the marked
  1820. region by one function.
  1821. A mark is left where the command started, unless the region is already active
  1822. \(in Transient Mark mode).
  1823. As opposed to \\[c-beginning-of-defun] and \\[c-end-of-defun], this
  1824. function does not require the declaration to contain a brace block."
  1825. (interactive)
  1826. (let (decl-limits case-fold-search)
  1827. (c-save-buffer-state nil
  1828. ;; We try to be line oriented, unless there are several
  1829. ;; declarations on the same line.
  1830. (if (looking-at c-syntactic-eol)
  1831. (c-backward-token-2 1 nil (c-point 'bol)))
  1832. (setq decl-limits (c-declaration-limits t)))
  1833. (if (not decl-limits)
  1834. (error "Cannot find any declaration")
  1835. (let* ((extend-region-p
  1836. (and (eq this-command 'c-mark-function)
  1837. (eq last-command 'c-mark-function)))
  1838. (push-mark-p (and (eq this-command 'c-mark-function)
  1839. (not extend-region-p)
  1840. (not (c-region-is-active-p)))))
  1841. (if push-mark-p (push-mark (point)))
  1842. (if extend-region-p
  1843. (progn
  1844. (exchange-point-and-mark)
  1845. (setq decl-limits (c-declaration-limits t))
  1846. (when (not decl-limits)
  1847. (exchange-point-and-mark)
  1848. (error "Cannot find any declaration"))
  1849. (goto-char (cdr decl-limits))
  1850. (exchange-point-and-mark))
  1851. (goto-char (car decl-limits))
  1852. (push-mark (cdr decl-limits) nil t))))))
  1853. (defun c-cpp-define-name ()
  1854. "Return the name of the current CPP macro, or NIL if we're not in one."
  1855. (interactive)
  1856. (let (case-fold-search)
  1857. (save-excursion
  1858. (and c-opt-cpp-macro-define-start
  1859. (c-beginning-of-macro)
  1860. (looking-at c-opt-cpp-macro-define-start)
  1861. (match-string-no-properties 1)))))
  1862. ;; Movement by statements.
  1863. (defun c-in-comment-line-prefix-p ()
  1864. ;; Point is within a comment. Is it also within a comment-prefix?
  1865. ;; Space at BOL which precedes a comment-prefix counts as part of it.
  1866. ;;
  1867. ;; This function might do hidden buffer changes.
  1868. (let ((here (point)))
  1869. (save-excursion
  1870. (beginning-of-line)
  1871. (skip-chars-forward " \t")
  1872. (and (looking-at c-current-comment-prefix)
  1873. (/= (match-beginning 0) (match-end 0))
  1874. (< here (match-end 0))))))
  1875. (defun c-narrow-to-comment-innards (range)
  1876. ;; Narrow to the "inside" of the comment (block) defined by range, as
  1877. ;; follows:
  1878. ;;
  1879. ;; A c-style block comment has its opening "/*" and its closing "*/" (if
  1880. ;; present) removed. A c++-style line comment retains its opening "//" but
  1881. ;; has any final NL removed. If POINT is currently outwith these innards,
  1882. ;; move it to the appropriate boundary.
  1883. ;;
  1884. ;; This narrowing simplifies the sentence movement functions, since it
  1885. ;; eliminates awkward things at the boundaries of the comment (block).
  1886. ;;
  1887. ;; This function might do hidden buffer changes.
  1888. (let* ((lit-type (c-literal-type range))
  1889. (beg (if (eq lit-type 'c) (+ (car range) 2) (car range)))
  1890. (end (if (eq lit-type 'c)
  1891. (if (and (eq (char-before (cdr range)) ?/)
  1892. (eq (char-before (1- (cdr range))) ?*))
  1893. (- (cdr range) 2)
  1894. (point-max))
  1895. (if (eq (cdr range) (point-max))
  1896. (point-max)
  1897. (- (cdr range) 1)))))
  1898. (if (> (point) end)
  1899. (goto-char end)) ; This would be done automatically by ...
  1900. (if (< (point) beg)
  1901. (goto-char beg)) ; ... narrow-to-region but is not documented.
  1902. (narrow-to-region beg end)))
  1903. (defun c-beginning-of-sentence-in-comment (range)
  1904. ;; Move backwards to the "beginning of a sentence" within the comment
  1905. ;; defined by RANGE, a cons of its starting and ending positions. If we
  1906. ;; find a BOS, return NIL. Otherwise, move point to just before the start
  1907. ;; of the comment and return T.
  1908. ;;
  1909. ;; The BOS is either text which follows a regexp match of sentence-end,
  1910. ;; or text which is a beginning of "paragraph".
  1911. ;; Comment-prefixes are treated like WS when calculating BOSes or BOPs.
  1912. ;;
  1913. ;; This code was adapted from GNU Emacs's forward-sentence in paragraphs.el.
  1914. ;; It is not a general function, but is intended only for calling from
  1915. ;; c-move-over-sentence. Not all preconditions have been explicitly stated.
  1916. ;;
  1917. ;; This function might do hidden buffer changes.
  1918. (save-match-data
  1919. (let ((start-point (point)))
  1920. (save-restriction
  1921. (c-narrow-to-comment-innards range) ; This may move point back.
  1922. (let* ((here (point))
  1923. last
  1924. (here-filler ; matches WS and comment-prefixes at point.
  1925. (concat "\\=\\(^[ \t]*\\(" c-current-comment-prefix "\\)"
  1926. "\\|[ \t\n\r\f]\\)*"))
  1927. (prefix-at-bol-here ; matches WS and prefix at BOL, just before point
  1928. (concat "^[ \t]*\\(" c-current-comment-prefix "\\)[ \t\n\r\f]*\\="))
  1929. ;; First, find the previous paragraph start, if any.
  1930. (par-beg ; point where non-WS/non-prefix text of paragraph starts.
  1931. (save-excursion
  1932. (forward-paragraph -1) ; uses cc-mode values of
  1933. ; paragraph-\(start\|separate\)
  1934. (if (> (re-search-forward here-filler nil t) here)
  1935. (goto-char here))
  1936. (when (>= (point) here)
  1937. (forward-paragraph -2)
  1938. (if (> (re-search-forward here-filler nil t) here)
  1939. (goto-char here)))
  1940. (point))))
  1941. ;; Now seek successively earlier sentence ends between PAR-BEG and
  1942. ;; HERE, until the "start of sentence" following it is earlier than
  1943. ;; HERE, or we hit PAR-BEG. Beware of comment prefixes!
  1944. (while (and (re-search-backward (c-sentence-end) par-beg 'limit)
  1945. (setq last (point))
  1946. (goto-char (match-end 0)) ; tentative beginning of sentence
  1947. (or (>= (point) here)
  1948. (and (not (bolp)) ; Found a non-blank comment-prefix?
  1949. (save-excursion
  1950. (if (re-search-backward prefix-at-bol-here nil t)
  1951. (/= (match-beginning 1) (match-end 1)))))
  1952. (progn ; Skip the crud to find a real b-o-s.
  1953. (if (c-in-comment-line-prefix-p)
  1954. (beginning-of-line))
  1955. (re-search-forward here-filler) ; always succeeds.
  1956. (>= (point) here))))
  1957. (goto-char last))
  1958. (re-search-forward here-filler)))
  1959. (if (< (point) start-point)
  1960. nil
  1961. (goto-char (car range))
  1962. t))))
  1963. (defun c-end-of-sentence-in-comment (range)
  1964. ;; Move forward to the "end of a sentence" within the comment defined by
  1965. ;; RANGE, a cons of its starting and ending positions (enclosing the opening
  1966. ;; comment delimiter and the terminating */ or newline). If we find an EOS,
  1967. ;; return NIL. Otherwise, move point to just after the end of the comment
  1968. ;; and return T.
  1969. ;;
  1970. ;; The EOS is just after the non-WS part of the next match of the regexp
  1971. ;; sentence-end. Typically, this is just after one of [.!?]. If there is
  1972. ;; no sentence-end match following point, any WS before the end of the
  1973. ;; comment will count as EOS, providing we're not already in it.
  1974. ;;
  1975. ;; This code was adapted from GNU Emacs's forward-sentence in paragraphs.el.
  1976. ;; It is not a general function, but is intended only for calling from
  1977. ;; c-move-over-sentence.
  1978. ;;
  1979. ;; This function might do hidden buffer changes.
  1980. (save-match-data
  1981. (let ((start-point (point))
  1982. ;; (lit-type (c-literal-type range)) ; Commented out, 2005/11/23, ACM
  1983. )
  1984. (save-restriction
  1985. (c-narrow-to-comment-innards range) ; This might move point forwards.
  1986. (let* ((here (point))
  1987. (par-end ; EOL position of last text in current/next paragraph.
  1988. (save-excursion
  1989. ;; The cc-mode values of paragraph-\(start\|separate\), set
  1990. ;; in c-setup-paragraph-variables, are used in the
  1991. ;; following.
  1992. (forward-paragraph 1)
  1993. (if (eq (preceding-char) ?\n) (forward-char -1))
  1994. (when (<= (point) here) ; can happen, e.g., when HERE is at EOL.
  1995. (goto-char here)
  1996. (forward-paragraph 2)
  1997. (if (eq (preceding-char) ?\n) (forward-char -1)))
  1998. (point)))
  1999. last
  2000. (prefix-at-bol-here
  2001. (concat "^[ \t]*\\(" c-current-comment-prefix "\\)\\=")))
  2002. ;; Go forward one "comment-prefix which looks like sentence-end"
  2003. ;; each time round the following:
  2004. (while (and (re-search-forward (c-sentence-end) par-end 'limit)
  2005. (progn
  2006. (setq last (point))
  2007. (skip-chars-backward " \t\n")
  2008. (or (and (not (bolp))
  2009. (re-search-backward prefix-at-bol-here nil t)
  2010. (/= (match-beginning 1) (match-end 1)))
  2011. (<= (point) here))))
  2012. (goto-char last))
  2013. ;; Take special action if we're up against the end of a comment (of
  2014. ;; either sort): Leave point just after the last non-ws text.
  2015. (if (eq (point) (point-max))
  2016. (while (or (/= (skip-chars-backward " \t\n") 0)
  2017. (and (re-search-backward prefix-at-bol-here nil t)
  2018. (/= (match-beginning 1) (match-end 1))))))))
  2019. (if (> (point) start-point)
  2020. nil
  2021. (goto-char (cdr range))
  2022. t))))
  2023. (defun c-beginning-of-sentence-in-string (range)
  2024. ;; Move backwards to the "beginning of a sentence" within the string defined
  2025. ;; by RANGE, a cons of its starting and ending positions (enclosing the
  2026. ;; string quotes). If we find a BOS, return NIL. Otherwise, move point to
  2027. ;; just before the start of the string and return T.
  2028. ;;
  2029. ;; The BOS is either the text which follows a regexp match of sentence-end
  2030. ;; or text which is a beginning of "paragraph". For the purposes of
  2031. ;; determining paragraph boundaries, escaped newlines are treated as
  2032. ;; ordinary newlines.
  2033. ;;
  2034. ;; This code was adapted from GNU Emacs's forward-sentence in paragraphs.el.
  2035. ;; It is not a general function, but is intended only for calling from
  2036. ;; c-move-over-sentence.
  2037. ;;
  2038. ;; This function might do hidden buffer changes.
  2039. (save-match-data
  2040. (let* ((here (point)) last
  2041. (end (1- (cdr range)))
  2042. (here-filler ; matches WS and escaped newlines at point.
  2043. "\\=\\([ \t\n\r\f]\\|\\\\[\n\r]\\)*")
  2044. ;; Enhance paragraph-start and paragraph-separate also to recognize
  2045. ;; blank lines terminated by escaped EOLs. IT MAY WELL BE that
  2046. ;; these values should be customizable user options, or something.
  2047. (paragraph-start c-string-par-start)
  2048. (paragraph-separate c-string-par-separate)
  2049. (par-beg ; beginning of current (or previous) paragraph.
  2050. (save-excursion
  2051. (save-restriction
  2052. (narrow-to-region (1+ (car range)) end)
  2053. (forward-paragraph -1) ; uses above values of
  2054. ; paragraph-\(start\|separate\)
  2055. (if (> (re-search-forward here-filler nil t) here)
  2056. (goto-char here))
  2057. (when (>= (point) here)
  2058. (forward-paragraph -2)
  2059. (if (> (re-search-forward here-filler nil t) here)
  2060. (goto-char here)))
  2061. (point)))))
  2062. ;; Now see if we can find a sentence end after PAR-BEG.
  2063. (while (and (re-search-backward c-sentence-end-with-esc-eol par-beg 'limit)
  2064. (setq last (point))
  2065. (goto-char (match-end 0))
  2066. (or (> (point) end)
  2067. (progn
  2068. (re-search-forward
  2069. here-filler end t) ; always succeeds. Use end rather
  2070. ; than here, in case point starts
  2071. ; beyond the closing quote.
  2072. (>= (point) here))))
  2073. (goto-char last))
  2074. (re-search-forward here-filler here t)
  2075. (if (< (point) here)
  2076. nil
  2077. (goto-char (car range))
  2078. t))))
  2079. (defun c-end-of-sentence-in-string (range)
  2080. ;; Move forward to the "end of a sentence" within the string defined by
  2081. ;; RANGE, a cons of its starting and ending positions. If we find an EOS,
  2082. ;; return NIL. Otherwise, move point to just after the end of the string
  2083. ;; and return T.
  2084. ;;
  2085. ;; The EOS is just after the non-WS part of the next match of the regexp
  2086. ;; sentence-end. Typically, this is just after one of [.!?]. If there is
  2087. ;; no sentence-end match following point, any WS before the end of the
  2088. ;; string will count as EOS, providing we're not already in it.
  2089. ;;
  2090. ;; This code was adapted from GNU Emacs's forward-sentence in paragraphs.el.
  2091. ;; It is not a general function, but is intended only for calling from
  2092. ;; c-move-over-sentence.
  2093. ;;
  2094. ;; This function might do hidden buffer changes.
  2095. (save-match-data
  2096. (let* ((here (point))
  2097. last
  2098. ;; Enhance paragraph-start and paragraph-separate to recognize
  2099. ;; blank lines terminated by escaped EOLs.
  2100. (paragraph-start c-string-par-start)
  2101. (paragraph-separate c-string-par-separate)
  2102. (par-end ; EOL position of last text in current/next paragraph.
  2103. (save-excursion
  2104. (save-restriction
  2105. (narrow-to-region (car range) (1- (cdr range)))
  2106. ;; The above values of paragraph-\(start\|separate\) are used
  2107. ;; in the following.
  2108. (forward-paragraph 1)
  2109. (setq last (point))
  2110. ;; (re-search-backward filler-here nil t) would find an empty
  2111. ;; string. Therefore we simulate it by the following:
  2112. (while (or (/= (skip-chars-backward " \t\n\r\f") 0)
  2113. (re-search-backward "\\\\\\($\\)\\=" nil t)))
  2114. (unless (> (point) here)
  2115. (goto-char last)
  2116. (forward-paragraph 1)
  2117. (while (or (/= (skip-chars-backward " \t\n\r\f") 0)
  2118. (re-search-backward "\\\\\\($\\)\\=" nil t))))
  2119. (point)))))
  2120. ;; Try to go forward a sentence.
  2121. (when (re-search-forward c-sentence-end-with-esc-eol par-end 'limit)
  2122. (setq last (point))
  2123. (while (or (/= (skip-chars-backward " \t\n") 0)
  2124. (re-search-backward "\\\\\\($\\)\\=" nil t))))
  2125. ;; Did we move a sentence, or did we hit the end of the string?
  2126. (if (> (point) here)
  2127. nil
  2128. (goto-char (cdr range))
  2129. t))))
  2130. (defun c-ascertain-preceding-literal ()
  2131. ;; Point is not in a literal (i.e. comment or string (include AWK regexp)).
  2132. ;; If a literal is the next thing (aside from whitespace) to be found before
  2133. ;; point, return a cons of its start.end positions (enclosing the
  2134. ;; delimiters). Otherwise return NIL.
  2135. ;;
  2136. ;; This function might do hidden buffer changes.
  2137. (save-excursion
  2138. (c-collect-line-comments
  2139. (let ((here (point))
  2140. pos)
  2141. (if (c-backward-single-comment)
  2142. (cons (point) (progn (c-forward-single-comment) (point)))
  2143. (save-restriction
  2144. ;; to prevent `looking-at' seeing a " at point.
  2145. (narrow-to-region (point-min) here)
  2146. (when
  2147. (or
  2148. ;; An EOL can act as an "open string" terminator in AWK.
  2149. (looking-at c-ws*-string-limit-regexp)
  2150. (and (not (bobp))
  2151. (progn (backward-char)
  2152. (looking-at c-string-limit-regexp))))
  2153. (goto-char (match-end 0)) ; just after the string terminator.
  2154. (setq pos (point))
  2155. (c-safe (c-backward-sexp 1) ; move back over the string.
  2156. (cons (point) pos)))))))))
  2157. (defun c-ascertain-following-literal ()
  2158. ;; Point is not in a literal (i.e. comment or string (include AWK regexp)).
  2159. ;; If a literal is the next thing (aside from whitespace) following point,
  2160. ;; return a cons of its start.end positions (enclosing the delimiters).
  2161. ;; Otherwise return NIL.
  2162. ;;
  2163. ;; This function might do hidden buffer changes.
  2164. (save-excursion
  2165. (c-collect-line-comments
  2166. (let (pos)
  2167. (c-skip-ws-forward)
  2168. (if (looking-at c-string-limit-regexp) ; string-delimiter.
  2169. (cons (point) (or (c-safe (progn (c-forward-sexp 1) (point)))
  2170. (point-max)))
  2171. (setq pos (point))
  2172. (if (c-forward-single-comment)
  2173. (cons pos (point))))))))
  2174. (defun c-after-statement-terminator-p () ; Should we pass in LIM here?
  2175. ;; Does point immediately follow a statement "terminator"? A virtual
  2176. ;; semicolon is regarded here as such. So is an opening brace ;-)
  2177. ;;
  2178. ;; This function might do hidden buffer changes.
  2179. (or (save-excursion
  2180. (backward-char)
  2181. (and (looking-at "[;{}]")
  2182. (not (and c-special-brace-lists ; Pike special brace lists.
  2183. (eq (char-after) ?{)
  2184. (c-looking-at-special-brace-list)))))
  2185. (c-at-vsemi-p)
  2186. ;; The following (for macros) is not strict about exactly where we are
  2187. ;; wrt white space at the end of the macro. Doesn't seem to matter too
  2188. ;; much. ACM 2004/3/29.
  2189. (let (eom)
  2190. (save-excursion
  2191. (if (c-beginning-of-macro)
  2192. (setq eom (progn (c-end-of-macro)
  2193. (point)))))
  2194. (when eom
  2195. (save-excursion
  2196. (c-forward-comments)
  2197. (>= (point) eom))))))
  2198. (defun c-back-over-illiterals (macro-start)
  2199. ;; Move backwards over code which isn't a literal (i.e. comment or string),
  2200. ;; stopping before reaching BOB or a literal or the boundary of a
  2201. ;; preprocessor statement or the "beginning of a statement". MACRO-START is
  2202. ;; the position of the '#' beginning the current preprocessor directive, or
  2203. ;; NIL if we're not in such.
  2204. ;;
  2205. ;; Return a cons (A.B), where
  2206. ;; A is NIL if we moved back to a BOS (and know it), T otherwise (we
  2207. ;; didn't move, or we hit a literal, or we're not sure about BOS).
  2208. ;; B is MACRO-BOUNDARY if we are about to cross the boundary out of or
  2209. ;; into a macro, otherwise LITERAL if we've hit a literal, otherwise NIL
  2210. ;;
  2211. ;; The total collection of returned values is as follows:
  2212. ;; (nil . nil): Found a BOS whilst remaining inside the illiterals.
  2213. ;; (t . literal): No BOS found: only a comment/string. We _might_ be at
  2214. ;; a BOS - the caller must check this.
  2215. ;; (nil . macro-boundary): only happens with non-nil macro-start. We've
  2216. ;; moved and reached the opening # of the macro.
  2217. ;; (t . macro-boundary): Every other circumstance in which we're at a
  2218. ;; macro-boundary. We might be at a BOS.
  2219. ;;
  2220. ;; Point is left either at the beginning-of-statement, or at the last non-ws
  2221. ;; code before encountering the literal/BOB or macro-boundary.
  2222. ;;
  2223. ;; Note that this function moves within either preprocessor commands
  2224. ;; (macros) or normal code, but will not cross a boundary between the two,
  2225. ;; or between two distinct preprocessor commands.
  2226. ;;
  2227. ;; Stop before `{' and after `;', `{', `}' and `};' when not followed by `}'
  2228. ;; or `)', but on the other side of the syntactic ws. Move by sexps and
  2229. ;; move into parens. Also stop before `#' when it's at boi on a line.
  2230. ;;
  2231. ;; This function might do hidden buffer changes.
  2232. (save-match-data
  2233. (let ((here (point))
  2234. last) ; marks the position of non-ws code, what'll be BOS if, say, a
  2235. ; semicolon precedes it.
  2236. (catch 'done
  2237. (while t ;; We go back one "token" each iteration of the loop.
  2238. (setq last (point))
  2239. (cond
  2240. ;; Stop at the token after a comment.
  2241. ((c-backward-single-comment) ; Also functions as backwards-ws.
  2242. (goto-char last)
  2243. (throw 'done '(t . literal)))
  2244. ;; If we've gone back over a LF, we might have moved into or out of
  2245. ;; a preprocessor line.
  2246. ((and (save-excursion
  2247. (beginning-of-line)
  2248. (re-search-forward "\\(^\\|[^\\]\\)[\n\r]" last t))
  2249. (if macro-start
  2250. (< (point) macro-start)
  2251. (c-beginning-of-macro)))
  2252. (goto-char last)
  2253. ;; Return a car of NIL ONLY if we've hit the opening # of a macro.
  2254. (throw 'done (cons (or (eq (point) here)
  2255. (not macro-start))
  2256. 'macro-boundary)))
  2257. ;; Have we found a virtual semicolon? If so, stop, unless the next
  2258. ;; statement is where we started from.
  2259. ((and (c-at-vsemi-p)
  2260. (< last here)
  2261. (not (memq (char-after last) '(?\) ?})))) ; we've moved back from ) or }
  2262. (goto-char last)
  2263. (throw 'done '(nil . nil)))
  2264. ;; Hit the beginning of the buffer/region?
  2265. ((bobp)
  2266. (if (/= here last)
  2267. (goto-char last))
  2268. (throw 'done '(nil . nil)))
  2269. ;; Move back a character.
  2270. ((progn (backward-char) nil))
  2271. ;; Stop at "{" (unless it's a PIKE special brace list.)
  2272. ((eq (char-after) ?\{)
  2273. (if (and c-special-brace-lists
  2274. (c-looking-at-special-brace-list))
  2275. (skip-syntax-backward "w_") ; Speedup only.
  2276. (if (/= here last)
  2277. (goto-char last))
  2278. (throw 'done '(nil . nil))))
  2279. ;; Have we reached the start of a macro? This always counts as
  2280. ;; BOS. (N.B. I don't think (eq (point) here) can ever be true
  2281. ;; here. FIXME!!! ACM 2004/3/29)
  2282. ((and macro-start (eq (point) macro-start))
  2283. (throw 'done (cons (eq (point) here) 'macro-boundary)))
  2284. ;; Stop at token just after "}" or ";".
  2285. ((looking-at "[;}]")
  2286. ;; If we've gone back over ;, {, or }, we're done.
  2287. (if (or (= here last)
  2288. (memq (char-after last) '(?\) ?}))) ; we've moved back from ) or }
  2289. (if (and (eq (char-before) ?}) ; If };, treat them as a unit.
  2290. (eq (char-after) ?\;))
  2291. (backward-char))
  2292. (goto-char last) ; To the statement starting after the ; or }.
  2293. (throw 'done '(nil . nil))))
  2294. ;; Stop at the token after a string.
  2295. ((looking-at c-string-limit-regexp) ; Just gone back over a string terminator?
  2296. (goto-char last)
  2297. (throw 'done '(t . literal)))
  2298. ;; Nothing special: go back word characters.
  2299. (t (skip-syntax-backward "w_")) ; Speedup only.
  2300. ))))))
  2301. (defun c-forward-over-illiterals (macro-end allow-early-stop)
  2302. ;; Move forwards over code, stopping before reaching EOB or a literal
  2303. ;; (i.e. a comment/string) or the boundary of a preprocessor statement or
  2304. ;; the "end of a statement". MACRO-END is the position of the EOL/EOB which
  2305. ;; terminates the current preprocessor directive, or NIL if we're not in
  2306. ;; such.
  2307. ;;
  2308. ;; ALLOW-EARLY-STOP is non-nil if it is permissible to return without moving
  2309. ;; forward at all, should we encounter a `{'. This is an ugly kludge, but
  2310. ;; seems unavoidable. Depending on the context this function is called
  2311. ;; from, we _sometimes_ need to stop there. Currently (2004/4/3),
  2312. ;; ALLOW-EARLY-STOP is applied only to open braces, not to virtual
  2313. ;; semicolons, or anything else.
  2314. ;;
  2315. ;; Return a cons (A.B), where
  2316. ;; A is NIL if we moved forward to an EOS, or stay at one (when
  2317. ;; ALLOW-EARLY-STOP is set), T otherwise (we hit a literal).
  2318. ;; B is 'MACRO-BOUNDARY if we are about to cross the boundary out of or
  2319. ;; into a macro, otherwise 'LITERAL if we've hit a literal, otherwise NIL
  2320. ;;
  2321. ;; Point is left either after the end-of-statement, or at the last non-ws
  2322. ;; code before encountering the literal, or the # of the preprocessor
  2323. ;; statement, or at EOB [or just after last non-WS stuff??].
  2324. ;;
  2325. ;; As a clarification of "after the end-of-statement", if a comment or
  2326. ;; whitespace follows a completed AWK statement, that statement is treated
  2327. ;; as ending just after the last non-ws character before the comment.
  2328. ;;
  2329. ;; Note that this function moves within either preprocessor commands
  2330. ;; (macros) or normal code, but not both within the same invocation.
  2331. ;;
  2332. ;; Stop before `{', `}', and `#' when it's at boi on a line, but on the
  2333. ;; other side of the syntactic ws, and after `;', `}' and `};'. Only
  2334. ;; stop before `{' if at top level or inside braces, though. Move by
  2335. ;; sexps and move into parens. Also stop at eol of lines with `#' at
  2336. ;; the boi.
  2337. ;;
  2338. ;; This function might do hidden buffer changes.
  2339. (let ((here (point))
  2340. last)
  2341. (catch 'done
  2342. (while t ;; We go one "token" forward each time round this loop.
  2343. (setq last (point))
  2344. ;; If we've moved forward to a virtual semicolon, we're done.
  2345. (if (and (> last here) ; Should we check ALLOW-EARLY-STOP, here? 2004/4/3
  2346. (c-at-vsemi-p))
  2347. (throw 'done '(nil . nil)))
  2348. (c-skip-ws-forward)
  2349. (cond
  2350. ;; Gone past the end of a macro?
  2351. ((and macro-end (> (point) macro-end))
  2352. (goto-char last)
  2353. (throw 'done (cons (eq (point) here) 'macro-boundary)))
  2354. ;; About to hit a comment?
  2355. ((save-excursion (c-forward-single-comment))
  2356. (goto-char last)
  2357. (throw 'done '(t . literal)))
  2358. ;; End of buffer?
  2359. ((eobp)
  2360. (if (/= here last)
  2361. (goto-char last))
  2362. (throw 'done '(nil . nil)))
  2363. ;; If we encounter a '{', stop just after the previous token.
  2364. ((and (eq (char-after) ?{)
  2365. (not (and c-special-brace-lists
  2366. (c-looking-at-special-brace-list)))
  2367. (or allow-early-stop (/= here last))
  2368. (save-excursion ; Is this a check that we're NOT at top level?
  2369. ;;;; NO! This seems to check that (i) EITHER we're at the top level; OR (ii) The next enclosing
  2370. ;;;; level of bracketing is a '{'. HMM. Doesn't seem to make sense.
  2371. ;;;; 2003/8/8 This might have something to do with the GCC extension "Statement Expressions", e.g.
  2372. ;;;; while ({stmt1 ; stmt2 ; exp ;}). This form excludes such Statement Expressions.
  2373. (or (not (c-safe (up-list -1) t))
  2374. (= (char-after) ?{))))
  2375. (goto-char last)
  2376. (throw 'done '(nil . nil)))
  2377. ;; End of a PIKE special brace list? If so, step over it and continue.
  2378. ((and c-special-brace-lists
  2379. (eq (char-after) ?})
  2380. (save-excursion
  2381. (and (c-safe (up-list -1) t)
  2382. (c-looking-at-special-brace-list))))
  2383. (forward-char)
  2384. (skip-syntax-forward "w_")) ; Speedup only.
  2385. ;; Have we got a '}' after having moved? If so, stop after the
  2386. ;; previous token.
  2387. ((and (eq (char-after) ?})
  2388. (/= here last))
  2389. (goto-char last)
  2390. (throw 'done '(nil . nil)))
  2391. ;; Stop if we encounter a preprocessor line. Continue if we
  2392. ;; hit a naked #
  2393. ((and c-opt-cpp-prefix
  2394. (not macro-end)
  2395. (eq (char-after) ?#)
  2396. (= (point) (c-point 'boi)))
  2397. (if (= (point) here) ; Not a macro, therefore naked #.
  2398. (forward-char)
  2399. (throw 'done '(t . macro-boundary))))
  2400. ;; Stop after a ';', '}', or "};"
  2401. ((looking-at ";\\|};?")
  2402. (goto-char (match-end 0))
  2403. (throw 'done '(nil . nil)))
  2404. ;; Found a string (this subsumes AWK regexps)?
  2405. ((looking-at c-string-limit-regexp)
  2406. (goto-char last)
  2407. (throw 'done '(t . literal)))
  2408. (t
  2409. (forward-char) ; Can't fail - we checked (eobp) earlier on.
  2410. (skip-syntax-forward "w_") ; Speedup only.
  2411. (when (and macro-end (> (point) macro-end))
  2412. (goto-char last)
  2413. (throw 'done (cons (eq (point) here) 'macro-boundary))))
  2414. )))))
  2415. (defun c-one-line-string-p (range)
  2416. ;; Is the literal defined by RANGE a string contained in a single line?
  2417. ;;
  2418. ;; This function might do hidden buffer changes.
  2419. (save-excursion
  2420. (goto-char (car range))
  2421. (and (looking-at c-string-limit-regexp)
  2422. (progn (skip-chars-forward "^\n" (cdr range))
  2423. (eq (point) (cdr range))))))
  2424. (defun c-beginning-of-statement (&optional count lim sentence-flag)
  2425. "Go to the beginning of the innermost C statement.
  2426. With prefix arg, go back N - 1 statements. If already at the
  2427. beginning of a statement then go to the beginning of the closest
  2428. preceding one, moving into nested blocks if necessary (use
  2429. \\[backward-sexp] to skip over a block). If within or next to a
  2430. comment or multiline string, move by sentences instead of statements.
  2431. When called from a program, this function takes 3 optional args: the
  2432. repetition count, a buffer position limit which is the farthest back
  2433. to search for the syntactic context, and a flag saying whether to do
  2434. sentence motion in or near comments and multiline strings.
  2435. Note that for use in programs, `c-beginning-of-statement-1' is
  2436. usually better. It has much better defined semantics than this one,
  2437. which is intended for interactive use, and might therefore change to
  2438. be more \"DWIM:ey\"."
  2439. (interactive (list (prefix-numeric-value current-prefix-arg)
  2440. nil t))
  2441. (if (< count 0)
  2442. (c-end-of-statement (- count) lim sentence-flag)
  2443. (c-save-buffer-state
  2444. ((count (or count 1))
  2445. last ; start point for going back ONE chunk. Updated each chunk movement.
  2446. (macro-fence
  2447. (save-excursion (and (not (bobp)) (c-beginning-of-macro) (point))))
  2448. res ; result from sub-function call
  2449. not-bos ; "not beginning-of-statement"
  2450. (range (c-collect-line-comments (c-literal-limits lim)))) ; (start.end) of current literal or NIL
  2451. ;; Go back one statement at each iteration of the following loop.
  2452. (while (and (/= count 0)
  2453. (or (not lim) (> (point) lim)))
  2454. ;; Go back one "chunk" each time round the following loop, stopping
  2455. ;; when we reach a statement boundary, etc.
  2456. (setq last (point))
  2457. (while
  2458. (cond ; Each arm of this cond returns NIL on reaching a desired
  2459. ; statement boundary, non-NIL otherwise.
  2460. ((bobp)
  2461. (setq count 0)
  2462. nil)
  2463. (range ; point is within or approaching a literal.
  2464. (cond
  2465. ;; Single line string or sentence-flag is null => skip the
  2466. ;; entire literal.
  2467. ((or (null sentence-flag)
  2468. (c-one-line-string-p range))
  2469. (goto-char (car range))
  2470. (setq range (c-ascertain-preceding-literal))
  2471. ;; N.B. The following is essentially testing for an AWK regexp
  2472. ;; at BOS:
  2473. ;; Was the previous non-ws thing an end of statement?
  2474. (save-excursion
  2475. (if macro-fence
  2476. (c-backward-comments)
  2477. (c-backward-syntactic-ws))
  2478. (not (or (bobp) (c-after-statement-terminator-p)))))
  2479. ;; Comment inside a statement or a multi-line string.
  2480. (t (when (setq res ; returns non-nil when we go out of the literal
  2481. (if (eq (c-literal-type range) 'string)
  2482. (c-beginning-of-sentence-in-string range)
  2483. (c-beginning-of-sentence-in-comment range)))
  2484. (setq range (c-ascertain-preceding-literal)))
  2485. res)))
  2486. ;; Non-literal code.
  2487. (t (setq res (c-back-over-illiterals macro-fence))
  2488. (setq not-bos ; "not reached beginning-of-statement".
  2489. (or (= (point) last)
  2490. (memq (char-after) '(?\) ?\}))
  2491. (and
  2492. (car res)
  2493. ;; We're at a tentative BOS. The next form goes
  2494. ;; back over WS looking for an end of previous
  2495. ;; statement.
  2496. (not (save-excursion
  2497. (if macro-fence
  2498. (c-backward-comments)
  2499. (c-backward-syntactic-ws))
  2500. (or (bobp) (c-after-statement-terminator-p)))))))
  2501. ;; Are we about to move backwards into or out of a
  2502. ;; preprocessor command? If so, locate its beginning.
  2503. (when (eq (cdr res) 'macro-boundary)
  2504. (save-excursion
  2505. (beginning-of-line)
  2506. (setq macro-fence
  2507. (and (not (bobp))
  2508. (progn (c-skip-ws-backward) (c-beginning-of-macro))
  2509. (point)))))
  2510. ;; Are we about to move backwards into a literal?
  2511. (when (memq (cdr res) '(macro-boundary literal))
  2512. (setq range (c-ascertain-preceding-literal)))
  2513. not-bos))
  2514. (setq last (point)))
  2515. (if (/= count 0) (setq count (1- count))))
  2516. (c-keep-region-active))))
  2517. (defun c-end-of-statement (&optional count lim sentence-flag)
  2518. "Go to the end of the innermost C statement.
  2519. With prefix arg, go forward N - 1 statements. Move forward to the end
  2520. of the next statement if already at end, and move into nested blocks
  2521. \(use \\[forward-sexp] to skip over a block). If within or next to a
  2522. comment or multiline string, move by sentences instead of statements.
  2523. When called from a program, this function takes 3 optional args: the
  2524. repetition count, a buffer position limit which is the farthest back
  2525. to search for the syntactic context, and a flag saying whether to do
  2526. sentence motion in or near comments and multiline strings."
  2527. (interactive (list (prefix-numeric-value current-prefix-arg)
  2528. nil t))
  2529. (setq count (or count 1))
  2530. (if (< count 0) (c-beginning-of-statement (- count) lim sentence-flag)
  2531. (c-save-buffer-state
  2532. (here ; start point for going forward ONE statement. Updated each statement.
  2533. (macro-fence
  2534. (save-excursion
  2535. (and (not (eobp)) (c-beginning-of-macro)
  2536. (progn (c-end-of-macro) (point)))))
  2537. res
  2538. (range (c-collect-line-comments (c-literal-limits lim)))) ; (start.end) of current literal or NIL
  2539. ;; Go back/forward one statement at each iteration of the following loop.
  2540. (while (and (/= count 0)
  2541. (or (not lim) (< (point) lim)))
  2542. (setq here (point)) ; ONLY HERE is HERE updated
  2543. ;; Go forward one "chunk" each time round the following loop, stopping
  2544. ;; when we reach a statement boundary, etc.
  2545. (while
  2546. (cond ; Each arm of this cond returns NIL on reaching a desired
  2547. ; statement boundary, non-NIL otherwise.
  2548. ((eobp)
  2549. (setq count 0)
  2550. nil)
  2551. (range ; point is within a literal.
  2552. (cond
  2553. ;; sentence-flag is null => skip the entire literal.
  2554. ;; or a Single line string.
  2555. ((or (null sentence-flag)
  2556. (c-one-line-string-p range))
  2557. (goto-char (cdr range))
  2558. (setq range (c-ascertain-following-literal))
  2559. ;; Is there a virtual semicolon here (e.g. for AWK)?
  2560. (not (c-at-vsemi-p)))
  2561. ;; Comment or multi-line string.
  2562. (t (when (setq res ; gets non-nil when we go out of the literal
  2563. (if (eq (c-literal-type range) 'string)
  2564. (c-end-of-sentence-in-string range)
  2565. (c-end-of-sentence-in-comment range)))
  2566. (setq range (c-ascertain-following-literal)))
  2567. ;; If we've just come forward out of a literal, check for
  2568. ;; vsemi. (N.B. AWK can't have a vsemi after a comment, but
  2569. ;; some other language may do in the future)
  2570. (and res
  2571. (not (c-at-vsemi-p))))))
  2572. ;; Non-literal code.
  2573. (t (setq res (c-forward-over-illiterals macro-fence
  2574. (> (point) here)))
  2575. ;; Are we about to move forward into or out of a
  2576. ;; preprocessor command?
  2577. (when (eq (cdr res) 'macro-boundary)
  2578. (setq macro-fence
  2579. (save-excursion
  2580. (if macro-fence
  2581. (progn
  2582. (end-of-line)
  2583. (and (not (eobp))
  2584. (progn (c-skip-ws-forward)
  2585. (c-beginning-of-macro))
  2586. (progn (c-end-of-macro)
  2587. (point))))
  2588. (and (not (eobp))
  2589. (c-beginning-of-macro)
  2590. (progn (c-end-of-macro) (point)))))))
  2591. ;; Are we about to move forward into a literal?
  2592. (when (memq (cdr res) '(macro-boundary literal))
  2593. (setq range (c-ascertain-following-literal)))
  2594. (car res))))
  2595. (if (/= count 0) (setq count (1- count))))
  2596. (c-keep-region-active))))
  2597. ;; set up electric character functions to work with pending-del,
  2598. ;; (a.k.a. delsel) mode. All symbols get the t value except
  2599. ;; the functions which delete, which gets 'supersede, and (from Emacs
  2600. ;; 25) `c-electric-brace' and `c-electric-paren' get special handling
  2601. ;; so as to work gracefully with `electric-pair-mode'.
  2602. (mapc
  2603. (function
  2604. (lambda (sym)
  2605. (put sym 'delete-selection t) ; for delsel (Emacs)
  2606. (put sym 'pending-delete t))) ; for pending-del (XEmacs)
  2607. '(c-electric-pound
  2608. c-electric-slash
  2609. c-electric-star
  2610. c-electric-semi&comma
  2611. c-electric-lt-gt
  2612. c-electric-colon))
  2613. (mapc
  2614. (function
  2615. (lambda (sym)
  2616. (put sym 'delete-selection (if (fboundp 'delete-selection-uses-region-p)
  2617. 'delete-selection-uses-region-p
  2618. t))
  2619. (put sym 'pending-delete t)))
  2620. '(c-electric-brace
  2621. c-electric-paren))
  2622. (put 'c-electric-delete 'delete-selection 'supersede) ; delsel
  2623. (put 'c-electric-delete 'pending-delete 'supersede) ; pending-del
  2624. (put 'c-electric-backspace 'delete-selection 'supersede) ; delsel
  2625. (put 'c-electric-backspace 'pending-delete 'supersede) ; pending-del
  2626. (put 'c-electric-delete-forward 'delete-selection 'supersede) ; delsel
  2627. (put 'c-electric-delete-forward 'pending-delete 'supersede) ; pending-del
  2628. ;; Inserting/indenting comments
  2629. (defun c-calc-comment-indent (entry)
  2630. ;; This function might do hidden buffer changes.
  2631. (if (symbolp entry)
  2632. (setq entry (or (assq entry c-indent-comment-alist)
  2633. (assq 'other c-indent-comment-alist)
  2634. '(default . (column . nil)))))
  2635. (let ((action (car (cdr entry)))
  2636. (value (cdr (cdr entry)))
  2637. (col (current-column)))
  2638. (cond ((eq action 'space)
  2639. (+ col value))
  2640. ((eq action 'column)
  2641. (unless value (setq value comment-column))
  2642. (if (bolp)
  2643. ;; Do not pad with one space if we're at bol.
  2644. value
  2645. (max (1+ col) value)))
  2646. ((eq action 'align)
  2647. (or (save-excursion
  2648. (beginning-of-line)
  2649. (unless (bobp)
  2650. (backward-char)
  2651. (let ((lim (c-literal-limits (c-point 'bol) t)))
  2652. (when (consp lim)
  2653. (goto-char (car lim))
  2654. (when (looking-at "/[/*]") ; FIXME!!! Adapt for AWK! (ACM, 2005/11/18)
  2655. ;; Found comment to align with.
  2656. (if (bolp)
  2657. ;; Do not pad with one space if we're at bol.
  2658. 0
  2659. (max (1+ col) (current-column))))))))
  2660. ;; Recurse to handle value as a new spec.
  2661. (c-calc-comment-indent (cdr entry)))))))
  2662. (defun c-comment-indent ()
  2663. "Used by `indent-for-comment' to create and indent comments.
  2664. See `c-indent-comment-alist' for a description."
  2665. (save-excursion
  2666. (end-of-line)
  2667. (c-save-buffer-state
  2668. ((eot (let ((lim (c-literal-limits (c-point 'bol) t)))
  2669. (or (when (consp lim)
  2670. (goto-char (car lim))
  2671. (when (looking-at "/[/*]")
  2672. (skip-chars-backward " \t")
  2673. (point)))
  2674. (progn
  2675. (skip-chars-backward " \t")
  2676. (point)))))
  2677. (line-type
  2678. (cond ((looking-at "^/[/*]")
  2679. 'anchored-comment)
  2680. ((progn (beginning-of-line)
  2681. (eq (point) eot))
  2682. 'empty-line)
  2683. ((progn (back-to-indentation)
  2684. (and (eq (char-after) ?})
  2685. (eq (point) (1- eot))))
  2686. 'end-block)
  2687. ((and (looking-at "#[ \t]*\\(endif\\|else\\)")
  2688. (eq (match-end 0) eot))
  2689. 'cpp-end-block)
  2690. (t
  2691. 'other)))
  2692. case-fold-search)
  2693. (if (and (memq line-type '(anchored-comment empty-line))
  2694. c-indent-comments-syntactically-p)
  2695. (let ((c-syntactic-context (c-guess-basic-syntax)))
  2696. ;; BOGOSITY ALERT: if we're looking at the eol, its
  2697. ;; because indent-for-comment hasn't put the comment-start
  2698. ;; in the buffer yet. this will screw up the syntactic
  2699. ;; analysis so we kludge in the necessary info. Another
  2700. ;; kludge is that if we're at the bol, then we really want
  2701. ;; to ignore any anchoring as specified by
  2702. ;; c-comment-only-line-offset since it doesn't apply here.
  2703. (if (eolp)
  2704. (c-add-syntax 'comment-intro))
  2705. (let ((c-comment-only-line-offset
  2706. (if (consp c-comment-only-line-offset)
  2707. c-comment-only-line-offset
  2708. (cons c-comment-only-line-offset
  2709. c-comment-only-line-offset))))
  2710. (c-get-syntactic-indentation c-syntactic-context)))
  2711. (goto-char eot)
  2712. (c-calc-comment-indent line-type)))))
  2713. ;; used by outline-minor-mode
  2714. (defun c-outline-level ()
  2715. (let (buffer-invisibility-spec);; This so that `current-column' DTRT
  2716. ;; in otherwise-hidden text.
  2717. (save-excursion
  2718. (skip-chars-forward "\t ")
  2719. (current-column))))
  2720. ;; Movement by CPP conditionals.
  2721. (defun c-up-conditional (count)
  2722. "Move back to the containing preprocessor conditional, leaving mark behind.
  2723. A prefix argument acts as a repeat count. With a negative argument,
  2724. move forward to the end of the containing preprocessor conditional.
  2725. \"#elif\" is treated like \"#else\" followed by \"#if\", so the
  2726. function stops at them when going backward, but not when going
  2727. forward."
  2728. (interactive "p")
  2729. (let ((new-point (c-scan-conditionals (- count) -1)))
  2730. (push-mark)
  2731. (goto-char new-point))
  2732. (c-keep-region-active))
  2733. (defun c-up-conditional-with-else (count)
  2734. "Move back to the containing preprocessor conditional, including \"#else\".
  2735. Just like `c-up-conditional', except it also stops at \"#else\"
  2736. directives."
  2737. (interactive "p")
  2738. (let ((new-point (c-scan-conditionals (- count) -1 t)))
  2739. (push-mark)
  2740. (goto-char new-point))
  2741. (c-keep-region-active))
  2742. (defun c-down-conditional (count)
  2743. "Move forward into the next preprocessor conditional, leaving mark behind.
  2744. A prefix argument acts as a repeat count. With a negative argument,
  2745. move backward into the previous preprocessor conditional.
  2746. \"#elif\" is treated like \"#else\" followed by \"#if\", so the
  2747. function stops at them when going forward, but not when going
  2748. backward."
  2749. (interactive "p")
  2750. (let ((new-point (c-scan-conditionals count 1)))
  2751. (push-mark)
  2752. (goto-char new-point))
  2753. (c-keep-region-active))
  2754. (defun c-down-conditional-with-else (count)
  2755. "Move forward into the next preprocessor conditional, including \"#else\".
  2756. Just like `c-down-conditional', except it also stops at \"#else\"
  2757. directives."
  2758. (interactive "p")
  2759. (let ((new-point (c-scan-conditionals count 1 t)))
  2760. (push-mark)
  2761. (goto-char new-point))
  2762. (c-keep-region-active))
  2763. (defun c-backward-conditional (count &optional target-depth with-else)
  2764. "Move back across a preprocessor conditional, leaving mark behind.
  2765. A prefix argument acts as a repeat count. With a negative argument,
  2766. move forward across a preprocessor conditional.
  2767. The optional arguments TARGET-DEPTH and WITH-ELSE are historical,
  2768. and have the same meanings as in `c-scan-conditionals'. If you
  2769. are calling c-forward-conditional from a program, you might want
  2770. to call `c-scan-conditionals' directly instead."
  2771. (interactive "p")
  2772. (let ((new-point (c-scan-conditionals (- count) target-depth with-else)))
  2773. (push-mark)
  2774. (goto-char new-point))
  2775. (c-keep-region-active))
  2776. (defun c-forward-conditional (count &optional target-depth with-else)
  2777. "Move forward across a preprocessor conditional, leaving mark behind.
  2778. A prefix argument acts as a repeat count. With a negative argument,
  2779. move backward across a preprocessor conditional.
  2780. If there aren't enough conditionals after \(or before) point, an
  2781. error is signaled.
  2782. \"#elif\" is treated like \"#else\" followed by \"#if\", except that
  2783. the nesting level isn't changed when tracking subconditionals.
  2784. The optional arguments TARGET-DEPTH and WITH-ELSE are historical,
  2785. and have the same meanings as in `c-scan-conditionals'. If you
  2786. are calling c-forward-conditional from a program, you might want
  2787. to call `c-scan-conditionals' directly instead."
  2788. (interactive "p")
  2789. (let ((new-point (c-scan-conditionals count target-depth with-else)))
  2790. (push-mark)
  2791. (goto-char new-point)))
  2792. (defun c-scan-conditionals (count &optional target-depth with-else)
  2793. "Scan forward across COUNT preprocessor conditionals.
  2794. With a negative argument, scan backward across preprocessor
  2795. conditionals. Return the end position. Point is not moved.
  2796. If there aren't enough preprocessor conditionals, throw an error.
  2797. \"#elif\" is treated like \"#else\" followed by \"#if\", except that
  2798. the nesting level isn't changed when tracking subconditionals.
  2799. The optional argument TARGET-DEPTH specifies the wanted nesting depth
  2800. after each scan. E.g. if TARGET-DEPTH is -1, the end position will be
  2801. outside the enclosing conditional. A non-integer non-nil TARGET-DEPTH
  2802. counts as -1.
  2803. If the optional argument WITH-ELSE is non-nil, \"#else\" directives
  2804. are treated as conditional clause limits. Normally they are ignored."
  2805. (let* ((forward (> count 0))
  2806. (increment (if forward -1 1))
  2807. (search-function (if forward 're-search-forward 're-search-backward))
  2808. new case-fold-search)
  2809. (unless (integerp target-depth)
  2810. (setq target-depth (if target-depth -1 0)))
  2811. (save-excursion
  2812. (while (/= count 0)
  2813. (let ((depth 0)
  2814. ;; subdepth is the depth in "uninteresting" subtrees,
  2815. ;; i.e. those that takes us farther from the target
  2816. ;; depth instead of closer.
  2817. (subdepth 0)
  2818. found)
  2819. (save-excursion
  2820. ;; Find the "next" significant line in the proper direction.
  2821. (while (and (not found)
  2822. ;; Rather than searching for a # sign that
  2823. ;; comes at the beginning of a line aside from
  2824. ;; whitespace, search first for a string
  2825. ;; starting with # sign. Then verify what
  2826. ;; precedes it. This is faster on account of
  2827. ;; the fastmap feature of the regexp matcher.
  2828. (funcall search-function
  2829. "#[ \t]*\\(if\\|elif\\|endif\\|else\\)"
  2830. nil t))
  2831. (beginning-of-line)
  2832. ;; Now verify it is really a preproc line.
  2833. (if (looking-at "^[ \t]*#[ \t]*\\(if\\|elif\\|endif\\|else\\)")
  2834. (let (dchange (directive (match-string 1)))
  2835. (cond ((string= directive "if")
  2836. (setq dchange (- increment)))
  2837. ((string= directive "endif")
  2838. (setq dchange increment))
  2839. ((= subdepth 0)
  2840. ;; When we're not in an "uninteresting"
  2841. ;; subtree, we might want to act on "elif"
  2842. ;; and "else" too.
  2843. (if (cond (with-else
  2844. ;; Always move toward the target depth.
  2845. (setq dchange
  2846. (if (> target-depth 0) 1 -1)))
  2847. ((string= directive "elif")
  2848. (setq dchange (- increment))))
  2849. ;; Ignore the change if it'd take us
  2850. ;; into an "uninteresting" subtree.
  2851. (if (eq (> dchange 0) (<= target-depth 0))
  2852. (setq dchange nil)))))
  2853. (when dchange
  2854. (when (or (/= subdepth 0)
  2855. (eq (> dchange 0) (<= target-depth 0)))
  2856. (setq subdepth (+ subdepth dchange)))
  2857. (setq depth (+ depth dchange))
  2858. ;; If we are trying to move across, and we find an
  2859. ;; end before we find a beginning, get an error.
  2860. (if (and (< depth target-depth) (< dchange 0))
  2861. (error (if forward
  2862. "No following conditional at this level"
  2863. "No previous conditional at this level"))))
  2864. ;; When searching forward, start from next line so
  2865. ;; that we don't find the same line again.
  2866. (if forward (forward-line 1))
  2867. ;; We found something if we've arrived at the
  2868. ;; target depth.
  2869. (if (and dchange (= depth target-depth))
  2870. (setq found (point))))
  2871. ;; else
  2872. (if forward (forward-line 1)))))
  2873. (or found
  2874. (error "No containing preprocessor conditional"))
  2875. (goto-char (setq new found)))
  2876. (setq count (+ count increment))))
  2877. (c-keep-region-active)
  2878. new))
  2879. ;; commands to indent lines, regions, defuns, and expressions
  2880. (defun c-indent-command (&optional arg)
  2881. "Indent current line as C code, and/or insert some whitespace.
  2882. If `c-tab-always-indent' is t, always just indent the current line.
  2883. If nil, indent the current line only if point is at the left margin or
  2884. in the line's indentation; otherwise insert some whitespace[*]. If
  2885. other than nil or t, then some whitespace[*] is inserted only within
  2886. literals (comments and strings), but the line is always reindented.
  2887. If `c-syntactic-indentation' is t, indentation is done according to
  2888. the syntactic context. A numeric argument, regardless of its value,
  2889. means indent rigidly all the lines of the expression starting after
  2890. point so that this line becomes properly indented. The relative
  2891. indentation among the lines of the expression is preserved.
  2892. If `c-syntactic-indentation' is nil, the line is just indented one
  2893. step according to `c-basic-offset'. In this mode, a numeric argument
  2894. indents a number of such steps, positive or negative, and an empty
  2895. prefix argument is equivalent to -1.
  2896. [*] The amount and kind of whitespace inserted is controlled by the
  2897. variable `c-insert-tab-function', which is called to do the actual
  2898. insertion of whitespace. Normally the function in this variable
  2899. just inserts a tab character, or the equivalent number of spaces,
  2900. depending on the variable `indent-tabs-mode'."
  2901. (interactive "P")
  2902. (let ((indent-function
  2903. (if c-syntactic-indentation
  2904. (symbol-function 'indent-according-to-mode)
  2905. (lambda ()
  2906. (let ((c-macro-start c-macro-start)
  2907. (steps (if (equal arg '(4))
  2908. -1
  2909. (prefix-numeric-value arg))))
  2910. (c-shift-line-indentation (* steps c-basic-offset))
  2911. (when (and c-auto-align-backslashes
  2912. (save-excursion
  2913. (end-of-line)
  2914. (eq (char-before) ?\\))
  2915. (c-query-and-set-macro-start))
  2916. ;; Realign the line continuation backslash if inside a macro.
  2917. (c-backslash-region (point) (point) nil t)))
  2918. ))))
  2919. (if (and c-syntactic-indentation arg)
  2920. ;; If c-syntactic-indentation and got arg, always indent this
  2921. ;; line as C and shift remaining lines of expression the same
  2922. ;; amount.
  2923. (let ((shift-amt (save-excursion
  2924. (back-to-indentation)
  2925. (current-column)))
  2926. beg end)
  2927. (c-indent-line)
  2928. (setq shift-amt (- (save-excursion
  2929. (back-to-indentation)
  2930. (current-column))
  2931. shift-amt))
  2932. (save-excursion
  2933. (if (eq c-tab-always-indent t)
  2934. (beginning-of-line)) ; FIXME!!! What is this here for? ACM 2005/10/31
  2935. (setq beg (point))
  2936. (c-forward-sexp 1)
  2937. (setq end (point))
  2938. (goto-char beg)
  2939. (forward-line 1)
  2940. (setq beg (point)))
  2941. (if (> end beg)
  2942. (indent-code-rigidly beg end shift-amt "#")))
  2943. ;; Else use c-tab-always-indent to determine behavior.
  2944. (cond
  2945. ;; CASE 1: indent when at column zero or in line's indentation,
  2946. ;; otherwise insert a tab
  2947. ((not c-tab-always-indent)
  2948. (if (save-excursion
  2949. (skip-chars-backward " \t")
  2950. (not (bolp)))
  2951. (funcall c-insert-tab-function)
  2952. (funcall indent-function)))
  2953. ;; CASE 2: just indent the line
  2954. ((eq c-tab-always-indent t)
  2955. (funcall indent-function))
  2956. ;; CASE 3: if in a literal, insert a tab, but always indent the
  2957. ;; line
  2958. (t
  2959. (if (c-save-buffer-state () (c-in-literal))
  2960. (funcall c-insert-tab-function))
  2961. (funcall indent-function)
  2962. )))))
  2963. (defun c-indent-exp (&optional shutup-p)
  2964. "Indent each line in the balanced expression following point syntactically.
  2965. If optional SHUTUP-P is non-nil, no errors are signaled if no
  2966. balanced expression is found."
  2967. (interactive "*P")
  2968. (let ((here (point-marker))
  2969. end)
  2970. (set-marker-insertion-type here t)
  2971. (unwind-protect
  2972. (let ((start (save-restriction
  2973. ;; Find the closest following open paren that
  2974. ;; ends on another line.
  2975. (narrow-to-region (point-min) (c-point 'eol))
  2976. (let (beg (end (point)))
  2977. (while (and (setq beg (c-down-list-forward end))
  2978. (setq end (c-up-list-forward beg))))
  2979. (and beg
  2980. (eq (char-syntax (char-before beg)) ?\()
  2981. (1- beg))))))
  2982. ;; sanity check
  2983. (if (not start)
  2984. (unless shutup-p
  2985. (error "Cannot find start of balanced expression to indent"))
  2986. (goto-char start)
  2987. (setq end (c-safe (scan-sexps (point) 1)))
  2988. (if (not end)
  2989. (unless shutup-p
  2990. (error "Cannot find end of balanced expression to indent"))
  2991. (forward-line)
  2992. (if (< (point) end)
  2993. (c-indent-region (point) end)))))
  2994. (goto-char here)
  2995. (set-marker here nil))))
  2996. (defun c-indent-defun ()
  2997. "Indent the current top-level declaration or macro syntactically.
  2998. In the macro case this also has the effect of realigning any line
  2999. continuation backslashes, unless `c-auto-align-backslashes' is nil."
  3000. (interactive "*")
  3001. (let ((here (point-marker)) decl-limits case-fold-search)
  3002. (unwind-protect
  3003. (progn
  3004. (c-save-buffer-state nil
  3005. ;; We try to be line oriented, unless there are several
  3006. ;; declarations on the same line.
  3007. (if (looking-at c-syntactic-eol)
  3008. (c-backward-token-2 1 nil (c-point 'bol))
  3009. (c-forward-token-2 0 nil (c-point 'eol)))
  3010. (setq decl-limits (c-declaration-limits nil)))
  3011. (if decl-limits
  3012. (c-indent-region (car decl-limits)
  3013. (cdr decl-limits))))
  3014. (goto-char here)
  3015. (set-marker here nil))))
  3016. (defun c-indent-region (start end &optional quiet)
  3017. "Indent syntactically every line whose first char is between START
  3018. and END inclusive. If the optional argument QUIET is non-nil then no
  3019. syntactic errors are reported, even if `c-report-syntactic-errors' is
  3020. non-nil."
  3021. (save-excursion
  3022. (goto-char end)
  3023. (skip-chars-backward " \t\n\r\f\v")
  3024. (setq end (point))
  3025. (goto-char start)
  3026. ;; Advance to first nonblank line.
  3027. (beginning-of-line)
  3028. (skip-chars-forward " \t\n\r\f\v")
  3029. (setq start (point))
  3030. (beginning-of-line)
  3031. (setq c-parsing-error
  3032. (or (let ((endmark (copy-marker end))
  3033. (c-parsing-error nil)
  3034. ;; shut up any echo msgs on indiv lines
  3035. (c-echo-syntactic-information-p nil)
  3036. (ml-macro-start ; Start pos of multi-line macro.
  3037. (and (c-save-buffer-state ()
  3038. (save-excursion (c-beginning-of-macro)))
  3039. (eq (char-before (c-point 'eol)) ?\\)
  3040. start))
  3041. (c-fix-backslashes nil)
  3042. syntax)
  3043. (unwind-protect
  3044. (progn
  3045. (c-progress-init start end 'c-indent-region)
  3046. (while (and (bolp) ;; One line each time round the loop.
  3047. (not (eobp))
  3048. (< (point) endmark))
  3049. ;; update progress
  3050. (c-progress-update)
  3051. ;; skip empty lines
  3052. (unless (or (looking-at "\\s *$")
  3053. (and ml-macro-start (looking-at "\\s *\\\\$")))
  3054. ;; Get syntax and indent.
  3055. (c-save-buffer-state nil
  3056. (setq syntax (c-guess-basic-syntax)))
  3057. (c-indent-line syntax t t))
  3058. (if ml-macro-start
  3059. ;; End of current multi-line macro?
  3060. (when (and c-auto-align-backslashes
  3061. (not (eq (char-before (c-point 'eol)) ?\\)))
  3062. ;; Fixup macro backslashes.
  3063. (c-backslash-region ml-macro-start (c-point 'bonl) nil)
  3064. (setq ml-macro-start nil))
  3065. ;; New multi-line macro?
  3066. (if (and (assq 'cpp-macro syntax)
  3067. (eq (char-before (c-point 'eol)) ?\\))
  3068. (setq ml-macro-start (point))))
  3069. (forward-line))
  3070. (if (and ml-macro-start c-auto-align-backslashes)
  3071. (c-backslash-region ml-macro-start (c-point 'bopl) nil t)))
  3072. (set-marker endmark nil)
  3073. (c-progress-fini 'c-indent-region))
  3074. (c-echo-parsing-error quiet))
  3075. c-parsing-error))))
  3076. (defun c-fn-region-is-active-p ()
  3077. ;; Function version of the macro for use in places that aren't
  3078. ;; compiled, e.g. in the menus.
  3079. (c-region-is-active-p))
  3080. (defun c-indent-line-or-region (&optional arg region)
  3081. "Indent active region, current line, or block starting on this line.
  3082. In Transient Mark mode, when the region is active, reindent the region.
  3083. Otherwise, with a prefix argument, rigidly reindent the expression
  3084. starting on the current line.
  3085. Otherwise reindent just the current line."
  3086. (interactive
  3087. (list current-prefix-arg (c-region-is-active-p)))
  3088. (if region
  3089. (c-indent-region (region-beginning) (region-end))
  3090. (c-indent-command arg)))
  3091. ;; for progress reporting
  3092. (defvar c-progress-info nil)
  3093. (defun c-progress-init (start end context)
  3094. (cond
  3095. ;; Be silent
  3096. ((not c-progress-interval))
  3097. ;; Start the progress update messages. If this Emacs doesn't have
  3098. ;; a built-in timer, just be dumb about it.
  3099. ((not (fboundp 'current-time))
  3100. (message "Indenting region... (this may take a while)"))
  3101. ;; If progress has already been initialized, do nothing. otherwise
  3102. ;; initialize the counter with a vector of:
  3103. ;; [start end lastsec context]
  3104. (c-progress-info)
  3105. (t (setq c-progress-info (vector start
  3106. (save-excursion
  3107. (goto-char end)
  3108. (point-marker))
  3109. (nth 1 (current-time))
  3110. context))
  3111. (message "Indenting region..."))
  3112. ))
  3113. (defun c-progress-update ()
  3114. (if (not (and c-progress-info c-progress-interval))
  3115. nil
  3116. (let ((now (nth 1 (current-time)))
  3117. (start (aref c-progress-info 0))
  3118. (end (aref c-progress-info 1))
  3119. (lastsecs (aref c-progress-info 2)))
  3120. ;; should we update? currently, update happens every 2 seconds,
  3121. ;; what's the right value?
  3122. (if (< c-progress-interval (- now lastsecs))
  3123. (progn
  3124. (message "Indenting region... (%d%% complete)"
  3125. (floor (* 100.0 (- (point) start)) (- end start)))
  3126. (aset c-progress-info 2 now)))
  3127. )))
  3128. (defun c-progress-fini (context)
  3129. (if (not c-progress-interval)
  3130. nil
  3131. (if (or (eq context (aref c-progress-info 3))
  3132. (eq context t))
  3133. (progn
  3134. (set-marker (aref c-progress-info 1) nil)
  3135. (setq c-progress-info nil)
  3136. (message "Indenting region... done")))))
  3137. ;;; This page handles insertion and removal of backslashes for C macros.
  3138. (defun c-backslash-region (from to delete-flag &optional line-mode)
  3139. "Insert, align, or delete end-of-line backslashes on the lines in the region.
  3140. With no argument, inserts backslashes and aligns existing backslashes.
  3141. With an argument, deletes the backslashes. The backslash alignment is
  3142. done according to the settings in `c-backslash-column',
  3143. `c-backslash-max-column' and `c-auto-align-backslashes'.
  3144. This function does not modify blank lines at the start of the region.
  3145. If the region ends at the start of a line and the macro doesn't
  3146. continue below it, the backslash (if any) at the end of the previous
  3147. line is deleted.
  3148. You can put the region around an entire macro definition and use this
  3149. command to conveniently insert and align the necessary backslashes."
  3150. (interactive "*r\nP")
  3151. (let ((endmark (make-marker))
  3152. ;; Keep the backslash trimming functions from changing the
  3153. ;; whitespace around point, since in this case it's only the
  3154. ;; position of point that tells the indentation of the line.
  3155. (point-pos (if (save-excursion
  3156. (skip-chars-backward " \t")
  3157. (and (bolp) (looking-at "[ \t]*\\\\?$")))
  3158. (point-marker)
  3159. (point-min)))
  3160. column longest-line-col bs-col-after-end)
  3161. (save-excursion
  3162. (goto-char to)
  3163. (if (and (not line-mode) (bobp))
  3164. ;; Nothing to do if to is at bob, since we should back up
  3165. ;; and there's no line to back up to.
  3166. nil
  3167. (when (and (not line-mode) (bolp))
  3168. ;; Do not back up the to line if line-mode is set, to make
  3169. ;; e.g. c-newline-and-indent consistent regardless whether
  3170. ;; the (newline) call leaves point at bol or not.
  3171. (backward-char)
  3172. (setq to (point)))
  3173. (if delete-flag
  3174. (progn
  3175. (set-marker endmark (point))
  3176. (goto-char from)
  3177. (c-delete-backslashes-forward endmark point-pos))
  3178. ;; Set bs-col-after-end to the column of any backslash
  3179. ;; following the region, or nil if there is none.
  3180. (setq bs-col-after-end
  3181. (and (progn (end-of-line)
  3182. (eq (char-before) ?\\))
  3183. (= (forward-line 1) 0)
  3184. (progn (end-of-line)
  3185. (eq (char-before) ?\\))
  3186. (1- (current-column))))
  3187. (when line-mode
  3188. ;; Back up the to line if line-mode is set, since the line
  3189. ;; after the newly inserted line break should not be
  3190. ;; touched in c-newline-and-indent.
  3191. (setq to (max from (or (c-safe (c-point 'eopl)) from)))
  3192. (unless bs-col-after-end
  3193. ;; Set bs-col-after-end to non-nil in any case, since we
  3194. ;; do not want to delete the backslash at the last line.
  3195. (setq bs-col-after-end t)))
  3196. (if (and line-mode
  3197. (not c-auto-align-backslashes))
  3198. (goto-char from)
  3199. ;; Compute the smallest column number past the ends of all
  3200. ;; the lines.
  3201. (setq longest-line-col 0)
  3202. (goto-char to)
  3203. (if bs-col-after-end
  3204. ;; Include one more line in the max column
  3205. ;; calculation, since the to line will be backslashed
  3206. ;; too.
  3207. (forward-line 1))
  3208. (end-of-line)
  3209. (while (and (>= (point) from)
  3210. (progn
  3211. (if (eq (char-before) ?\\)
  3212. (forward-char -1))
  3213. (skip-chars-backward " \t")
  3214. (setq longest-line-col (max longest-line-col
  3215. (1+ (current-column))))
  3216. (beginning-of-line)
  3217. (not (bobp))))
  3218. (backward-char))
  3219. ;; Try to align with surrounding backslashes.
  3220. (goto-char from)
  3221. (beginning-of-line)
  3222. (if (and (not (bobp))
  3223. (progn (backward-char)
  3224. (eq (char-before) ?\\)))
  3225. (progn
  3226. (setq column (1- (current-column)))
  3227. (if (numberp bs-col-after-end)
  3228. ;; Both a preceding and a following backslash.
  3229. ;; Choose the greatest of them.
  3230. (setq column (max column bs-col-after-end)))
  3231. (goto-char from))
  3232. ;; No preceding backslash. Try to align with one
  3233. ;; following the region. Disregard the backslash at the
  3234. ;; to line since it's likely to be bogus (e.g. when
  3235. ;; called from c-newline-and-indent).
  3236. (if (numberp bs-col-after-end)
  3237. (setq column bs-col-after-end))
  3238. ;; Don't modify blank lines at start of region.
  3239. (goto-char from)
  3240. (while (and (< (point) to) (bolp) (eolp))
  3241. (forward-line 1)))
  3242. (if (and column (< column longest-line-col))
  3243. ;; Don't try to align with surrounding backslashes if
  3244. ;; any line is too long.
  3245. (setq column nil))
  3246. (unless column
  3247. ;; Impose minimum limit and tab width alignment only if
  3248. ;; we can't align with surrounding backslashes.
  3249. (if (> (% longest-line-col tab-width) 0)
  3250. (setq longest-line-col
  3251. (* (/ (+ longest-line-col tab-width -1)
  3252. tab-width)
  3253. tab-width)))
  3254. (setq column (max c-backslash-column
  3255. longest-line-col)))
  3256. ;; Always impose maximum limit.
  3257. (setq column (min column c-backslash-max-column)))
  3258. (if bs-col-after-end
  3259. ;; Add backslashes on all lines if the macro continues
  3260. ;; after the to line.
  3261. (progn
  3262. (set-marker endmark to)
  3263. (c-append-backslashes-forward endmark column point-pos))
  3264. ;; Add backslashes on all lines except the last, and
  3265. ;; remove any on the last line.
  3266. (if (save-excursion
  3267. (goto-char to)
  3268. (beginning-of-line)
  3269. (if (not (bobp))
  3270. (set-marker endmark (1- (point)))))
  3271. (progn
  3272. (c-append-backslashes-forward endmark column point-pos)
  3273. ;; The function above leaves point on the line
  3274. ;; following endmark.
  3275. (set-marker endmark (point)))
  3276. (set-marker endmark to))
  3277. (c-delete-backslashes-forward endmark point-pos)))))
  3278. (set-marker endmark nil)
  3279. (if (markerp point-pos)
  3280. (set-marker point-pos nil))))
  3281. (defun c-append-backslashes-forward (to-mark column point-pos)
  3282. (let ((state (parse-partial-sexp (c-point 'bol) (point))))
  3283. (if column
  3284. (while
  3285. (and
  3286. (<= (point) to-mark)
  3287. (let ((start (point)) (inserted nil) end col)
  3288. (end-of-line)
  3289. (unless (eq (char-before) ?\\)
  3290. (insert ?\\)
  3291. (setq inserted t))
  3292. (setq state (parse-partial-sexp
  3293. start (point) nil nil state))
  3294. (backward-char)
  3295. (setq col (current-column))
  3296. ;; Avoid unnecessary changes of the buffer.
  3297. (cond ((and (not inserted) (nth 3 state))
  3298. ;; Don't realign backslashes in string literals
  3299. ;; since that would change them.
  3300. )
  3301. ((< col column)
  3302. (delete-region
  3303. (point)
  3304. (progn
  3305. (skip-chars-backward
  3306. " \t" (if (>= (point) point-pos) point-pos))
  3307. (point)))
  3308. (indent-to column))
  3309. ((and (= col column)
  3310. (memq (char-before) '(?\ ?\t))))
  3311. ((progn
  3312. (setq end (point))
  3313. (or (/= (skip-chars-backward
  3314. " \t" (if (>= (point) point-pos) point-pos))
  3315. -1)
  3316. (/= (char-after) ?\ )))
  3317. (delete-region (point) end)
  3318. (indent-to column 1)))
  3319. (zerop (forward-line 1)))
  3320. (bolp))) ; forward-line has funny behavior at eob.
  3321. ;; Make sure there are backslashes with at least one space in
  3322. ;; front of them.
  3323. (while
  3324. (and
  3325. (<= (point) to-mark)
  3326. (let ((start (point)))
  3327. (end-of-line)
  3328. (setq state (parse-partial-sexp
  3329. start (point) nil nil state))
  3330. (if (eq (char-before) ?\\)
  3331. (unless (nth 3 state)
  3332. (backward-char)
  3333. (unless (and (memq (char-before) '(?\ ?\t))
  3334. (/= (point) point-pos))
  3335. (insert ?\ )))
  3336. (if (and (memq (char-before) '(?\ ?\t))
  3337. (/= (point) point-pos))
  3338. (insert ?\\)
  3339. (insert ?\ ?\\)))
  3340. (zerop (forward-line 1)))
  3341. (bolp)))))) ; forward-line has funny behavior at eob.
  3342. (defun c-delete-backslashes-forward (to-mark point-pos)
  3343. (while
  3344. (and (<= (point) to-mark)
  3345. (progn
  3346. (end-of-line)
  3347. (if (eq (char-before) ?\\)
  3348. (delete-region
  3349. (point)
  3350. (progn (backward-char)
  3351. (skip-chars-backward " \t" (if (>= (point) point-pos)
  3352. point-pos))
  3353. (point))))
  3354. (zerop (forward-line 1)))
  3355. (bolp)))) ; forward-line has funny behavior at eob.
  3356. ;;; Line breaking and paragraph filling.
  3357. (defvar c-auto-fill-prefix t)
  3358. (defvar c-lit-limits nil)
  3359. (defvar c-lit-type nil)
  3360. ;; The filling code is based on a simple theory; leave the intricacies
  3361. ;; of the text handling to the currently active mode for that
  3362. ;; (e.g. adaptive-fill-mode or filladapt-mode) and do as little as
  3363. ;; possible to make them work correctly wrt the comment and string
  3364. ;; separators, one-line paragraphs etc. Unfortunately, when it comes
  3365. ;; to it, there's quite a lot of special cases to handle which makes
  3366. ;; the code anything but simple. The intention is that it will work
  3367. ;; with any well-written text filling package that preserves a fill
  3368. ;; prefix.
  3369. ;;
  3370. ;; We temporarily mask comment starters and enders as necessary for
  3371. ;; the filling code to do its job on a seemingly normal text block.
  3372. ;; We do _not_ mask the fill prefix, so it's up to the filling code to
  3373. ;; preserve it correctly (especially important when filling C++ style
  3374. ;; line comments). By default, we set up and use adaptive-fill-mode,
  3375. ;; which is standard in all supported Emacs flavors.
  3376. (defun c-guess-fill-prefix (lit-limits lit-type)
  3377. ;; Determine the appropriate comment fill prefix for a block or line
  3378. ;; comment. Return a cons of the prefix string and the column where
  3379. ;; it ends. If fill-prefix is set, it'll override. Note that this
  3380. ;; function also uses the value of point in some heuristics.
  3381. ;;
  3382. ;; This function might do hidden buffer changes.
  3383. (let* ((here (point))
  3384. (prefix-regexp (concat "[ \t]*\\("
  3385. c-current-comment-prefix
  3386. "\\)[ \t]*"))
  3387. (comment-start-regexp (if (eq lit-type 'c++)
  3388. prefix-regexp
  3389. comment-start-skip))
  3390. prefix-line comment-prefix res comment-text-end)
  3391. (cond
  3392. (fill-prefix
  3393. (setq res (cons fill-prefix
  3394. ;; Ugly way of getting the column after the fill
  3395. ;; prefix; it'd be nice with a current-column
  3396. ;; that works on strings..
  3397. (let ((start (point)))
  3398. (unwind-protect
  3399. (progn
  3400. (insert-and-inherit "\n" fill-prefix)
  3401. (current-column))
  3402. (delete-region start (point)))))))
  3403. ((eq lit-type 'c++)
  3404. (save-excursion
  3405. ;; Set fallback for comment-prefix if none is found.
  3406. (setq comment-prefix "// "
  3407. comment-text-end (cdr lit-limits))
  3408. (beginning-of-line)
  3409. (if (> (point) (car lit-limits))
  3410. ;; The current line is not the comment starter, so the
  3411. ;; comment has more than one line, and it can therefore be
  3412. ;; used to find the comment fill prefix.
  3413. (setq prefix-line (point))
  3414. (goto-char (car lit-limits))
  3415. (if (and (= (forward-line 1) 0)
  3416. (< (point) (cdr lit-limits)))
  3417. ;; The line after the comment starter is inside the
  3418. ;; comment, so we can use it.
  3419. (setq prefix-line (point))
  3420. ;; The comment is only one line. Take the comment prefix
  3421. ;; from it and keep the indentation.
  3422. (goto-char (car lit-limits))
  3423. (if (looking-at prefix-regexp)
  3424. (goto-char (match-end 0))
  3425. (forward-char 2)
  3426. (skip-chars-forward " \t"))
  3427. (let (str col)
  3428. (if (eq (c-point 'boi) (car lit-limits))
  3429. ;; There is only whitespace before the comment
  3430. ;; starter; take the prefix straight from this line.
  3431. (setq str (buffer-substring-no-properties
  3432. (c-point 'bol) (point))
  3433. col (current-column))
  3434. ;; There is code before the comment starter, so we
  3435. ;; have to temporarily insert and indent a new line to
  3436. ;; get the right space/tab mix in the indentation.
  3437. (let ((prefix-len (- (point) (car lit-limits)))
  3438. tmp)
  3439. (unwind-protect
  3440. (progn
  3441. (goto-char (car lit-limits))
  3442. (indent-to (prog1 (current-column)
  3443. (insert ?\n)))
  3444. (setq tmp (point))
  3445. (forward-char prefix-len)
  3446. (setq str (buffer-substring-no-properties
  3447. (c-point 'bol) (point))
  3448. col (current-column)))
  3449. (delete-region (car lit-limits) tmp))))
  3450. (setq res
  3451. (if (or (string-match "\\s \\'" str) (not (eolp)))
  3452. (cons str col)
  3453. ;; The prefix ends the line with no whitespace
  3454. ;; after it. Default to a single space.
  3455. (cons (concat str " ") (1+ col))))
  3456. )))))
  3457. (t
  3458. (setq comment-text-end
  3459. (save-excursion
  3460. (goto-char (- (cdr lit-limits) 2))
  3461. (if (looking-at "\\*/") (point) (cdr lit-limits))))
  3462. (save-excursion
  3463. (beginning-of-line)
  3464. (if (and (> (point) (car lit-limits))
  3465. (not (and (looking-at "[ \t]*\\*/")
  3466. (eq (cdr lit-limits) (match-end 0)))))
  3467. ;; The current line is not the comment starter and
  3468. ;; contains more than just the ender, so it's good enough
  3469. ;; to be used for the comment fill prefix.
  3470. (setq prefix-line (point))
  3471. (goto-char (car lit-limits))
  3472. (cond ((or (/= (forward-line 1) 0)
  3473. (>= (point) (cdr lit-limits))
  3474. (and (looking-at "[ \t]*\\*/")
  3475. (eq (cdr lit-limits) (match-end 0)))
  3476. (and (looking-at prefix-regexp)
  3477. (<= (1- (cdr lit-limits)) (match-end 0))))
  3478. ;; The comment is either one line or the next line contains
  3479. ;; just the comment ender. In this case we have no
  3480. ;; information about a suitable comment prefix, so we resort
  3481. ;; to c-block-comment-prefix.
  3482. (setq comment-prefix (or c-block-comment-prefix "")))
  3483. ((< here (point))
  3484. ;; The point was on the comment opener line, so we might want
  3485. ;; to treat this as a not yet closed comment.
  3486. (if (and (match-beginning 1)
  3487. (/= (match-beginning 1) (match-end 1)))
  3488. ;; Above `prefix-regexp' matched a nonempty prefix on the
  3489. ;; second line, so let's use it. Normally it should do
  3490. ;; to set `prefix-line' and let the code below pick up
  3491. ;; the whole prefix, but if there's no text after the
  3492. ;; match then it will probably fall back to no prefix at
  3493. ;; all if the comment isn't closed yet, so in that case
  3494. ;; it's better to force use of the prefix matched now.
  3495. (if (= (match-end 0) (c-point 'eol))
  3496. (setq comment-prefix (match-string 1))
  3497. (setq prefix-line (point)))
  3498. ;; There's no nonempty prefix on the line after the
  3499. ;; comment opener. If the line is empty, or if the
  3500. ;; text on it has less or equal indentation than the
  3501. ;; comment starter we assume it's an unclosed
  3502. ;; comment starter, i.e. that
  3503. ;; `c-block-comment-prefix' should be used.
  3504. ;; Otherwise we assume it's a closed comment where
  3505. ;; the prefix really is the empty string.
  3506. ;; E.g. this is an unclosed comment:
  3507. ;;
  3508. ;; /*
  3509. ;; foo
  3510. ;;
  3511. ;; But this is not:
  3512. ;;
  3513. ;; /*
  3514. ;; foo
  3515. ;; */
  3516. ;;
  3517. ;; (Looking for the presence of the comment closer
  3518. ;; rarely works since it's probably the closer of
  3519. ;; some comment further down when the comment
  3520. ;; really is unclosed.)
  3521. (if (<= (save-excursion (back-to-indentation)
  3522. (current-column))
  3523. (save-excursion (goto-char (car lit-limits))
  3524. (current-column)))
  3525. (setq comment-prefix (or c-block-comment-prefix ""))
  3526. (setq prefix-line (point)))))
  3527. (t
  3528. ;; Otherwise the line after the comment starter is good
  3529. ;; enough to find the prefix in.
  3530. (setq prefix-line (point))))
  3531. (when comment-prefix
  3532. ;; Haven't got the comment prefix on any real line that we
  3533. ;; can take it from, so we have to temporarily insert
  3534. ;; `comment-prefix' on a line and indent it to find the
  3535. ;; correct column and the correct mix of tabs and spaces.
  3536. (setq res
  3537. (let (tmp-pre tmp-post)
  3538. (unwind-protect
  3539. (progn
  3540. (goto-char (car lit-limits))
  3541. (if (looking-at comment-start-regexp)
  3542. (goto-char (min (match-end 0)
  3543. comment-text-end))
  3544. (forward-char 2)
  3545. (skip-chars-forward " \t"))
  3546. (when (eq (char-syntax (char-before)) ?\ )
  3547. ;; If there's ws on the current line, we'll use it
  3548. ;; instead of what's ending comment-prefix.
  3549. (setq comment-prefix
  3550. (concat (substring comment-prefix
  3551. 0 (string-match
  3552. "\\s *\\'"
  3553. comment-prefix))
  3554. (buffer-substring-no-properties
  3555. (save-excursion
  3556. (skip-chars-backward " \t")
  3557. (point))
  3558. (point)))))
  3559. (setq tmp-pre (point-marker))
  3560. ;; We insert an extra non-whitespace character
  3561. ;; before the line break and after comment-prefix in
  3562. ;; case it's "" or ends with whitespace.
  3563. (insert-and-inherit "x\n" comment-prefix "x")
  3564. (setq tmp-post (point-marker))
  3565. (indent-according-to-mode)
  3566. (goto-char (1- tmp-post))
  3567. (cons (buffer-substring-no-properties
  3568. (c-point 'bol) (point))
  3569. (current-column)))
  3570. (when tmp-post
  3571. (delete-region tmp-pre tmp-post)
  3572. (set-marker tmp-pre nil)
  3573. (set-marker tmp-post nil))))))))))
  3574. (or res ; Found a good prefix above.
  3575. (save-excursion
  3576. ;; prefix-line is the bol of a line on which we should try
  3577. ;; to find the prefix.
  3578. (let* (fb-string fb-endpos ; Contains any fallback prefix found.
  3579. (test-line
  3580. (lambda ()
  3581. (when (and (looking-at prefix-regexp)
  3582. (<= (match-end 0) comment-text-end))
  3583. (unless (eq (match-end 0) (c-point 'eol))
  3584. ;; The match is fine if there's text after it.
  3585. (throw 'found (cons (buffer-substring-no-properties
  3586. (match-beginning 0) (match-end 0))
  3587. (progn (goto-char (match-end 0))
  3588. (current-column)))))
  3589. (unless fb-string
  3590. ;; This match is better than nothing, so let's
  3591. ;; remember it in case nothing better is found
  3592. ;; on another line.
  3593. (setq fb-string (buffer-substring-no-properties
  3594. (match-beginning 0) (match-end 0))
  3595. fb-endpos (match-end 0)))
  3596. t))))
  3597. (or (catch 'found
  3598. ;; Search for a line which has text after the prefix
  3599. ;; so that we get the proper amount of whitespace
  3600. ;; after it. We start with the current line, then
  3601. ;; search backwards, then forwards.
  3602. (goto-char prefix-line)
  3603. (when (and (funcall test-line)
  3604. (or (/= (match-end 1) (match-end 0))
  3605. ;; The whitespace is sucked up by the
  3606. ;; first [ \t]* glob if the prefix is empty.
  3607. (and (= (match-beginning 1) (match-end 1))
  3608. (/= (match-beginning 0) (match-end 0)))))
  3609. ;; If the current line doesn't have text but do
  3610. ;; have whitespace after the prefix, we'll use it.
  3611. (throw 'found (cons fb-string
  3612. (progn (goto-char fb-endpos)
  3613. (current-column)))))
  3614. (if (eq lit-type 'c++)
  3615. ;; For line comments we can search up to and
  3616. ;; including the first line.
  3617. (while (and (zerop (forward-line -1))
  3618. (>= (point) (car lit-limits)))
  3619. (funcall test-line))
  3620. ;; For block comments we must stop before the
  3621. ;; block starter.
  3622. (while (and (zerop (forward-line -1))
  3623. (> (point) (car lit-limits)))
  3624. (funcall test-line)))
  3625. (goto-char prefix-line)
  3626. (while (and (zerop (forward-line 1))
  3627. (< (point) (cdr lit-limits)))
  3628. (funcall test-line))
  3629. (goto-char prefix-line)
  3630. nil)
  3631. (when fb-string
  3632. ;; A good line wasn't found, but at least we have a
  3633. ;; fallback that matches the comment prefix regexp.
  3634. (cond ((or (string-match "\\s \\'" fb-string)
  3635. (progn
  3636. (goto-char fb-endpos)
  3637. (not (eolp))))
  3638. ;; There are ws or text after the prefix, so
  3639. ;; let's use it.
  3640. (cons fb-string (current-column)))
  3641. ((progn
  3642. ;; Check if there's any whitespace padding
  3643. ;; on the comment start line that we can
  3644. ;; use after the prefix.
  3645. (goto-char (car lit-limits))
  3646. (if (looking-at comment-start-regexp)
  3647. (goto-char (match-end 0))
  3648. (forward-char 2)
  3649. (skip-chars-forward " \t"))
  3650. (or (not (eolp))
  3651. (eq (char-syntax (char-before)) ?\ )))
  3652. (setq fb-string (buffer-substring-no-properties
  3653. (save-excursion
  3654. (skip-chars-backward " \t")
  3655. (point))
  3656. (point)))
  3657. (goto-char fb-endpos)
  3658. (skip-chars-backward " \t")
  3659. (let ((tmp (point)))
  3660. ;; Got to mess in the buffer once again to
  3661. ;; ensure the column gets correct. :P
  3662. (unwind-protect
  3663. (progn
  3664. (insert-and-inherit fb-string)
  3665. (cons (buffer-substring-no-properties
  3666. (c-point 'bol)
  3667. (point))
  3668. (current-column)))
  3669. (delete-region tmp (point)))))
  3670. (t
  3671. ;; Last resort: Just add a single space after
  3672. ;; the prefix.
  3673. (cons (concat fb-string " ")
  3674. (progn (goto-char fb-endpos)
  3675. (1+ (current-column)))))))
  3676. ;; The line doesn't match the comment prefix regexp.
  3677. (if comment-prefix
  3678. ;; We have a fallback for line comments that we must use.
  3679. (cons (concat (buffer-substring-no-properties
  3680. prefix-line (c-point 'boi))
  3681. comment-prefix)
  3682. (progn (back-to-indentation)
  3683. (+ (current-column) (length comment-prefix))))
  3684. ;; Assume we are dealing with a "free text" block
  3685. ;; comment where the lines doesn't have any comment
  3686. ;; prefix at all and we should just fill it as
  3687. ;; normal text.
  3688. '("" . 0))))))
  3689. ))
  3690. (defun c-mask-paragraph (fill-paragraph apply-outside-literal fun &rest args)
  3691. ;; Calls FUN with ARGS ar arguments while the current paragraph is
  3692. ;; masked to allow adaptive filling to work correctly. That
  3693. ;; includes narrowing the buffer and, if point is inside a comment,
  3694. ;; masking the comment starter and ender appropriately.
  3695. ;;
  3696. ;; FILL-PARAGRAPH is non-nil if called for whole paragraph filling.
  3697. ;; The position of point is then less significant when doing masking
  3698. ;; and narrowing.
  3699. ;;
  3700. ;; If APPLY-OUTSIDE-LITERAL is nil then the function will be called
  3701. ;; only if the point turns out to be inside a comment or a string.
  3702. ;;
  3703. ;; Note that this function does not do any hidden buffer changes.
  3704. (let (fill
  3705. ;; beg and end limit the region to narrow. end is a marker.
  3706. beg end
  3707. ;; tmp-pre and tmp-post mark strings that are temporarily
  3708. ;; inserted at the start and end of the region. tmp-pre is a
  3709. ;; cons of the positions of the prepended string. tmp-post is
  3710. ;; a marker pointing to the single character of the appended
  3711. ;; string.
  3712. tmp-pre tmp-post
  3713. ;; If hang-ender-stuck isn't nil, the comment ender is
  3714. ;; hanging. In that case it's set to the number of spaces
  3715. ;; that should be between the text and the ender.
  3716. hang-ender-stuck
  3717. ;; auto-fill-spaces is the exact sequence of whitespace between a
  3718. ;; comment's last word and the comment ender, temporarily replaced
  3719. ;; with 'x's before calling FUN when FILL-PARAGRAPH is nil.
  3720. auto-fill-spaces
  3721. (here (point))
  3722. (c-lit-limits c-lit-limits)
  3723. (c-lit-type c-lit-type))
  3724. ;; Restore point on undo. It's necessary since we do a lot of
  3725. ;; hidden inserts and deletes below that should be as transparent
  3726. ;; as possible.
  3727. (if (and buffer-undo-list (not (eq buffer-undo-list t)))
  3728. (setq buffer-undo-list (cons (point) buffer-undo-list)))
  3729. ;; Determine the limits and type of the containing literal (if any):
  3730. ;; C-LIT-LIMITS, C-LIT-TYPE; and the limits of the current paragraph:
  3731. ;; BEG and END.
  3732. (c-save-buffer-state ()
  3733. (save-restriction
  3734. ;; Widen to catch comment limits correctly.
  3735. (widen)
  3736. (unless c-lit-limits
  3737. (setq c-lit-limits (c-literal-limits nil fill-paragraph)))
  3738. (setq c-lit-limits (c-collect-line-comments c-lit-limits))
  3739. (unless c-lit-type
  3740. (setq c-lit-type (c-literal-type c-lit-limits))))
  3741. (save-excursion
  3742. (unless (c-safe (backward-char)
  3743. (forward-paragraph)
  3744. (>= (point) here))
  3745. (goto-char here)
  3746. (forward-paragraph))
  3747. (setq end (point-marker)))
  3748. (save-excursion
  3749. (unless (c-safe (forward-char)
  3750. (backward-paragraph)
  3751. (<= (point) here))
  3752. (goto-char here)
  3753. (backward-paragraph))
  3754. (setq beg (point))))
  3755. (unwind-protect
  3756. (progn
  3757. ;; For each of the possible types of text (string, C comment ...)
  3758. ;; determine BEG and END, the region we will narrow to. If we're in
  3759. ;; a literal, constrain BEG and END to the limits of this literal.
  3760. ;;
  3761. ;; For some of these text types, particularly a block comment, we
  3762. ;; may need to massage whitespace near literal delimiters, so that
  3763. ;; these don't get filled inappropriately.
  3764. (cond
  3765. ((eq c-lit-type 'c++) ; Line comment.
  3766. (save-excursion
  3767. ;; Limit to the comment or paragraph end, whichever
  3768. ;; comes first.
  3769. (set-marker end (min end (cdr c-lit-limits)))
  3770. (when (<= beg (car c-lit-limits))
  3771. ;; The region includes the comment starter, so we must
  3772. ;; check it.
  3773. (goto-char (car c-lit-limits))
  3774. (back-to-indentation)
  3775. (if (eq (point) (car c-lit-limits))
  3776. ;; Include the first line in the region.
  3777. (setq beg (c-point 'bol))
  3778. ;; The first line contains code before the
  3779. ;; comment. We must fake a line that doesn't.
  3780. (setq tmp-pre t))))
  3781. (setq apply-outside-literal t))
  3782. ((eq c-lit-type 'c) ; Block comment.
  3783. (when
  3784. (or (> end (cdr c-lit-limits))
  3785. (and (= end (cdr c-lit-limits))
  3786. (eq (char-before end) ?/)
  3787. (eq (char-before (1- end)) ?*)
  3788. ;; disallow "/*/"
  3789. (> (- (cdr c-lit-limits) (car c-lit-limits)) 3)))
  3790. ;; There is a comment ender, and the region includes it. If
  3791. ;; it's on its own line, it stays on its own line. If it's got
  3792. ;; company on the line, it keeps (at least one word of) it.
  3793. ;; "=====*/" counts as a comment ender here, but "===== */"
  3794. ;; doesn't and "foo*/" doesn't.
  3795. (unless
  3796. (save-excursion
  3797. (goto-char (cdr c-lit-limits))
  3798. (beginning-of-line)
  3799. ;; The following conjunct was added to avoid an
  3800. ;; "Invalid search bound (wrong side of point)"
  3801. ;; error in the subsequent re-search. Maybe
  3802. ;; another fix would be needed (2007-12-08).
  3803. ; (or (<= (- (cdr c-lit-limits) 2) (point))
  3804. ; 2010-10-17 Construct removed.
  3805. ; (or (< (- (cdr c-lit-limits) 2) (point))
  3806. (and
  3807. (search-forward-regexp
  3808. (concat "\\=[ \t]*\\(" c-current-comment-prefix "\\)")
  3809. (- (cdr c-lit-limits) 2) t)
  3810. (not (search-forward-regexp
  3811. "\\(\\s \\|\\sw\\)"
  3812. (- (cdr c-lit-limits) 2) 'limit))
  3813. ;; The comment ender IS on its own line. Exclude this
  3814. ;; line from the filling.
  3815. (set-marker end (c-point 'bol))));)
  3816. ;; The comment ender is hanging. Replace all space between it
  3817. ;; and the last word either by one or two 'x's (when
  3818. ;; FILL-PARAGRAPH is non-nil), or a row of x's the same width
  3819. ;; as the whitespace (when auto filling), and include it in
  3820. ;; the region. We'll change them back to whitespace
  3821. ;; afterwards. The effect of this is to glue the comment
  3822. ;; ender to the last word in the comment during filling.
  3823. (let* ((ender-start (save-excursion
  3824. (goto-char (cdr c-lit-limits))
  3825. (skip-syntax-backward "^w ")
  3826. (point)))
  3827. (ender-column (save-excursion
  3828. (goto-char ender-start)
  3829. (current-column)))
  3830. (point-rel (- ender-start here))
  3831. (sentence-ends-comment
  3832. (save-excursion
  3833. (goto-char ender-start)
  3834. (and (search-backward-regexp
  3835. (c-sentence-end) (c-point 'bol) t)
  3836. (goto-char (match-end 0))
  3837. (looking-at "[ \t]*")
  3838. (= (match-end 0) ender-start))))
  3839. spaces)
  3840. (save-excursion
  3841. ;; Insert a CR after the "*/", adjust END
  3842. (goto-char (cdr c-lit-limits))
  3843. (setq tmp-post (point-marker))
  3844. (insert ?\n)
  3845. (set-marker end (point))
  3846. (forward-line -1) ; last line of the comment
  3847. (if (and (looking-at (concat "[ \t]*\\(\\("
  3848. c-current-comment-prefix
  3849. "\\)[ \t]*\\)"))
  3850. (eq ender-start (match-end 0)))
  3851. ;; The comment ender is prefixed by nothing but a
  3852. ;; comment line prefix. IS THIS POSSIBLE? (ACM,
  3853. ;; 2006/4/28). Remove it along with surrounding ws.
  3854. (setq spaces (- (match-end 1) (match-end 2)))
  3855. (goto-char ender-start))
  3856. (skip-chars-backward " \t\r\n") ; Surely this can be
  3857. ; " \t"? "*/" is NOT alone on the line (ACM, 2005/8/18)
  3858. ;; What's being tested here? 2006/4/20. FIXME!!!
  3859. (if (/= (point) ender-start)
  3860. (progn
  3861. (if (<= here (point))
  3862. ;; Don't adjust point below if it's
  3863. ;; before the string we replace.
  3864. (setq point-rel -1))
  3865. ;; Keep one or two spaces between the
  3866. ;; text and the ender, depending on how
  3867. ;; many there are now.
  3868. (unless spaces
  3869. (setq spaces (- ender-column (current-column))))
  3870. (setq auto-fill-spaces (c-delete-and-extract-region
  3871. (point) ender-start))
  3872. ;; paragraph filling condenses multiple spaces to
  3873. ;; single or double spaces. auto-fill doesn't.
  3874. (if fill-paragraph
  3875. (setq spaces
  3876. (max
  3877. (min spaces
  3878. (if (and sentence-ends-comment
  3879. sentence-end-double-space)
  3880. 2 1))
  3881. 1)))
  3882. ;; Insert the filler first to keep marks right.
  3883. (insert-char ?x spaces t)
  3884. (setq hang-ender-stuck spaces)
  3885. (setq point-rel
  3886. (and (>= point-rel 0)
  3887. (- (point) (min point-rel spaces)))))
  3888. (setq point-rel nil)))
  3889. (if point-rel
  3890. ;; Point was in the middle of the string we
  3891. ;; replaced above, so put it back in the same
  3892. ;; relative position, counting from the end.
  3893. (goto-char point-rel)))
  3894. ))
  3895. (when (<= beg (car c-lit-limits))
  3896. ;; The region includes the comment starter.
  3897. (save-excursion
  3898. (goto-char (car c-lit-limits))
  3899. (if (looking-at (concat "\\(" comment-start-skip "\\)$"))
  3900. ;; Begin with the next line.
  3901. (setq beg (c-point 'bonl))
  3902. ;; Fake the fill prefix in the first line.
  3903. (setq tmp-pre t))))
  3904. (setq apply-outside-literal t))
  3905. ((eq c-lit-type 'string) ; String.
  3906. (save-excursion
  3907. (when (>= end (cdr c-lit-limits))
  3908. (goto-char (1- (cdr c-lit-limits)))
  3909. (setq tmp-post (point-marker))
  3910. (insert ?\n)
  3911. (set-marker end (point)))
  3912. (when (<= beg (car c-lit-limits))
  3913. (goto-char (1+ (car c-lit-limits)))
  3914. (setq beg (if (looking-at "\\\\$")
  3915. ;; Leave the start line if it's
  3916. ;; nothing but an escaped newline.
  3917. (1+ (match-end 0))
  3918. (point)))))
  3919. (setq apply-outside-literal t))
  3920. ((eq c-lit-type 'pound) ; Macro
  3921. ;; Narrow to the macro limits if they are nearer than the
  3922. ;; paragraph limits. Don't know if this is necessary but
  3923. ;; do it for completeness sake (doing auto filling at all
  3924. ;; inside macros is bogus to begin with since the line
  3925. ;; continuation backslashes aren't handled).
  3926. (save-excursion
  3927. (c-save-buffer-state ()
  3928. (c-beginning-of-macro)
  3929. (beginning-of-line)
  3930. (if (> (point) beg)
  3931. (setq beg (point)))
  3932. (c-end-of-macro)
  3933. (forward-line)
  3934. (if (< (point) end)
  3935. (set-marker end (point))))))
  3936. (t ; Other code.
  3937. ;; Try to avoid comments and macros in the paragraph to
  3938. ;; avoid that the adaptive fill mode gets the prefix from
  3939. ;; them.
  3940. (c-save-buffer-state nil
  3941. (save-excursion
  3942. (goto-char beg)
  3943. (c-forward-syntactic-ws end)
  3944. (beginning-of-line)
  3945. (setq beg (point))
  3946. (goto-char end)
  3947. (c-backward-syntactic-ws beg)
  3948. (forward-line)
  3949. (set-marker end (point))))))
  3950. (when tmp-pre
  3951. ;; Temporarily insert the fill prefix after the comment
  3952. ;; starter so that the first line looks like any other
  3953. ;; comment line in the narrowed region.
  3954. (setq fill (c-save-buffer-state nil
  3955. (c-guess-fill-prefix c-lit-limits c-lit-type)))
  3956. (unless (string-match (concat "\\`[ \t]*\\("
  3957. c-current-comment-prefix
  3958. "\\)[ \t]*\\'")
  3959. (car fill))
  3960. ;; Oops, the prefix doesn't match the comment prefix
  3961. ;; regexp. This could produce very confusing
  3962. ;; results with adaptive fill packages together with
  3963. ;; the insert prefix magic below, since the prefix
  3964. ;; often doesn't appear at all. So let's warn about
  3965. ;; it.
  3966. (message "\
  3967. Warning: Regexp from `c-comment-prefix-regexp' doesn't match the comment prefix %S"
  3968. (car fill)))
  3969. ;; Find the right spot on the line, break it, insert
  3970. ;; the fill prefix and make sure we're back in the
  3971. ;; same column by temporarily prefixing the first word
  3972. ;; with a number of 'x'.
  3973. (save-excursion
  3974. (goto-char (car c-lit-limits))
  3975. (if (looking-at (if (eq c-lit-type 'c++)
  3976. c-current-comment-prefix
  3977. comment-start-skip))
  3978. (goto-char (match-end 0))
  3979. (forward-char 2)
  3980. (skip-chars-forward " \t"))
  3981. (while (and (< (current-column) (cdr fill))
  3982. (not (eolp)))
  3983. (forward-char 1))
  3984. (let ((col (current-column)))
  3985. (setq beg (1+ (point))
  3986. tmp-pre (list (point)))
  3987. (unwind-protect
  3988. (progn
  3989. (insert-and-inherit "\n" (car fill))
  3990. (insert-char ?x (- col (current-column)) t))
  3991. (setcdr tmp-pre (point))))))
  3992. (when apply-outside-literal
  3993. ;; `apply-outside-literal' is always set to t here if
  3994. ;; we're inside a literal.
  3995. (let ((fill-prefix
  3996. (or fill-prefix
  3997. ;; Kludge: If the function that adapts the fill prefix
  3998. ;; doesn't produce the required comment starter for
  3999. ;; line comments, then force it by setting fill-prefix.
  4000. (when (and (eq c-lit-type 'c++)
  4001. ;; Kludge the kludge: filladapt-mode doesn't
  4002. ;; have this problem, but it currently
  4003. ;; doesn't override fill-context-prefix
  4004. ;; (version 2.12).
  4005. (not (and (boundp 'filladapt-mode)
  4006. filladapt-mode))
  4007. (not (string-match
  4008. "\\`[ \t]*//"
  4009. (or (fill-context-prefix beg end)
  4010. ""))))
  4011. (c-save-buffer-state nil
  4012. (car (or fill (c-guess-fill-prefix
  4013. c-lit-limits c-lit-type)))))))
  4014. ;; Save the relative position of point if it's outside the
  4015. ;; region we're going to narrow. Want to restore it in that
  4016. ;; case, but otherwise it should be moved according to the
  4017. ;; called function.
  4018. (point-rel (cond ((< (point) beg) (- (point) beg))
  4019. ((> (point) end) (- (point) end)))))
  4020. ;; Preparations finally done! Now we can call the
  4021. ;; actual function.
  4022. (prog1
  4023. (save-restriction
  4024. (narrow-to-region beg end)
  4025. (apply fun args))
  4026. (if point-rel
  4027. ;; Restore point if it was outside the region.
  4028. (if (< point-rel 0)
  4029. (goto-char (+ beg point-rel))
  4030. (goto-char (+ end point-rel))))))))
  4031. (when (consp tmp-pre)
  4032. (delete-region (car tmp-pre) (cdr tmp-pre)))
  4033. (when tmp-post
  4034. (save-excursion
  4035. (goto-char tmp-post)
  4036. (delete-char 1))
  4037. (when hang-ender-stuck
  4038. ;; Preserve point even if it's in the middle of the string
  4039. ;; we replace; save-excursion doesn't work in that case.
  4040. (setq here (point))
  4041. (goto-char tmp-post)
  4042. (skip-syntax-backward "^w ")
  4043. (forward-char (- hang-ender-stuck))
  4044. (if (or fill-paragraph (not auto-fill-spaces))
  4045. (insert-char ?\ hang-ender-stuck t)
  4046. (insert auto-fill-spaces))
  4047. (delete-char hang-ender-stuck)
  4048. (goto-char here))
  4049. (set-marker tmp-post nil))
  4050. (set-marker end nil))))
  4051. (defun c-fill-paragraph (&optional arg)
  4052. "Like \\[fill-paragraph] but handles C and C++ style comments.
  4053. If any of the current line is a comment or within a comment, fill the
  4054. comment or the paragraph of it that point is in, preserving the
  4055. comment indentation or line-starting decorations (see the
  4056. `c-comment-prefix-regexp' and `c-block-comment-prefix' variables for
  4057. details).
  4058. If point is inside multiline string literal, fill it. This currently
  4059. does not respect escaped newlines, except for the special case when it
  4060. is the very first thing in the string. The intended use for this rule
  4061. is in situations like the following:
  4062. char description[] = \"\\
  4063. A very long description of something that you want to fill to make
  4064. nicely formatted output.\"\;
  4065. If point is in any other situation, i.e. in normal code, do nothing.
  4066. Optional prefix ARG means justify paragraph as well."
  4067. (interactive "*P")
  4068. (let ((fill-paragraph-function
  4069. ;; Avoid infinite recursion.
  4070. (if (not (eq fill-paragraph-function 'c-fill-paragraph))
  4071. fill-paragraph-function)))
  4072. (c-mask-paragraph t nil 'fill-paragraph arg))
  4073. ;; Always return t. This has the effect that if filling isn't done
  4074. ;; above, it isn't done at all, and it's therefore effectively
  4075. ;; disabled in normal code.
  4076. t)
  4077. (defun c-do-auto-fill ()
  4078. ;; Do automatic filling if not inside a context where it should be
  4079. ;; ignored.
  4080. (let ((c-auto-fill-prefix
  4081. ;; The decision whether the line should be broken is actually
  4082. ;; done in c-indent-new-comment-line, which do-auto-fill
  4083. ;; calls to break lines. We just set this special variable
  4084. ;; so that we'll know when we're called from there. It's
  4085. ;; also used to detect whether fill-prefix is user set or
  4086. ;; generated automatically by do-auto-fill.
  4087. fill-prefix))
  4088. (c-mask-paragraph nil t 'do-auto-fill)))
  4089. (defun c-indent-new-comment-line (&optional soft allow-auto-fill)
  4090. "Break line at point and indent, continuing comment or macro if within one.
  4091. If inside a comment and `comment-multi-line' is non-nil, the
  4092. indentation and line prefix are preserved (see the
  4093. `c-comment-prefix-regexp' and `c-block-comment-prefix' variables for
  4094. details). If inside a single line comment and `comment-multi-line' is
  4095. nil, a new comment of the same type is started on the next line and
  4096. indented as appropriate for comments. If inside a macro, a line
  4097. continuation backslash is inserted and aligned as appropriate, and the
  4098. new line is indented according to `c-syntactic-indentation'.
  4099. If a fill prefix is specified, it overrides all the above."
  4100. ;; allow-auto-fill is used from c-context-line-break to allow auto
  4101. ;; filling to break the line more than once. Since this function is
  4102. ;; used from auto-fill itself, that's normally disabled to avoid
  4103. ;; unnecessary recursion.
  4104. (interactive)
  4105. (let ((fill-prefix fill-prefix)
  4106. (do-line-break
  4107. (lambda ()
  4108. (delete-horizontal-space)
  4109. (if soft
  4110. (insert-and-inherit ?\n)
  4111. (newline (if allow-auto-fill nil 1)))))
  4112. ;; Already know the literal type and limits when called from
  4113. ;; c-context-line-break.
  4114. (c-lit-limits c-lit-limits)
  4115. (c-lit-type c-lit-type)
  4116. (c-macro-start c-macro-start))
  4117. (c-save-buffer-state ()
  4118. (when (not (eq c-auto-fill-prefix t))
  4119. ;; Called from do-auto-fill.
  4120. (unless c-lit-limits
  4121. (setq c-lit-limits (c-literal-limits nil nil t)))
  4122. (unless c-lit-type
  4123. (setq c-lit-type (c-literal-type c-lit-limits)))
  4124. (if (memq (cond ((c-query-and-set-macro-start) 'cpp)
  4125. ((null c-lit-type) 'code)
  4126. (t c-lit-type))
  4127. c-ignore-auto-fill)
  4128. (setq fill-prefix t) ; Used as flag in the cond.
  4129. (if (and (null c-auto-fill-prefix)
  4130. (eq c-lit-type 'c)
  4131. (<= (c-point 'bol) (car c-lit-limits)))
  4132. ;; The adaptive fill function has generated a prefix, but
  4133. ;; we're on the first line in a block comment so it'll be
  4134. ;; wrong. Ignore it to guess a better one below.
  4135. (setq fill-prefix nil)
  4136. (when (and (eq c-lit-type 'c++)
  4137. (not (string-match (concat "\\`[ \t]*"
  4138. c-line-comment-starter)
  4139. (or fill-prefix ""))))
  4140. ;; Kludge: If the function that adapted the fill prefix
  4141. ;; doesn't produce the required comment starter for line
  4142. ;; comments, then we ignore it.
  4143. (setq fill-prefix nil)))
  4144. )))
  4145. (cond ((eq fill-prefix t)
  4146. ;; A call from do-auto-fill which should be ignored.
  4147. )
  4148. (fill-prefix
  4149. ;; A fill-prefix overrides anything.
  4150. (funcall do-line-break)
  4151. (insert-and-inherit fill-prefix))
  4152. ((c-save-buffer-state ()
  4153. (unless c-lit-limits
  4154. (setq c-lit-limits (c-literal-limits)))
  4155. (unless c-lit-type
  4156. (setq c-lit-type (c-literal-type c-lit-limits)))
  4157. (memq c-lit-type '(c c++)))
  4158. ;; Some sort of comment.
  4159. (if (or comment-multi-line
  4160. (save-excursion
  4161. (goto-char (car c-lit-limits))
  4162. (end-of-line)
  4163. (< (point) (cdr c-lit-limits))))
  4164. ;; Inside a comment that should be continued.
  4165. (let ((fill (c-save-buffer-state nil
  4166. (c-guess-fill-prefix
  4167. (setq c-lit-limits
  4168. (c-collect-line-comments c-lit-limits))
  4169. c-lit-type)))
  4170. (pos (point))
  4171. (start-col (current-column))
  4172. (comment-text-end
  4173. (or (and (eq c-lit-type 'c)
  4174. (save-excursion
  4175. (goto-char (- (cdr c-lit-limits) 2))
  4176. (if (looking-at "\\*/") (point))))
  4177. (cdr c-lit-limits))))
  4178. ;; Skip forward past the fill prefix in case
  4179. ;; we're standing in it.
  4180. ;;
  4181. ;; FIXME: This doesn't work well in cases like
  4182. ;;
  4183. ;; /* Bla bla bla bla bla
  4184. ;; bla bla
  4185. ;;
  4186. ;; If point is on the 'B' then the line will be
  4187. ;; broken after "Bla b".
  4188. ;;
  4189. ;; If we have an empty comment, /* */, the next
  4190. ;; lot of code pushes point to the */. We fix
  4191. ;; this by never allowing point to end up to the
  4192. ;; right of where it started.
  4193. (while (and (< (current-column) (cdr fill))
  4194. (not (eolp)))
  4195. (forward-char 1))
  4196. (if (and (> (point) comment-text-end)
  4197. (> (c-point 'bol) (car c-lit-limits)))
  4198. (progn
  4199. ;; The skip takes us out of the (block)
  4200. ;; comment; insert the fill prefix at bol
  4201. ;; instead and keep the position.
  4202. (setq pos (copy-marker pos t))
  4203. (beginning-of-line)
  4204. (insert-and-inherit (car fill))
  4205. (if soft (insert-and-inherit ?\n) (newline 1))
  4206. (goto-char pos)
  4207. (set-marker pos nil))
  4208. ;; Don't break in the middle of a comment starter
  4209. ;; or ender.
  4210. (cond ((> (point) comment-text-end)
  4211. (goto-char comment-text-end))
  4212. ((< (point) (+ (car c-lit-limits) 2))
  4213. (goto-char (+ (car c-lit-limits) 2))))
  4214. (funcall do-line-break)
  4215. (insert-and-inherit (car fill))
  4216. (if (> (current-column) start-col)
  4217. (move-to-column start-col)))) ; can this hit the
  4218. ; middle of a TAB?
  4219. ;; Inside a comment that should be broken.
  4220. (let ((comment-start comment-start)
  4221. (comment-end comment-end)
  4222. col)
  4223. (if (eq c-lit-type 'c)
  4224. (unless (string-match "[ \t]*/\\*" comment-start)
  4225. (setq comment-start "/* " comment-end " */"))
  4226. (unless (string-match "[ \t]*//" comment-start)
  4227. (setq comment-start "// " comment-end "")))
  4228. (setq col (save-excursion
  4229. (back-to-indentation)
  4230. (current-column)))
  4231. (funcall do-line-break)
  4232. (when (and comment-end (not (equal comment-end "")))
  4233. (forward-char -1)
  4234. (insert-and-inherit comment-end)
  4235. (forward-char 1))
  4236. ;; c-comment-indent may look at the current
  4237. ;; indentation, so let's start out with the same
  4238. ;; indentation as the previous one.
  4239. (indent-to col)
  4240. (insert-and-inherit comment-start)
  4241. (indent-for-comment))))
  4242. ((c-query-and-set-macro-start)
  4243. ;; In a macro.
  4244. (unless (looking-at "[ \t]*\\\\$")
  4245. ;; Do not clobber the alignment of the line continuation
  4246. ;; slash; c-backslash-region might look at it.
  4247. (delete-horizontal-space))
  4248. ;; Got an asymmetry here: In normal code this command
  4249. ;; doesn't indent the next line syntactically, and otoh a
  4250. ;; normal syntactically indenting newline doesn't continue
  4251. ;; the macro.
  4252. (c-newline-and-indent (if allow-auto-fill nil 1)))
  4253. (t
  4254. ;; Somewhere else in the code.
  4255. (let ((col (save-excursion
  4256. (beginning-of-line)
  4257. (while (and (looking-at "[ \t]*\\\\?$")
  4258. (= (forward-line -1) 0)))
  4259. (current-indentation))))
  4260. (funcall do-line-break)
  4261. (indent-to col))))))
  4262. (defalias 'c-comment-line-break-function 'c-indent-new-comment-line)
  4263. (make-obsolete 'c-comment-line-break-function 'c-indent-new-comment-line "21.1")
  4264. ;; advice for indent-new-comment-line for older Emacsen
  4265. (unless (boundp 'comment-line-break-function)
  4266. (defvar c-inside-line-break-advice nil)
  4267. (defadvice indent-new-comment-line (around c-line-break-advice
  4268. activate preactivate)
  4269. "Call `c-indent-new-comment-line' if in CC Mode."
  4270. (if (or c-inside-line-break-advice
  4271. (not c-buffer-is-cc-mode))
  4272. ad-do-it
  4273. (let ((c-inside-line-break-advice t))
  4274. (c-indent-new-comment-line (ad-get-arg 0))))))
  4275. (defun c-context-line-break ()
  4276. "Do a line break suitable to the context.
  4277. When point is outside a comment or macro, insert a newline and indent
  4278. according to the syntactic context, unless `c-syntactic-indentation'
  4279. is nil, in which case the new line is indented as the previous
  4280. non-empty line instead.
  4281. When point is inside the content of a preprocessor directive, a line
  4282. continuation backslash is inserted before the line break and aligned
  4283. appropriately. The end of the cpp directive doesn't count as inside
  4284. it.
  4285. When point is inside a comment, continue it with the appropriate
  4286. comment prefix (see the `c-comment-prefix-regexp' and
  4287. `c-block-comment-prefix' variables for details). The end of a
  4288. C++-style line comment doesn't count as inside it.
  4289. When point is inside a string, only insert a backslash when it is also
  4290. inside a preprocessor directive."
  4291. (interactive "*")
  4292. (let* (c-lit-limits c-lit-type
  4293. (c-macro-start c-macro-start)
  4294. case-fold-search)
  4295. (c-save-buffer-state ()
  4296. (setq c-lit-limits (c-literal-limits nil nil t)
  4297. c-lit-type (c-literal-type c-lit-limits))
  4298. (when (eq c-lit-type 'c++)
  4299. (setq c-lit-limits (c-collect-line-comments c-lit-limits)))
  4300. (c-query-and-set-macro-start))
  4301. (cond
  4302. ((or (eq c-lit-type 'c)
  4303. (and (eq c-lit-type 'c++) ; C++ comment, but not at the very end of it.
  4304. (< (save-excursion
  4305. (skip-chars-forward " \t")
  4306. (point))
  4307. (1- (cdr c-lit-limits))))
  4308. (and (numberp c-macro-start) ; Macro, but not at the very end of
  4309. ; it, not in a string, and not in the
  4310. ; cpp keyword.
  4311. (not (eq c-lit-type 'string))
  4312. (or (not (looking-at "\\s *$"))
  4313. (eq (char-before) ?\\))
  4314. (<= (save-excursion
  4315. (goto-char c-macro-start)
  4316. (if (looking-at c-opt-cpp-start)
  4317. (goto-char (match-end 0)))
  4318. (point))
  4319. (point))))
  4320. (let ((comment-multi-line t)
  4321. (fill-prefix nil))
  4322. (c-indent-new-comment-line nil t)))
  4323. ((eq c-lit-type 'string)
  4324. (if (and (numberp c-macro-start)
  4325. (not (eq (char-before) ?\\)))
  4326. (insert ?\\))
  4327. (newline))
  4328. (t (delete-horizontal-space)
  4329. (newline)
  4330. ;; c-indent-line may look at the current indentation, so let's
  4331. ;; start out with the same indentation as the previous line.
  4332. (let ((col (save-excursion
  4333. (backward-char)
  4334. (forward-line 0)
  4335. (while (and (looking-at "[ \t]*\\\\?$")
  4336. (= (forward-line -1) 0)))
  4337. (current-indentation))))
  4338. (indent-to col))
  4339. (indent-according-to-mode)))))
  4340. (defun c-context-open-line ()
  4341. "Insert a line break suitable to the context and leave point before it.
  4342. This is the `c-context-line-break' equivalent to `open-line', which is
  4343. normally bound to C-o. See `c-context-line-break' for the details."
  4344. (interactive "*")
  4345. (let ((here (point)))
  4346. (unwind-protect
  4347. (progn
  4348. ;; Temporarily insert a non-whitespace char to keep any
  4349. ;; preceding whitespace intact.
  4350. (insert ?x)
  4351. (c-context-line-break))
  4352. (goto-char here)
  4353. (delete-char 1))))
  4354. (cc-provide 'cc-cmds)
  4355. ;; Local Variables:
  4356. ;; indent-tabs-mode: t
  4357. ;; tab-width: 8
  4358. ;; End:
  4359. ;;; cc-cmds.el ends here