2011-03-11 15:04:22 -05:00
|
|
|
;;; bibtex-style.el --- Major mode for BibTeX Style files -*- lexical-binding: t -*-
|
2007-08-10 06:52:54 +00:00
|
|
|
|
2022-01-01 02:45:51 -05:00
|
|
|
;; Copyright (C) 2005, 2007-2022 Free Software Foundation, Inc.
|
2007-08-10 06:52:54 +00:00
|
|
|
|
|
|
|
;; Author: Stefan Monnier <monnier@iro.umontreal.ca>
|
2009-11-26 00:01:20 +00:00
|
|
|
;; Keywords: tex
|
2007-08-10 06:52:54 +00:00
|
|
|
|
2008-05-06 04:34:22 +00:00
|
|
|
;; This file is part of GNU Emacs.
|
|
|
|
|
|
|
|
;; GNU Emacs is free software: you can redistribute it and/or modify
|
2007-08-10 06:52:54 +00:00
|
|
|
;; it under the terms of the GNU General Public License as published by
|
2008-05-06 04:34:22 +00:00
|
|
|
;; the Free Software Foundation, either version 3 of the License, or
|
|
|
|
;; (at your option) any later version.
|
2007-08-10 06:52:54 +00:00
|
|
|
|
2008-05-06 04:34:22 +00:00
|
|
|
;; GNU Emacs is distributed in the hope that it will be useful,
|
2007-08-10 06:52:54 +00:00
|
|
|
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
;; GNU General Public License for more details.
|
|
|
|
|
|
|
|
;; You should have received a copy of the GNU General Public License
|
2017-09-13 15:52:52 -07:00
|
|
|
;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
|
2007-08-10 06:52:54 +00:00
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
|
|
|
|
;; Done: font-lock, imenu, outline, commenting, indentation.
|
|
|
|
;; Todo: tab-completion.
|
|
|
|
|
|
|
|
;;; Code:
|
|
|
|
|
|
|
|
(defvar bibtex-style-mode-syntax-table
|
|
|
|
(let ((st (make-syntax-table)))
|
|
|
|
(modify-syntax-entry ?% "<" st)
|
|
|
|
(modify-syntax-entry ?\n ">" st)
|
|
|
|
(modify-syntax-entry ?\{ "(}" st)
|
|
|
|
(modify-syntax-entry ?\} "){" st)
|
|
|
|
(modify-syntax-entry ?\" "\"" st)
|
|
|
|
(modify-syntax-entry ?. "_" st)
|
|
|
|
(modify-syntax-entry ?' "'" st)
|
|
|
|
(modify-syntax-entry ?# "'" st)
|
|
|
|
(modify-syntax-entry ?* "." st)
|
|
|
|
(modify-syntax-entry ?= "." st)
|
|
|
|
(modify-syntax-entry ?$ "_" st)
|
|
|
|
st))
|
|
|
|
|
|
|
|
|
|
|
|
(defconst bibtex-style-commands
|
|
|
|
'("ENTRY" "EXECUTE" "FUNCTION" "INTEGERS" "ITERATE" "MACRO" "READ"
|
|
|
|
"REVERSE" "SORT" "STRINGS"))
|
|
|
|
|
|
|
|
(defconst bibtex-style-functions
|
2021-03-24 09:28:32 +01:00
|
|
|
;; From https://www.eeng.dcu.ie/local-docs/btxdocs/btxhak/btxhak/node4.html.
|
2007-08-10 06:52:54 +00:00
|
|
|
'("<" ">" "=" "+" "-" "*" ":="
|
|
|
|
"add.period$" "call.type$" "change.case$" "chr.to.int$" "cite$"
|
|
|
|
"duplicate$" "empty$" "format.name$" "if$" "int.to.chr$" "int.to.str$"
|
|
|
|
"missing$" "newline$" "num.names$" "pop$" "preamble$" "purify$" "quote$"
|
|
|
|
"skip$" "stack$" "substring$" "swap$" "text.length$" "text.prefix$"
|
|
|
|
"top$" "type$" "warning$" "while$" "width$" "write$"))
|
|
|
|
|
|
|
|
(defvar bibtex-style-font-lock-keywords
|
|
|
|
`((,(regexp-opt bibtex-style-commands 'words) . font-lock-keyword-face)
|
|
|
|
("\\w+\\$" . font-lock-keyword-face)
|
|
|
|
("\\<\\(FUNCTION\\|MACRO\\)\\s-+{\\([^}\n]+\\)}"
|
|
|
|
(2 font-lock-function-name-face))))
|
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(define-derived-mode bibtex-style-mode nil "BibStyle"
|
|
|
|
"Major mode for editing BibTeX style files."
|
2020-12-10 09:44:25 +01:00
|
|
|
(setq-local comment-start "%")
|
|
|
|
(setq-local outline-regexp "^[a-z]")
|
|
|
|
(setq-local imenu-generic-expression
|
|
|
|
'((nil "\\<\\(FUNCTION\\|MACRO\\)\\s-+{\\([^}\n]+\\)}" 2)))
|
* lisp/textmodes: Use lexical-binding
* lisp/textmodes/enriched.el: Use lexical-binding.
(enriched-mode): Use `delete-dups` to avoid `add-to-list` on
a local variable.
* lisp/textmodes/makeinfo.el: Use lexical-binding.
(makeinfo-region): Remove unused var `filename-or-header`.
* lisp/textmodes/refbib.el: Use lexical-binding.
(r2b-put-field): Remove unused var `multi-line`.
(r2b-barf-output): Remove unused var `match`.
* lisp/textmodes/refer.el: Use lexical-binding.
(refer-find-entry-internal): Remove unused vars `old-buffer` and `found`.
* lisp/textmodes/reftex-auc.el: Use lexical-binding.
(LaTeX-add-bibitems): Declare function.
(reftex-plug-into-AUCTeX): Use `add-function` and `advice-add` so we
can properly unplug.
* lisp/textmodes/reftex-cite.el: Use lexical-binding.
(reftex-create-bibtex-file): Remove unused var `file`.
(reftex--found-list): Declare var.
(reftex-offer-bib-menu): Rename local var to `reftex--found-list`.
* lisp/textmodes/reftex-dcr.el: Use lexical-binding.
(reftex-use-itimer-in-xemacs): Delete XEmacs-only var.
(reftex-toggle-auto-view-crossref): Delete XEmacs-only code.
(reftex-start-itimer-once): Delete XEmacs-only function.
* lisp/textmodes/reftex-global.el: Use lexical-binding.
(reftex-isearch-push-state-function): Use a closure instead of `(lambda).
* lisp/textmodes/reftex-index.el: Use lexical-binding.
(mark-active, transient-mark-mode): Delete var declarations.
(reftex-index-mode-map): Remove XEmacs-only code.
Use `mapc` so we can use closures instead of hand-built lambdas.
(reftex-index-next, reftex-index-previous): Tweak interactive spec to
remove unused prefix arg and mark it as a motion command.
(reftex-index-phrases-font-lock-keywords)
(reftex-index-phrases-font-lock-keywords): Move initialization into
declaration.
(reftex-index-initialize-phrases-buffer, reftex-index-phrases-mode)
reftex-index-phrases-apply-to-region: Remove XEmacs-only code.
(TeX-master): Remove redundant declaration.
(reftex--chars-first): Rename dynvar from `chars-first`. Adjust all uses.
* lisp/textmodes/reftex-parse.el: Use lexical-binding.
* lisp/textmodes/reftex-ref.el: Use lexical-binding.
(reftex-label): Remove always-nil var `text`.
(reftex-refstyle): Declare before first use.
(<toplevel>): Use closures rather than `eval` when building commands from
`reftex-ref-style-alist`.
* lisp/textmodes/reftex-sel.el: Use lexical-binding.
(reftex-select-label-mode-map, reftex-select-bib-mode-map):
Use `mapc` so we can use closures instead of hand-built lambdas.
(reftex-select-label-mode, reftex-select-bib-mode): Remove XEmacs-only code.
(reftex-select-data, reftex-select-prompt, reftex-refstyle):
Move declaration before first use.
(reftex--found-list, reftex--cb-flag, reftex--last-data)
(reftex--call-back, reftex--help-string): Move declaration before use,
and rename by adding `reftext--` prefix. Adjust all uses in this file.
For `reftex--found-list` adjust corresponding uses in `reftex-cite.el`.
(reftex-select-item): Explicitly let-bind them.
Remove XEmacs-only code.
* lisp/textmodes/reftex-toc.el: Use lexical-binding.
(reftex-toc-mode-map, reftex-toc-mode, reftex-toc-restore-region)
(reftex-toc-next, reftex-toc-previous, reftex-toc-next-heading)
(reftex-toc-previous-heading, reftex-toggle-auto-toc-recenter
(reftex-make-separate-toc-frame): Remove XEmacs-only code.
* lisp/textmodes/reftex-vars.el: Use lexical-binding.
* lisp/textmodes/reftex.el: Use lexical-binding.
(reftex-mode-map, reftex-mode, reftex-fontify-select-label-buffer)
(reftex-verified-face): Remove XEmacs-only code.
(reftex-region-active-p, reftex-overlay-put, reftex-move-overlay)
(reftex-make-overlay, reftex-get-buffer-visiting, reftex-delete-overlay):
Redefine as obsolete aliases. Replace all callers.
(current-message): Remove XEmacs-only definition.
* lisp/textmodes/remember.el: Use lexical-binding.
* lisp/textmodes/table.el (<toplevel>): Use closures rather than `(lambda)
to build commands.
* lisp/textmodes/texinfmt.el: Use lexical-binding.
(texinfo-example-start): Declare var.
(texinfo-format-region, texinfo-format-buffer-1): Remove unused var
`last-input-buffer`.
(texinfo-format-scan): Use `dlet` to bind `whitespace-silent`.
(texinfo-optional-braces-discard, texinfo-format-parse-line-args)
(texinfo-format-parse-args): Remove unused var `start`.
(texinfo-multitable-widths): Remove unused var `start-of-templates`.
(texinfo-multitable-item): Strength-reduce `eval` to `symbol-value`.
(texinfo-alias): Remove unused vars `start` and `args`.
(texinfo-defun-type symbol-property): Change the car to help the type
symbol rather than an expression returning it.
(texinfo-format-deffn): Remove corresponding `eval`.
(texinfo-clear): Remove unused var `value`.
(texinfo-format-ifeq): Remove unused var `end`.
* lisp/textmodes/texinfo.el: Use lexical-binding.
(tex-show-print-queue): Declare function.
* lisp/textmodes/texnfo-upd.el: Use lexical-binding.
(texinfo-start-menu-description): Remove unused var `end`.
(texinfo-insert-node-lines): Remove unused var `beginning-marker`.
(texinfo-multiple-files-update): Remove unused vars `next-node-name`
and `previous-node-name`.
* lisp/textmodes/two-column.el: Use lexical-binding.
2021-03-17 23:32:39 -04:00
|
|
|
(setq-local indent-line-function #'bibtex-style-indent-line)
|
2020-12-10 09:44:25 +01:00
|
|
|
(setq-local parse-sexp-ignore-comments t)
|
2007-08-10 06:52:54 +00:00
|
|
|
(setq font-lock-defaults
|
|
|
|
'(bibtex-style-font-lock-keywords nil t
|
|
|
|
((?. . "w")))))
|
|
|
|
|
|
|
|
(defun bibtex-style-indent-line ()
|
|
|
|
"Indent current line of BibTeX Style code."
|
|
|
|
(interactive)
|
|
|
|
(let* ((savep (point))
|
|
|
|
(indent (condition-case nil
|
|
|
|
(save-excursion
|
|
|
|
(forward-line 0)
|
|
|
|
(skip-chars-forward " \t")
|
|
|
|
(if (>= (point) savep) (setq savep nil))
|
|
|
|
(max (bibtex-style-calculate-indentation) 0))
|
|
|
|
(error 0))))
|
|
|
|
(if savep
|
|
|
|
(save-excursion (indent-line-to indent))
|
|
|
|
(indent-line-to indent))))
|
|
|
|
|
|
|
|
(defcustom bibtex-style-indent-basic 2
|
|
|
|
"Basic amount of indentation to use in BibTeX Style mode."
|
2008-03-01 19:16:02 +00:00
|
|
|
:version "22.2"
|
|
|
|
:type 'integer
|
|
|
|
:group 'bibtex)
|
2007-08-10 06:52:54 +00:00
|
|
|
|
|
|
|
(defun bibtex-style-calculate-indentation (&optional virt)
|
|
|
|
(or
|
|
|
|
;; Stick the first line at column 0.
|
|
|
|
(and (= (point-min) (line-beginning-position)) 0)
|
|
|
|
;; Commands start at column 0.
|
|
|
|
(and (looking-at (regexp-opt bibtex-style-commands 'words)) 0)
|
|
|
|
;; Trust the current indentation, if such info is applicable.
|
|
|
|
(and virt (save-excursion (skip-chars-backward " \t{") (bolp))
|
|
|
|
(current-column))
|
|
|
|
;; Put leading close-paren where the matching open brace would be.
|
|
|
|
(and (looking-at "}")
|
|
|
|
(condition-case nil
|
|
|
|
(save-excursion
|
|
|
|
(up-list -1)
|
|
|
|
(bibtex-style-calculate-indentation 'virt))
|
|
|
|
(scan-error nil)))
|
|
|
|
;; Align leading "if$" with previous command.
|
|
|
|
(and (looking-at "if\\$")
|
|
|
|
(condition-case nil
|
|
|
|
(save-excursion
|
|
|
|
(backward-sexp 3)
|
|
|
|
(bibtex-style-calculate-indentation 'virt))
|
|
|
|
(scan-error
|
|
|
|
;; There is no command before the "if$".
|
|
|
|
(condition-case nil
|
|
|
|
(save-excursion
|
|
|
|
(up-list -1)
|
|
|
|
(+ bibtex-style-indent-basic
|
|
|
|
(bibtex-style-calculate-indentation 'virt)))
|
|
|
|
(scan-error nil)))))
|
|
|
|
;; Right after an opening brace.
|
|
|
|
(condition-case err (save-excursion (backward-sexp 1) nil)
|
|
|
|
(scan-error (goto-char (nth 2 err))
|
|
|
|
(+ bibtex-style-indent-basic
|
|
|
|
(bibtex-style-calculate-indentation 'virt))))
|
|
|
|
;; Default, align with previous command.
|
|
|
|
(let ((fai ;; First arm of an "if$".
|
|
|
|
(condition-case nil
|
|
|
|
(save-excursion
|
|
|
|
(forward-sexp 2)
|
|
|
|
(forward-comment (point-max))
|
|
|
|
(looking-at "if\\$"))
|
|
|
|
(scan-error nil))))
|
|
|
|
(save-excursion
|
2011-03-11 15:04:22 -05:00
|
|
|
(condition-case nil
|
2007-08-10 06:52:54 +00:00
|
|
|
(while (progn
|
|
|
|
(backward-sexp 1)
|
|
|
|
(save-excursion (skip-chars-backward " \t{") (not (bolp)))))
|
|
|
|
(scan-error nil))
|
|
|
|
(+ (current-column)
|
|
|
|
(if (or fai (looking-at "ENTRY")) bibtex-style-indent-basic 0))))))
|
|
|
|
|
|
|
|
|
|
|
|
(provide 'bibtex-style)
|
|
|
|
;;; bibtex-style.el ends here
|