diff options
author | Craig Earls <enderw88@gmail.com> | 2014-08-24 18:37:24 -0700 |
---|---|---|
committer | Craig Earls <enderw88@gmail.com> | 2014-08-24 18:37:24 -0700 |
commit | 4deaeb02c9dfb3f1bf51e998b85b0a433ac9f212 (patch) | |
tree | 718b833e5ff63e7e40fc8c580ad718f1fb6f2ad6 /lisp/ledger-fontify.el | |
parent | ce3102923658ce53194832b524d455b24a38af3f (diff) | |
download | fork-ledger-4deaeb02c9dfb3f1bf51e998b85b0a433ac9f212.tar.gz fork-ledger-4deaeb02c9dfb3f1bf51e998b85b0a433ac9f212.tar.bz2 fork-ledger-4deaeb02c9dfb3f1bf51e998b85b0a433ac9f212.zip |
Set up fontification independent of font-lock.
Basic functionality in place. need to test further and expand detail fortification.
Diffstat (limited to 'lisp/ledger-fontify.el')
-rw-r--r-- | lisp/ledger-fontify.el | 173 |
1 files changed, 173 insertions, 0 deletions
diff --git a/lisp/ledger-fontify.el b/lisp/ledger-fontify.el new file mode 100644 index 00000000..ff194649 --- /dev/null +++ b/lisp/ledger-fontify.el @@ -0,0 +1,173 @@ +;;; ledger-fontify.el --- Provide custom fontification for ledger-mode + + +;; Copyright (C) 2014 Craig P. Earls (enderw88 at gmail dot com) + +;; This file is not part of GNU Emacs. + +;; This is free software; you can redistribute it and/or modify it under +;; the terms of the GNU General Public License as published by the Free +;; Software Foundation; either version 2, or (at your option) any later +;; version. +;; +;; This is distributed in the hope that it will be useful, 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 +;; along with GNU Emacs; see the file COPYING. If not, write to the +;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, +;; MA 02110-1301 USA. + +;;; Commentary: +;; Font-lock-mode doesn't handle multiline syntax very well. This +;; code provides font lock that is sensitive to overall transaction +;; states + + +(provide 'ledger-fontify) + +(defcustom ledger-fontify-xact-state-overrides t + "If t the overall xact state (cleard, pending, nil) will + control the font of the entire transaction, not just the payee + line." + :type 'boolean + :group 'ledger-fontification) + +(defun ledger-fontify-whole-buffer () + (interactive) + (save-excursion + (goto-char (point-min)) + (while (not (eobp)) + (cond ((looking-at ledger-xact-start-regex) + (ledger-fontify-xact-at (point))) + ((looking-at ledger-directive-start-regex) + (ledger-fontify-directive-at (point)))) + + (forward-paragraph) + (forward-char)))) + +(defun ledger-fontify-activate () + "add hook to fontify after buffer changes" + (interactive) + (if (string= (format-mode-line 'mode-name) "Ledger") + (progn + (add-hook 'post-command-hook 'ledger-fontify-buffer-part) + ;; this is a silly work around to emacs bug 16796 wherein + ;; after-change-functions is randomly reset to nil. Before + ;; each change make sure after-change-functions is properly + ;; set. +; (add-hook 'before-change-functions 'ledger-fontify-ensure-after-change-hook) + ))) + +;; (defun ledger-fontify-ensure-after-change-hook (beg end) +;; (if (string= (format-mode-line 'mode-name) "Ledger") +;; (add-hook 'after-change-functions 'ledger-fontify-buffer-part))) + +(defun ledger-fontify-buffer-part () + (save-excursion + (backward-paragraph) + (forward-char) + (cond ((looking-at ledger-xact-start-regex) + (ledger-fontify-xact-at (point))) + ((looking-at ledger-directive-start-regex) + (ledger-fontify-directive-at (point)))))) + +(defun ledger-fontify-xact-at (position) + (interactive "d") + (let ((extents (ledger-find-xact-extents position)) + (state (ledger-transaction-state))) + (if (and ledger-fontify-xact-state-overrides state) + (cond ((eq state 'cleared) + (ledger-fontify-set-face extents 'ledger-font-xact-cleared-face)) + ((eq state 'pending) + (ledger-fontify-set-face extents 'ledger-font-xact-pending-face))) + (ledger-fontify-xact-by-line extents)))) + +(defun ledger-fontify-xact-by-line (extends) + "do line-by-line detailed fontification of xact" + (save-excursion + (ledger-fontify-xact-start (car extents)))) + +(defun ledger-fontify-xact-start (pos) + (interactive "d") + (goto-char pos) + (let ((state nil)) + (re-search-forward ledger-xact-start-regex) + (ledger-fontify-set-face (list (match-beginning 1) (match-end 1)) 'ledger-font-posting-date-face) + (save-match-data (setq state (ledger-state-from-string (s-trim (match-string 5))))) + (ledger-fontify-set-face (list (match-beginning 7) (match-end 7)) + (cond ((eq state 'pending) + 'ledger-font-payee-pending-face) + ((eq state 'cleared) + 'ledger-font-payee-cleared-face) + (t + 'ledger-font-payee-uncleared-face))) + (ledger-fontify-set-face (list (match-beginning 8) + (match-end 8)) 'ledger-font-comment-face))) + +(defun ledger-fontify-directive-at (position) + (interactive "d") + (let ((extents (ledger-find-xact-extents position)) + (face 'ledger-font-default-face)) + (cond ((looking-at "=") + (setq face 'ledger-font-auto-xact-face)) + ((looking-at "~") + (setq face 'ledger-font-periodic-xact-face)) + ((looking-at "[;#%|\\*]") + (setq face 'ledger-font-comment-face)) + ((looking-at "\\(year\\)\\|Y") + (setq face 'ledger-font-year-directive-face)) + ((looking-at "account") + (setq face 'ledger-font-account-directive-face)) + ((looking-at "apply") + (setq face 'ledger-font-apply-directive-face)) + ((looking-at "alias") + (setq face 'ledger-font-alias-directive-face)) + ((looking-at "assert") + (setq face 'ledger-font-assert-directive-face)) + ((looking-at "\\(bucket\\)\\|A") + (setq face 'ledger-font-bucket-directive-face)) + ((looking-at "capture") + (setq face 'ledger-font-capture-directive-face)) + ((looking-at "check") + (setq face 'ledger-font-check-directive-face)) + ((looking-at "commodity") + (setq face 'ledger-font-commodity-directive-face)) + ((looking-at "define") + (setq face 'ledger-font-define-directive-face)) + ((looking-at "end") + (setq face 'ledger-font-end-directive-face)) + ((looking-at "expr") + (setq face 'ledger-font-expr-directive-face)) + ((looking-at "fixed") + (setq face 'ledger-font-fixed-directive-face)) + ((looking-at "include") + (setq face 'ledger-font-include-directive-face)) + ((looking-at "payee") + (setq face 'ledger-font-payee-directive-face)) + ((looking-at "tag") + (setq face 'ledger-font-tag-directive-face))) + (ledger-fontify-set-face extents face))) + +(defun ledger-fontify-set-face (extents face) + (put-text-property (car extents) (cadr extents) 'face face)) + + +(defun s-trim-left (s) + "Remove whitespace at the beginning of S." + (if (string-match "\\`[ \t\n\r]+" s) + (replace-match "" t t s) + s)) + +(defun s-trim-right (s) + "Remove whitespace at the end of S." + (if (string-match "[ \t\n\r]+\\'" s) + (replace-match "" t t s) + s)) + +(defun s-trim (s) + "Remove whitespace at the beginning and end of S." + (s-trim-left (s-trim-right s))) +;;; ledger-fontify.el ends here |