bui-history.el 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. ;;; bui-history.el --- Buffer history -*- lexical-binding: t -*-
  2. ;; Copyright © 2014, 2016 Alex Kost <alezost@gmail.com>
  3. ;; This program is free software; you can redistribute it and/or modify
  4. ;; it under the terms of the GNU General Public License as published by
  5. ;; the Free Software Foundation, either version 3 of the License, or
  6. ;; (at your option) any later version.
  7. ;;
  8. ;; This program is distributed in the hope that it will be useful,
  9. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. ;; GNU General Public License for more details.
  12. ;;
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. ;;; Commentary:
  16. ;; This file provides a general support for buffer history similar to
  17. ;; the history of a `help-mode' buffer.
  18. ;;; Code:
  19. (require 'cl-lib)
  20. (require 'help-mode) ; for button labels
  21. (require 'bui-button)
  22. (require 'bui-utils)
  23. (bui-define-groups bui-history
  24. :group-doc "Settings for BUI buffers history."
  25. :faces-group-doc "Faces for history buttons.")
  26. (defvar-local bui-history-stack-item nil
  27. "Current item of the history.
  28. A list of the form (FUNCTION [ARGS ...]).
  29. The item is used by calling (apply FUNCTION ARGS).")
  30. (put 'bui-history-stack-item 'permanent-local t)
  31. (defvar-local bui-history-back-stack nil
  32. "Stack (list) of visited items.
  33. Each element of the list has a form of `bui-history-stack-item'.")
  34. (put 'bui-history-back-stack 'permanent-local t)
  35. (defvar-local bui-history-forward-stack nil
  36. "Stack (list) of items visited with `bui-history-back'.
  37. Each element of the list has a form of `bui-history-stack-item'.")
  38. (put 'bui-history-forward-stack 'permanent-local t)
  39. (defcustom bui-history-size 16
  40. "Maximum number of items saved in history.
  41. If 0, the history is disabled.
  42. If nil, the history is infinite (until Emacs eats all your memory :-))."
  43. :type '(choice integer (const :tag "Infinite" nil))
  44. :group 'bui-history)
  45. (defun bui-history-add (item)
  46. "Add ITEM to history."
  47. (and bui-history-stack-item
  48. (push bui-history-stack-item bui-history-back-stack))
  49. (setq bui-history-forward-stack nil
  50. bui-history-stack-item item)
  51. (when (and bui-history-size
  52. (>= (length bui-history-back-stack)
  53. bui-history-size))
  54. (setq bui-history-back-stack
  55. (cl-loop for elt in bui-history-back-stack
  56. for i from 1 to bui-history-size
  57. collect elt))))
  58. (defun bui-history-replace (item)
  59. "Replace current item in history with ITEM."
  60. (setq bui-history-stack-item item))
  61. (defun bui-history-goto (item)
  62. "Go to the ITEM of history.
  63. ITEM should have the form of `bui-history-stack-item'."
  64. (or (listp item)
  65. (error "Wrong value of history element"))
  66. (setq bui-history-stack-item item)
  67. (apply (car item) (cdr item)))
  68. (defun bui-history-back ()
  69. "Go back to the previous element of history in the current buffer."
  70. (interactive)
  71. (or bui-history-back-stack
  72. (user-error "No previous element in history"))
  73. (push bui-history-stack-item bui-history-forward-stack)
  74. (bui-history-goto (pop bui-history-back-stack)))
  75. (defun bui-history-forward ()
  76. "Go forward to the next element of history in the current buffer."
  77. (interactive)
  78. (or bui-history-forward-stack
  79. (user-error "No next element in history"))
  80. (push bui-history-stack-item bui-history-back-stack)
  81. (bui-history-goto (pop bui-history-forward-stack)))
  82. ;;; History buttons
  83. (defface bui-history-button
  84. '((t :inherit button))
  85. "Face used for history buttons (back/forward)."
  86. :group 'bui-history-faces)
  87. (defcustom bui-history-back-label help-back-label
  88. "Label of a button used to move backward by history."
  89. :type 'string
  90. :group 'bui-history)
  91. (defcustom bui-history-forward-label help-forward-label
  92. "Label of a button used to move forward by history."
  93. :type 'string
  94. :group 'bui-history)
  95. (define-button-type 'bui-history
  96. :supertype 'bui
  97. 'face 'bui-history-button)
  98. (defun bui-history-insert-button (label action &optional message
  99. &rest properties)
  100. "Insert history button with LABEL at point.
  101. ACTION is a function called without arguments when the button is
  102. pressed. MESSAGE is a button help message. See
  103. `insert-text-button' for the meaning of PROPERTIES."
  104. (apply #'bui-insert-button
  105. label 'bui-history
  106. 'action (lambda (_btn) (funcall action))
  107. 'help-echo message
  108. properties))
  109. (defun bui-history-insert-buttons ()
  110. "Insert back/forward history buttons at point if needed."
  111. (let ((insert-back? bui-history-back-stack)
  112. (insert-forward? bui-history-forward-stack)
  113. (insert-any? (or bui-history-back-stack
  114. bui-history-forward-stack)))
  115. (when insert-any? (bui-newline))
  116. (when insert-back?
  117. (bui-history-insert-button bui-history-back-label
  118. #'bui-history-back
  119. "Go back to the previous info"))
  120. (when insert-forward?
  121. (when insert-back? (insert "\t"))
  122. (bui-history-insert-button bui-history-forward-label
  123. #'bui-history-forward
  124. "Go forward to the next info"))
  125. (when insert-any? (bui-newline))))
  126. (provide 'bui-history)
  127. ;;; bui-history.el ends here