summaryrefslogtreecommitdiffstats
path: root/emacs.d/nxhtml/util/rxi.el
blob: 505d0b46f0dbd84d0b0f248e295d58bc13d8a429 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
;;; rxi.el --- Interactive regexp reading using rx format
;;
;; Author: Lennart Borgman (lennart O borgman A gmail O com)
;; Created: 2008-04-07T18:18:39+0200 Mon
;; Version:
;; Last-Updated:
;; URL:
;; Keywords:
;; Compatibility:
;;
;; Features that might be required by this library:
;;
;;   None
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; Commentary:
;;
;; Read regexp as `rx' forms from minibuffer.
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; Change log:
;;
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; This program is free software; you can redistribute it and/or
;; modify it under the terms of the GNU General Public License as
;; published by the Free Software Foundation; either version 2, or
;; (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;; General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program; see the file COPYING.  If not, write to
;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth
;; Floor, Boston, MA 02110-1301, USA.
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; Code:

(defvar rxi-read-hist nil)

(defun rxi-find-definition (rx-sym)
  (let* ((rec (assoc rx-sym rx-constituents))
         )
    (while (symbolp (cdr rec))
      (setq rec (assoc (cdr rec) rx-constituents)))
    (cdr rec)))

(defun rxi-list-type-p (rx-sym)
  (listp (rxi-find-definition rx-sym)))

(defun rxi-complete ()
  "Complete `rx' constituents."
  (interactive)
  ;; Don't care about state for now, there will be an error instead
  (let* ((partial (when (looking-back (rx (1+ (any "a-z01:|=>*?+\\-"))) nil t)
                    (match-string-no-properties 0)))
         (candidates (let ((want-list
                            (= ?\( (char-before (match-beginning 0)))))
                       (delq nil
                             (mapcar (lambda (rec)
                                       (let* ((sym (car rec))
                                              (lst (rxi-list-type-p sym)))
                                         (when (or (and want-list lst)
                                                   (and (not want-list)
                                                        (not lst)))
                                           (symbol-name sym))))
                                     rx-constituents))))
         (match-set (when partial
                      (all-completions
                       partial
                       candidates))))
    (cond
     ((not match-set)
      (message "No completions"))
     ((= 1 (length match-set))
      (insert (substring (car match-set) (length partial))))
     (t
      (with-output-to-temp-buffer "*Completions*"
          (display-completion-list match-set partial))))))

(defvar rxi-read-keymap
  (let ((map (make-sparse-keymap)))
    (set-keymap-parent map minibuffer-local-completion-map)
    (define-key map [tab] 'rxi-complete)
    (define-key map [(meta tab)] 'rxi-complete)
    (define-key map [?\ ] 'self-insert-command)
    map))

(defvar rxi-trailing-overlay nil)

(defun rxi-minibuf-setup ()
  (when rxi-trailing-overlay (delete-overlay rxi-trailing-overlay))
  (setq rxi-trailing-overlay
        (make-overlay (point-max) (point-max)
                      (current-buffer)
                      t t))
  (overlay-put rxi-trailing-overlay 'after-string
               (propertize ")"
                           'face
                           (if (and
                                (fboundp 'noticeable-minibuffer-prompts-mode)
                                noticeable-minibuffer-prompts-mode)
                               'minibuffer-noticeable-prompt
                             'minibuffer-prompt)))
  (remove-hook 'minibuffer-setup-hook 'rxi-minibuf-setup))

(defun rxi-minibuf-exit ()
  (when rxi-trailing-overlay
    (delete-overlay rxi-trailing-overlay)
    (setq rxi-trailing-overlay nil))
  (remove-hook 'minibuffer-exit-hook 'rxi-minibuf-exit))

(defun rxi-read (prompt)
  "Read a `rx' regexp form from minibuffer.
Return cons of rx and regexp, both as strings."
  (interactive (list "Test (rx "))
  (let (rx-str rx-full-str res-regexp)
    (while (not res-regexp)
      (condition-case err
          (progn
            (add-hook 'minibuffer-setup-hook 'rxi-minibuf-setup)
            (add-hook 'minibuffer-exit-hook 'rxi-minibuf-exit)
            (setq rx-str (read-from-minibuffer prompt
                                               rx-str ;; initial-contents
                                               rxi-read-keymap
                                               nil ;; read
                                               'rxi-read-hist
                                               nil ;; inherit-input-method - no idea...
                                               ))
            (setq rx-full-str (concat "(rx " rx-str ")"))
            (setq res-regexp (eval (read rx-full-str))))
        (error (message "%s" (error-message-string err))
               (sit-for 2))))
    (when (with-no-warnings (called-interactively-p)) (message "%s => \"%s\"" rx-full-str res-regexp))
    (cons rx-full-str res-regexp)))


(provide 'rxi)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; rxi.el ends here