2022-11-28 22:03:38 +01:00
|
|
|
;;; boxes.el --- ASCII boxes unlimited! -*- lexical-binding: t -*-
|
2006-07-12 08:23:59 +02:00
|
|
|
|
2006-07-12 08:32:35 +02:00
|
|
|
;; Copyright (C) 1999, 2001, 2006 Jason L. Shiffer
|
2006-07-12 08:23:59 +02:00
|
|
|
|
|
|
|
;; Author: Jason L. Shiffer <jshiffer@zerotao.com>
|
2022-11-28 22:03:38 +01:00
|
|
|
;; Maintainer: Jason L. Shiffer <jshiffer@zerotao.com>
|
2022-12-05 13:26:48 +01:00
|
|
|
;; Version: 0.0
|
|
|
|
;; Package-Requires: ((emacs "24.3"))
|
2006-07-12 08:23:59 +02:00
|
|
|
;; Keywords: extensions
|
2022-11-28 22:03:38 +01:00
|
|
|
;; URL: https://boxes.thomasjensen.com
|
2006-07-12 08:23:59 +02:00
|
|
|
;; Created: 1999-10-30
|
2015-12-11 15:26:58 +01:00
|
|
|
;; Others:
|
|
|
|
;; Vijay Lakshminarayanan: support for choosing boxes comment by current buffer mode.
|
2022-11-28 22:03:38 +01:00
|
|
|
;; Mike Woolley: Customise and packaging support.
|
2006-07-12 08:23:59 +02:00
|
|
|
|
2022-09-18 14:56:30 +02:00
|
|
|
;; boxes - Command line filter to draw/remove ASCII boxes around text
|
2023-03-26 21:32:08 +02:00
|
|
|
;; Copyright (c) 1999-2023 Thomas Jensen and the boxes contributors
|
2006-07-12 08:23:59 +02:00
|
|
|
;;
|
2022-09-18 14:56:30 +02:00
|
|
|
;; This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
|
|
|
|
;; License, version 3, as published by the Free Software Foundation.
|
|
|
|
;; This program 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 this program.
|
|
|
|
;; If not, see <https://www.gnu.org/licenses/>.
|
2006-07-12 08:23:59 +02:00
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
|
|
|
|
;; This program provides an interface to the boxes application which can be found at
|
2019-02-14 21:41:11 +01:00
|
|
|
;; https://boxes.thomasjensen.com/
|
2006-07-12 08:23:59 +02:00
|
|
|
|
2022-11-28 22:03:38 +01:00
|
|
|
;; To use this, add the following lines to your startup file:
|
|
|
|
;;
|
|
|
|
;; (global-set-key "\C-cb" 'boxes-command-on-region)
|
|
|
|
;; (global-set-key "\C-cq" 'boxes-create)
|
|
|
|
;; (global-set-key "\C-cr" 'boxes-remove)
|
|
|
|
;;
|
|
|
|
;; If you didn't install the package version then you will also need to put this file somewhere in your load path and
|
|
|
|
;; add these lines to your startup file:
|
2006-07-12 08:23:59 +02:00
|
|
|
;;
|
|
|
|
;; (autoload 'boxes-command-on-region "boxes" nil t)
|
|
|
|
;; (autoload 'boxes-remove "boxes" nil t)
|
|
|
|
;; (autoload 'boxes-create "boxes" nil t)
|
2022-11-28 22:03:38 +01:00
|
|
|
;;
|
|
|
|
;; If `use-package' is installed you can automatically install the boxes binary and elisp package with:
|
|
|
|
;;
|
|
|
|
;; (use-package boxes
|
|
|
|
;; :ensure t
|
|
|
|
;; :ensure-system-package boxes
|
|
|
|
;; :bind (("C-c b" . boxes-command-on-region)
|
2022-11-29 19:18:46 +01:00
|
|
|
;; ("C-c q" . boxes-create)
|
|
|
|
;; ("C-c r" . boxes-remove)))
|
2006-07-12 08:23:59 +02:00
|
|
|
|
|
|
|
;;; Code:
|
|
|
|
|
2022-11-17 21:28:29 +01:00
|
|
|
(defgroup boxes nil
|
|
|
|
"ASCII boxes unlimited!"
|
2022-12-05 13:26:48 +01:00
|
|
|
:prefix "boxes-"
|
2022-11-17 21:28:29 +01:00
|
|
|
:group 'convenience)
|
2006-07-12 08:23:59 +02:00
|
|
|
|
2022-11-17 21:28:29 +01:00
|
|
|
(defcustom boxes-command "boxes"
|
|
|
|
"The boxes command."
|
|
|
|
:type 'string
|
|
|
|
:group 'boxes)
|
2006-07-12 08:23:59 +02:00
|
|
|
|
2022-11-17 21:28:29 +01:00
|
|
|
(defcustom boxes-args ""
|
|
|
|
"Additional arguments to the boxes command."
|
|
|
|
:type 'string
|
|
|
|
:group 'boxes)
|
2006-07-12 08:23:59 +02:00
|
|
|
|
2021-08-12 13:22:38 +02:00
|
|
|
(defcustom boxes-known-modes
|
2006-07-12 08:32:35 +02:00
|
|
|
'((c-mode . "c-cmt2") (c++-mode . "c-cmt2") (java-mode . "java-cmt")
|
|
|
|
(html-mode . "html-cmt") (sh-mode . "pound-cmt") (perl-mode . "pound-cmt")
|
|
|
|
(python-mode . "pound-cmt") (ruby-mode . "pound-cmt")
|
|
|
|
(emacs-lisp-mode . "lisp-cmt") (lisp-mode . "lisp-cmt"))
|
2022-11-17 21:28:29 +01:00
|
|
|
"Default box type based on the major mode of the buffer."
|
|
|
|
:type '(alist :key-type symbol :value-type string)
|
|
|
|
:group 'boxes)
|
2006-07-12 08:32:35 +02:00
|
|
|
|
2022-12-13 21:47:13 +01:00
|
|
|
(defconst boxes-minimum-version "2.1.0"
|
|
|
|
"Minimum required version of `boxes' to support querying the available box types.")
|
|
|
|
|
2022-11-17 21:28:29 +01:00
|
|
|
(defvar boxes-history nil
|
|
|
|
"Boxes types history.")
|
|
|
|
|
|
|
|
(defvar-local boxes-default-type nil
|
|
|
|
"The default type of box.")
|
|
|
|
|
2022-12-05 13:26:48 +01:00
|
|
|
(defvar boxes-types-list nil
|
|
|
|
"List of types available to the current boxes implementation, nil if not set yet.")
|
|
|
|
|
|
|
|
(defun boxes-types ()
|
2022-12-13 21:47:13 +01:00
|
|
|
"Return the list of types available to the current boxes implementation.
|
|
|
|
Signal error if a supported version of `boxes' is not available."
|
|
|
|
(condition-case nil
|
|
|
|
(or boxes-types-list
|
|
|
|
(setq boxes-types-list
|
|
|
|
(let ((types (process-lines boxes-command "-q" "(all)")))
|
|
|
|
(mapcar (lambda(type) (replace-regexp-in-string " *\(alias\) *$" "" type))
|
|
|
|
types))))
|
|
|
|
(error (error "Please install Boxes %s or later" boxes-minimum-version))))
|
2022-12-05 13:26:48 +01:00
|
|
|
|
2022-11-17 21:28:29 +01:00
|
|
|
(defun boxes-default-type (mode)
|
|
|
|
"Get the default box type for the given buffer major MODE."
|
|
|
|
(or (cdr (assoc mode boxes-known-modes)) "c-cmt2"))
|
2006-07-12 08:23:59 +02:00
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun boxes-create ()
|
2006-07-12 08:32:35 +02:00
|
|
|
"Automagicly create a new box around the region based on the default type."
|
|
|
|
(interactive "*")
|
|
|
|
(boxes-command-on-region (region-beginning) (region-end) boxes-default-type))
|
|
|
|
|
2006-07-12 08:23:59 +02:00
|
|
|
;;;###autoload
|
|
|
|
(defun boxes-remove ()
|
2006-07-12 08:32:35 +02:00
|
|
|
"Automagicly remove a new box around the region based on the default type."
|
|
|
|
(interactive "*")
|
|
|
|
(boxes-command-on-region (region-beginning) (region-end) boxes-default-type 1))
|
|
|
|
|
2006-07-12 08:23:59 +02:00
|
|
|
;;;###autoload
|
|
|
|
(defun boxes-command-on-region (start end type &optional remove)
|
2022-11-29 19:18:46 +01:00
|
|
|
"Create or Remove boxes from a region.
|
2022-12-13 21:47:13 +01:00
|
|
|
|
|
|
|
To create a box select a region, hit \\[boxes-command-on-region]
|
|
|
|
& enter a box type. Box type selection uses tab completion on
|
|
|
|
the supported types.
|
|
|
|
|
|
|
|
To remove a box simply prefix a 1 to the call, eg M-1
|
|
|
|
\\[boxes-command-on-region] will remove a box from a region.
|
|
|
|
|
|
|
|
Note that interactive use requires `boxes' >= 2.1.0 to support
|
|
|
|
querying the supported types.
|
|
|
|
|
|
|
|
When calling from Lisp, supply the region START & END and the box
|
|
|
|
TYPE to create a box. Specifying a non-nil value for REMOVE,
|
|
|
|
removes the box."
|
2006-07-12 08:23:59 +02:00
|
|
|
(interactive (let ((string
|
|
|
|
(completing-read (format "Box type (%s): " boxes-default-type)
|
2022-12-05 13:26:48 +01:00
|
|
|
(boxes-types) nil t nil 'boxes-history boxes-default-type)))
|
2015-12-11 15:26:58 +01:00
|
|
|
(list (region-beginning) (region-end)
|
2006-07-12 08:23:59 +02:00
|
|
|
string
|
|
|
|
current-prefix-arg)))
|
2022-11-17 21:28:29 +01:00
|
|
|
(when (or (null type) (string= type ""))
|
|
|
|
(setq type (boxes-default-type major-mode)))
|
|
|
|
(setq boxes-default-type type)
|
2006-07-12 08:23:59 +02:00
|
|
|
(let ((command-string
|
2015-12-11 15:26:58 +01:00
|
|
|
(concat boxes-command
|
2006-07-12 08:23:59 +02:00
|
|
|
(if remove
|
|
|
|
(concat boxes-args " -r "))
|
|
|
|
(if type
|
|
|
|
(concat boxes-args " -d " type)))))
|
2015-12-11 15:31:08 +01:00
|
|
|
(shell-command-on-region start end command-string nil 1)))
|
2006-07-12 08:23:59 +02:00
|
|
|
|
|
|
|
(provide 'boxes)
|
|
|
|
;;; boxes.el ends here
|