bibtex-style.el 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. ;;; bibtex-style.el --- Major mode for BibTeX Style files -*- lexical-binding: t -*-
  2. ;; Copyright (C) 2005, 2007-2012 Free Software Foundation, Inc.
  3. ;; Author: Stefan Monnier <monnier@iro.umontreal.ca>
  4. ;; Keywords: tex
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;; Done: font-lock, imenu, outline, commenting, indentation.
  18. ;; Todo: tab-completion.
  19. ;; Bugs:
  20. ;;; Code:
  21. (defvar bibtex-style-mode-syntax-table
  22. (let ((st (make-syntax-table)))
  23. (modify-syntax-entry ?% "<" st)
  24. (modify-syntax-entry ?\n ">" st)
  25. (modify-syntax-entry ?\{ "(}" st)
  26. (modify-syntax-entry ?\} "){" st)
  27. (modify-syntax-entry ?\" "\"" st)
  28. (modify-syntax-entry ?. "_" st)
  29. (modify-syntax-entry ?' "'" st)
  30. (modify-syntax-entry ?# "'" st)
  31. (modify-syntax-entry ?* "." st)
  32. (modify-syntax-entry ?= "." st)
  33. (modify-syntax-entry ?$ "_" st)
  34. st))
  35. (defconst bibtex-style-commands
  36. '("ENTRY" "EXECUTE" "FUNCTION" "INTEGERS" "ITERATE" "MACRO" "READ"
  37. "REVERSE" "SORT" "STRINGS"))
  38. (defconst bibtex-style-functions
  39. ;; From http://www.eeng.dcu.ie/local-docs/btxdocs/btxhak/btxhak/node4.html.
  40. '("<" ">" "=" "+" "-" "*" ":="
  41. "add.period$" "call.type$" "change.case$" "chr.to.int$" "cite$"
  42. "duplicate$" "empty$" "format.name$" "if$" "int.to.chr$" "int.to.str$"
  43. "missing$" "newline$" "num.names$" "pop$" "preamble$" "purify$" "quote$"
  44. "skip$" "stack$" "substring$" "swap$" "text.length$" "text.prefix$"
  45. "top$" "type$" "warning$" "while$" "width$" "write$"))
  46. (defvar bibtex-style-font-lock-keywords
  47. `((,(regexp-opt bibtex-style-commands 'words) . font-lock-keyword-face)
  48. ("\\w+\\$" . font-lock-keyword-face)
  49. ("\\<\\(FUNCTION\\|MACRO\\)\\s-+{\\([^}\n]+\\)}"
  50. (2 font-lock-function-name-face))))
  51. ;;;###autoload
  52. (define-derived-mode bibtex-style-mode nil "BibStyle"
  53. "Major mode for editing BibTeX style files."
  54. (set (make-local-variable 'comment-start) "%")
  55. (set (make-local-variable 'outline-regexp) "^[a-z]")
  56. (set (make-local-variable 'imenu-generic-expression)
  57. '((nil "\\<\\(FUNCTION\\|MACRO\\)\\s-+{\\([^}\n]+\\)}" 2)))
  58. (set (make-local-variable 'indent-line-function) 'bibtex-style-indent-line)
  59. (set (make-local-variable 'parse-sexp-ignore-comments) t)
  60. (setq font-lock-defaults
  61. '(bibtex-style-font-lock-keywords nil t
  62. ((?. . "w")))))
  63. (defun bibtex-style-indent-line ()
  64. "Indent current line of BibTeX Style code."
  65. (interactive)
  66. (let* ((savep (point))
  67. (indent (condition-case nil
  68. (save-excursion
  69. (forward-line 0)
  70. (skip-chars-forward " \t")
  71. (if (>= (point) savep) (setq savep nil))
  72. (max (bibtex-style-calculate-indentation) 0))
  73. (error 0))))
  74. (if savep
  75. (save-excursion (indent-line-to indent))
  76. (indent-line-to indent))))
  77. (defcustom bibtex-style-indent-basic 2
  78. "Basic amount of indentation to use in BibTeX Style mode."
  79. :version "22.2"
  80. :type 'integer
  81. :group 'bibtex)
  82. (defun bibtex-style-calculate-indentation (&optional virt)
  83. (or
  84. ;; Stick the first line at column 0.
  85. (and (= (point-min) (line-beginning-position)) 0)
  86. ;; Commands start at column 0.
  87. (and (looking-at (regexp-opt bibtex-style-commands 'words)) 0)
  88. ;; Trust the current indentation, if such info is applicable.
  89. (and virt (save-excursion (skip-chars-backward " \t{") (bolp))
  90. (current-column))
  91. ;; Put leading close-paren where the matching open brace would be.
  92. (and (looking-at "}")
  93. (condition-case nil
  94. (save-excursion
  95. (up-list -1)
  96. (bibtex-style-calculate-indentation 'virt))
  97. (scan-error nil)))
  98. ;; Align leading "if$" with previous command.
  99. (and (looking-at "if\\$")
  100. (condition-case nil
  101. (save-excursion
  102. (backward-sexp 3)
  103. (bibtex-style-calculate-indentation 'virt))
  104. (scan-error
  105. ;; There is no command before the "if$".
  106. (condition-case nil
  107. (save-excursion
  108. (up-list -1)
  109. (+ bibtex-style-indent-basic
  110. (bibtex-style-calculate-indentation 'virt)))
  111. (scan-error nil)))))
  112. ;; Right after an opening brace.
  113. (condition-case err (save-excursion (backward-sexp 1) nil)
  114. (scan-error (goto-char (nth 2 err))
  115. (+ bibtex-style-indent-basic
  116. (bibtex-style-calculate-indentation 'virt))))
  117. ;; Default, align with previous command.
  118. (let ((fai ;; First arm of an "if$".
  119. (condition-case nil
  120. (save-excursion
  121. (forward-sexp 2)
  122. (forward-comment (point-max))
  123. (looking-at "if\\$"))
  124. (scan-error nil))))
  125. (save-excursion
  126. (condition-case nil
  127. (while (progn
  128. (backward-sexp 1)
  129. (save-excursion (skip-chars-backward " \t{") (not (bolp)))))
  130. (scan-error nil))
  131. (+ (current-column)
  132. (if (or fai (looking-at "ENTRY")) bibtex-style-indent-basic 0))))))
  133. (provide 'bibtex-style)
  134. ;;; bibtex-style.el ends here