From: "Simen Heggestøyl" <simenheg@gmail.com>
To: Stefan Monnier <monnier@iro.umontreal.ca>
Cc: emacs-devel@gnu.org, Dmitry Gutov <dgutov@yandex.ru>
Subject: Re: [Emacs-diffs] master 269d563: Support completion of attribute values in CSS mode
Date: Sat, 25 Jun 2016 15:19:07 +0200 [thread overview]
Message-ID: <1466860747.1138.1@smtp.gmail.com> (raw)
In-Reply-To: <jwv8u15e4g2.fsf-monnier+emacsdiffs@gnu.org>
[-- Attachment #1: Type: text/plain, Size: 899 bytes --]
> When we don't have any html-mode buffer at hand, that's obviously the
> better choice, yes. When we do have html-mode buffers around, both
> choices make sense, depending on the situation. Maybe it's better not
> to try to be clever about it, I don't know.
I think it's better not to be clever about it; I think having tags
disappear from the completion list because you have few HTML buffers
open will hurt more than presenting a too long completion list.
However maybe it would make sense to extend the completion list when
tags that aren't in `html-tag-alist' are used in a HTML buffer?
> Completion of user-chosen classes is a different matter, it would be
> great, and I've seen people asking for it a few times.
I've played around with this a bit and have got a basic version
working, see the attached patch. If it looks like something I can
chisel it onto a proper patch.
-- Simen
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-WIP-Support-completion-of-classes-and-IDs-in-CSS-mod.patch --]
[-- Type: text/x-patch, Size: 5354 bytes --]
From 1ca24c576e10477bdf360dc1433bf7d7eb166d4e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Simen=20Heggest=C3=B8yl?= <simenheg@gmail.com>
Date: Sat, 25 Jun 2016 15:12:09 +0200
Subject: [PATCH] WIP: Support completion of classes and IDs in CSS mode
---
lisp/textmodes/css-mode.el | 55 +++++++++++++++++++++++++++++++++++++++------
lisp/textmodes/sgml-mode.el | 29 ++++++++++++++++++++++++
2 files changed, 77 insertions(+), 7 deletions(-)
diff --git a/lisp/textmodes/css-mode.el b/lisp/textmodes/css-mode.el
index 060af33..a77c8ba 100644
--- a/lisp/textmodes/css-mode.el
+++ b/lisp/textmodes/css-mode.el
@@ -30,7 +30,6 @@
;; - electric ; and }
;; - filling code with auto-fill-mode
;; - fix font-lock errors with multi-line selectors
-;; - support completion of user-defined classes names and IDs
;;; Code:
@@ -863,16 +862,58 @@ css--nested-selectors-allowed
"Non-nil if nested selectors are allowed in the current mode.")
(make-variable-buffer-local 'css--nested-selectors-allowed)
-;; TODO: Currently only supports completion of HTML tags. By looking
-;; at open HTML mode buffers we should be able to provide completion
-;; of user-defined classes and IDs too.
+(defun css--complete-html-tag ()
+ (save-excursion
+ (let ((end (point)))
+ (skip-chars-backward "-[:alnum:]")
+ (list (point) end css--html-tags))))
+
+(defvar css--foreign-completion-cache
+ (list 'html-class-extractor-function (make-hash-table :test 'equal)
+ 'html-id-extractor-function (make-hash-table :test 'equal))
+ "Cache of completions provided by other buffers.
+This is a property list where each property is the name of an
+extractor function and the associated value is a hash table
+serving as a cache for that function.")
+
+(defun css--foreign-completions (extractor)
+ "Return a list of completions provided by other buffers.
+EXTRACTOR should be the name of a function that may be defined in
+one or more buffers. In each of the buffers where EXTRACTOR is
+defined, EXTRACTOR is called and the results are accumulated into
+a list."
+ (seq-uniq
+ (seq-mapcat
+ (lambda (buf)
+ (with-current-buffer buf
+ (when (boundp extractor)
+ (let ((cache
+ (plist-get css--foreign-completion-cache extractor)))
+ (if cache
+ (let ((hash (buffer-hash buf)))
+ (or (gethash hash cache)
+ (puthash hash (funcall (symbol-value extractor))
+ cache)))
+ (funcall (symbol-value extractor)))))))
+ (buffer-list))))
+
(defun css--complete-selector ()
"Complete part of a CSS selector at point."
(when (or (= (nth 0 (syntax-ppss)) 0) css--nested-selectors-allowed)
- (save-excursion
- (let ((end (point)))
+ (let ((end (point)))
+ (save-excursion
(skip-chars-backward "-[:alnum:]")
- (list (point) end css--html-tags)))))
+ (let ((start-char (char-before)))
+ (list
+ (point) end
+ (completion-table-dynamic
+ (lambda (_)
+ (cond
+ ((eq start-char ?.) (css--foreign-completions
+ 'html-class-extractor-function))
+ ((eq start-char ?#) (css--foreign-completions
+ 'html-id-extractor-function))
+ (t css--html-tags))))))))))
(defun css-completion-at-point ()
"Complete current symbol at point.
diff --git a/lisp/textmodes/sgml-mode.el b/lisp/textmodes/sgml-mode.el
index 990c09b..5e943de 100644
--- a/lisp/textmodes/sgml-mode.el
+++ b/lisp/textmodes/sgml-mode.el
@@ -32,6 +32,9 @@
;;; Code:
+(require 'dom)
+(require 'seq)
+(require 'subr-x)
(eval-when-compile
(require 'skeleton)
(require 'cl-lib))
@@ -2168,6 +2171,27 @@ html-current-defun-name
nil t)
(match-string-no-properties 1))))
+(defun html-current-buffer-classes ()
+ "Return a list of class names used in the current buffer."
+ (let ((dom (libxml-parse-html-region (point-min) (point-max))))
+ (seq-mapcat
+ (lambda (el)
+ (when-let (class-list (cdr (assq 'class (dom-attributes el))))
+ (split-string class-list)))
+ (dom-by-class dom ""))))
+
+(defun html-current-buffer-ids ()
+ "Return a list of IDs used in the current buffer."
+ (let ((dom (libxml-parse-html-region (point-min) (point-max))))
+ (seq-mapcat
+ (lambda (el)
+ (when-let (id-list (cdr (assq 'id (dom-attributes el))))
+ (split-string id-list)))
+ (dom-by-id dom ""))))
+
+(defvar html-class-extractor-function)
+(defvar html-id-extractor-function)
+
\f
;;;###autoload
(define-derived-mode html-mode sgml-mode '(sgml-xml-mode "XHTML" "HTML")
@@ -2218,6 +2242,11 @@ html-mode
(setq-local add-log-current-defun-function #'html-current-defun-name)
(setq-local sentence-end-base "[.?!][]\"'”)}]*\\(<[^>]*>\\)*")
+ (when (fboundp 'libxml-parse-html-region)
+ (setq-local html-class-extractor-function
+ #'html-current-buffer-classes)
+ (setq-local html-id-extractor-function #'html-current-buffer-ids))
+
(setq imenu-create-index-function 'html-imenu-index)
(setq-local sgml-empty-tags
--
2.8.1
prev parent reply other threads:[~2016-06-25 13:19 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <20160323180952.31942.96525@vcs.savannah.gnu.org>
[not found] ` <E1ainE8-0008Jo-CW@vcs.savannah.gnu.org>
2016-03-23 21:31 ` [Emacs-diffs] master 269d563: Support completion of attribute values in CSS mode Stefan Monnier
2016-03-24 14:12 ` Simen Heggestøyl
[not found] ` <=?ISO-8859-1?Q?1458828752.7618.0@smtp.gmail.com>
2016-03-24 14:31 ` Stefan Monnier
2016-03-26 12:14 ` Simen Heggestøyl
2016-03-26 13:23 ` Dmitry Gutov
2016-03-26 13:52 ` Stefan Monnier
2016-06-25 13:19 ` Simen Heggestøyl [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1466860747.1138.1@smtp.gmail.com \
--to=simenheg@gmail.com \
--cc=dgutov@yandex.ru \
--cc=emacs-devel@gnu.org \
--cc=monnier@iro.umontreal.ca \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.