📄 tex-mode.el
字号:
(make-local-variable 'TeX-command) (make-local-variable 'TeX-start-of-header) (make-local-variable 'TeX-end-of-header) (make-local-variable 'TeX-trailer))(defun TeX-comment-indent () (if (looking-at "%%%") (current-column) (skip-chars-backward " \t") (max (if (bolp) 0 (1+ (current-column))) comment-column)))(defun TeX-insert-quote (arg) "Insert ``, '' or \" according to preceding character.With prefix argument, always insert \" characters." (interactive "P") (if arg (let ((count (prefix-numeric-value arg))) (if (listp arg) (self-insert-command 1) ;C-u always inserts just one (self-insert-command count))) (insert (cond ((or (bobp) (save-excursion (forward-char -1) (looking-at "[ \t\n]\\|\\s("))) "``") ((= (preceding-char) ?\\) ?\") (t "''")))))(defun validate-TeX-buffer () "Check current buffer for paragraphs containing mismatched $'s.As each such paragraph is found, a mark is pushed at its beginning,and the location is displayed for a few seconds." (interactive) (let ((opoint (point))) (goto-char (point-max)) ;; Does not use save-excursion ;; because we do not want to save the mark. (unwind-protect (while (and (not (input-pending-p)) (not (bobp))) (let ((end (point))) (search-backward "\n\n" nil 'move) (or (TeX-validate-paragraph (point) end) (progn (push-mark (point)) (message "Mismatch found in pararaph starting here") (sit-for 4))))) (goto-char opoint))))(defun TeX-validate-paragraph (start end) (condition-case () (save-excursion (save-restriction (narrow-to-region start end) (goto-char start) (forward-sexp (- end start)) t)) (error nil)))(defun TeX-terminate-paragraph (inhibit-validation) "Insert two newlines, breaking a paragraph for TeX.Check for mismatched braces/$'s in paragraph being terminated.A prefix arg inhibits the checking." (interactive "P") (or inhibit-validation (TeX-validate-paragraph (save-excursion (search-backward "\n\n" nil 'move) (point)) (point)) (message "Paragraph being closed appears to contain a mismatch")) (insert "\n\n"))(defun TeX-insert-braces () "Make a pair of braces and be poised to type inside of them." (interactive) (insert ?\{) (save-excursion (insert ?})));;; Like TeX-insert-braces, but for LaTeX.(defun TeX-close-LaTeX-block () "Creates an \\end{...} to match \\begin{...} on the current line andputs point on the blank line between them." (interactive "*") (let ((fail-point (point))) (end-of-line) (if (re-search-backward "\\\\begin{\\([^}\n]*\\)}" (save-excursion (beginning-of-line) (point)) t) (let ((text (buffer-substring (match-beginning 1) (match-end 1))) (indentation (current-column))) (end-of-line) (delete-horizontal-space) (insert "\n\n") (indent-to indentation) (insert "\\end{" text "}") (forward-line -1)) (goto-char fail-point) (ding))));;; Invoking TeX in an inferior shell.;;; Why use a shell instead of running TeX directly? Because if TeX;;; gets stuck, the user can switch to the shell window and type at it.;;; The utility functions:(defun TeX-start-shell () (require 'shell) (save-excursion (set-buffer (make-shell "TeX-shell" nil nil "-v")) (setq TeX-shell-map (copy-keymap shell-mode-map)) (TeX-define-common-keys TeX-shell-map) (use-local-map TeX-shell-map) (if (zerop (buffer-size)) (sleep-for 1))))(defun set-buffer-directory (buffer directory) "Set BUFFER's default directory to be DIRECTORY." (setq directory (file-name-as-directory (expand-file-name directory))) (if (not (file-directory-p directory)) (error "%s is not a directory" directory) (save-excursion (set-buffer buffer) (setq default-directory directory))));;; The commands:;;; It's a kludge that we have to create a special buffer just ;;; to write out the TeX-trailer. It would nice if there were a;;; function like write-region that would write literal strings.(defun TeX-region (beg end) "Run TeX on the current region. A temporary file (TeX-zap-file) iswritten in directory TeX-directory, and TeX is run in that directory.If the buffer has a header, it is written to the temporary file beforethe region itself. The buffer's header is all lines between thestrings defined by TeX-start-of-header and TeX-end-of-headerinclusive. The header must start in the first 100 lines. The valueof TeX-trailer is appended to the temporary file after the region." (interactive "r") (if (get-buffer "*TeX-shell*") (TeX-kill-job) (TeX-start-shell)) (or TeX-zap-file (setq TeX-zap-file (make-temp-name "#tz"))) (let ((tex-out-file (concat TeX-zap-file ".tex")) (temp-buffer (get-buffer-create " TeX-Output-Buffer")) (zap-directory (file-name-as-directory (expand-file-name TeX-directory)))) (save-excursion (save-restriction (widen) (goto-char (point-min)) (forward-line 100) (let ((search-end (point)) (hbeg (point-min)) (hend (point-min)) (default-directory zap-directory)) (goto-char (point-min)) ;; Initialize the temp file with either the header or nothing (if (search-forward TeX-start-of-header search-end t) (progn (beginning-of-line) (setq hbeg (point)) ;mark beginning of header (if (search-forward TeX-end-of-header nil t) (progn (forward-line 1) (setq hend (point))) ;mark end of header (setq hbeg (point-min))))) ;no header (write-region (min hbeg beg) hend tex-out-file nil nil) (write-region (max beg hend) end tex-out-file t nil)) (let ((local-tex-trailer TeX-trailer)) (set-buffer temp-buffer) (erase-buffer) ;; make sure trailer isn't hidden by a comment (insert-string "\n") (if local-tex-trailer (insert-string local-tex-trailer)) (set-buffer-directory temp-buffer zap-directory) (write-region (point-min) (point-max) tex-out-file t nil)))) (set-buffer-directory "*TeX-shell*" zap-directory) (send-string "TeX-shell" (concat TeX-shell-cd-command " " zap-directory "\n")) (send-string "TeX-shell" (concat TeX-command " \"" tex-out-file "\"\n"))) (TeX-recenter-output-buffer 0))(defun TeX-buffer () "Run TeX on current buffer. See \\[TeX-region] for more information." (interactive) (TeX-region (point-min) (point-max)))(defun TeX-kill-job () "Kill the currently running TeX job." (interactive) (quit-process "TeX-shell" t))(defun TeX-recenter-output-buffer (linenum) "Redisplay buffer of TeX job output so that most recent output can be seen.The last line of the buffer is displayed online LINE of the window, or centered if LINE is nil." (interactive "P") (let ((tex-shell (get-buffer "*TeX-shell*")) (old-buffer (current-buffer))) (if (null tex-shell) (message "No TeX output buffer") (pop-to-buffer tex-shell) (bury-buffer tex-shell) (goto-char (point-max)) (recenter (if linenum (prefix-numeric-value linenum) (/ (window-height) 2))) (pop-to-buffer old-buffer) )))(defun TeX-print () "Print the .dvi file made by \\[TeX-region] or \\[TeX-buffer].Runs the shell command defined by TeX-dvi-print-command." (interactive) (send-string "TeX-shell" (concat TeX-dvi-print-command " \"" TeX-zap-file ".dvi\"\n")) (TeX-recenter-output-buffer nil))(defun TeX-show-print-queue () "Show the print queue that \\[TeX-print] put your job on.Runs the shell command defined by TeX-show-queue-command." (interactive) (if (not (get-buffer "*TeX-shell*")) (TeX-start-shell)) (send-string "TeX-shell" (concat TeX-show-queue-command "\n")) (TeX-recenter-output-buffer nil))
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -