~a-roehler/python-mode/components-python-mode

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
;;; python-components-macros.el --- Macro definitions

;; Copyright (C) 2012  Andreas Roehler

;; Author: Andreas Roehler <andreas.roehler@online.de>
;; Keywords:

;; 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/>.

;;; Commentary:

;;

;;; Code:

;; (defmacro py-beginning-of-comment ()
;;   "Go to the beginning of current line's comment, if any.
;; 
;; Returns position if succesful. "
;;   `(save-restriction
;;      (widen)
;;      (if (looking-at ,comment-start)
;;          (point)
;;        (let ((pps (parse-partial-sexp (line-beginning-position) (point))))
;;          (and (nth 4 pps)
;;               (goto-char (nth 8 pps)))))))

;; (defun py-beginning-of-comment ()
;;   "Go to the beginning of current line's comment, if any.
;;
;; Returns position if succesful. "
;;   (interactive)
;;   (save-restriction
;;     (widen)
;;     (if (looking-at comment-start)
;;         (point)
;;       (let ((pps (parse-partial-sexp (line-beginning-position) (point))))
;;         (and (nth 4 pps)
;;              (goto-char (nth 8 pps)))))))

;; (defun py-separator-char ()
;;   "Return the file-path separator char from current machine.
;; 
;; When `py-separator-char' is customized, its taken.
;; Returns char found. "
;;   (let ((erg (cond ((characterp py-separator-char)
;;                     (char-to-string py-separator-char))
;;                    ;; epd hack
;;                    ((and
;;                      (string-match "[Ii][Pp]ython" py-shell-name)
;;                      (string-match "epd\\|EPD" py-shell-name))
;;                     (setq erg (shell-command-to-string (concat py-shell-name " -c \"import os; print(os.sep)\"")))
;;                     (setq erg (replace-regexp-in-string "\n" "" erg))
;;                     (when (string-match "^$" erg)
;;                       (setq erg (substring erg (string-match "^$" erg)))))
;;                    (t (setq erg (shell-command-to-string (concat py-shell-name " -W ignore" " -c \"import os; print(os.sep)\"")))))))
;;     (replace-regexp-in-string "\n" "" erg)))

(defmacro pps-emacs-version ()
  "Include the appropriate `parse-partial-sexp' "
  `(if (featurep 'xemacs)
       '(parse-partial-sexp (point-min) (point))
     '(syntax-ppss)))

(defmacro empty-line-p ()
  "Returns t if cursor is at an line with nothing but whitespace-characters, nil otherwise."
  (interactive "p")
  `(save-excursion
     (progn
       (beginning-of-line)
       (looking-at "\\s-*$"))))

(defmacro py-escaped ()
  "Return t if char is preceded by an odd number of backslashes. "
  `(save-excursion
     (< 0 (% (abs (skip-chars-backward "\\\\")) 2))))

(defmacro py-current-line-backslashed-p ()
  "Return t if current line is a backslashed continuation line. "
  `(save-excursion
     (end-of-line)
     (skip-chars-backward " \t\r\n\f")
     (and (eq (char-before (point)) ?\\ )
          (py-escaped))))

(defmacro py-preceding-line-backslashed-p ()
  "Return t if preceding line is a backslashed continuation line. "
  `(save-excursion
     (beginning-of-line)
     (skip-chars-backward " \t\r\n\f")
     (and (eq (char-before (point)) ?\\ )
          (py-escaped))))

;; (defmacro py-continuation-line-p ()
;;   "Return t iff current line is a continuation line."
;;   `(save-excursion
;;      (beginning-of-line)
;;      (or (py-preceding-line-backslashed-p)
;;          (< 0 (nth 0 (syntax-ppss))))))

(defmacro py-count-lines ()
  "Count lines in buffer, optional without given boundaries.

See http://debbugs.gnu.org/cgi/bugreport.cgi?bug=7115"
  (save-restriction
    (widen)
    `(if (featurep 'xemacs)
         (count-lines (point-min) (point-max))
       (count-matches "[\n\C-m]" (point-min) (point-max)))))

;; (defun py-in-string-or-comment-p ()
;;   "Returns beginning position if inside a string or comment, nil otherwise. "
;;   (interactive)
;;   (let* ((erg (nth 8 (if (featurep 'xemacs)
;;                          (parse-partial-sexp (point-min) (point))
;;                        (syntax-ppss))))
;;          (la (unless erg (when (or (looking-at "\"")(looking-at comment-start)(looking-at comment-start-skip))
;;                            (match-beginning 0)))))
;;     (setq erg (or erg la))
;;     (when (interactive-p) (message "%s" erg))
;;     erg))  a4643 (#o11043, #x1223, ?ሣ)

;; (defmacro py-in-string-or-comment-p ()
;;   "Returns beginning position if inside a string or comment, nil otherwise. "
;;   `(or (nth 8 (syntax-ppss))
;;        (when (or (looking-at "\"")(looking-at "[ \t]*#[ \t]*"))
;;          (match-beginning 0))))

(provide 'python-components-macros)
;;; python-components-macros.el ends here