2022-09-30 20:46:35 +00:00
|
|
|
;;; ansi-osc.el --- Support for OSC escape sequences -*- lexical-binding: t; -*-
|
2022-09-18 11:54:46 +00:00
|
|
|
|
|
|
|
;; Copyright (C) 2022 Free Software Foundation, Inc.
|
|
|
|
|
|
|
|
;; Author: Augusto Stoffel <arstoffel@gmail.com>
|
|
|
|
;; Matthias Meulien <orontee@gmail.com>
|
|
|
|
;; Maintainer: emacs-devel@gnu.org
|
|
|
|
;; Keywords: processes, terminals, services
|
|
|
|
|
|
|
|
;; This program 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 3 of the License, or
|
|
|
|
;; (at your option) any later version.
|
|
|
|
|
|
|
|
;; 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/>.
|
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
|
2022-09-19 18:47:00 +00:00
|
|
|
;; Interpretation of OSC (Operating System Commands) escape sequences.
|
|
|
|
;; Handlers for OSC 2, 7 and 8 (for window title, current directory
|
|
|
|
;; and hyperlinks respectively) are provided.
|
2022-09-18 11:54:46 +00:00
|
|
|
|
2022-09-30 20:46:35 +00:00
|
|
|
;; The function `ansi-osc-compilation-filter' can be added to
|
2022-09-17 16:59:31 +00:00
|
|
|
;; `compilation-filter-hook' to collect OSC sequences in compilation
|
2022-09-30 20:46:35 +00:00
|
|
|
;; buffers. The variable `ansi-osc-for-compilation-buffer' tells what
|
|
|
|
;; to do with collected sequences.
|
2022-09-17 16:59:31 +00:00
|
|
|
|
2022-09-18 11:54:46 +00:00
|
|
|
;;; Code:
|
|
|
|
|
2022-09-30 20:46:35 +00:00
|
|
|
(defconst ansi-osc-control-seq-regexp
|
2022-09-17 16:59:31 +00:00
|
|
|
;; See ECMA 48, section 8.3.89 "OSC - OPERATING SYSTEM COMMAND".
|
|
|
|
"\e\\][\x08-\x0D]*[\x20-\x7E]*\\(\a\\|\e\\\\\\)"
|
|
|
|
"Regexp matching an OSC control sequence.")
|
|
|
|
|
2022-09-30 20:46:35 +00:00
|
|
|
(defun ansi-osc-filter-region (begin end)
|
2022-09-18 16:09:54 +00:00
|
|
|
"Filter out all OSC control sequences from region between BEGIN and END."
|
2022-09-17 16:59:31 +00:00
|
|
|
(save-excursion
|
|
|
|
(goto-char begin)
|
|
|
|
;; Delete escape sequences.
|
2022-09-30 20:46:35 +00:00
|
|
|
(while (re-search-forward ansi-osc-control-seq-regexp end t)
|
2022-09-17 16:59:31 +00:00
|
|
|
(delete-region (match-beginning 0) (match-end 0)))))
|
|
|
|
|
2022-09-30 20:46:35 +00:00
|
|
|
(defvar-local ansi-osc-handlers '(("2" . ansi-osc-window-title-handler)
|
|
|
|
("7" . ansi-osc-directory-tracker)
|
|
|
|
("8" . ansi-osc-hyperlink-handler))
|
2022-09-18 11:54:46 +00:00
|
|
|
"Alist of handlers for OSC escape sequences.
|
2022-09-30 20:46:35 +00:00
|
|
|
See `ansi-osc-apply-on-region' for details.")
|
2022-09-18 11:54:46 +00:00
|
|
|
|
2022-09-30 20:46:35 +00:00
|
|
|
(defvar-local ansi-osc--marker nil)
|
|
|
|
;; The function `ansi-osc-apply-on-region' can set `ansi-osc--marker'
|
|
|
|
;; to the start position of an escape sequence without termination.
|
2022-09-18 11:54:46 +00:00
|
|
|
|
2022-09-30 20:46:35 +00:00
|
|
|
(defun ansi-osc-apply-on-region (begin end)
|
2022-09-18 16:09:54 +00:00
|
|
|
"Interpret OSC escape sequences in region between BEGIN and END.
|
|
|
|
This function searches for escape sequences of the forms
|
2022-09-18 11:54:46 +00:00
|
|
|
|
|
|
|
ESC ] command ; text BEL
|
|
|
|
ESC ] command ; text ESC \\
|
|
|
|
|
|
|
|
Every occurrence of such escape sequences is removed from the
|
2022-09-30 20:46:35 +00:00
|
|
|
buffer. Then, if `command' is a key in the alist that is the
|
|
|
|
value of the local variable `ansi-osc-handlers', that key's
|
|
|
|
value, which should be a function, is called with `command' and
|
|
|
|
`text' as arguments, with point where the escape sequence was
|
|
|
|
located."
|
2022-09-18 11:54:46 +00:00
|
|
|
(save-excursion
|
2022-09-30 20:46:35 +00:00
|
|
|
(goto-char (or ansi-osc--marker begin))
|
2022-09-18 11:54:46 +00:00
|
|
|
(when (eq (char-before) ?\e) (backward-char))
|
|
|
|
(while (re-search-forward "\e]" end t)
|
|
|
|
(let ((pos0 (match-beginning 0))
|
|
|
|
(code (and (re-search-forward "\\=\\([0-9A-Za-z]*\\);" end t)
|
|
|
|
(match-string 1)))
|
|
|
|
(pos1 (point)))
|
|
|
|
(if (re-search-forward "\a\\|\e\\\\" end t)
|
|
|
|
(let ((text (buffer-substring-no-properties
|
|
|
|
pos1 (match-beginning 0))))
|
2022-09-30 20:46:35 +00:00
|
|
|
(setq ansi-osc--marker nil)
|
2022-09-18 11:54:46 +00:00
|
|
|
(delete-region pos0 (point))
|
2022-09-30 20:46:35 +00:00
|
|
|
(when-let ((fun (cdr (assoc-string code ansi-osc-handlers))))
|
2022-09-18 11:54:46 +00:00
|
|
|
(funcall fun code text)))
|
|
|
|
(put-text-property pos0 end 'invisible t)
|
2022-09-30 20:46:35 +00:00
|
|
|
(setq ansi-osc--marker (copy-marker pos0)))))))
|
2022-09-18 11:54:46 +00:00
|
|
|
|
2022-09-17 15:54:24 +00:00
|
|
|
;; Window title handling (OSC 2)
|
|
|
|
|
2022-09-30 20:46:35 +00:00
|
|
|
(defvar-local ansi-osc-window-title nil)
|
|
|
|
(defun ansi-osc-window-title-handler (_ text)
|
|
|
|
"Set value of `ansi-osc-window-title' from an OSC 2 escape sequence.
|
|
|
|
The variable `ansi-osc-window-title' can then be referenced in
|
2022-09-17 15:54:24 +00:00
|
|
|
`frame-title-format' to dynamically set the frame title.
|
|
|
|
|
2022-09-18 16:09:54 +00:00
|
|
|
This function is intended to be included as an element of the
|
2022-09-30 20:46:35 +00:00
|
|
|
list that is the value of `ansi-osc-handlers'."
|
|
|
|
(setq ansi-osc-window-title text))
|
2022-09-17 15:54:24 +00:00
|
|
|
|
2022-09-18 11:54:46 +00:00
|
|
|
;; Current directory tracking (OSC 7)
|
|
|
|
|
|
|
|
(declare-function url-host "url/url-parse.el")
|
|
|
|
(declare-function url-type "url/url-parse.el")
|
|
|
|
(declare-function url-filename "url/url-parse.el")
|
2022-09-30 20:46:35 +00:00
|
|
|
(defun ansi-osc-directory-tracker (_ text)
|
2022-09-18 11:54:46 +00:00
|
|
|
"Update `default-directory' from OSC 7 escape sequences.
|
|
|
|
|
2022-09-18 16:09:54 +00:00
|
|
|
This function is intended to be included as an element of the
|
2022-09-30 20:46:35 +00:00
|
|
|
the list that is the value of `ansi-osc-handlers'. You should arrange
|
2022-09-18 16:09:54 +00:00
|
|
|
for your shell to print the appropriate escape sequence at each prompt,
|
|
|
|
such as with the following command:
|
2022-09-18 11:54:46 +00:00
|
|
|
|
|
|
|
printf \"\\e]7;file://%s%s\\e\\\\\" \"$HOSTNAME\" \"$PWD\"
|
|
|
|
|
|
|
|
This functionality serves as an alternative to `dirtrack-mode'
|
|
|
|
and `shell-dirtrack-mode'."
|
|
|
|
(let ((url (url-generic-parse-url text)))
|
|
|
|
(when (and (string= (url-type url) "file")
|
|
|
|
(or (null (url-host url))
|
|
|
|
(string= (url-host url) (system-name))))
|
|
|
|
(ignore-errors
|
|
|
|
(cd-absolute (url-unhex-string (url-filename url)))))))
|
|
|
|
|
|
|
|
;; Hyperlink handling (OSC 8)
|
|
|
|
|
2022-10-16 11:53:37 +00:00
|
|
|
(defvar-keymap ansi-osc-hyperlink-map
|
|
|
|
:doc "Keymap used by OSC 8 hyperlink buttons."
|
|
|
|
"C-c RET" #'browse-url-button-open
|
|
|
|
"<mouse-2>" #'browse-url-button-open
|
|
|
|
"<follow-link>" 'mouse-face)
|
2022-09-18 11:54:46 +00:00
|
|
|
|
2022-09-30 20:46:35 +00:00
|
|
|
(define-button-type 'ansi-osc-hyperlink
|
|
|
|
'keymap ansi-osc-hyperlink-map
|
2022-09-18 11:54:46 +00:00
|
|
|
'help-echo (lambda (_ buffer pos)
|
|
|
|
(when-let ((url (get-text-property pos 'browse-url-data buffer)))
|
|
|
|
(format "mouse-2, C-c RET: Open %s" url))))
|
|
|
|
|
2022-09-30 20:46:35 +00:00
|
|
|
(defvar-local ansi-osc-hyperlink--state nil)
|
2022-09-18 11:54:46 +00:00
|
|
|
|
2022-09-30 20:46:35 +00:00
|
|
|
(defun ansi-osc-hyperlink-handler (_ text)
|
2022-09-18 11:54:46 +00:00
|
|
|
"Create a hyperlink from an OSC 8 escape sequence.
|
2022-09-18 16:09:54 +00:00
|
|
|
This function is intended to be included as an elemnt of the list
|
2022-09-30 20:46:35 +00:00
|
|
|
that is the value of `ansi-osc-handlers'."
|
|
|
|
(when ansi-osc-hyperlink--state
|
|
|
|
(let ((start (car ansi-osc-hyperlink--state))
|
|
|
|
(url (cdr ansi-osc-hyperlink--state)))
|
2022-09-18 11:54:46 +00:00
|
|
|
(make-text-button start (point)
|
2022-09-30 20:46:35 +00:00
|
|
|
'type 'ansi-osc-hyperlink
|
2022-09-18 11:54:46 +00:00
|
|
|
'browse-url-data url)))
|
2022-09-30 20:46:35 +00:00
|
|
|
(setq ansi-osc-hyperlink--state
|
2022-09-18 11:54:46 +00:00
|
|
|
(and (string-match ";\\(.+\\)" text)
|
|
|
|
(cons (point-marker) (match-string-no-properties 1 text)))))
|
|
|
|
|
2022-10-01 06:46:50 +00:00
|
|
|
(defgroup ansi-osc nil
|
|
|
|
"Interpretation of OSC escape sequences.
|
|
|
|
Handlers for OSC 2, 7 and 8 (for window title, current directory
|
|
|
|
and hyperlinks respectively) are provided. OSC (Operating System
|
|
|
|
Commands) control sequences are defined in section 8.3.89 of the
|
|
|
|
ECMA-48 standard is freely available at
|
|
|
|
<URL:https://www.ecma-international.org/publications/standards/Ecma-048.htm>
|
|
|
|
as a PDF file."
|
|
|
|
:version "29.1"
|
|
|
|
:group 'processes)
|
|
|
|
|
2022-09-30 20:46:35 +00:00
|
|
|
(defcustom ansi-osc-for-compilation-buffer 'filter
|
2022-09-18 16:09:54 +00:00
|
|
|
"What to do with OSC escape sequences in compilation output.
|
|
|
|
|
2022-09-17 16:59:31 +00:00
|
|
|
If nil, do nothing.
|
|
|
|
|
|
|
|
If the symbol `filter', then filter out all OSC control sequences.
|
|
|
|
|
2022-09-18 16:09:54 +00:00
|
|
|
If any other non-nil value, then collect OSC control sequences
|
2022-09-30 20:46:35 +00:00
|
|
|
and call the appropriate handlers as described in `ansi-osc-handlers'.
|
2022-09-17 16:59:31 +00:00
|
|
|
|
2022-09-30 20:46:35 +00:00
|
|
|
In order for this to have any effect, `ansi-osc-compilation-filter'
|
2022-09-17 16:59:31 +00:00
|
|
|
must be in `compilation-filter-hook'."
|
|
|
|
:type '(choice (const :tag "Do nothing" nil)
|
2022-09-18 16:09:54 +00:00
|
|
|
(const :tag "Filter out OSC" filter)
|
|
|
|
(other :tag "Translate OSC" t))
|
2022-10-01 06:46:50 +00:00
|
|
|
:group 'ansi-osc
|
2022-09-18 16:09:54 +00:00
|
|
|
:version "29.1")
|
2022-09-17 16:59:31 +00:00
|
|
|
|
2022-09-18 12:51:37 +00:00
|
|
|
(defvar compilation-filter-start)
|
|
|
|
|
2022-09-17 16:59:31 +00:00
|
|
|
;;;###autoload
|
2022-09-30 20:46:35 +00:00
|
|
|
(defun ansi-osc-compilation-filter ()
|
2022-09-17 16:59:31 +00:00
|
|
|
"Maybe collect OSC control sequences.
|
2022-09-30 20:46:35 +00:00
|
|
|
This function depends on the variable `ansi-osc-for-compilation-buffer',
|
2022-09-18 16:09:54 +00:00
|
|
|
and is meant to be used in `compilation-filter-hook'."
|
2022-09-17 16:59:31 +00:00
|
|
|
(let ((inhibit-read-only t))
|
2022-09-30 20:46:35 +00:00
|
|
|
(pcase ansi-osc-for-compilation-buffer
|
2022-09-17 16:59:31 +00:00
|
|
|
('nil nil)
|
|
|
|
('filter
|
2022-09-30 20:46:35 +00:00
|
|
|
(ansi-osc-filter-region compilation-filter-start (point)))
|
2022-09-17 16:59:31 +00:00
|
|
|
(_
|
2022-09-30 20:46:35 +00:00
|
|
|
(ansi-osc-apply-on-region compilation-filter-start (point))))))
|
2022-09-17 16:59:31 +00:00
|
|
|
|
2022-09-30 20:46:35 +00:00
|
|
|
(provide 'ansi-osc)
|
|
|
|
;;; ansi-osc.el ends here
|