Advertisement
Guest User

Untitled

a guest
Apr 19th, 2015
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.04 KB | None | 0 0
  1. ;; Emacs lisp for distinguishing mode between matlab, objective-c, c, c++.
  2. ;; Usage: put the following into emacs init file
  3. ;; (require 'choose-mode)
  4. ;; (when (featurep 'choose-mode)
  5. ;; (push '("\\.h\\'" . choose-mode-for-dot-h) auto-mode-alist)
  6. ;; (push '("\\.m\\'" . choose-mode-for-dot-m) auto-mode-alist))
  7.  
  8. (provide 'choose-mode)
  9.  
  10. ;;;======================================================================
  11. ;;; utility functions
  12. ;;;======================================================================
  13. (defun filename-with-new-extension (file ext)
  14. (unless (char-equal (aref ext 0) ?.)
  15. (setq ext (concat "." ext)))
  16. (concat (file-name-sans-extension file) ext))
  17.  
  18. (defun buffer-contains-regexp (regexp)
  19. (save-excursion
  20. (goto-char (point-min))
  21. (search-forward-regexp regexp nil t)))
  22.  
  23. (defun visit-file-for-fn (file func &rest args)
  24. (if (not (file-exists-p file))
  25. nil
  26. (let ((buf (create-file-buffer file))
  27. result)
  28. (set-buffer buf)
  29. (insert-file-contents file nil nil nil t)
  30. (setq result (apply func args))
  31. (kill-buffer buf)
  32. result)))
  33.  
  34. (defun num-files-in-dir-with-ext (dir ext)
  35. (let ((count 0))
  36. (dolist (fnm (directory-files dir) count)
  37. (when (equal (file-name-extension fnm) ext)
  38. (setq count (+ 1 count))))))
  39.  
  40. (defun other-files-in-buffer-dir-with-ext-p (ext)
  41. (let ((bufdir (file-name-directory (buffer-file-name))))
  42. (> (num-files-in-dir-with-ext bufdir ext) 0)))
  43.  
  44. (defun choose-mode (mode)
  45. ;(message "choosing %s" (prin1-to-string mode))
  46. (apply mode ())
  47. t)
  48.  
  49.  
  50. ;;;======================================================================
  51. ;;; .h: distinguish between c, c++, and objective-c header files
  52. ;;;======================================================================
  53. (defun choose-mode-for-dot-h ()
  54. (when (string-match "\\(.*\\)\.h$" (buffer-file-name))
  55. (let ((base_str (match-string 1 (buffer-file-name))))
  56.  
  57. ;; first see if there's a corresponding implemetation file
  58. (unless
  59. (cond ((file-exists-p (concat base_str ".c")) (c-mode) t)
  60. ((file-exists-p (concat base_str ".cc")) (c++-mode) t)
  61. ((file-exists-p (concat base_str ".cpp")) (c++-mode) t)
  62. ((file-exists-p (concat base_str ".cxx")) (c++-mode) t)
  63. ((file-exists-p (concat base_str ".m")) (objc-mode) t)
  64. ((file-exists-p (concat base_str ".mm")) (objc-mode) t))
  65.  
  66. ;; if not, search the buffer for hints
  67. (message "no implementation file; searching for hints")
  68. (cond ((buffer-contains-regexp "@interface") (objc-mode))
  69. ((buffer-contains-regexp "^[ \t]*class") (c++-mode))
  70. (t (c-mode)))))))
  71.  
  72. (push '("\\.h\\'" . choose-mode-for-dot-h) auto-mode-alist)
  73.  
  74.  
  75.  
  76. ;;;======================================================================
  77. ;;; .m: distinguish between objective-c and matlab files
  78. ;;;======================================================================
  79.  
  80. (defun matlab-ish-mbuffer-p ()
  81. ;(message "checking for matlab-ish .m buffer")
  82. (or (buffer-contains-regexp "^[ \t]*function[ \t]")
  83. (buffer-contains-regexp "^[ \t]*%")))
  84.  
  85. (defun objc-implementation-ish-mbuffer-p ()
  86. (or (buffer-contains-regexp "@implementation")
  87. (buffer-contains-regexp "@import")
  88. (buffer-contains-regexp "@NSString")))
  89.  
  90. (defun matlab-mode-if-available ()
  91. (if (functionp 'matlab-mode)
  92. (choose-mode 'matlab-mode)
  93. ;(message "matlab-mode is unavailable")
  94. nil))
  95.  
  96. (defun matlab-ish-mbuffer-check ()
  97. ;(message "checking for matlab-ish .m file")
  98. (if (not (matlab-ish-mbuffer-p)) nil
  99. (message "matlab-ish buffer detected");
  100. (matlab-mode-if-available)))
  101.  
  102. (defun objc-implementation-ish-mbuffer-check ()
  103. ;(message "checking for objc-ish .m file")
  104. (if (not (objc-implementation-ish-mbuffer-p)) nil
  105. (message "objc-ish buffer detected")
  106. (choose-mode 'objc-mode)))
  107.  
  108. (defun objc-corresponding-hfile-check ()
  109. ;(message "checking for objc-ish corresponding .h file")
  110. (if (not (file-exists-p (filename-with-new-extension (buffer-file-name) ".h")))
  111. nil
  112. (message "corresponding .h file detected")
  113. (choose-mode 'objc-mode)))
  114.  
  115. (defun apply-objc-matlab-last-mode ()
  116. (if 'objc-matlab-last-mode
  117. (progn
  118. (message "applying last mode (%s)"
  119. (prin1-to-string objc-matlab-last-mode))
  120. (apply objc-matlab-last-mode ()))
  121. (message "choose-mode-for-dot-m: no objc-matlab-last-mode to apply")))
  122.  
  123. (defun choose-mode-for-dot-m ()
  124. (or
  125. ; check for mode-specific keywords
  126. (matlab-ish-mbuffer-check)
  127. (objc-implementation-ish-mbuffer-check)
  128. ; if the corresponding header file is empty, assume objc
  129. (objc-corresponding-hfile-check)
  130. ; run the most recently used mode
  131. (apply-objc-matlab-last-mode)))
  132.  
  133. (defun choose-mode-matlab-mode-hook ()
  134. ;(message "objc-matlab-last-mode = matlab-mode")
  135. (setq objc-matlab-last-mode 'matlab-mode))
  136. (add-hook 'matlab-mode-hook 'choose-mode-matlab-mode-hook)
  137.  
  138. (defun choose-mode-objc-mode-hook ()
  139. ;(message "objc-matlab-last-mode = objc-mode")
  140. (setq objc-matlab-last-mode 'objc-mode))
  141. (add-hook 'objc-mode-hook 'choose-mode-objc-mode-hook);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement