all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Re: [Emacs-diffs] master 269d563: Support completion of attribute values in CSS mode
       [not found] ` <E1ainE8-0008Jo-CW@vcs.savannah.gnu.org>
@ 2016-03-23 21:31   ` Stefan Monnier
  2016-03-24 14:12     ` Simen Heggestøyl
  0 siblings, 1 reply; 7+ messages in thread
From: Stefan Monnier @ 2016-03-23 21:31 UTC (permalink / raw
  To: Simen Heggestøyl; +Cc: emacs-devel

> +** CSS mode
> +
> +---
> +*** Support for completing attribute values using the `completion-at-point'
> +command.

Yay!

Does that now fully make company-css obsolete (well, obviously only for
those users that use the bleeding edge version of Emacs)? Or are there
still some functionality that hasn't yet been ported?


        Stefan



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [Emacs-diffs] master 269d563: Support completion of attribute values in CSS mode
  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
  0 siblings, 2 replies; 7+ messages in thread
From: Simen Heggestøyl @ 2016-03-24 14:12 UTC (permalink / raw
  To: Stefan Monnier; +Cc: Dmitry Gutov, emacs-devel

[-- Attachment #1: Type: text/plain, Size: 963 bytes --]

On Wed, Mar 23, 2016 at 10:31 PM, Stefan Monnier 
<monnier@iro.umontreal.ca> wrote:
> Does that now fully make company-css obsolete (well, obviously only 
> for
> those users that use the bleeding edge version of Emacs)? Or are there
> still some functionality that hasn't yet been ported?

It depends on whether we want to support completion of HTML tags in
selectors, that's the last remaining functionality that hasn't been
ported I think.

Personally I don't find much value in it, since I mostly use class names
in selectors, and the HTML tags I do use are short and easy to type. But
if others find it useful I can port it.

Also, in SCSS there's no way of knowing whether the user is going to
type a selector or a property (except at top-level), so we'll have to
mix HTML tags and CSS property names in the completion list. For that
reason I'll probably add an option to turn off HTML tag completion, if
we decide to port it.

What do you think?

-- Simen

[-- Attachment #2: Type: text/html, Size: 1260 bytes --]

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [Emacs-diffs] master 269d563: Support completion of attribute values in CSS mode
  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
  1 sibling, 2 replies; 7+ messages in thread
From: Stefan Monnier @ 2016-03-24 14:31 UTC (permalink / raw
  To: Simen Heggestøyl; +Cc: Dmitry Gutov, emacs-devel

>> Does that now fully make company-css obsolete (well, obviously only for
>> those users that use the bleeding edge version of Emacs)? Or are there
>> still some functionality that hasn't yet been ported?
> It depends on whether we want to support completion of HTML tags in
> selectors, that's the last remaining functionality that hasn't been
> ported I think.

> Personally I don't find much value in it, since I mostly use class names
> in selectors, and the HTML tags I do use are short and easy to type. But
> if others find it useful I can port it.

The important part, for me, is to try and make sure we can declare
company-css obsolete.

In theory, at least, completion of HTML tags in selectors makes sense to
me, but I'd hope we can do it without hardcoding the list of tags in
css-mode.el (e.g. using html-tag-alist from sgml-mode.el, or using
nxml).

BTW, even better (for me) would be to make the completion pay attention
to existing html buffers, so you can limit the completion to those tags
actually relevant (and you can additionally provide completion of
user-chosen classes).


        Stefan



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [Emacs-diffs] master 269d563: Support completion of attribute values in CSS mode
  2016-03-24 14:31       ` Stefan Monnier
@ 2016-03-26 12:14         ` Simen Heggestøyl
  2016-03-26 13:23         ` Dmitry Gutov
  1 sibling, 0 replies; 7+ messages in thread
From: Simen Heggestøyl @ 2016-03-26 12:14 UTC (permalink / raw
  To: Stefan Monnier; +Cc: Dmitry Gutov, emacs-devel

[-- Attachment #1: Type: text/plain, Size: 435 bytes --]

On Thu, Mar 24, 2016 at 3:31 PM, Stefan Monnier 
<monnier@iro.umontreal.ca> wrote:
> BTW, even better (for me) would be to make the completion pay 
> attention
> to existing html buffers, so you can limit the completion to those 
> tags
> actually relevant (and you can additionally provide completion of
> user-chosen classes).

That sounds like a good idea. I'll start off by porting what's left from
company-css.el, then.

-- Simen

[-- Attachment #2: Type: text/html, Size: 583 bytes --]

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [Emacs-diffs] master 269d563: Support completion of attribute values in CSS mode
  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
  1 sibling, 1 reply; 7+ messages in thread
From: Dmitry Gutov @ 2016-03-26 13:23 UTC (permalink / raw
  To: Stefan Monnier, Simen Heggestøyl; +Cc: emacs-devel

On 03/24/2016 04:31 PM, Stefan Monnier wrote:

> BTW, even better (for me) would be to make the completion pay attention
> to existing html buffers, so you can limit the completion to those tags
> actually relevant (and you can additionally provide completion of
> user-chosen classes).

IMHO, it's not out of the question for the user to write some portion of 
the CSS file before they write the HTML. Or they might just opt not to 
visit any HTML files yet in the current session. Since the whole list of 
HTML tags is knows, it's better to offer them all.

Completion of user-chosen classes is a different matter, it would be 
great, and I've seen people asking for it a few times.



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [Emacs-diffs] master 269d563: Support completion of attribute values in CSS mode
  2016-03-26 13:23         ` Dmitry Gutov
@ 2016-03-26 13:52           ` Stefan Monnier
  2016-06-25 13:19             ` Simen Heggestøyl
  0 siblings, 1 reply; 7+ messages in thread
From: Stefan Monnier @ 2016-03-26 13:52 UTC (permalink / raw
  To: Dmitry Gutov; +Cc: Simen Heggestøyl, emacs-devel

>> BTW, even better (for me) would be to make the completion pay attention
>> to existing html buffers, so you can limit the completion to those tags
>> actually relevant (and you can additionally provide completion of
>> user-chosen classes).

> IMHO, it's not out of the question for the user to write some portion
> of the CSS file before they write the HTML.  Or they might just opt
> not to visit any HTML files yet in the current session.

Of course.  And the HTML code may not be held in an html-mode buffer
anyway (e.g. for my BuGit website, all the HTML templates are kept
directly in the sh script itself).

> Since the whole list of HTML tags is known, it's better to offer
> them all.

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.


        Stefan



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [Emacs-diffs] master 269d563: Support completion of attribute values in CSS mode
  2016-03-26 13:52           ` Stefan Monnier
@ 2016-06-25 13:19             ` Simen Heggestøyl
  0 siblings, 0 replies; 7+ messages in thread
From: Simen Heggestøyl @ 2016-06-25 13:19 UTC (permalink / raw
  To: Stefan Monnier; +Cc: emacs-devel, Dmitry Gutov

[-- 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


^ permalink raw reply related	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2016-06-25 13:19 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [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

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.