From 7844c97291b2a7c0b0ec9e87587b9360f1275220 Mon Sep 17 00:00:00 2001 From: Ivan Malison Date: Tue, 14 Jul 2015 03:49:33 -0700 Subject: [PATCH 1/3] Add imenu support. --- restclient.el | 70 ++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 64 insertions(+), 6 deletions(-) diff --git a/restclient.el b/restclient.el index d65e65d..bc8ba1b 100644 --- a/restclient.el +++ b/restclient.el @@ -251,6 +251,10 @@ (defconst restclient-evar-regexp "^\\(:[^: ]+\\)\\s-+:=\\s-+\\(.+\\)$") +(defun restclient-goto-current-min () + (interactive) + (goto-char (restclient-current-min))) + (defun restclient-current-min () (save-excursion (beginning-of-line) @@ -296,12 +300,12 @@ (defun restclient-eval-var (string) (with-output-to-string (princ (eval (read string))))) -(defun restclient-http-parse-current-and-do (func &rest args) +(defun restclient-http-parse-current () (save-excursion (goto-char (restclient-current-min)) (when (re-search-forward restclient-method-url-regexp (point-max) t) - (let ((method (buffer-substring-no-properties (match-beginning 1) (match-end 1))) - (url (buffer-substring-no-properties (match-beginning 2) (match-end 2))) + (let ((method (match-string-no-properties 1)) + (url (match-string-no-properties 2)) (headers '())) (forward-line) (while (re-search-forward restclient-header-regexp (point-at-eol) t) @@ -317,7 +321,12 @@ (url (restclient-replace-all-in-string vars url)) (headers (restclient-replace-all-in-headers vars headers)) (entity (restclient-replace-all-in-string vars entity))) - (apply func method url headers entity args)))))) + (list method url headers entity)))))) + +(defun restclient-http-parse-current-and-do (func &rest args) + (cl-destructuring-bind (method url headers entity) + (restclient-http-parse-current) + (apply func method url headers entity args))) (defun restclient-copy-curl-command () "formats the request as a curl command and copies the command to the clipboard" @@ -353,8 +362,58 @@ (goto-char (restclient-current-min)) (setq last-min (point)))) (goto-char (restclient-current-max)) + (unless (eobp) (forward-char)) (goto-char (restclient-current-min))) +(defun restclient-current-comment () + (save-excursion + (let ((current-min (restclient-current-min)) + (previous-max (progn (restclient-jump-prev) + (restclient-current-max)))) + (when (> current-min previous-max) + (goto-char previous-max) + (re-search-forward "^#" current-min t) + (beginning-of-line) + (while (looking-at "^#") + (forward-line) + (beginning-of-line)) + (buffer-substring-no-properties previous-max (point)))))) + +(defun restclient-create-imenu-index-function () + (save-excursion + (beginning-of-buffer) + (let ((last -1) heading) + (cl-loop while (< last (point)) + do + (progn + (setq last (point)) + (setq marker (point-marker)) + (setq heading (restclient-create-imenu-index-heading)) + (message heading) + (restclient-jump-next) + ) + when heading + collect `(,heading . ,marker))))) + +(defun restclient-create-imenu-index-heading () + (save-excursion + (let ((current-comment (restclient-oneline-comment))) + (when current-comment + (let ((method (progn (restclient-current-min) + (re-search-forward restclient-method-url-regexp (point-max) t) + (match-string-no-properties 1))) + (url (match-string-no-properties 2))) + (format "%s %s (%s)" method url current-comment)))))) + +(defun restclient-oneline-comment () + (let* ((original-comment (restclient-current-comment)) + (res (when original-comment (replace-regexp-in-string "\n" "|" original-comment)))) + (when res + (cl-loop for string-to-remove in '("#" "^|" "^ " "|$") + do + (setq res (replace-regexp-in-string string-to-remove "" res)))) + res)) + (defun restclient-jump-prev () (interactive) (let* ((current-min (restclient-current-min)) @@ -367,9 +426,8 @@ (forward-line -1) (beginning-of-line)) (point))))) - (unless (eq (point-min) end-of-entity) (goto-char end-of-entity) - (goto-char (restclient-current-min))))) + (goto-char (restclient-current-min)))) (defun restclient-mark-current () (interactive) From 14a55d61dc62893552cab6524ac9dca5872a583e Mon Sep 17 00:00:00 2001 From: Ivan Malison Date: Tue, 18 Aug 2015 12:16:16 -0700 Subject: [PATCH 2/3] actually set imenu functions up --- restclient.el | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/restclient.el b/restclient.el index bc8ba1b..15b27b7 100644 --- a/restclient.el +++ b/restclient.el @@ -379,7 +379,7 @@ (beginning-of-line)) (buffer-substring-no-properties previous-max (point)))))) -(defun restclient-create-imenu-index-function () +(defun restclient-create-imenu-index () (save-excursion (beginning-of-buffer) (let ((last -1) heading) @@ -462,7 +462,7 @@ (set (make-local-variable 'comment-start) "# ") (set (make-local-variable 'comment-start-skip) "# *") (set (make-local-variable 'comment-column) 48) - + (set (make-local-variable 'imenu-create-index-function) #'restclient-create-imenu-index) (set (make-local-variable 'font-lock-defaults) '(restclient-mode-keywords))) From b0f63f8748882aee0211624971a2639f29de5c61 Mon Sep 17 00:00:00 2001 From: Ivan Malison Date: Thu, 19 Nov 2015 18:54:49 -0800 Subject: [PATCH 3/3] Remove restclient-goto-min, fix compile warnings, restore old restclient-gooooot-current-min behavior. --- restclient.el | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/restclient.el b/restclient.el index c2b0e40..c858df3 100644 --- a/restclient.el +++ b/restclient.el @@ -261,10 +261,6 @@ The buffer contains the raw HTTP response sent by the server." (defconst restclient-evar-regexp "^\\(:[^: ]+\\)\\s-+:=\\s-+\\(.+\\)$") -(defun restclient-goto-current-min () - (interactive) - (goto-char (restclient-current-min))) - (defun restclient-current-min () (save-excursion (beginning-of-line) @@ -397,17 +393,15 @@ Optional argument STAY-IN-WINDOW do not move focus to response buffer if t." (defun restclient-create-imenu-index () (save-excursion - (beginning-of-buffer) - (let ((last -1) heading) + (goto-char (point-min)) + (let ((last -1) heading marker) (cl-loop while (< last (point)) do (progn (setq last (point)) (setq marker (point-marker)) (setq heading (restclient-create-imenu-index-heading)) - (message heading) - (restclient-jump-next) - ) + (restclient-jump-next)) when heading collect `(,heading . ,marker))))) @@ -443,8 +437,7 @@ Optional argument STAY-IN-WINDOW do not move focus to response buffer if t." (forward-line -1) (beginning-of-line)) (point))))) - (goto-char end-of-entity) - (goto-char (restclient-current-min)))) + (goto-char end-of-entity))) (defun restclient-mark-current () "Mark current request."