summaryrefslogtreecommitdiffstats
path: root/scripts/kde-emacs/kde-emacs-general.el
blob: 23bc9b7f051d336576e3e92bafadd18088a7229a (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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
;; kde-emacs-general.el
;;
;; Copyright (C)  2002  KDE Development Team <www.kde.org>
;;
;; This library is free software; you can redistribute it and/or
;; modify it under the terms of the GNU Lesser General Public
;; License as published by the Free Software Foundation; either
;; version 2.1 of the License, or (at your option) any later version.
;;
;; This library 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
;; Lesser General Public License for more details.
;;
;; You should have received a copy of the GNU Lesser General Public
;; License along with this library; if not, write to the Free Software
;; Foundation, Inc., 51 Franklin Steet, Fifth Floor, Boston, MA
;; 02110-1301  USA

;;; Commentary:
;;

;;; Code:

(require 'kde-emacs-vars)

;*---------------------------------------------------------------------*/
;*    Functions  ...                                                   */
;*---------------------------------------------------------------------*/

;; Helper for kde-file-get-cpp-h
(defun kde-find-file (filename basedir)
  "Looks for \"filename\" under \"basedir\""
  (if basedir
      (let ((path (concat basedir "/" filename)))
	(if (file-readable-p path)
	    path))
    )
)

(defun kde-file-get-cpp-h ()
  "Function returns a corresponding source or header file. The returned
variable is a list of the form (FILENAME IS_READABLE) e.g. when being in
test.h file and having test.cpp file readable in the same directory it will
return (\"test.cpp\" t)."
  (interactive)
  (let* ((name (buffer-file-name))
	 (nname (file-name-sans-extension name))
	 (ext (file-name-extension name))
	 (path nil)
	 (ret nil)
	 (listit nil))
    (cond
     ((member ext kde-header-files)
      (set listit kde-source-files)
      (while (and listit (not ret)) ; loop over the list but stop once ret is set
	(set path (concat nname "." (car listit)))
	(if (file-readable-p path)
	    (set ret (cons path t))
	  )
	(if (not ret)
	    (if (string-match "_p$" nname)
		(progn 
		  (set path (concat (substring nname 0 (string-match "_p$" nname)) "." (car listit)))
		  (if (file-readable-p path)
		      (set ret (cons path t))
		    )))
	  )
	(if (not ret)
	    (progn ; look in kde-source-directory
	      (set path (kde-find-file (file-name-nondirectory path) kde-source-directory))
	      (if (and
		   path
		   (file-readable-p path))
		  (set ret (cons path t))
		))
	  )
	(set listit (cdr listit)) ; ++listit
	)
      ; not found, will create one
      (if (not ret)
	  (set ret (cons (concat nname "." kde-prefered-source-extension) nil ))
	))

     ((member ext kde-source-files)
      (set listit kde-header-files)
      (while (and listit (not ret)) ; loop over the list but stop once ret is set
	(set path (concat nname "." (car listit)))
        ; look in current dir
	(if (file-readable-p path)
	    (set ret (cons path t)))
	(if (not ret) ;check for header_p.h files
	    (progn (set path (concat nname "_p." (car listit)))
		   (if (file-readable-p path)
		       (set ret (cons path t)))))
	(if (not (file-readable-p path))
	    (progn ;  look in kde-include-directory
	      (set path (kde-find-file (file-name-nondirectory path) kde-include-directory))
	      (if (and 
		   path
		   (file-readable-p path))
		  (set ret (cons path t))
		))
	  )
	(set listit (cdr listit)) ; ++listit
	)
      ; not found, will create one
      (if (not ret)
	  (set ret (cons (concat nname "." (car kde-header-files)) nil ))
	))
     )
    ret
    ))

(defun kde-switch-cpp-h ()
  "Switches between the source and the header file 
(both directions)."
  (interactive)
  (let ((file (kde-file-get-cpp-h)))
    (if (car file)
	(find-file (car file))
      (error "Corresponding source file doesn't exist.")
    )
  ))

(defun kde-delete-backward-ws ()
  "Function deletes all preceding whitespace characters."
  (interactive)
  (let ((start (point))
	end)
    (save-excursion
      (set end (re-search-backward "[^ \t]" (point-at-bol) t))
      (if (not end)
	  (set end (point-at-bol))
	(set end (1+ end))))
    (delete-backward-char (- start end))))

(defun kde-skip-blank-lines ()
  "Skips backwards past blank lines, stopping
at a first non-blank line"
  (let* ((start (point-at-bol))
	 (end (point-at-eol))
	 (mstring (buffer-substring start end))
	 (ret 0))
    (while (or 
	    (string-match "^[ \t\r\n]+$" mstring)
	    (and (string= mstring "")
		 (= ret 0)))
        (set ret (forward-line -1))	; if ret != 0, we stop, since we're at the first line...
	(set start (point-at-bol)
	      end   (point-at-eol))
	(set mstring (buffer-substring start end))
	)
    ))

(defun kde-comments-begin ()
  "Skip back from current point past any preceding C-based comments at the beginning of lines.
Presumes no \"/*\" strings are nested within multi-line comments."
  (let ((opoint))
    (while (progn (set opoint (point))
		  ;; To previous line
		  (if (zerop (forward-line -1))
		      (cond
		       ;; If begins with "//" or ends with "*/", then is a
		       ;; comment.
		       ((looking-at "[ \t]*\\(//\\|$\\)"))
		       ((looking-at ".*\\*/[ \t]*$")
			(progn (end-of-line)
			       ;; Avoid //*** single line comments here.
			       (if (re-search-backward "\\(^\\|[^/]\\)/\\*" nil t)
				   (progn (beginning-of-line)
					  (looking-at "[ \t]*/\\*")))))
		       (t nil)))))
    (goto-char opoint)
    ;; Skip past whitespace
    (skip-chars-forward " \t\n\r\f")
    (beginning-of-line)))

(provide 'kde-emacs-general)