scel/el/sclang-widgets.el

173 lines
5.8 KiB
EmacsLisp
Raw Permalink Normal View History

2022-07-30 17:15:38 +00:00
;;; sclang-widgets.el --- Widget definitions for SCLang -*- coding: utf-8; lexical-binding: t -*-
2022-07-30 17:15:38 +00:00
;; Copyright (C) 2005 Free Software Foundation, Inc.
2019-12-12 09:37:11 +00:00
;; Author: Mario Lang <mlang@blind.guru>
;; This file 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 file 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:
;;
2022-07-30 17:15:38 +00:00
;; Widget definitions for SCLang
;;; Code:
(require 'cl-lib)
(require 'sclang-util)
(require 'sclang-language)
(require 'sclang-interp)
2022-07-30 17:15:38 +00:00
(require 'widget)
(require 'wid-edit)
(defvar sclang-widgets nil)
(make-variable-buffer-local 'sclang-widgets)
;; Button (not used yet)
(define-widget 'sclang-button 'item
"A button."
:create #'sclang-widget-button-create
:action #'sclang-widget-button-action)
(defun sclang-widget-button-create (widget)
"Create WIDGET at point in the current buffer."
(widget-specify-insert
(let ((from (point))
2022-07-30 17:15:38 +00:00
button-begin button-end)
(setq button-begin from)
(insert (widget-get-indirect widget :button-prefix))
(princ (nth (widget-get widget :value) (widget-get widget :states)) (current-buffer))
(insert (widget-get-indirect widget :button-suffix))
2022-07-30 17:15:38 +00:00
(setq button-end from)
;; Specify button, and insert value.
(and button-begin button-end
2022-07-30 17:15:38 +00:00
(widget-specify-button widget button-begin button-end)))
(let ((from (point-min-marker))
2022-07-30 17:15:38 +00:00
(to (point-max-marker)))
(set-marker-insertion-type from t)
(set-marker-insertion-type to nil)
(widget-put widget :from from)
(widget-put widget :to to)))
(widget-clear-undo))
2022-07-30 17:15:38 +00:00
(defun sclang-widget-button-action (widget _event)
"Set button action for WIDGET."
(widget-value-set widget
2022-07-30 17:15:38 +00:00
(if (>= (widget-get widget :value) (1- (length (widget-get widget :states))))
0
(1+ (widget-get widget :value))))
(sclang-eval-string
(sclang-format "EmacsWidget.idmap[%o].valueFromEmacs(%o)"
2022-07-30 17:15:38 +00:00
(widget-get widget :id) (widget-get widget :value))))
(sclang-set-command-handler
'_widgetSetStates
(lambda (arg)
(cl-multiple-value-bind (buffer id states value) arg
(with-current-buffer (get-buffer buffer)
(let ((widget (cdr (cl-find id sclang-widgets :key 'car))))
2022-07-30 17:15:38 +00:00
(widget-put widget :states states)
(widget-value-set widget value)
value)))))
(define-widget 'sclang-slider 'default
"Slider widget."
:size 20
:create #'sclang-widget-slider-create
:button-prefix "["
:button-suffix "]"
:value 0.5
:value-get #'widget-value-value-get
:value-set #'sclang-widget-slider-value-set
:action (lambda (widget event)
2022-07-30 17:15:38 +00:00
(let ((pos (if event (posn-point (event-start event)) (point))))
(widget-value-set widget (/ (float (- pos (widget-get widget :from))) (widget-get widget :size))))))
(defun sclang-widget-slider-create (widget)
"Create WIDGET at point in the current buffer."
(widget-specify-insert
(let ((from (point))
2022-07-30 17:15:38 +00:00
(inhibit-redisplay t)
button-begin button-end)
(setq button-begin from)
(insert (widget-get-indirect widget :button-prefix))
(insert-char ?- (widget-get widget :size))
(backward-char (1+ (widget-put widget :current-pos (- (widget-get widget :size) (round (* (widget-get widget :value) (widget-get widget :size)))))))
(delete-char 1) (insert "|")
(goto-char (point-max))
(insert (widget-get-indirect widget :button-suffix))
(setq button-end (point))
;; Specify button
(and button-begin button-end
2022-07-30 17:15:38 +00:00
(widget-specify-button widget button-begin button-end)))
(let ((from (point-min-marker))
2022-07-30 17:15:38 +00:00
(to (point-max-marker)))
(set-marker-insertion-type from t)
(set-marker-insertion-type to nil)
(widget-put widget :from from)
(widget-put widget :to to)))
(widget-clear-undo))
(defun sclang-widget-slider-value-set (widget value)
2022-07-30 17:15:38 +00:00
"Set slider WIDGET to VALUE."
(save-excursion
(let ((inhibit-read-only t))
(goto-char (widget-get widget :from))
(forward-char (widget-get widget :current-pos))
(insert "-") (delete-char 1)
(widget-put widget :value value)
(goto-char (widget-get widget :from))
(let ((n (round (* value (widget-get widget :size)))))
2022-07-30 17:15:38 +00:00
(widget-put widget :current-pos n)
(forward-char n)
(insert "|") (delete-char 1)))))
;; Class Tree
(require 'tree-widget)
(define-widget 'sclang-class-tree 'tree-widget
"Widget for displaying the SCLang Class Tree."
:dynargs #'sclang-widget-class-tree-dynargs)
(defun sclang-widget-class-tree-dynargs (widget)
2022-07-30 17:15:38 +00:00
"Class tree WIDGET."
(sclang-eval-sync (sclang-format "EmacsClassTree.dynargs(%o)"
2022-07-30 17:15:38 +00:00
(widget-get widget :tag))))
(define-widget 'sclang-file-position 'item
"File position link for the SCLang Class Tree widget."
:format "%[%t%]\n"
:action (lambda (widget event)
2022-07-30 17:15:38 +00:00
(find-file-other-window (widget-get widget :filename))
(goto-char (widget-get widget :char-pos))))
(defun sclang-class-tree (class-name)
"Display a tree-view of the sub-classes and methods of CLASS-NAME."
(interactive
(list (sclang-read-symbol "Class: " "Object" #'sclang-class-name-p)))
(sclang-eval-string (format "EmacsClassBrowser(%s)" class-name)))
2022-07-30 17:15:38 +00:00
(provide 'sclang-widgets)
2022-07-30 17:15:38 +00:00
;;; sclang-widgets.el ends here