Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trying to add support for elisp under restclient-mode #213

Closed
dsvensson opened this issue Mar 15, 2019 · 11 comments
Closed

Trying to add support for elisp under restclient-mode #213

dsvensson opened this issue Mar 15, 2019 · 11 comments

Comments

@dsvensson
Copy link

New to this package, seems very cool! But I'm having problems adding support for marking elisp under restclient-mode to be handled by polymode.

It has the following ways of declaring variables via emacs-lisp:

Single line:

:myvar := (some (artbitrary 'elisp)

Multi line:

:myvar := <<
(some-long-elisp
    (code spanning many lines)
#

My polymode declarations are as follows:

(defcustom pm-host/restclient
 (pm-host-chunkmode :name "restclient-mode"
                    :mode 'restclient-mode)
 "restclient emacs lisp variables mode"
 :group 'poly-hostmodes
 :type 'object)

(defcustom pm-inner/restclient-emacs-lisp-single
  (pm-inner-auto-chunkmode :name "restclient-emacs-single-line"
                           :head-matcher "^:[^ ]+ :="
                           :tail-matcher "$"
                           :mode-matcher 'emacs-lisp-mode)
  "Single line Emacs LISP."
  :group 'poly-innermodes
  :type 'object)

(defcustom pm-inner/restclient-emacs-lisp-multi
  (pm-inner-auto-chunkmode :name "restclient-emacs-lisp-multi-line"
                           :head-matcher "^:[^ ]+ := <<"
                           :tail-matcher "^#$"
                           :mode-matcher 'emacs-lisp-mode)
  "Multi line Emacs LISP."
  :group 'poly-innermodes
  :type 'object)

(define-polymode poly-restclient-mode
  :hostmode 'pm-host/restclient
  :innermodes '(pm-inner/restclient-emacs-lisp-single
                pm-inner/restclient-emacs-lisp-multi))

When I open a file and M-x restclient-mode nothing happens. If I M-x polymode-minor-mode, nothing happens, and if I M-x poly-restclient-mode I think I get emacs-lisp-code for the whole file.

@vspinu
Copy link
Collaborator

vspinu commented Mar 15, 2019

You need to use :mode slot not :mode-matcher, but there is a bug in polymode and it won't work as is. Will fix tomorrow. For now you can use this:

[Deleted wrong code]

@vspinu
Copy link
Collaborator

vspinu commented Mar 15, 2019

Arh, sorry. I got confused. You don't need the auto-innermode. Those are only for cases when you don't know the mode in advance. This will do:

[deleted obsoleted code]

@vspinu vspinu closed this as completed Mar 15, 2019
@dsvensson
Copy link
Author

@vspinu Thank you so much, works great. There's one issue with hl-line, but I'll check he issue list and file a bug if that's not in the list already. This is really cool stuff you've built!

@vspinu
Copy link
Collaborator

vspinu commented Mar 21, 2019

Actually it occurred to me that you would like to have the head of the chunk in restclient-mode as well.

I have changed the naming conventions and added new sugar macros to define innermodes. So the canonical way to define this mode would now be:

(define-hostmode poly-restclient-hostmode  
  :mode 'restclient-mode)

(define-innermode poly-restclient-elisp-root-innermode
  :mode 'emacs-lisp-mode
  :head-mode 'host
  :tail-mode 'host)

(define-innermode poly-restclient-elisp-single-innermode poly-restclient-elisp-root-innermode
  :head-matcher "^:[^ ]+ :="
  :tail-matcher "\n")

(define-innermode poly-restclient-elisp-multi-innermode poly-restclient-elisp-root-innermode
  :head-matcher "^:[^ ]+ := <<"
  :tail-matcher "^#$")

(define-polymode poly-restclient-mode
  :hostmode 'poly-restclient-hostmode
  :innermodes '(poly-restclient-elisp-single-innermode
                poly-restclient-elisp-multi-innermode))

@dsvensson
Copy link
Author

@vspinu Ah, thanks! Updated the package now and applied the configuration changes. hl-line still gets stuck though, possibly less, but is cleared when entering next section. But maybe your changes only fixed so that poly-mode is applied to the full section (a problem I hadn't noticed before).

Sample with some comments trying to describe what happens:

# -*- restclient -*-

:service-url = https://some.url/path/

:auth-token := (auth-source-pass-get 'secret "some/auth-token")

:headers = <<
Authorization: Token :auth-token
Content-Type: application/json
#

:query-stuff1 := << ;; stuck here when scrolling down
(graphql-query      ;; stuck here when scrolling up
 ((field1
   (field2
    subfield1))))   ;; stuck here when scrolling down, but clears the stuck line above
#                   #  stuck here when scrolling up

:query-stuff2 := << ;; same thing here, stuck again
(graphql-query      ;; again, stuck here on scroll up, but here is where the previous
 ((field1           ;; section :query-stuff1 last line clears
   field2
   (field3
    subfield1))))
#

# Fetch stuff
POST :service-url
:headers

{"query": ":query-stuff2"}

The benefits of having poly-mode greatly outweighs the minor annoyance with the hl-line issue, so thanks again!

@vspinu
Copy link
Collaborator

vspinu commented Mar 23, 2019

Is this the same as with #100? As far as I know hl-line should be treated fine. Would you mind opening a new issue with a screenshot?

@vspinu
Copy link
Collaborator

vspinu commented Mar 23, 2019

I cannot reproduce the hl-line problem with your example but I see some font-lock issues when highlighting headers though. The highlighting is changing as you type in the host mode. Do you see that?

@vspinu
Copy link
Collaborator

vspinu commented Mar 23, 2019

I see some font-lock issues when highlighting headers though

Ok, I got this. It's fixed now. Still no sign of your hl-line problem on my side.

@dsvensson
Copy link
Author

I've put together a minimal-isch init.el that reproduces the problem. I've verified it both in latest emacs from git on macOS, as well as 26.1 under Linux.

init.el:

(require 'package)

(add-to-list 'package-archives
             '("melpa" . "https://melpa.org/packages/")
             '("gnu" . "http://elpa.gnu.org/packages/"))

(package-initialize)

(custom-set-variables
 '(custom-file "~/.emacs.d/custom.el"))

(condition-case err
    (require 'use-package)
  (error (progn
           (package-refresh-contents)
           (package-install 'use-package)
           (require 'use-package))))

(custom-set-variables
 '(use-package-always-ensure t)
 '(use-package-compute-statistics t))

(use-package hl-line
  :config
  (global-hl-line-mode))

(use-package polymode
  :init
  (define-hostmode poly-restclient-hostmode
    :mode 'restclient-mode)

  (define-innermode poly-restclient-elisp-root-innermode
    :mode 'emacs-lisp-mode
    :head-mode 'host
    :tail-mode 'host)

  (define-innermode poly-restclient-elisp-single-innermode poly-restclient-elisp-root-innermode
    :head-matcher "^:[^ ]+ :="
    :tail-matcher "\n")

  (define-innermode poly-restclient-elisp-multi-innermode poly-restclient-elisp-root-innermode
    :head-matcher "^:[^ ]+ := <<"
    :tail-matcher "^#$")

  (define-polymode poly-restclient-mode
    :hostmode 'poly-restclient-hostmode
    :innermodes '(poly-restclient-elisp-single-innermode
                  poly-restclient-elisp-multi-innermode)))


(use-package restclient
  :commands
  (restclient-mode)
  :hook
  (restclient-mode . poly-restclient-mode)
  :custom
  (restclient-same-buffer-response t))

@vspinu
Copy link
Collaborator

vspinu commented Mar 25, 2019

Arh, I see. It's because of the global mode. It's activated in all buffers, base and indirect alike, so you get multiple lines. I was always testing with M-x hl-line-mode in the buffer.

@vspinu
Copy link
Collaborator

vspinu commented Mar 25, 2019

Fixed in master.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants