Personal emacs config
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

111 lines
4.1 KiB

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