From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Stefan Monnier Newsgroups: gmane.emacs.devel Subject: css-mode Date: Fri, 13 Jan 2006 12:27:58 -0500 Message-ID: <878xtkghxj.fsf-monnier+emacs@gnu.org> NNTP-Posting-Host: main.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" X-Trace: sea.gmane.org 1137182518 32142 80.91.229.2 (13 Jan 2006 20:01:58 GMT) X-Complaints-To: usenet@sea.gmane.org NNTP-Posting-Date: Fri, 13 Jan 2006 20:01:58 +0000 (UTC) Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Fri Jan 13 21:01:54 2006 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by ciao.gmane.org with esmtp (Exim 4.43) id 1ExV7P-0005Br-OY for ged-emacs-devel@m.gmane.org; Fri, 13 Jan 2006 21:01:52 +0100 Original-Received: from localhost ([127.0.0.1] helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1ExV9Y-0000o6-Vx for ged-emacs-devel@m.gmane.org; Fri, 13 Jan 2006 15:04:05 -0500 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1ExSlD-0008NG-Aj for emacs-devel@gnu.org; Fri, 13 Jan 2006 12:30:47 -0500 Original-Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1ExSki-0008D8-AA for emacs-devel@gnu.org; Fri, 13 Jan 2006 12:30:21 -0500 Original-Received: from [199.232.76.173] (helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1ExSkg-0008CE-Pd for emacs-devel@gnu.org; Fri, 13 Jan 2006 12:30:15 -0500 Original-Received: from [209.226.175.74] (helo=tomts20-srv.bellnexxia.net) by monty-python.gnu.org with esmtp (Exim 4.34) id 1ExSnh-0006gW-HY for emacs-devel@gnu.org; Fri, 13 Jan 2006 12:33:21 -0500 Original-Received: from alfajor ([67.71.26.73]) by tomts20-srv.bellnexxia.net (InterMail vM.5.01.06.13 201-253-122-130-113-20050324) with ESMTP id <20060113172759.PUFL8316.tomts20-srv.bellnexxia.net@alfajor>; Fri, 13 Jan 2006 12:27:59 -0500 Original-Received: by alfajor (Postfix, from userid 1000) id 4BBE4D736A; Fri, 13 Jan 2006 12:27:58 -0500 (EST) Original-To: emacs-devel@gnu.org User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.0.50 (gnu/linux) X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.devel:49011 Archived-At: --=-=-= I wish Emacs came builtin with a mode to edit CSS style files. There are several floating around the net and I have my own (see below). I don't really care which one is installed, but I just hope one of them makes it into Emacs-22. Stefan --=-=-= Content-Type: application/emacs-lisp Content-Disposition: attachment; filename=css-mode.el Content-Transfer-Encoding: quoted-printable ;;; css-mode.el --- Major mode to edit CSS files ;; Copyright (C) 2006 Free Software Foundation, Inc. ;; Author: Stefan Monnier ;; Keywords: hypermedia ;; This file is free software; you can redistribute it and/or modify ;; it under the terms of the GNU General Public License as published by ;; the Free Software Foundation; either version 2, or (at your option) ;; any later version. ;; This file is distributed in the hope that it will be useful, ;; but WITHOUT ANY WARRANTY; without even the implied warranty of ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ;; GNU General Public License for more details. ;; You should have received a copy of the GNU General Public License ;; along with GNU Emacs; see the file COPYING. If not, write to ;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, ;; Boston, MA 02110-1301, USA. ;;; Commentary: ;; Yet another CSS mode. ;;; Code: (defun css-extract-keyword-list (res) (with-temp-buffer (url-insert-file-contents "http://www.w3.org/TR/REC-CSS2/css2.txt") (goto-char (point-max)) (search-backward "Appendix H. Index") (forward-line) (delete-region (point-min) (point)) (let ((result nil) keys) (dolist (re res) (goto-char (point-min)) (setq keys nil) (while (re-search-forward (cdr re) nil t) (push (match-string 1) keys)) (push (cons (car re) (sort keys 'string-lessp)) result)) (nreverse result)))) ;; Extraction was done with: ;; (css-extract-keyword-list ;; '((pseudo . "^ +\\* :\\([^ \n,]+\\)") ;; (at . "^ +\\* @\\([^ \n,]+\\)") ;; (descriptor . "^ +\\* '\\([^ '\n]+\\)' (descriptor)") ;; (media . "^ +\\* '\\([^ '\n]+\\)' media group") ;; (property . "^ +\\* '\\([^ '\n]+\\)',"))) (defconst css-pseudo-ids '("active" "after" "before" "first" "first-child" "first-letter" "first-l= ine" "focus" "hover" "lang" "left" "link" "right" "visited") "Identifiers for pseudo-elements and pseudo-classes.") (defconst css-at-ids '("charset" "font-face" "import" "media" "page") "Identifiers that appear in the form @foo.") (defconst css-descriptor-ids '("ascent" "baseline" "bbox" "cap-height" "centerline" "definition-src" "descent" "font-family" "font-size" "font-stretch" "font-style" "font-variant" "font-weight" "mathline" "panose-1" "slope" "src" "stemh" "stemv" "topline" "unicode-range" "units-per-em" "widths" "x-height") "Identifiers for font descriptors.") (defconst css-media-ids '("all" "aural" "bitmap" "continuous" "grid" "paged" "static" "tactile" "visual") "Identifiers for types of media.") (defconst css-property-ids '("azimuth" "background" "background-attachment" "background-color" "background-image" "background-position" "background-repeat" "block" "border" "border-bottom" "border-bottom-color" "border-bottom-style" "border-bottom-width" "border-collapse" "border-color" "border-left" "border-left-color" "border-left-style" "border-left-width" "border-rig= ht" "border-right-color" "border-right-style" "border-right-width" "border-spacing" "border-style" "border-top" "border-top-color" "border-top-style" "border-top-width" "border-width" "bottom" "caption-side" "clear" "clip" "color" "compact" "content" "counter-increment" "counter-reset" "cue" "cue-after" "cue-before" "cursor" "dashed" "direction" "display" "dotted" "double" "elevation" "empty-cells" "float" "font" "font-family" "font-size" "font-size-adjus= t" "font-stretch" "font-style" "font-variant" "font-weight" "groove" "heig= ht" "hidden" "inline" "inline-table" "inset" "left" "letter-spacing" "line-height" "list-item" "list-style" "list-style-image" "list-style-position" "list-style-type" "margin" "margin-bottom" "margin-left" "margin-right" "margin-top" "marker-offset" "marks" "max-height" "max-width" "min-height" "min-width" "orphans" "outline" "outline-color" "outline-style" "outline-width" "outset" "overflow" "padding" "padding-bottom" "padding-left" "padding-right" "padding-top" "page" "page-break-after" "page-break-before" "page-break-inside" "paus= e" "pause-after" "pause-before" "pitch" "pitch-range" "play-during" "posit= ion" "quotes" "richness" "ridge" "right" "run-in" "size" "solid" "speak" "speak-header" "speak-numeral" "speak-punctuation" "speech-rate" "stres= s" "table" "table-caption" "table-cell" "table-column" "table-column-group" "table-footer-group" "table-header-group" "table-layout" "table-row" "table-row-group" "text-align" "text-decoration" "text-indent" "text-shadow" "text-transform" "top" "unicode-bidi" "vertical-align" "visibility" "voice-family" "volume" "white-space" "widows" "width" "word-spacing" "z-index") "Identifiers for properties.") (defvar css-mode-syntax-table (let ((st (make-syntax-table))) ;; C-style comments. (modify-syntax-entry ?/ ". 14" st) (modify-syntax-entry ?* ". 23" st) ;; Strings. (modify-syntax-entry ?\" "\"" st) (modify-syntax-entry ?\' "\"" st) ;; Blocks. (modify-syntax-entry ?\{ "(}" st) (modify-syntax-entry ?\} "){" st) ;; Args in url(...) thingies and other "function calls". (modify-syntax-entry ?\( "()" st) (modify-syntax-entry ?\) ")(" st) ;; To match attributes in selectors. (modify-syntax-entry ?\[ "(]" st) (modify-syntax-entry ?\] ")[" st) ;; Special chars that sometimes come at the beginning of words. (modify-syntax-entry ?@ "'" st) ;; (modify-syntax-entry ?: "'" st) (modify-syntax-entry ?# "'" st) ;; Distinction between words and symbols. (modify-syntax-entry ?- "_" st) st)) (defconst css-escapes-re "\\\\\\(?:[^\000-\037\177]\\|[0-9a-fA-F]+[ \n\t\r\f]?\\)") (defconst css-nmchar-re "\\(?:[-[:alnum:]]\\|" css-escapes-re "\\)") (defconst css-nmstart-re "\\(?:[[:alpha:]]\\|" css-escapes-re "\\)") (defconst css-ident-re (concat css-nmstart-re css-nmchar-re "*")) (defconst css-name-re (concat css-nmchar-re "+")) (defface css-selector '((t :inherit font-lock-function-name-face)) "Face to use for selectors.") (defface css-property '((t :inherit font-lock-variable-name-face)) "Face to use for properties.") (defvar css-font-lock-keywords `(("!\\s-*important") ;; Atrules keywords. IDs not in css-at-ids are valid (ignored). (,(concat "@" css-ident-re) . font-lock-builtin-face) ;; Selectors. (,(concat "^\\([ \t]*[^:{\n]+\\(?::" (regexp-opt css-pseudo-ids t) "\\(([^)]+)\\)?[^:{\n]+\\)*\\){") (1 'css-selector)) ;; Properties. Again, we don't limit ourselves to css-property-ids. (,(concat "\\(?:[{;]\\|^\\)[ \t]*\\(" css-ident-re "\\)\\s-*:") (1 'css-property)))) (defvar css-font-lock-defaults '(css-font-lock-keywords nil t)) ;;;###autoload (add-to-list 'auto-mode-alist '("\\.css\\'" . css-mode)) (define-derived-mode css-mode prog-mode "CSS" "Major mode to edit Cascading Style Sheets." (set (make-local-variable 'font-lock-defaults) css-font-lock-defaults) (set (make-local-variable 'comment-start) "/*") (set (make-local-variable 'comment-start-skip) "/\\*+[ \t]*") (set (make-local-variable 'comment-end) "*/") (set (make-local-variable 'comment-end-skip) "[ \t]*\\*+/") (set (make-local-variable 'forward-sexp-function) 'css-forward-sexp) (set (make-local-variable 'indent-line-function) 'css-indent-line)) ;;; Navigation and indentation. (defun css-backward-sexp (n) (let ((forward-sexp-function nil)) (if (< n 0) (css-forward-sexp (- n)) (while (> n 0) (setq n (1- n)) (forward-comment (- (point-max))) (if (not (eq (char-before) ?\;)) (backward-sexp 1) (while (progn (backward-sexp 1) (save-excursion (forward-comment (- (point-max))) ;; FIXME: We should also skip punctuation. (not (memq (char-before) '(?\; ?\{))))))))))) (defun css-forward-sexp (n) (let ((forward-sexp-function nil)) (if (< n 0) (css-backward-sexp (- n)) (while (> n 0) (setq n (1- n)) (forward-comment (point-max)) (if (not (eq (char-after) ?\;)) (forward-sexp 1) (while (progn (forward-sexp 1) (save-excursion (forward-comment (point-max)) ;; FIXME: We should also skip punctuation. (not (memq (char-after) '(?\; ?\}))))))))))) (defun css-calculate-indent-virtual () (if (or (save-excursion (skip-chars-backward " \t") (bolp)) (if (looking-at "\\s(") (save-excursion (forward-char 1) (skip-chars-forward " \t") (not (eolp))))) (current-column) (css-calculate-indent))) (defcustom css-basic-offset 4 "Basic size of one indentation step." :type 'integer) (defun css-calculate-indent () (let ((ppss (syntax-ppss)) pos) (save-excursion (cond ;; Inside a string. ((nth 3 ppss) 'noindent) ;; Inside a comment. ((nth 4 ppss) (setq pos (point)) (forward-line -1) (skip-chars-forward " \t") (if (>=3D (nth 8 ppss) (point)) (progn (goto-char (nth 8 ppss)) (if (eq (char-after pos) ?*) (forward-char 1) (if (not (looking-at comment-start-skip)) (error "Something really fishy here") (goto-char (match-end 0)))) (current-column)) (if (and (eq (char-after pos) ?*) (eq (char-after) ?*)) (current-column) ;; 'noindent (current-column) ))) ;; In normal code. (t (or (when (looking-at "\\s)") (forward-char 1) (backward-sexp 1) (css-calculate-indent-virtual)) (when (looking-at comment-start-skip) (forward-comment (point-max)) (css-calculate-indent)) (when (save-excursion (forward-comment (- (point-max))) (setq pos (point)) (eq (char-syntax (preceding-char)) ?\()) (goto-char (1- pos)) (+ (css-calculate-indent-virtual) css-basic-offset)) (progn (css-backward-sexp 1) (if (looking-at "\\s(") (css-calculate-indent) (css-calculate-indent-virtual))))))))) =20=20=20=20=20 (defun css-indent-line () "Indent current line according to CSS indentation rules." (interactive) (let* ((savep (point)) (forward-sexp-function nil) (indent (condition-case nil (save-excursion (forward-line 0) (skip-chars-forward " \t") (if (>=3D (point) savep) (setq savep nil)) (css-calculate-indent)) (error nil)))) (if (not (numberp indent)) 'noindent (if savep (save-excursion (indent-line-to indent)) (indent-line-to indent))))) (provide 'css-mode) ;; arch-tag: b4d8b8e2-b130-4e74-b3aa-cd8f1ab659d0 ;;; css-mode.el ends here --=-=-= Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ Emacs-devel mailing list Emacs-devel@gnu.org http://lists.gnu.org/mailman/listinfo/emacs-devel --=-=-=--