0

I'm trying to create a major-mode for HTTP requests in emacs, I'm stuck at the syntax coloring for regexs at the same line.

let this be the HTTP request I'm trying to highlight:

    GET /print_hello_world HTTP/2.0
    host: mydomain.com
    accept-encoding: gzip, deflate, br
    accept-language: en-US,en;q=0.5
    accept: */*
    
    {"message" :"hello world"}

now in the first line want to highlight the first line GET with a face and the endpoint /print_hello_world with another, and for headers I want to use a face for header key and another for header value. I tried using:

(define-derived-mode http-request-mode prog-mode 
  (setq-local font-lock-defaults '(http-request-font-lock))
  (setq-local comment-start "# "))
  
;;; 
(defvar http-request-font-lock
    `(
        ;; endpoint
        (,(rx
             line-start
             symbol-start (or "GET" "POST" "PATCH" "PUT" "DELETE" "TRACE" "OPTIONS") symbol-end 
             space
             symbol-start (one-or-more (any alphanumeric punct)) symbol-end
             space) .
             'font-lock-file-face)
         ;; method
        (,(rx line-start
                    symbol-start
                    (or "GET" "POST" "PATCH" "PUT" "DELETE" "TRACE" "OPTIONS")
                    symbol-end )
         . 'font-lock-method-face)
         ;; http header key
        (,(rx line-start symbol-start (one-or-more (any alphanumeric ?- ?_)) symbol-end ":") . 'font-lock-header-key-face )
        ;; http header value 
        (,(rx ": " symbol-start (one-or-more not-newline) symbol-end eol) . 'font-lock-header-value-face )
        ))

;;; faces
(defface font-lock-header-key-face
  '((t :foreground "white" :background "blue"))
  "Font Lock mode face used to highlight variable names."
  )

(defface font-lock-method-face
  '((t :weight bold ))
  "Font Lock mode face used to highlight variable names."
  )

(defface font-lock-path-face
  '((t :slant italic :foreground "red"))
  "Font Lock mode face used to highlight variable names."
  )


(defface font-lock-hversion-face
  '((t :foreground "yellow"))
  "Font Lock mode face used to highlight variable names."
  )

(defface font-lock-header-value-face
  '((t :weight bold :slant italic ))
  "Font Lock mode face used to highlight variable names."
  )

        

this will result in the following:

  1. neither the method nor the endpoint got highlighted
  2. only the header key is hightlighted properly
  3. the header value is only hightlighted when I have a line with only this : header-value

1 Answer 1

0

I was supposed to use regex groups and their index to match faces like this:

(defvar http-headers-font-lock
    `((,(rx
             line-start
             (group (or "GET" "POST" "PATCH" "PUT" "DELETE" "TRACE" "OPTIONS") space)
             
             (group (+? not-newline) space)
             (group (+? not-newline) ) "\n"
             )
         (1 'font-lock-http-method-face )
         (2 'font-lock-http-path-face )
         (3 'font-lock-http-hversion-face))
        (,(rx
             (group (+? (any alnum ?- ?_ ))) "=" (group (+? (any alnum punct )) ) (or "; " "&" eol)
             )
         (1 'font-lock-http-lvalue-key-face)
         
         )
        (,(rx
                (group (+? (any alnum ?- ?_ ))) ": " (group (+? (any alnum punct space)) eol))
         (1 'font-lock-http-header-key-face)
         (2 'font-lock-http-header-value-face)
         )
        
        )
    )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.