]> code.delx.au - gnu-emacs-elpa/blob - packages/realgud/realgud/common/lang.el
3d32891c9b52ba460e80929299b5a03dafc1cc6c
[gnu-emacs-elpa] / packages / realgud / realgud / common / lang.el
1 ;;; Copyright (C) 2010, 2012, 2014-2015 Rocky Bernstein <rocky@gnu.org>
2 ;;; Programming language specific stuff.
3 (require 'load-relative)
4
5 (defun realgud-lang-mode? (filename lang-str)
6 "Return true if FILENAME is a buffer we are visiting a buffer
7 that is in LANG-STR mode. The test is just that the major mode
8 starts LANG-STR."
9 (let ((buffer (and filename (find-buffer-visiting filename)))
10 (match-pos))
11 (cond (buffer
12 (save-current-buffer
13 (set-buffer buffer)
14 (setq match-pos
15 (string-match (format "^%s-" lang-str)
16 (format "%s" major-mode))))
17 (and match-pos (= 0 match-pos)))
18 ('t nil)
19 ))
20 )
21
22 (defun realgud:suggest-file-from-buffer (lang-str &optional opt-buff-list)
23 "Suggest the first in the buffer list for which test-func is
24 't. Typically this is used. To search for a buffer in one of
25 the programming modes like Ruby or Python."
26 (let ((file)
27 (buff)
28 (not-found 't)
29 (buff-list (or opt-buff-list (buffer-list)))
30 )
31 (while (and not-found (setq buff (car-safe buff-list)))
32 (setq buff-list (cdr buff-list))
33 (setq file (buffer-file-name buff))
34 (if (realgud-lang-mode? file lang-str)
35 (setq not-found nil)
36 ))
37 (if not-found nil file)
38 )
39 )
40
41 (defun realgud-suggest-lang-file (lang-str lang-ext-regexp &optional last-resort)
42 "Suggest a file to debug. We search for the the major mode for
43 that programming language using we check filenames using
44 LANG-EXT-REGEXP. For example, for ruby LANG-STR would be 'ruby'
45 and LANG-EXT-REGEXP would be '\\.rb$'.
46
47 Buffers and files are ranked with a priority. Higher is more
48 priority and selected will be selected over lower-priorities.
49
50 The first priority is given to the current buffer. If the major
51 mode matches LANG-STR, then we are done. If not, we'll set
52 priority 2 and we keep going. Then we will try files in the
53 default-directory. Of those buffers we are visiting, we check the
54 major mode. The first one we find we will return. Failing this,
55 we see if the file is executable and has a LANG-EXT suffix. These
56 have priority 8. Failing that, we'll go for just having a
57 LANG-EXT suffix. These have priority 7. And other executable
58 files that are not directories have priority 6 if they have the
59 right LANG-EXT, otherwise they are priority 5.
60
61 Within a given priority, we use the first one we find."
62 (let* ((file)
63 (file-list (directory-files default-directory))
64 (priority 2)
65 (is-not-directory)
66 (result (buffer-file-name)))
67 (unless (realgud-lang-mode? result lang-str)
68 (while (and (setq file (car-safe file-list)) (< priority 8))
69 (setq file-list (cdr file-list))
70 (when (realgud-lang-mode? file lang-str)
71 (setq result file)
72 (setq priority
73 (if (file-executable-p file)
74 (setq priority 8)
75 (setq priority 7))))
76 ;; The file isn't in a language-mode buffer,
77 ;; Check for an executable file with a language extension.
78 (if (and file (file-executable-p file)
79 (setq is-not-directory (not (file-directory-p file))))
80 (if (and (string-match lang-ext-regexp file))
81 (if (< priority 6)
82 (progn
83 (setq result file)
84 (setq priority 6))))
85 (when (and is-not-directory (< priority 5))
86 ;; Found some sort of regular file.
87 (setq result file)
88 (setq priority 5))
89 ))
90 )
91 (if (< priority 6)
92 (if (setq file (realgud:suggest-file-from-buffer lang-str))
93 (setq result file)
94 (if last-resort (setq result last-resort))))
95 result)
96 )
97
98 (provide-me "realgud-")