-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtog-parse.el
70 lines (53 loc) · 2.42 KB
/
tog-parse.el
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
;;; tog-parse.el --- Parsing functions for tog -*- lexical-binding: t; -*-
;; Copyright (c) 2019 Abhinav Tushar
;; Author: Abhinav Tushar <[email protected]>
;;; Commentary:
;; Parsing functions for tog
;; This file is not a part of GNU Emacs.
;;; License:
;; 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 3 of the License, 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. If not, see <http://www.gnu.org/licenses/>.
;;; Code:
(defun tog-parse-line-id (&optional pattern)
"Return line id in integer. The default assumption is that the
text goes like:
1. text here"
(let ((pattern (or pattern "^\\([[:digit:]]\\)\. ")))
(save-excursion
(goto-char (line-beginning-position))
(when (re-search-forward pattern (line-end-position) t)
(string-to-number (match-string-no-properties 1))))))
(defun tog-parse-line (&optional pattern)
"Parse line string. PATTERN identifies the part to take by
specifying group number 1."
(let ((pattern (or pattern "^[[:digit:]]\. \\(.*\\)")))
(save-excursion
(goto-char (line-beginning-position))
(when (re-search-forward pattern (line-end-position) t)
(match-string-no-properties 1)))))
(defun tog-parse-text-range (&optional pattern)
"Return marked range relative to the text. PATTERN identifies
the parts to ignore. If defaults to the usual assumption about
having an integer with a full stop at the end.
We return a list instead of cons since that goes well in
json-encode*."
(when (region-active-p)
(let ((bounds (car (region-bounds)))
(line-start (line-beginning-position))
(pattern (or pattern "^[[:digit:]]\. ")))
(save-excursion
(goto-char (line-beginning-position))
(when (re-search-forward pattern (line-end-position) t)
(let ((offset (+ line-start (length (match-string-no-properties 0)))))
(list (- (car bounds) offset)
(- (cdr bounds) offset))))))))
(provide 'tog-parse)
;;; tog-parse.el ends here