├── etc └── images │ ├── popup1.png │ ├── popup2.png │ └── popup3.png ├── .github ├── dependabot.yml └── workflows │ └── test.yml ├── .gitignore ├── Eask ├── Makefile ├── tests ├── run-test.el ├── popup-interactive-test.el └── popup-test.el ├── README.md ├── LICENSE └── popup.el /etc/images/popup1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/auto-complete/popup-el/HEAD/etc/images/popup1.png -------------------------------------------------------------------------------- /etc/images/popup2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/auto-complete/popup-el/HEAD/etc/images/popup2.png -------------------------------------------------------------------------------- /etc/images/popup3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/auto-complete/popup-el/HEAD/etc/images/popup3.png -------------------------------------------------------------------------------- /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | updates: 3 | - package-ecosystem: github-actions 4 | directory: / 5 | schedule: 6 | interval: daily 7 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # ignore these directories. 2 | /.git 3 | /recipes 4 | /test 5 | 6 | # ignore generated files. 7 | *.elc 8 | 9 | # eask packages 10 | .eask/ 11 | dist/ 12 | 13 | # packaging 14 | *-autoloads.el 15 | *-pkg.el 16 | 17 | # OS generated 18 | .DS_Store 19 | -------------------------------------------------------------------------------- /Eask: -------------------------------------------------------------------------------- 1 | ;; -*- mode: eask; lexical-binding: t -*- 2 | 3 | (package "popup" 4 | "0.5.9" 5 | "Visual Popup User Interface") 6 | 7 | (website-url "https://github.com/auto-complete/popup-el") 8 | (keywords "lisp") 9 | 10 | (package-file "popup.el") 11 | 12 | (script "test" "echo \"Error: no test specified\" && exit 1") 13 | 14 | (source "gnu") 15 | (source "melpa") 16 | 17 | (depends-on "emacs" "24.3") 18 | 19 | (development 20 | (depends-on "ert")) 21 | 22 | (setq network-security-level 'low) ; see https://github.com/jcs090218/setup-emacs-windows/issues/156#issuecomment-932956432 23 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | EMACS ?= emacs 2 | EASK ?= eask 3 | 4 | .PHONY: clean checkdoc lint package install compile test 5 | 6 | ci: clean package install compile 7 | 8 | package: 9 | @echo "Packaging..." 10 | $(EASK) package 11 | 12 | install: 13 | @echo "Installing..." 14 | $(EASK) install 15 | 16 | compile: 17 | @echo "Compiling..." 18 | $(EASK) compile 19 | 20 | test: 21 | @echo "Testing..." 22 | $(EASK) test ert ./test/*.el 23 | 24 | checkdoc: 25 | @echo "Run checkdoc..." 26 | $(EASK) lint checkdoc 27 | 28 | lint: 29 | @echo "Run package-lint..." 30 | $(EASK) lint package 31 | 32 | clean: 33 | $(EASK) clean all 34 | -------------------------------------------------------------------------------- /tests/run-test.el: -------------------------------------------------------------------------------- 1 | ;; Usage: 2 | ;; 3 | ;; cask exec emacs -Q -l tests/run-test.el # interactive mode 4 | ;; cask exec emacs -batch -Q -l tests/run-test.el # batch mode 5 | 6 | 7 | ;; Utils 8 | (defun popup-test-join-path (path &rest rest) 9 | "Join a list of PATHS with appropriate separator (such as /). 10 | 11 | \(fn &rest paths)" 12 | (if rest 13 | (concat (file-name-as-directory path) (apply 'popup-test-join-path rest)) 14 | path)) 15 | 16 | (defvar popup-test-dir (file-name-directory load-file-name)) 17 | (defvar popup-root-dir (concat popup-test-dir "..")) 18 | 19 | 20 | ;; Setup `load-path' 21 | (mapc (lambda (p) (add-to-list 'load-path p)) 22 | (list popup-test-dir 23 | popup-root-dir)) 24 | 25 | ;; Load tests 26 | (load "popup-test") 27 | 28 | 29 | ;; Run tests 30 | (if noninteractive 31 | (ert-run-tests-batch-and-exit) 32 | (ert t)) 33 | -------------------------------------------------------------------------------- /.github/workflows/test.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | 3 | on: 4 | push: 5 | branches: 6 | - master 7 | pull_request: 8 | workflow_dispatch: 9 | 10 | concurrency: 11 | group: ${{ github.workflow }}-${{ github.ref }} 12 | cancel-in-progress: true 13 | 14 | jobs: 15 | test: 16 | runs-on: ${{ matrix.os }} 17 | continue-on-error: ${{ matrix.experimental }} 18 | strategy: 19 | fail-fast: false 20 | matrix: 21 | os: [ubuntu-latest, macos-latest, windows-latest] 22 | emacs-version: 23 | - 26.3 24 | - 27.2 25 | - 28.2 26 | - 29.4 27 | - 30.2 28 | experimental: [false] 29 | include: 30 | - os: ubuntu-latest 31 | emacs-version: snapshot 32 | experimental: true 33 | - os: macos-latest 34 | emacs-version: snapshot 35 | experimental: true 36 | - os: windows-latest 37 | emacs-version: snapshot 38 | experimental: true 39 | exclude: 40 | - os: macos-latest 41 | emacs-version: 26.3 42 | - os: macos-latest 43 | emacs-version: 27.2 44 | 45 | steps: 46 | - uses: actions/checkout@v6 47 | 48 | - uses: jcs090218/setup-emacs@master 49 | with: 50 | version: ${{ matrix.emacs-version }} 51 | 52 | - uses: emacs-eask/setup-eask@master 53 | with: 54 | version: 'snapshot' 55 | 56 | - name: Run tests 57 | run: 58 | make ci 59 | -------------------------------------------------------------------------------- /tests/popup-interactive-test.el: -------------------------------------------------------------------------------- 1 | (require 'popup) 2 | 3 | (defmacro test (explain &rest body) 4 | (declare (indent 1)) 5 | `(let ((buf "*buf*") 6 | (window-config (current-window-configuration))) 7 | (unwind-protect 8 | (progn 9 | (delete-other-windows) 10 | (switch-to-buffer buf) 11 | (erase-buffer) 12 | (insert " ") 13 | (let ((success (progn ,@body))) 14 | (unless success 15 | (error "failed: %s" ,explain)))) 16 | (when popup 17 | (popup-delete popup) 18 | (setq popup nil)) 19 | (kill-buffer buf) 20 | (set-window-configuration window-config)))) 21 | 22 | (defmacro ui-test (prompt &rest body) 23 | (declare (indent 1)) 24 | `(test ,prompt ,@body (yes-or-no-p ,prompt))) 25 | 26 | (defun input (key) 27 | (push key unread-command-events)) 28 | 29 | (defvar popup nil) 30 | 31 | (test "popup-create" 32 | (setq popup (popup-create (point) 10 10))) 33 | 34 | (test "popup-delete" 35 | (setq popup (popup-create (point) 10 10)) 36 | (popup-delete popup) 37 | (not (popup-live-p popup))) 38 | 39 | (ui-test "popup?" 40 | (setq popup (popup-create (point) 10 10)) 41 | (popup-set-list popup '("hello" "world")) 42 | (popup-draw popup)) 43 | 44 | (ui-test "hidden?" 45 | (setq popup (popup-create (point) 10 10)) 46 | (popup-set-list popup '("hello" "world")) 47 | (popup-draw popup) 48 | (popup-hide popup)) 49 | 50 | (ui-test "isearch?" 51 | (setq popup (popup-create (point) 10 10)) 52 | (popup-set-list popup '("hello" "world")) 53 | (popup-draw popup) 54 | (input ?e) 55 | (popup-isearch popup)) 56 | 57 | (ui-test "tip?" 58 | (popup-tip 59 | "Start isearch on POPUP. This function is synchronized, meaning 60 | event loop waits for quiting of isearch. 61 | 62 | CURSOR-COLOR is a cursor color during isearch. The default value 63 | is `popup-isearch-cursor-color'. 64 | 65 | KEYMAP is a keymap which is used when processing events during 66 | event loop. The default value is `popup-isearch-keymap'. 67 | 68 | CALLBACK is a function taking one argument. `popup-isearch' calls 69 | CALLBACK, if specified, after isearch finished or isearch 70 | canceled. The arguments is whole filtered list of items. 71 | 72 | HELP-DELAY is a delay of displaying helps." 73 | :nowait t)) 74 | 75 | (ui-test "fold?" 76 | (let ((s (make-string (- (window-width) 3) ? ))) 77 | (insert s) 78 | (setq popup (popup-tip "long long long long line" :nowait t)))) 79 | 80 | (ui-test "fold?" 81 | (let ((s (make-string (- (window-height) 3) ?\n))) 82 | (insert s) 83 | (setq popup (popup-tip "bla\nbla\nbla\nbla\nbla" :nowait t)))) 84 | 85 | (ui-test "margin?" 86 | (setq popup (popup-tip "Margin?" :nowait t :margin t))) 87 | 88 | (ui-test "two lines?" 89 | (setq popup (popup-tip "Foo\nBar\nBaz" :nowait t :height 2))) 90 | 91 | (ui-test "scroll bar?" 92 | (setq popup (popup-tip "Foo\nBar\nBaz\nFez\nOz" :nowait t :height 3 :scroll-bar t :margin t))) 93 | 94 | (ui-test "min-height?" 95 | (setq popup (popup-tip "Hello" :nowait t :min-height 10))) 96 | 97 | (ui-test "menu?" 98 | (setq popup (popup-menu* '("Foo" "Bar" "Baz") :nowait t))) 99 | 100 | (ui-test "cascade menu?" 101 | (setq popup (popup-cascade-menu '(("Foo" "Foo1" "Foo2") "Bar" "Baz") :nowait t :margin t))) 102 | 103 | (ui-test "next?" 104 | (setq popup (popup-cascade-menu '("Foo" "Bar" "Baz") :nowait t :margin t)) 105 | (popup-next popup)) 106 | 107 | (ui-test "previous?" 108 | (setq popup (popup-cascade-menu '("Foo" "Bar" "Baz") :nowait t :margin t)) 109 | (popup-previous popup)) 110 | 111 | (ui-test "select?" 112 | (setq popup (popup-cascade-menu '("Foo" "Bar" "Baz") :nowait t :margin t)) 113 | (popup-select popup 1)) 114 | 115 | (ui-test "scroll-down?" 116 | (setq popup (popup-cascade-menu (loop repeat 100 collect "Foo") :nowait t :height 10 :margin t :scroll-bar t)) 117 | (popup-scroll-down popup 10)) 118 | 119 | (ui-test "scroll-up?" 120 | (setq popup (popup-cascade-menu (loop repeat 100 collect "Foo") :nowait t :height 10 :margin t :scroll-bar t)) 121 | (popup-scroll-down popup 999) 122 | (popup-scroll-up popup 10)) 123 | 124 | (message "Congratulations!") 125 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![License: GPL v3](https://img.shields.io/badge/License-GPL%20v3-blue.svg)](https://www.gnu.org/licenses/gpl-3.0) 2 | [![melpa badge][melpa-badge]][melpa-link] 3 | [![melpa stable badge][melpa-stable-badge]][melpa-stable-link] 4 | 5 | # popup.el 6 | 7 | [![CI](https://github.com/auto-complete/popup-el/actions/workflows/test.yml/badge.svg)](https://github.com/auto-complete/popup-el/actions/workflows/test.yml) 8 | 9 | ## Overview 10 | 11 | popup.el is a visual popup user interface library for Emacs. This 12 | provides a basic API and common UI widgets such as popup tooltips and 13 | popup menus. 14 | 15 | ## Screenshots 16 | 17 | | Tooltip | Popup Menu | Popup Cascade Menu | 18 | |:-------------------------------------------------------------------------------------------|:-------------------------------------------------------------------------------------------|:-------------------------------------------------------------------------------------------| 19 | | ![](https://raw.githubusercontent.com/auto-complete/popup-el/master/etc/images/popup1.png) | ![](https://raw.githubusercontent.com/auto-complete/popup-el/master/etc/images/popup2.png) | ![](https://raw.githubusercontent.com/auto-complete/popup-el/master/etc/images/popup3.png) | 20 | 21 | ## Installation 22 | 23 | You can install `popup.el` from [MELPA](https://melpa.org/) with package.el. 24 | popwin is tested under GNU Emacs 24 or later. 25 | 26 | Alternatively, users of Debian 9 or later or Ubuntu 16.04 or later may 27 | simply `apt-get install elpa-popup`. 28 | 29 | ## Popup Items 30 | 31 | Elements of `popup-list` have to be popup items. A popup item is 32 | substantially a string but it may involve some text-properties. There 33 | are two ways to make popup items. One is just using strings. Another 34 | is to use the `popup-make-item` function, which just returns the string 35 | after adding text-properties of its keywords. Effective text-properties 36 | are: 37 | 38 | * `value` -- This represents the **real** value of the item. This will 39 | be used when returning the value but not the item (or string) from 40 | some synchronous functions such as `popup-menu*`. 41 | * `face` -- The background face of the item. The value of `popup-face` 42 | will be overridden. 43 | * `selection-face` -- The selection face of the item. The value of 44 | `popup-selection-face` will be overridden. 45 | * `document` -- The documentation string or function of the item. 46 | * `summary` -- The summary string of the item. This will be shown 47 | inline with the item. 48 | * `symbol` -- The symbol character of the item. 49 | * `sublist` -- The sublist of the item. This is effective only with 50 | `popup-cascade-menu`. 51 | 52 | All of properties can be accessed by `popup-item-` utility function. 53 | 54 | ### Function: `popup-item-propertize` 55 | 56 | ``` 57 | popup-item-propertize item &rest properties => item 58 | ``` 59 | 60 | Same as `propertize` except that this avoids overriding existed value 61 | with `nil` property. 62 | 63 | ### Function: `popup-make-item` 64 | 65 | ``` 66 | popup-make-item name &key value popup-face selection-face sublist 67 | document symbol summary => item 68 | ``` 69 | 70 | The utility function of `popup-item-propertize`. 71 | 72 | ## Popups 73 | 74 | This section describes the basic data structures and operations of 75 | popups. 76 | 77 | ### Struct: `popup` 78 | 79 | Any instance of `popup` structure has the following fields (some 80 | unimportant fields are not listed): 81 | 82 | * `point` 83 | * `row` -- The line number. 84 | * `column` 85 | * `width` -- Max width of `popup` instance. 86 | * `height` -- Max height of `popup` instance. 87 | * `min-height` 88 | * `current-height` 89 | * `direction` -- Positive number means forward, negative number means backward. 90 | * `parent` -- The parent of `popup` instance. 91 | * `face` -- The background face. 92 | * `selection-face` 93 | * `margin-left` 94 | * `margin-right` 95 | * `scroll-bar` -- Non-nil means `popup` instance has a scroll bar. 96 | * `symbol` -- Non-nil means `popup` instance has a space for displaying symbols of item. 97 | * `cursor` -- The current position of `list`. 98 | * `scroll-top` -- The offset of scrolling. 99 | * `list` -- The contents of `popup` instance in a list of items (strings). 100 | * `original-list` -- Same as `list` except that this is not filtered. 101 | 102 | All of these fields can be accessed by `popup-` function. 103 | 104 | ### Function: `popup-create` 105 | 106 | ``` 107 | popup-create point width height &key min-height max-width around face 108 | selection-face scroll-bar margin-left margin-right symbol parent 109 | parent-offset => popup 110 | ``` 111 | 112 | Create a popup instance at `POINT` with `WIDTH` and `HEIGHT`. 113 | 114 | `MIN-HEIGHT` is the minimal height of the popup. The default value is 0. 115 | 116 | `MAX-WIDTH` is the maximum width of the popup. The default value is 117 | nil (no limit). If a floating point, the value refers to the ratio of 118 | the window. If an integer, limit is in characters. 119 | 120 | If `AROUND` is non-nil, the popup will be displayed around the point 121 | but not at the point. 122 | 123 | `FACE` is the background face of the popup. The default value is 124 | `popup-face`. 125 | 126 | `SELECTION-FACE` is the foreground (selection) face of the popup The 127 | default value is `popup-face`. 128 | 129 | If `SCROLL-BAR` is non-nil, the popup will have a scroll bar at the 130 | right. 131 | 132 | If `MARGIN-LEFT` is non-nil, the popup will have a margin at the left. 133 | 134 | If `MARGIN-RIGHT` is non-nil, the popup will have a margin at the 135 | right. 136 | 137 | `SYMBOL` is a single character which indicates the kind of the item. 138 | 139 | `PARENT` is the parent popup instance. If `PARENT` is omitted, the popup 140 | will be a root instance. 141 | 142 | `PARENT-OFFSET` is a row offset from the parent popup. 143 | 144 | Here is an example: 145 | 146 | ```elisp 147 | (setq popup (popup-create (point) 10 10)) 148 | (popup-set-list popup '("Foo" "Bar" "Baz")) 149 | (popup-draw popup) 150 | ;; do something here 151 | (popup-delete popup) 152 | ``` 153 | 154 | ### Function: `popup-delete` 155 | 156 | ``` 157 | popup-delete popup 158 | ``` 159 | 160 | Delete the `POPUP`. 161 | 162 | ### Function: `popup-live-p` 163 | 164 | ``` 165 | popup-live-p popup => boolean 166 | ``` 167 | 168 | ### Function: `popup-set-list` 169 | 170 | ``` 171 | popup-set-list popup list 172 | ``` 173 | 174 | Set the contents of the `POPUP`. `LIST` has to be popup items. 175 | 176 | ### Function: `popup-draw` 177 | 178 | ``` 179 | popup-draw popup 180 | ``` 181 | 182 | Draw the contents of the `POPUP`. 183 | 184 | ### Function: `popup-hide` 185 | 186 | ``` 187 | popup-hide popup 188 | ``` 189 | 190 | Hide the `POPUP`. To show again, call `popup-draw`. 191 | 192 | ### Function: `popup-hidden-p` 193 | 194 | ``` 195 | popup-hidden-p popup 196 | ``` 197 | 198 | Return non-nil if the `POPUP` is hidden. 199 | 200 | ### Function: `popup-select` 201 | 202 | ``` 203 | popup-select popup index 204 | ``` 205 | 206 | Select the item of `INDEX` of the `POPUP`. 207 | 208 | ### Function: `popup-selected-item` 209 | 210 | ``` 211 | popup-selected-item popup => item 212 | ``` 213 | 214 | Return the selected item of the `POPUP`. 215 | 216 | Return non-nil if the `POPUP` is still alive. 217 | 218 | ### Function: `popup-next` 219 | 220 | ``` 221 | popup-next popup 222 | ``` 223 | 224 | Select the next item of the `POPUP`. 225 | 226 | ### Function: `popup-previous` 227 | 228 | ``` 229 | popup-previous popup 230 | ``` 231 | 232 | Select the next item of the `POPUP`. 233 | 234 | ### Function: `popup-scroll-down` 235 | 236 | ``` 237 | popup-scroll-down popup n 238 | ``` 239 | 240 | Scroll down `N` items of the `POPUP`. This won't wrap. 241 | 242 | ### Function: `popup-scroll-up` 243 | 244 | ``` 245 | popup-scroll-up popup n 246 | ``` 247 | 248 | Scroll up `N` items of the `POPUP`. This won't wrap. 249 | 250 | ### Function: `popup-isearch` 251 | 252 | ``` 253 | popup-isearch popup &key cursor-color keymap callback help-delay 254 | => boolean 255 | ``` 256 | 257 | Enter incremental search event loop of `POPUP`. 258 | 259 | ## Tooltips 260 | 261 | A tooltip is an useful visual UI widget for displaying information 262 | something about what cursor points to. 263 | 264 | ### Function: `popup-tip` 265 | 266 | ``` 267 | popup-tip string &key point around width height min-height max-width 268 | truncate margin margin-left margin-right scroll-bar parent 269 | parent-offset nowait nostrip prompt 270 | ``` 271 | 272 | Show a tooltip with message `STRING` at `POINT`. This function is 273 | synchronized unless `NOWAIT` specified. Almost all arguments are same as 274 | `popup-create` except for `TRUNCATE`, `NOWAIT`, `NOSTRIP` and `PROMPT`. 275 | 276 | If `TRUNCATE` is non-nil, the tooltip can be truncated. 277 | 278 | If `NOWAIT` is non-nil, this function immediately returns the tooltip 279 | instance without entering event loop. 280 | 281 | If `NOSTRIP` is non-nil, `STRING` properties are not stripped. 282 | 283 | `PROMPT` is a prompt string used when reading events during the event 284 | loop. 285 | 286 | Here is an example: 287 | 288 | ```elisp 289 | (popup-tip "Hello, World!") 290 | ;; reach here after the tooltip disappeared 291 | ``` 292 | 293 | ## Popup Menus 294 | 295 | Popup menu is an useful visual UI widget for prompting users to 296 | select an item of a list. 297 | 298 | ### Function: `popup-menu*` 299 | 300 | ```elisp 301 | popup-menu* list &key point around width height margin margin-left 302 | margin-right scroll-bar symbol parent parent-offset keymap 303 | fallback help-delay nowait prompt isearch isearch-filter isearch-cursor-color 304 | isearch-keymap isearch-callback initial-index => selected-value 305 | ``` 306 | 307 | Show a popup menu of `LIST` at `POINT`. This function returns the value 308 | of the selected item. Almost all arguments are same as `popup-create` 309 | except for `KEYMAP`, `FALLBACK`, `HELP-DELAY`, `PROMPT`, `ISEARCH`, 310 | `ISEARCH-FILTER`, `ISEARCH-CURSOR-COLOR`, `ISEARCH-KEYMAP` 311 | and `ISEARCH-CALLBACK`. 312 | 313 | If `KEYMAP` is provided, it is a keymap which is used when processing 314 | events during event loop. 315 | 316 | If `FALLBACK` is provided, it is a function taking two arguments; a key 317 | and a command. `FALLBACK` is called when no special operation is found 318 | on the key. The default value is `popup-menu-fallback`, which does 319 | nothing. 320 | 321 | `HELP-DELAY` is a delay of displaying helps. 322 | 323 | If `NOWAIT` is non-nil, this function immediately returns the menu 324 | instance without entering event loop. 325 | 326 | `PROMPT` is a prompt string when reading events during event loop. 327 | 328 | If `ISEARCH` is non-nil, do isearch as soon as displaying the popup 329 | menu. 330 | 331 | `ISEARCH-FILTER` is a filtering function taking two arguments: 332 | search pattern and list of items. Returns a list of matching items. 333 | 334 | `ISEARCH-CURSOR-COLOR` is a cursor color during isearch. The default 335 | value is `popup-isearch-cursor-color'. 336 | 337 | `ISEARCH-KEYMAP` is a keymap which is used when processing events 338 | during event loop. The default value is `popup-isearch-keymap`. 339 | 340 | `ISEARCH-CALLBACK` is a function taking one argument. `popup-menu` 341 | calls `ISEARCH-CALLBACK`, if specified, after isearch finished or 342 | isearch canceled. The arguments is whole filtered list of items. 343 | 344 | If `INITIAL-INDEX` is non-nil, this is an initial index value for 345 | `popup-select`. Only positive integer is valid. 346 | 347 | Here is an example: 348 | 349 | ```elisp 350 | (popup-menu* '("Foo" "Bar" "Baz")) 351 | ;; => "Baz" if you select Baz 352 | (popup-menu* (list (popup-make-item "Yes" :value t) 353 | (popup-make-item "No" :value nil))) 354 | ;; => t if you select Yes 355 | ``` 356 | 357 | ### Function: `popup-cascade-menu` 358 | 359 | Same as `popup-menu` except that an element of `LIST` can be also a 360 | sub-menu if the element is a cons cell formed `(ITEM . SUBLIST)` where 361 | `ITEM` is an usual item and `SUBLIST` is a list of the sub menu. 362 | 363 | Here is an example: 364 | 365 | ```elisp 366 | (popup-cascade-menu '(("Top1" "Sub1" "Sub2") "Top2")) 367 | ``` 368 | 369 | ### Customize Variables 370 | 371 | #### `popup-isearch-regexp-builder-function` 372 | 373 | Function used to construct a regexp from a pattern. You may for instance 374 | provide a function that replaces spaces by '.+' if you like helm or ivy style 375 | of completion. Default value is `#'regexp-quote`. 376 | 377 | ---- 378 | 379 | Copyright (C) 2011-2015 Tomohiro Matsuyama <>
380 | Copyright (C) 2020-2022 Jen-Chieh Shen <> 381 | 382 | [melpa-link]: https://melpa.org/#/popup 383 | [melpa-stable-link]: https://stable.melpa.org/#/popup 384 | [melpa-badge]: https://melpa.org/packages/popup-badge.svg 385 | [melpa-stable-badge]: https://stable.melpa.org/packages/popup-badge.svg 386 | -------------------------------------------------------------------------------- /tests/popup-test.el: -------------------------------------------------------------------------------- 1 | (require 'ert) 2 | 3 | (require 'popup) 4 | 5 | (when (< (frame-width) (length "long long long long line")) 6 | (set-frame-size (selected-frame) 80 35)) 7 | 8 | (defun popup-test-helper-posn-col-row (dummy) 9 | "This function is workaround. Because `posn-col-row' and `posn-at-point' 10 | can not work well in batch mode." 11 | (cons (current-column) (line-number-at-pos (point)))) 12 | 13 | (defmacro popup-test-with-common-setup (&rest body) 14 | (declare (indent 0) (debug t)) 15 | `(save-excursion 16 | (with-temp-buffer 17 | (switch-to-buffer (current-buffer)) 18 | (delete-other-windows) 19 | (erase-buffer) 20 | (if noninteractive 21 | (cl-letf (((symbol-function 'posn-col-row) 22 | #'popup-test-helper-posn-col-row)) 23 | ,@body) 24 | ,@body)))) 25 | 26 | (defun popup-test-helper-line-move-visual (arg) 27 | "This function is workaround. Because `line-move-visual' can not work well in 28 | batch mode." 29 | (let ((cur-col 30 | (- (current-column) 31 | (save-excursion (vertical-motion 0) (current-column))))) 32 | (vertical-motion arg) 33 | (move-to-column (+ (current-column) cur-col)))) 34 | 35 | (defun popup-test-helper-rectangle-match (str) 36 | (let ((buffer-contents (popup-test-helper-buffer-contents))) 37 | (with-temp-buffer 38 | (insert buffer-contents) 39 | (goto-char (point-min)) 40 | (let ((strings (split-string str "\n"))) 41 | (when (search-forward (car strings) nil t) 42 | (goto-char (match-beginning 0)) 43 | (cl-every 44 | 'identity 45 | (mapcar 46 | (lambda (elem) 47 | (popup-test-helper-line-move-visual 1) 48 | (looking-at (regexp-quote elem))) 49 | (cdr strings)))))))) 50 | 51 | (defun popup-test-helper-buffer-contents () 52 | (cl-loop with start = (point-min) 53 | with contents 54 | for overlay in (cl-sort (overlays-in (point-min) (point-max)) 55 | '< :key 'overlay-start) 56 | for overlay-start = (overlay-start overlay) 57 | for overlay-end = (overlay-end overlay) 58 | for prefix = (buffer-substring start overlay-start) 59 | for befstr = (overlay-get overlay 'before-string) 60 | for substr = (or (overlay-get overlay 'display) 61 | (buffer-substring overlay-start overlay-end)) 62 | for aftstr = (overlay-get overlay 'after-string) 63 | collect prefix into contents 64 | unless (overlay-get overlay 'invisible) collect 65 | (concat befstr substr aftstr) into contents 66 | do (setq start overlay-end) 67 | finally (return (concat (apply 'concat contents) 68 | (buffer-substring start (point-max)))) 69 | )) 70 | 71 | (defun popup-test-helper-create-popup (str) 72 | (setq popup (popup-create (point) 10 10)) 73 | (popup-set-list popup (split-string str "\n")) 74 | (popup-draw popup)) 75 | 76 | (defun popup-test-helper-in-popup-p () 77 | (let* ((faces (get-text-property (point) 'face)) 78 | (faces (if (listp faces) faces (list faces)))) 79 | (or (memq 'popup-tip-face faces) 80 | (memq 'popup-menu-face faces) 81 | (memq 'popup-menu-selection-face faces) 82 | (memq 'popup-face faces)))) 83 | 84 | (defun popup-test-helper-popup-selected-item (str) 85 | (let ((buffer-contents (popup-test-helper-buffer-contents))) 86 | (with-temp-buffer 87 | (insert buffer-contents) 88 | (goto-char (point-min)) 89 | (goto-char 90 | (text-property-any (point-min) (point-max) 91 | 'face 'popup-menu-selection-face)) 92 | (looking-at str) 93 | ))) 94 | 95 | (defun popup-test-helper-popup-beginning-line () 96 | (let ((buffer-contents (popup-test-helper-buffer-contents))) 97 | (with-temp-buffer 98 | (insert buffer-contents) 99 | (goto-char (point-min)) 100 | (let ((end (point))) 101 | (while (and (not (eobp)) 102 | (not (popup-test-helper-in-popup-p))) 103 | (goto-char (or (next-single-property-change (point) 'face) 104 | (point-max)))) 105 | (if (popup-test-helper-in-popup-p) 106 | ;; todo visual line 107 | (line-number-at-pos (point)) nil) 108 | )))) 109 | 110 | (defun popup-test-helper-popup-beginning-column () 111 | (let ((buffer-contents (popup-test-helper-buffer-contents))) 112 | (with-temp-buffer 113 | (insert buffer-contents) 114 | (goto-char (point-min)) 115 | (let ((end (point))) 116 | (while (and (not (eobp)) 117 | (not (popup-test-helper-in-popup-p))) 118 | (goto-char (or (next-single-property-change (point) 'face) 119 | (point-max)))) 120 | (if (popup-test-helper-in-popup-p) 121 | (current-column) nil) 122 | )))) 123 | 124 | (defun popup-test-helper-popup-end-line () 125 | (let ((buffer-contents (popup-test-helper-buffer-contents))) 126 | (with-temp-buffer 127 | (insert buffer-contents) 128 | (goto-char (point-max)) 129 | (let ((end (point))) 130 | (while (and (not (bobp)) 131 | (not (popup-test-helper-in-popup-p))) 132 | (setq end (point)) 133 | (goto-char (or (previous-single-property-change (point) 'face) 134 | (point-min)))) 135 | (if (popup-test-helper-in-popup-p) 136 | ;; todo visual line 137 | (line-number-at-pos end) nil) 138 | )))) 139 | 140 | (defun popup-test-helper-popup-end-column () 141 | (let ((buffer-contents (popup-test-helper-buffer-contents))) 142 | (with-temp-buffer 143 | (insert buffer-contents) 144 | (goto-char (point-max)) 145 | (let ((end (point))) 146 | (while (and (not (bobp)) 147 | (not (popup-test-helper-in-popup-p))) 148 | (setq end (point)) 149 | (goto-char (or (previous-single-property-change (point) 'face) 150 | (point-min)))) 151 | (when (popup-test-helper-in-popup-p) 152 | (goto-char end) 153 | (current-column)) 154 | )))) 155 | 156 | (defun popup-test-helper-debug () 157 | (let ((buffer-contents (popup-test-helper-buffer-contents))) 158 | (with-current-buffer (get-buffer-create "*dump*") 159 | (erase-buffer) 160 | (insert buffer-contents) 161 | (buffer-string) 162 | ))) 163 | ;; Test for helper method 164 | (ert-deftest popup-test-no-truncated () 165 | (popup-test-with-common-setup 166 | (insert (make-string (- (window-width) 4) ? )) (insert "Foo\n") 167 | (insert (make-string (- (window-width) 4) ? )) (insert "Bar\n") 168 | (insert (make-string (- (window-width) 4) ? )) (insert "Baz\n") 169 | (should (eq t (popup-test-helper-rectangle-match "\ 170 | Foo 171 | Bar 172 | Baz"))) 173 | )) 174 | 175 | (ert-deftest popup-test-truncated () 176 | (popup-test-with-common-setup 177 | (insert (make-string (- (window-width) 2) ? )) (insert "Foo\n") 178 | (insert (make-string (- (window-width) 2) ? )) (insert "Bar\n") 179 | (insert (make-string (- (window-width) 2) ? )) (insert "Baz\n") 180 | (should (eq nil (popup-test-helper-rectangle-match "\ 181 | Foo 182 | Bar 183 | Baz"))) 184 | )) 185 | 186 | (ert-deftest popup-test-misaligned () 187 | (popup-test-with-common-setup 188 | (progn 189 | (insert (make-string (- (window-width) 5) ? )) (insert "Foo\n") 190 | (insert (make-string (- (window-width) 4) ? )) (insert "Bar\n") 191 | (insert (make-string (- (window-width) 3) ? )) (insert "Baz\n")) 192 | (should (eq nil (popup-test-helper-rectangle-match "\ 193 | Foo 194 | Bar 195 | Baz"))) 196 | )) 197 | ;; Test for popup-el 198 | (ert-deftest popup-test-simple () 199 | (popup-test-with-common-setup 200 | (popup-test-helper-create-popup "\ 201 | foo 202 | bar 203 | baz") 204 | (should (popup-test-helper-rectangle-match "\ 205 | foo 206 | bar 207 | baz")) 208 | (should (eq (popup-test-helper-popup-beginning-column) 0)))) 209 | 210 | (ert-deftest popup-test-delete () 211 | (popup-test-with-common-setup 212 | (popup-test-helper-create-popup "\ 213 | foo 214 | bar 215 | baz") 216 | (popup-delete popup) 217 | (should-not (popup-test-helper-rectangle-match "\ 218 | foo 219 | bar 220 | baz")) 221 | )) 222 | 223 | (ert-deftest popup-test-hide () 224 | (popup-test-with-common-setup 225 | (popup-test-helper-create-popup "\ 226 | foo 227 | bar 228 | baz") 229 | (popup-hide popup) 230 | (should-not (popup-test-helper-rectangle-match "\ 231 | foo 232 | bar 233 | baz")) 234 | )) 235 | 236 | (ert-deftest popup-test-at-colum1 () 237 | (popup-test-with-common-setup 238 | (insert " ") 239 | (popup-test-helper-create-popup "\ 240 | foo 241 | bar 242 | baz") 243 | (should (popup-test-helper-rectangle-match "\ 244 | foo 245 | bar 246 | baz")) 247 | (should (eq (popup-test-helper-popup-beginning-column) 1)) 248 | )) 249 | 250 | (ert-deftest popup-test-tip () 251 | (popup-test-with-common-setup 252 | (popup-tip "\ 253 | Start isearch on POPUP. This function is synchronized, meaning 254 | event loop waits for quiting of isearch. 255 | 256 | CURSOR-COLOR is a cursor color during isearch. The default value 257 | is `popup-isearch-cursor-color'. 258 | 259 | KEYMAP is a keymap which is used when processing events during 260 | event loop. The default value is `popup-isearch-keymap'. 261 | 262 | CALLBACK is a function taking one argument. `popup-isearch' calls 263 | CALLBACK, if specified, after isearch finished or isearch 264 | canceled. The arguments is whole filtered list of items. 265 | 266 | HELP-DELAY is a delay of displaying helps." 267 | :nowait t) 268 | (should (popup-test-helper-rectangle-match "\ 269 | KEYMAP is a keymap which is used when processing events during 270 | event loop. The default value is `popup-isearch-keymap'.")) 271 | )) 272 | 273 | (ert-deftest popup-test-folding-long-line-right-top () 274 | (popup-test-with-common-setup 275 | ;; To use window-width because Emacs 23 does not have window-body-width 276 | (insert (make-string (- (window-width) 3) ? )) 277 | (popup-tip "long long long long line" :nowait t) 278 | (should (popup-test-helper-rectangle-match "long long long long line")) 279 | (should (eq (popup-test-helper-popup-beginning-line) 280 | 2)) 281 | (should (eq (popup-test-helper-popup-end-line) 2)) 282 | )) 283 | 284 | (ert-deftest popup-test-folding-long-line-left-bottom () 285 | (popup-test-with-common-setup 286 | (insert (make-string (- (window-body-height) 1) ?\n)) 287 | (popup-tip "long long long long line" :nowait t) 288 | (should (popup-test-helper-rectangle-match "long long long long line")) 289 | (should (eq (popup-test-helper-popup-beginning-line) 290 | (- (window-body-height) 1))) 291 | (should (eq (popup-test-helper-popup-end-line) (- (window-body-height) 1))) 292 | )) 293 | 294 | (ert-deftest popup-test-folding-long-line-right-bottom () 295 | (popup-test-with-common-setup 296 | (insert (make-string (- (window-body-height) 1) ?\n)) 297 | (insert (make-string (- (window-width) 3) ? )) 298 | (popup-tip "long long long long line" :nowait t) 299 | (should (popup-test-helper-rectangle-match "long long long long line")) 300 | (should (eq (popup-test-helper-popup-beginning-line) 301 | (- (window-body-height) 1))) 302 | (should (eq (popup-test-helper-popup-end-line) (- (window-body-height) 1))) 303 | )) 304 | 305 | (ert-deftest popup-test-folding-short-line-right-top () 306 | (popup-test-with-common-setup 307 | (insert (make-string (- (window-width) 4) ? )) 308 | (popup-tip "\ 309 | bla 310 | bla 311 | bla 312 | bla 313 | bla" :nowait t) 314 | (should (popup-test-helper-rectangle-match "\ 315 | bla 316 | bla 317 | bla 318 | bla 319 | bla")) 320 | (should (eq (popup-test-helper-popup-beginning-line) 2)) 321 | )) 322 | 323 | (ert-deftest popup-test-folding-short-line-left-bottom () 324 | (popup-test-with-common-setup 325 | (insert (make-string (- (window-body-height) 1) ?\n)) 326 | (popup-tip "\ 327 | bla 328 | bla 329 | bla 330 | bla 331 | bla" :nowait t) 332 | (should (popup-test-helper-rectangle-match "\ 333 | bla 334 | bla 335 | bla 336 | bla 337 | bla")) 338 | (should (eq (popup-test-helper-popup-end-line) (- (window-body-height) 1))))) 339 | 340 | (ert-deftest popup-test-folding-short-line-right-bottom () 341 | (popup-test-with-common-setup 342 | (insert (make-string (- (window-body-height) 1) ?\n)) 343 | (insert (make-string (- (window-width) 4) ? )) 344 | (popup-tip "\ 345 | bla 346 | bla 347 | bla 348 | bla 349 | bla" :nowait t) 350 | (should (popup-test-helper-rectangle-match "\ 351 | bla 352 | bla 353 | bla 354 | bla 355 | bla")) 356 | (should (eq (popup-test-helper-popup-end-line) (- (window-body-height) 1))) 357 | )) 358 | 359 | (ert-deftest popup-test-margin-at-column1 () 360 | (popup-test-with-common-setup 361 | (insert " ") 362 | (popup-tip "Margin?" :nowait t :margin t) 363 | (should (eq (popup-test-helper-popup-beginning-column) 364 | 0)) 365 | (should (popup-test-helper-rectangle-match " Margin? ")) 366 | )) 367 | 368 | (ert-deftest popup-test-margin-left () 369 | (popup-test-with-common-setup 370 | (popup-tip "Margin?" :nowait t :margin t) 371 | (should (eq (popup-test-helper-popup-beginning-column) 372 | 0)) 373 | ;; Pending: #19 374 | ;; (should (popup-test-helper-rectangle-match " Margin? ")) 375 | )) 376 | 377 | (ert-deftest popup-test-margin-right () 378 | (popup-test-with-common-setup 379 | (insert (make-string (- (window-width) 1) ? )) 380 | (popup-tip "Margin?" :nowait t :margin t) 381 | (should (popup-test-helper-rectangle-match " Margin? ")) 382 | ;; Pending: #19 383 | ;; (should (< (popup-test-helper-popup-end-column) (window-width))) 384 | )) 385 | 386 | (ert-deftest popup-test-height-limit () 387 | (popup-test-with-common-setup 388 | (popup-tip "\ 389 | Foo 390 | Bar 391 | Baz" :nowait t :height 2) 392 | (should (popup-test-helper-rectangle-match "\ 393 | Foo 394 | Bar")) 395 | (should-not (popup-test-helper-rectangle-match "Baz")) 396 | (should (eq (popup-test-helper-popup-beginning-line) 2)) 397 | (should (eq (popup-test-helper-popup-end-line) 3)) 398 | )) 399 | 400 | (ert-deftest popup-test-height-limit-bottom () 401 | (popup-test-with-common-setup 402 | (insert (make-string (- (window-body-height) 1) ?\n)) 403 | (popup-tip "\ 404 | Foo 405 | Bar 406 | Baz" :nowait t :height 2) 407 | (should (popup-test-helper-rectangle-match "\ 408 | Foo 409 | Bar")) 410 | (should-not (popup-test-helper-rectangle-match "Baz")) 411 | (should (eq (popup-test-helper-popup-end-line) (- (window-body-height) 1))) 412 | )) 413 | 414 | (ert-deftest popup-test-scroll-bar () 415 | (popup-test-with-common-setup 416 | (let ((popup-scroll-bar-foreground-char 417 | (propertize "f" 'face 'popup-scroll-bar-foreground-face)) 418 | (popup-scroll-bar-background-char 419 | (propertize "b" 'face 'popup-scroll-bar-background-face))) 420 | (popup-tip "\ 421 | Foo 422 | Bar 423 | Baz 424 | Fez 425 | Oz" 426 | :nowait t :height 3 :scroll-bar t :margin t) 427 | (should (popup-test-helper-rectangle-match "\ 428 | Foo f 429 | Bar b 430 | Baz b")) 431 | (should-not (popup-test-helper-rectangle-match "Fez")) 432 | (should-not (popup-test-helper-rectangle-match "Oz")) 433 | (should (eq (popup-test-helper-popup-beginning-line) 2)) 434 | (should (eq (popup-test-helper-popup-end-line) 4)) 435 | ))) 436 | 437 | (ert-deftest popup-test-scroll-bar-right-no-margin () 438 | (popup-test-with-common-setup 439 | (insert (make-string (- (window-width) 1) ? )) 440 | (let ((popup-scroll-bar-foreground-char 441 | (propertize "f" 'face 'popup-scroll-bar-foreground-face)) 442 | (popup-scroll-bar-background-char 443 | (propertize "b" 'face 'popup-scroll-bar-background-face))) 444 | (popup-tip "\ 445 | Foo 446 | Bar 447 | Baz 448 | Fez 449 | Oz" 450 | :nowait t :height 3 :scroll-bar t) 451 | (should (popup-test-helper-rectangle-match "\ 452 | Foof 453 | Barb 454 | Bazb")) 455 | (should-not (popup-test-helper-rectangle-match "Fez")) 456 | (should-not (popup-test-helper-rectangle-match "Oz")) 457 | (should (eq (popup-test-helper-popup-beginning-line) 2)) 458 | (should (eq (popup-test-helper-popup-end-line) 4)) 459 | ))) 460 | 461 | (ert-deftest popup-test-scroll-bar-right-margin () 462 | (popup-test-with-common-setup 463 | (insert (make-string (- (window-width) 1) ? )) 464 | (let ((popup-scroll-bar-foreground-char 465 | (propertize "f" 'face 'popup-scroll-bar-foreground-face)) 466 | (popup-scroll-bar-background-char 467 | (propertize "b" 'face 'popup-scroll-bar-background-face))) 468 | (popup-tip "\ 469 | Foo 470 | Bar 471 | Baz 472 | Fez 473 | Oz" 474 | :nowait t :height 3 :scroll-bar t :margin t) 475 | (should-not (popup-test-helper-rectangle-match "Fez")) 476 | (should-not (popup-test-helper-rectangle-match "Oz")) 477 | (should (eq (popup-test-helper-popup-beginning-line) 2)) 478 | (should (eq (popup-test-helper-popup-end-line) 4)) 479 | ;; Pending: #21 480 | ;; (should (popup-test-helper-rectangle-match "\ 481 | ;; Foof 482 | ;; Barb 483 | ;; Bazb")) 484 | ))) 485 | 486 | (ert-deftest popup-test-min-height () 487 | (popup-test-with-common-setup 488 | (insert (make-string (- (window-width) 1) ? )) 489 | (popup-tip "Hello" :nowait t :min-height 10) 490 | (should (popup-test-helper-rectangle-match "Hello")) 491 | (should (eq (popup-test-helper-popup-beginning-line) 2)) 492 | (should (eq (popup-test-helper-popup-end-line) 11)) 493 | )) 494 | 495 | (ert-deftest popup-test-menu () 496 | (popup-test-with-common-setup 497 | (popup-menu* '("Foo" "Bar" "Baz") :nowait t) 498 | (should (popup-test-helper-rectangle-match "\ 499 | Foo 500 | Bar 501 | Baz")) 502 | (should (eq (popup-test-helper-popup-beginning-line) 2)) 503 | )) 504 | 505 | (ert-deftest popup-test-cascade-menu () 506 | (popup-test-with-common-setup 507 | (popup-cascade-menu 508 | '(("Foo" "Foo1" "Foo2") "Bar" "Baz") :nowait t) 509 | (should (popup-test-helper-rectangle-match "Foo >")) 510 | (should (popup-test-helper-rectangle-match "\ 511 | Foo 512 | Bar 513 | Baz")) 514 | (should-not (popup-test-helper-rectangle-match "Foo1")) 515 | (should-not (popup-test-helper-rectangle-match "Foo2")) 516 | (should (eq (popup-test-helper-popup-beginning-line) 2)) 517 | )) 518 | 519 | (ert-deftest popup-test-next () 520 | (popup-test-with-common-setup 521 | (setq popup (popup-menu* '("Foo" "Bar" "Baz") :nowait t)) 522 | (should (popup-test-helper-popup-selected-item "Foo")) 523 | (popup-next popup) 524 | (should (popup-test-helper-popup-selected-item "Bar")) 525 | (popup-next popup) 526 | (should (popup-test-helper-popup-selected-item "Baz")) 527 | (popup-next popup) 528 | (should (popup-test-helper-popup-selected-item "Foo")) 529 | (should (popup-test-helper-rectangle-match "Foo\nBar\nBaz")) 530 | (should (eq (popup-test-helper-popup-beginning-line) 2)) 531 | )) 532 | 533 | (ert-deftest popup-test-previous () 534 | (popup-test-with-common-setup 535 | (setq popup (popup-menu* '("Foo" "Bar" "Baz") :nowait t)) 536 | (should (popup-test-helper-popup-selected-item "Foo")) 537 | (popup-previous popup) 538 | (should (popup-test-helper-popup-selected-item "Baz")) 539 | (popup-previous popup) 540 | (should (popup-test-helper-popup-selected-item "Bar")) 541 | (popup-previous popup) 542 | (should (popup-test-helper-popup-selected-item "Foo")) 543 | (should (popup-test-helper-rectangle-match "\ 544 | Foo 545 | Bar 546 | Baz")) 547 | (should (eq (popup-test-helper-popup-beginning-line) 2)) 548 | )) 549 | 550 | (ert-deftest popup-test-select () 551 | (popup-test-with-common-setup 552 | (setq popup (popup-menu* '("Foo" "Bar" "Baz") :nowait t)) 553 | (should (popup-test-helper-popup-selected-item "Foo")) 554 | (popup-select popup 1) 555 | (should (popup-test-helper-popup-selected-item "Bar")) 556 | (popup-select popup 0) 557 | (should (popup-test-helper-popup-selected-item "Foo")) 558 | (popup-select popup 2) 559 | (should (popup-test-helper-popup-selected-item "Baz")) 560 | (should (popup-test-helper-rectangle-match "\ 561 | Foo 562 | Bar 563 | Baz")) 564 | (should (eq (popup-test-helper-popup-beginning-line) 2)) 565 | )) 566 | 567 | (ert-deftest popup-test-scroll-down () 568 | (popup-test-with-common-setup 569 | (setq popup 570 | (popup-cascade-menu (cl-loop for x to 100 collect (format "Foo%d" x)) 571 | :nowait t :height 10 :margin t :scroll-bar t)) 572 | (should (popup-test-helper-rectangle-match "\ 573 | Foo0 574 | Foo1 575 | Foo2")) 576 | (should (popup-test-helper-popup-selected-item "Foo0")) 577 | (popup-scroll-down popup 10) 578 | (should (popup-test-helper-popup-selected-item "Foo10")) 579 | (popup-scroll-down popup 10) 580 | (should (popup-test-helper-popup-selected-item "Foo20")) 581 | (popup-scroll-down popup 100) 582 | (should-not (popup-test-helper-rectangle-match "Foo90")) 583 | (should (popup-test-helper-rectangle-match "Foo91")) 584 | (should (popup-test-helper-rectangle-match "Foo100")) 585 | (should-not (popup-test-helper-rectangle-match "Foo0")) 586 | (should (eq (popup-test-helper-popup-beginning-line) 2)) 587 | )) 588 | 589 | (ert-deftest popup-test-scroll-up () 590 | (popup-test-with-common-setup 591 | (setq popup 592 | (popup-cascade-menu (cl-loop for x to 100 collect (format "Foo%d" x)) 593 | :nowait t :height 10 :margin t :scroll-bar t)) 594 | (should (popup-test-helper-rectangle-match "\ 595 | Foo0 596 | Foo1 597 | Foo2")) 598 | (should (popup-test-helper-popup-selected-item "Foo0")) 599 | (popup-scroll-down popup 100) 600 | (should (popup-test-helper-popup-selected-item "Foo91")) 601 | (popup-scroll-up popup 10) 602 | (should (popup-test-helper-popup-selected-item "Foo81")) 603 | (popup-scroll-up popup 10) 604 | (should-not (popup-test-helper-rectangle-match "Foo70")) 605 | (should (popup-test-helper-rectangle-match "Foo71")) 606 | (should (popup-test-helper-rectangle-match "Foo80")) 607 | (should-not (popup-test-helper-rectangle-match "Foo81")) 608 | (should (eq (popup-test-helper-popup-beginning-line) 2)) 609 | )) 610 | 611 | (ert-deftest popup-test-two-tip () 612 | (popup-test-with-common-setup 613 | (popup-tip "\ 614 | Foo 615 | Bar" :nowait t) 616 | (save-excursion (insert "\n")) 617 | (popup-tip "\ 618 | Baz 619 | Qux" :nowait t) 620 | ;; Pending: #20 621 | ;; (should (popup-test-helper-rectangle-match "\ 622 | ;; Foo 623 | ;; Bar")) 624 | ;; (should (popup-test-helper-rectangle-match "\ 625 | ;; Baz 626 | ;; Qux")) 627 | )) 628 | 629 | (ert-deftest popup-test-initial-index () 630 | (popup-test-with-common-setup 631 | (setq popup (popup-menu* '("Foo" "Bar" "Baz") :initial-index 0 :nowait t)) 632 | (should (popup-test-helper-popup-selected-item "Foo"))) 633 | 634 | (popup-test-with-common-setup 635 | (setq popup (popup-menu* '("Foo" "Bar" "Baz") :initial-index 2 :nowait t)) 636 | (should (popup-test-helper-popup-selected-item "Baz"))) 637 | 638 | (popup-test-with-common-setup 639 | (setq popup (popup-menu* '("Foo" "Bar" "Baz") :initial-index 2 :height 1 :scroll-bar t :nowait t)) 640 | (should (popup-test-helper-popup-selected-item "Baz"))) 641 | 642 | (popup-test-with-common-setup 643 | (setq popup (popup-menu* '("Foo" "Bar" "Baz") :initial-index -1 :nowait t)) 644 | (should (popup-test-helper-popup-selected-item "Foo"))) 645 | 646 | (popup-test-with-common-setup 647 | (setq popup (popup-menu* '("Foo" "Bar" "Baz") :initial-index 100 :nowait t)) 648 | (should (popup-test-helper-popup-selected-item "Baz")))) 649 | 650 | (defun popup-test-helper-input (key) 651 | (push key unread-command-events)) 652 | 653 | (ert-deftest popup-test-isearch () 654 | (popup-test-with-common-setup 655 | (popup-test-helper-create-popup "\ 656 | foo 657 | bar 658 | baz") 659 | (popup-isearch-update popup 'popup-isearch-filter-list "a") 660 | (should (popup-test-helper-rectangle-match "\ 661 | bar 662 | baz")) 663 | (should-not (popup-test-helper-rectangle-match "foo")) 664 | )) 665 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | -------------------------------------------------------------------------------- /popup.el: -------------------------------------------------------------------------------- 1 | ;;; popup.el --- Visual Popup User Interface -*- lexical-binding: t; -*- 2 | 3 | ;; Copyright (C) 2009-2015 Tomohiro Matsuyama 4 | ;; Copyright (c) 2020-2025 Jen-Chieh Shen 5 | 6 | ;; Author: Tomohiro Matsuyama 7 | ;; Maintainer: Shen, Jen-Chieh 8 | ;; URL: https://github.com/auto-complete/popup-el 9 | ;; Keywords: lisp 10 | ;; Version: 0.5.9 11 | ;; Package-Requires: ((emacs "24.3")) 12 | 13 | ;; This program is free software; you can redistribute it and/or modify 14 | ;; it under the terms of the GNU General Public License as published by 15 | ;; the Free Software Foundation, either version 3 of the License, or 16 | ;; (at your option) any later version. 17 | 18 | ;; This program is distributed in the hope that it will be useful, 19 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of 20 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 21 | ;; GNU General Public License for more details. 22 | 23 | ;; You should have received a copy of the GNU General Public License 24 | ;; along with this program. If not, see . 25 | 26 | ;;; Commentary: 27 | 28 | ;; popup.el is a visual popup user interface library for Emacs. This 29 | ;; provides a basic API and common UI widgets such as popup tooltips 30 | ;; and popup menus. 31 | ;; See README.markdown for more information. 32 | 33 | ;;; Code: 34 | 35 | (require 'cl-lib) 36 | (require 'mule) 37 | 38 | (defconst popup-version "0.5.9") 39 | 40 | 41 | 42 | ;;; Utilities 43 | 44 | (defun popup-calculate-max-width (max-width) 45 | "Determines whether the width with MAX-WIDTH desired is character or window 46 | proportion based, And return the result." 47 | (cl-typecase max-width 48 | (integer max-width) 49 | (float (* (ceiling (/ (round (* max-width (window-width))) 10.0)) 10)))) 50 | 51 | (defvar popup-use-optimized-column-computation t 52 | "Use the optimized column computation routine. 53 | If there is a problem, please set it nil.") 54 | 55 | (defmacro popup-aif (test then &rest else) 56 | "Anaphoric if." 57 | (declare (indent 2)) 58 | `(let ((it ,test)) 59 | (if it ,then ,@else))) 60 | 61 | (defmacro popup-awhen (test &rest body) 62 | "Anaphoric when." 63 | (declare (indent 1)) 64 | `(let ((it ,test)) 65 | (when it ,@body))) 66 | 67 | (defun popup-x-to-string (x) 68 | "Convert any object to string efficiently. 69 | This is faster than `prin1-to-string' in many cases." 70 | (cl-typecase x 71 | (string x) 72 | (symbol (symbol-name x)) 73 | (integer (number-to-string x)) 74 | (float (number-to-string x)) 75 | (t (format "%s" x)))) 76 | 77 | (defun popup-substring-by-width (string width) 78 | "Return a cons cell of substring and remaining string by 79 | splitting with WIDTH." 80 | ;; Expand tabs into 4 spaces 81 | (setq string (replace-regexp-in-string "\t" " " string)) 82 | (cl-loop with len = (length string) 83 | with w = 0 84 | for l from 0 85 | for c in (append string nil) 86 | while (<= (cl-incf w (char-width c)) width) 87 | finally return 88 | (if (< l len) 89 | (cons (substring string 0 l) (substring string l)) 90 | (list string)))) 91 | 92 | (defun popup-fill-string (string &optional width max-width justify squeeze) 93 | "Split STRING into fixed width strings and return a cons cell 94 | like \(WIDTH . ROWS). Here, the car WIDTH indicates the actual 95 | maxim width of ROWS. 96 | 97 | The argument WIDTH specifies the width of filling each 98 | paragraph. WIDTH nil means don't perform any justification and 99 | word wrap. Note that this function doesn't add any padding 100 | characters at the end of each row. 101 | 102 | MAX-WIDTH, if WIDTH is nil, specifies the maximum number of 103 | columns. 104 | 105 | The optional fourth argument JUSTIFY specifies which kind of 106 | justification to do: `full', `left', `right', `center', or 107 | `none' (equivalent to nil). A value of t means handle each 108 | paragraph as specified by its text properties. 109 | 110 | SQUEEZE nil means leave whitespaces other than line breaks 111 | untouched." 112 | (if (eq width 0) 113 | (error "Can't fill string with 0 width")) 114 | (if width 115 | (setq max-width width)) 116 | (with-temp-buffer 117 | (let ((tab-width 4) 118 | (fill-column width) 119 | (left-margin 0) 120 | (kinsoku-limit 1) 121 | indent-tabs-mode 122 | row rows) 123 | (insert string) 124 | (untabify (point-min) (point-max)) 125 | (if width 126 | (fill-region (point-min) (point-max) justify (not squeeze))) 127 | (goto-char (point-min)) 128 | (setq width 0) 129 | (while (prog2 130 | (let ((line (buffer-substring 131 | (point) (progn (end-of-line) (point))))) 132 | (if max-width 133 | (while (progn 134 | (setq row (truncate-string-to-width line max-width) 135 | width (max width (string-width row))) 136 | (push row rows) 137 | (if (not (= (length row) (length line))) 138 | (setq line (substring line (length row)))))) 139 | (setq width (max width (string-width line))) 140 | (push line rows))) 141 | (< (point) (point-max)) 142 | (beginning-of-line 2))) 143 | (cons width (nreverse rows))))) 144 | 145 | (defmacro popup-save-buffer-state (&rest body) 146 | (declare (indent 0)) 147 | `(save-excursion 148 | (let ((buffer-undo-list t) 149 | (inhibit-read-only t) 150 | (modified (buffer-modified-p))) 151 | (unwind-protect 152 | (progn ,@body) 153 | (set-buffer-modified-p modified))))) 154 | 155 | (defun popup-vertical-motion (column direction) 156 | "A portable version of `vertical-motion'." 157 | (when (bound-and-true-p display-line-numbers-mode) 158 | (setq column (- column (line-number-display-width 'columns)))) 159 | (if (>= emacs-major-version 23) 160 | (vertical-motion (cons column direction)) 161 | (vertical-motion direction) 162 | (move-to-column (+ (current-column) column)))) 163 | 164 | (defun popup-last-line-of-buffer-p () 165 | "Return non-nil if the cursor is at the last line of the 166 | buffer." 167 | (save-excursion (end-of-line) (/= (forward-line) 0))) 168 | 169 | (defun popup-lookup-key-by-event (function event) 170 | (or (funcall function (vector event)) 171 | (if (symbolp event) 172 | (popup-aif (get event 'event-symbol-element-mask) 173 | (funcall function 174 | (vector (logior (or (get (car it) 'ascii-character) 175 | 0) 176 | (cadr it)))))))) 177 | 178 | 179 | 180 | ;;; Core 181 | 182 | (defgroup popup nil 183 | "Visual Popup User Interface" 184 | :group 'lisp 185 | :prefix "popup-") 186 | 187 | (defface popup-face 188 | '((t (:inherit default :background "lightgray" :foreground "black"))) 189 | "Face for popup." 190 | :group 'popup) 191 | 192 | (defface popup-summary-face 193 | '((t (:inherit popup-face :foreground "dimgray"))) 194 | "Face for popup summary." 195 | :group 'popup) 196 | 197 | (defface popup-scroll-bar-foreground-face 198 | '((t (:background "black"))) 199 | "Foreground face for scroll-bar." 200 | :group 'popup) 201 | 202 | (defface popup-scroll-bar-background-face 203 | '((t (:background "gray"))) 204 | "Background face for scroll-bar." 205 | :group 'popup) 206 | 207 | (defvar popup-instances nil 208 | "Popup instances.") 209 | 210 | (defvar popup-scroll-bar-foreground-char 211 | (propertize " " 'face 'popup-scroll-bar-foreground-face) 212 | "Foreground character for scroll-bar.") 213 | 214 | (defvar popup-scroll-bar-background-char 215 | (propertize " " 'face 'popup-scroll-bar-background-face) 216 | "Background character for scroll-bar.") 217 | 218 | (cl-defstruct popup 219 | point row column width height min-height direction overlays keymap 220 | parent depth 221 | face mouse-face selection-face summary-face 222 | margin-left margin-right margin-left-cancel scroll-bar symbol 223 | cursor offset scroll-top current-height list newlines 224 | pattern original-list invis-overlays) 225 | 226 | (defun popup-item-propertize (item &rest properties) 227 | "Same as `propertize' except that this avoids overriding 228 | existed value with `nil' property." 229 | (cl-loop for (k v) on properties by 'cddr 230 | if v append (list k v) into props 231 | finally return 232 | (apply 'propertize 233 | (popup-x-to-string item) 234 | props))) 235 | 236 | (defun popup-item-property (item property) 237 | "Same as `get-text-property' except that this returns nil if 238 | ITEM is not string." 239 | (if (stringp item) 240 | (get-text-property 0 property item))) 241 | 242 | (defun popup-replace-displayable (str &optional rep) 243 | "Replace non-displayable character from STR. 244 | 245 | Optional argument REP is the replacement string of 246 | non-displayable character." 247 | (let ((rep (or rep "")) 248 | (results (list))) 249 | (dolist (string (split-string str "")) 250 | (let* ((char (string-to-char string)) 251 | (string (if (char-displayable-p char) 252 | string 253 | rep))) 254 | (push string results))) 255 | (string-join (reverse results)))) 256 | 257 | (cl-defun popup-make-item (name 258 | &key 259 | value 260 | face 261 | mouse-face 262 | selection-face 263 | sublist 264 | document 265 | symbol 266 | summary) 267 | "Utility function to make popup item. See also 268 | `popup-item-propertize'." 269 | (popup-item-propertize name 270 | 'value value 271 | 'popup-face face 272 | 'popup-mouse-face mouse-face 273 | 'selection-face selection-face 274 | 'document document 275 | 'symbol symbol 276 | 'summary summary 277 | 'sublist sublist)) 278 | 279 | (defsubst popup-item-value (item) (popup-item-property item 'value)) 280 | (defsubst popup-item-value-or-self (item) (or (popup-item-value item) item)) 281 | (defsubst popup-item-face (item) (popup-item-property item 'popup-face)) 282 | (defsubst popup-item-mouse-face (item) (popup-item-property item 'popup-mouse-face)) 283 | (defsubst popup-item-selection-face (item) (popup-item-property item 'selection-face)) 284 | (defsubst popup-item-document (item) (popup-item-property item 'document)) 285 | (defsubst popup-item-summary (item) (popup-item-property item 'summary)) 286 | (defsubst popup-item-symbol (item) (popup-item-property item 'symbol)) 287 | (defsubst popup-item-sublist (item) (popup-item-property item 'sublist)) 288 | 289 | (defun popup-item-documentation (item) 290 | (let ((doc (popup-item-document item))) 291 | (if (functionp doc) 292 | (setq doc (funcall doc (popup-item-value-or-self item)))) 293 | doc)) 294 | 295 | (defun popup-item-show-help-1 (item) 296 | (let ((doc (popup-item-documentation item))) 297 | (when doc 298 | (with-current-buffer (get-buffer-create " *Popup Help*") 299 | (erase-buffer) 300 | (insert doc) 301 | (goto-char (point-min)) 302 | (display-buffer (current-buffer))) 303 | t))) 304 | 305 | (defun popup-item-show-help-with-event-loop (item) 306 | (save-window-excursion 307 | (when (popup-item-show-help-1 item) 308 | (cl-loop do (clear-this-command-keys) 309 | for key = (read-key-sequence-vector nil) 310 | do 311 | (cl-case (key-binding key) 312 | (scroll-other-window 313 | (scroll-other-window)) 314 | (scroll-other-window-down 315 | (scroll-other-window-down nil)) 316 | (otherwise 317 | (setq unread-command-events (append key unread-command-events)) 318 | (cl-return))))))) 319 | 320 | (defun popup-item-show-help (item &optional persist) 321 | "Display the documentation of ITEM with `display-buffer'. If 322 | PERSIST is nil, the documentation buffer will be closed 323 | automatically, meaning interal event loop ensures the buffer to 324 | be closed. Otherwise, the buffer will be just displayed as 325 | usual." 326 | (when item 327 | (if (not persist) 328 | (popup-item-show-help-with-event-loop item) 329 | (popup-item-show-help-1 item)))) 330 | 331 | (defun popup-set-list (popup list) 332 | (popup-set-filtered-list popup list) 333 | (setf (popup-pattern popup) nil) 334 | (setf (popup-original-list popup) list)) 335 | 336 | (defun popup-set-filtered-list (popup list) 337 | (let ((offset 338 | (if (> (popup-direction popup) 0) 339 | 0 340 | (max (- (popup-height popup) (length list)) 0)))) 341 | (setf (popup-list popup) list 342 | (popup-offset popup) offset))) 343 | 344 | (defun popup-selected-item (popup) 345 | (nth (popup-cursor popup) (popup-list popup))) 346 | 347 | (defun popup-selected-line (popup) 348 | (- (popup-cursor popup) (popup-scroll-top popup))) 349 | 350 | (defun popup-line-overlay (popup line) 351 | (aref (popup-overlays popup) line)) 352 | 353 | (defun popup-selected-line-overlay (popup) 354 | (popup-line-overlay popup (popup-selected-line popup))) 355 | 356 | (defun popup-hide-line (popup line) 357 | (let ((overlay (popup-line-overlay popup line))) 358 | (overlay-put overlay 'display nil) 359 | (overlay-put overlay 'after-string nil))) 360 | 361 | (defun popup-line-hidden-p (popup line) 362 | (let ((overlay (popup-line-overlay popup line))) 363 | (and (eq (overlay-get overlay 'display) nil) 364 | (eq (overlay-get overlay 'after-string) nil)))) 365 | 366 | (cl-defun popup-set-line-item (popup 367 | line 368 | &key 369 | item 370 | face 371 | mouse-face 372 | margin-left 373 | margin-right 374 | scroll-bar-char 375 | symbol 376 | summary 377 | summary-face 378 | keymap) 379 | (let* ((overlay (popup-line-overlay popup line)) 380 | (content (popup-create-line-string popup (popup-x-to-string item) 381 | :margin-left margin-left 382 | :margin-right margin-right 383 | :symbol symbol 384 | :summary summary 385 | :summary-face summary-face)) 386 | (start 0) 387 | (prefix (overlay-get overlay 'prefix)) 388 | (postfix (overlay-get overlay 'postfix)) 389 | end) 390 | (put-text-property 0 (length content) 'popup-item item content) 391 | (put-text-property 0 (length content) 'keymap keymap content) 392 | ;; Overlap face properties 393 | (when (get-text-property start 'face content) 394 | (setq start (next-single-property-change start 'face content))) 395 | (while (and start (setq end (next-single-property-change start 'face content))) 396 | (put-text-property start end 'face face content) 397 | (setq start (next-single-property-change end 'face content))) 398 | (when start 399 | (put-text-property start (length content) 'face face content)) 400 | (when mouse-face 401 | (put-text-property 0 (length content) 'mouse-face mouse-face content)) 402 | (let ((prop (if (overlay-get overlay 'dangle) 403 | 'after-string 404 | 'display))) 405 | (overlay-put overlay 406 | prop 407 | (concat prefix 408 | content 409 | scroll-bar-char 410 | postfix))))) 411 | 412 | (cl-defun popup-create-line-string (popup 413 | string 414 | &key 415 | margin-left 416 | margin-right 417 | symbol 418 | summary 419 | summary-face) 420 | (let* ((popup-width (popup-width popup)) 421 | (summary-width (string-width summary)) 422 | (content-width (max 423 | (min popup-width (string-width string)) 424 | (- popup-width 425 | (if (> summary-width 0) 426 | (+ summary-width 2) 427 | 0)))) 428 | (string (car (popup-substring-by-width string content-width))) 429 | (string-width (string-width string)) 430 | (spacing (max (- popup-width string-width summary-width) 431 | (if (> popup-width string-width) 1 0))) 432 | (truncated-summary 433 | (car (popup-substring-by-width 434 | summary (max (- popup-width string-width spacing) 0))))) 435 | (when summary-face 436 | (put-text-property 0 (length truncated-summary) 437 | 'face summary-face truncated-summary)) 438 | (concat margin-left 439 | string 440 | (make-string spacing ? ) 441 | truncated-summary 442 | symbol 443 | margin-right))) 444 | 445 | (defun popup-live-p (popup) 446 | "Return non-nil if POPUP is alive." 447 | (and popup (popup-overlays popup) t)) 448 | 449 | (defun popup-child-point (popup &optional offset) 450 | (overlay-end 451 | (popup-line-overlay 452 | popup 453 | (or offset 454 | (popup-selected-line popup))))) 455 | 456 | (defun popup-calculate-direction (height row) 457 | "Return a proper direction when displaying a popup on this 458 | window. HEIGHT is the a height of the popup, and ROW is a line 459 | number at the point." 460 | (let* ((remaining-rows (- (max 1 (- (window-text-height) 461 | (if mode-line-format 1 0) 462 | (if header-line-format 1 0))) 463 | (count-lines (window-start) (point)))) 464 | (enough-space-above (> row height)) 465 | (enough-space-below (<= height remaining-rows))) 466 | (if (and enough-space-above 467 | (not enough-space-below)) 468 | -1 469 | 1))) 470 | 471 | (cl-defun popup-create (point 472 | width 473 | height 474 | &key 475 | min-height 476 | max-width 477 | around 478 | (face 'popup-face) 479 | mouse-face 480 | (selection-face face) 481 | (summary-face 'popup-summary-face) 482 | scroll-bar 483 | margin-left 484 | margin-right 485 | symbol 486 | parent 487 | parent-offset 488 | keymap) 489 | "Create a popup instance at POINT with WIDTH and HEIGHT. 490 | 491 | MIN-HEIGHT is a minimal height of the popup. The default value is 492 | 0. 493 | 494 | MAX-WIDTH is the maximum width of the popup. The default value is 495 | nil (no limit). If a floating point, the value refers to the ratio of 496 | the window. If an integer, limit is in characters. 497 | 498 | If AROUND is non-nil, the popup will be displayed around the 499 | point but not at the point. 500 | 501 | FACE is a background face of the popup. The default value is POPUP-FACE. 502 | 503 | SELECTION-FACE is a foreground (selection) face of the popup The 504 | default value is POPUP-FACE. 505 | 506 | If SCROLL-BAR is non-nil, the popup will have a scroll bar at the 507 | right. 508 | 509 | If MARGIN-LEFT is non-nil, the popup will have a margin at the 510 | left. 511 | 512 | If MARGIN-RIGHT is non-nil, the popup will have a margin at the 513 | right. 514 | 515 | SYMBOL is a single character which indicates a kind of the item. 516 | 517 | PARENT is a parent popup instance. If PARENT is omitted, the 518 | popup will be a root instance. 519 | 520 | PARENT-OFFSET is a row offset from the parent popup. 521 | 522 | KEYMAP is a keymap that will be put on the popup contents." 523 | (or margin-left (setq margin-left 0)) 524 | (or margin-right (setq margin-right 0)) 525 | (unless point 526 | (setq point 527 | (if parent (popup-child-point parent parent-offset) (point)))) 528 | (when max-width 529 | (setq width (min width (popup-calculate-max-width max-width)))) 530 | (save-excursion 531 | (goto-char point) 532 | (let* ((col-row (posn-col-row (posn-at-point))) 533 | (row (cdr col-row)) 534 | (column (car col-row)) 535 | (overlays (make-vector height nil)) 536 | (popup-width (+ width 537 | (if scroll-bar 1 0) 538 | margin-left 539 | margin-right 540 | (if symbol 2 0))) 541 | margin-left-cancel 542 | (window (selected-window)) 543 | (window-start (window-start)) 544 | (window-hscroll (window-hscroll)) 545 | (window-width (window-width)) 546 | (right (+ column popup-width)) 547 | (overflow (and (> right window-width) 548 | (>= right popup-width))) 549 | (foldable (and (null parent) 550 | (>= column popup-width))) 551 | (direction (or 552 | ;; Currently the direction of cascade popup won't be changed 553 | (and parent (popup-direction parent)) 554 | 555 | ;; Calculate direction 556 | (popup-calculate-direction height row))) 557 | (depth (if parent (1+ (popup-depth parent)) 0)) 558 | (newlines (max 0 (+ (- height (count-lines point (point-max))) (if around 1 0)))) 559 | invis-overlays 560 | current-column) 561 | ;; Case: no newlines at the end of the buffer 562 | (when (> newlines 0) 563 | (popup-save-buffer-state 564 | (goto-char (point-max)) 565 | (insert (make-string newlines ?\n)))) 566 | 567 | ;; Case: the popup overflows 568 | (if overflow 569 | (if foldable 570 | (progn 571 | (cl-decf column (- popup-width margin-left margin-right)) 572 | (unless around (move-to-column column))) 573 | (when (not truncate-lines) 574 | ;; Truncate. 575 | (let ((d (1+ (- popup-width (- window-width column))))) 576 | (cl-decf popup-width d) 577 | (cl-decf width d))) 578 | (cl-decf column margin-left)) 579 | (cl-decf column margin-left)) 580 | 581 | ;; Case: no space at the left 582 | (when (and (null parent) 583 | (< column 0)) 584 | ;; Cancel margin left 585 | (setq column 0) 586 | (cl-decf popup-width margin-left) 587 | (setq margin-left-cancel t)) 588 | 589 | (dotimes (i height) 590 | (let (overlay begin w (dangle t) (prefix "") (postfix "")) 591 | (when around 592 | (popup-vertical-motion column direction)) 593 | (cl-loop for ov in (overlays-in (save-excursion 594 | (beginning-of-visual-line) 595 | (point)) 596 | (save-excursion 597 | (end-of-visual-line) 598 | (point))) 599 | when (and (not (overlay-get ov 'popup)) 600 | (not (overlay-get ov 'popup-item)) 601 | (or (overlay-get ov 'invisible) 602 | (overlay-get ov 'display))) 603 | do (progn 604 | (push (list ov (overlay-get ov 'display)) invis-overlays) 605 | (overlay-put ov 'display ""))) 606 | (setq around t) 607 | (setq current-column (car (posn-col-row (posn-at-point)))) 608 | 609 | (when (< current-column column) 610 | ;; Extend short buffer lines by popup prefix (line of spaces) 611 | (setq prefix (make-string 612 | (+ (if (= current-column 0) 613 | (- window-hscroll current-column) 614 | 0) 615 | (- column current-column)) 616 | ? ))) 617 | 618 | (setq begin (point)) 619 | (setq w (+ popup-width (length prefix))) 620 | (while (and (not (eolp)) (> w 0)) 621 | (setq dangle nil) 622 | (cl-decf w (char-width (char-after))) 623 | (forward-char)) 624 | (if (< w 0) 625 | (setq postfix (make-string (- w) ? ))) 626 | 627 | (setq overlay (make-overlay begin (point))) 628 | (overlay-put overlay 'popup t) 629 | (overlay-put overlay 'window window) 630 | (overlay-put overlay 'dangle dangle) 631 | (overlay-put overlay 'prefix prefix) 632 | (overlay-put overlay 'postfix postfix) 633 | (overlay-put overlay 'width width) 634 | (aset overlays 635 | (if (> direction 0) i (- height i 1)) 636 | overlay))) 637 | (cl-loop for p from (- 10000 (* depth 1000)) 638 | for overlay in (nreverse (append overlays nil)) 639 | do (overlay-put overlay 'priority p)) 640 | (let ((it (make-popup :point point 641 | :row row 642 | :column column 643 | :width width 644 | :height height 645 | :min-height min-height 646 | :direction direction 647 | :parent parent 648 | :depth depth 649 | :face face 650 | :mouse-face mouse-face 651 | :selection-face selection-face 652 | :summary-face summary-face 653 | :margin-left margin-left 654 | :margin-right margin-right 655 | :margin-left-cancel margin-left-cancel 656 | :scroll-bar scroll-bar 657 | :symbol symbol 658 | :cursor 0 659 | :offset 0 660 | :scroll-top 0 661 | :current-height 0 662 | :list nil 663 | :newlines newlines 664 | :overlays overlays 665 | :invis-overlays invis-overlays 666 | :keymap keymap))) 667 | (push it popup-instances) 668 | it)))) 669 | 670 | (defun popup-delete (popup) 671 | "Delete POPUP instance." 672 | (when (popup-live-p popup) 673 | (popup-hide popup) 674 | (mapc 'delete-overlay (popup-overlays popup)) 675 | (setf (popup-overlays popup) nil) 676 | (setq popup-instances (delq popup popup-instances)) 677 | ;; Restore newlines state 678 | (let ((newlines (popup-newlines popup))) 679 | (when (> newlines 0) 680 | (popup-save-buffer-state 681 | (goto-char (point-max)) 682 | (dotimes (i newlines) 683 | (if (and (char-before) 684 | (= (char-before) ?\n)) 685 | (delete-char -1))))))) 686 | nil) 687 | 688 | (defun popup-draw (popup) 689 | "Draw POPUP." 690 | (cl-loop for (ov olddisplay) in (popup-invis-overlays popup) 691 | do (overlay-put ov 'display "")) 692 | 693 | (cl-loop with height = (popup-height popup) 694 | with min-height = (popup-min-height popup) 695 | with popup-face = (popup-face popup) 696 | with mouse-face = (popup-mouse-face popup) 697 | with selection-face = (popup-selection-face popup) 698 | with summary-face-0 = (popup-summary-face popup) 699 | with list = (popup-list popup) 700 | with length = (length list) 701 | with thum-size = (max (/ (* height height) (max length 1)) 1) 702 | with page-size = (/ (+ 0.0 (max length 1)) height) 703 | with scroll-bar = (popup-scroll-bar popup) 704 | with margin-left = (make-string (if (popup-margin-left-cancel popup) 0 (popup-margin-left popup)) ? ) 705 | with margin-right = (make-string (popup-margin-right popup) ? ) 706 | with symbol = (popup-symbol popup) 707 | with cursor = (popup-cursor popup) 708 | with scroll-top = (popup-scroll-top popup) 709 | with offset = (popup-offset popup) 710 | with keymap = (popup-keymap popup) 711 | for o from offset 712 | for i from scroll-top 713 | while (< o height) 714 | for item in (nthcdr scroll-top list) 715 | for page-index = (* thum-size (/ o thum-size)) 716 | for face = (if (= i cursor) 717 | (or (popup-item-selection-face item) selection-face) 718 | (or (popup-item-face item) popup-face)) 719 | for summary-face = (unless (= i cursor) summary-face-0) 720 | for empty-char = (propertize " " 'face face) 721 | for scroll-bar-char = (if scroll-bar 722 | (cond 723 | ((and (not (eq scroll-bar :always)) 724 | (<= page-size 1)) 725 | empty-char) 726 | ((and (> page-size 1) 727 | (>= cursor (* page-index page-size)) 728 | (< cursor (* (+ page-index thum-size) page-size))) 729 | popup-scroll-bar-foreground-char) 730 | (t 731 | popup-scroll-bar-background-char)) 732 | "") 733 | for sym = (if symbol 734 | (concat " " (or (popup-item-symbol item) " ")) 735 | "") 736 | for summary = (or (popup-item-summary item) "") 737 | 738 | do 739 | ;; Show line and set item to the line 740 | (popup-set-line-item popup o 741 | :item item 742 | :face face 743 | :mouse-face mouse-face 744 | :margin-left margin-left 745 | :margin-right margin-right 746 | :scroll-bar-char scroll-bar-char 747 | :symbol sym 748 | :summary summary 749 | :summary-face summary-face 750 | :keymap keymap) 751 | 752 | finally 753 | ;; Remember current height 754 | (setf (popup-current-height popup) (- o offset)) 755 | 756 | ;; Hide remaining lines 757 | (let ((scroll-bar-char (if scroll-bar (propertize " " 'face popup-face) "")) 758 | (symbol (if symbol " " ""))) 759 | (if (> (popup-direction popup) 0) 760 | (progn 761 | (when min-height 762 | (while (< o min-height) 763 | (popup-set-line-item popup o 764 | :item "" 765 | :face popup-face 766 | :margin-left margin-left 767 | :margin-right margin-right 768 | :scroll-bar-char scroll-bar-char 769 | :symbol symbol 770 | :summary "") 771 | (cl-incf o))) 772 | (while (< o height) 773 | (popup-hide-line popup o) 774 | (cl-incf o))) 775 | (cl-loop with h = (if min-height (- height min-height) offset) 776 | for o from 0 below offset 777 | if (< o h) 778 | do (popup-hide-line popup o) 779 | if (>= o h) 780 | do (popup-set-line-item popup o 781 | :item "" 782 | :face popup-face 783 | :margin-left margin-left 784 | :margin-right margin-right 785 | :scroll-bar-char scroll-bar-char 786 | :symbol symbol 787 | :summary "")))))) 788 | 789 | (defun popup-hide (popup) 790 | "Hide POPUP." 791 | (cl-loop for (ov olddisplay) in (popup-invis-overlays popup) 792 | do (overlay-put ov 'display olddisplay)) 793 | (dotimes (i (popup-height popup)) 794 | (popup-hide-line popup i))) 795 | 796 | (defun popup-hidden-p (popup) 797 | "Return non-nil if POPUP is hidden." 798 | (let ((hidden t)) 799 | (when (popup-live-p popup) 800 | (dotimes (i (popup-height popup)) 801 | (unless (popup-line-hidden-p popup i) 802 | (setq hidden nil)))) 803 | hidden)) 804 | 805 | (defun popup-jump (popup cursor) 806 | "Jump to a position specified by CURSOR of POPUP and draw." 807 | (let ((scroll-top (popup-scroll-top popup))) 808 | ;; Do not change page as much as possible. 809 | (unless (and (<= scroll-top cursor) 810 | (< cursor (+ scroll-top (popup-height popup)))) 811 | (setf (popup-scroll-top popup) cursor)) 812 | (setf (popup-cursor popup) cursor) 813 | (popup-draw popup))) 814 | 815 | (defun popup-select (popup i) 816 | "Select the item at I of POPUP and draw." 817 | (setq i (+ i (popup-offset popup))) 818 | (when (and (<= 0 i) (< i (popup-height popup))) 819 | (setf (popup-cursor popup) i) 820 | (popup-draw popup) 821 | t)) 822 | 823 | (defun popup-next (popup) 824 | "Select the next item of POPUP and draw." 825 | (let ((height (popup-height popup)) 826 | (cursor (1+ (popup-cursor popup))) 827 | (scroll-top (popup-scroll-top popup)) 828 | (length (length (popup-list popup)))) 829 | (cond 830 | ((>= cursor length) 831 | ;; Back to first page 832 | (setq cursor 0 833 | scroll-top 0)) 834 | ((= cursor (+ scroll-top height)) 835 | ;; Go to next page 836 | (setq scroll-top (min (1+ scroll-top) (max (- length height) 0))))) 837 | (setf (popup-cursor popup) cursor 838 | (popup-scroll-top popup) scroll-top) 839 | (popup-draw popup))) 840 | 841 | (defun popup-previous (popup) 842 | "Select the previous item of POPUP and draw." 843 | (let ((height (popup-height popup)) 844 | (cursor (1- (popup-cursor popup))) 845 | (scroll-top (popup-scroll-top popup)) 846 | (length (length (popup-list popup)))) 847 | (cond 848 | ((< cursor 0) 849 | ;; Go to last page 850 | (setq cursor (1- length) 851 | scroll-top (max (- length height) 0))) 852 | ((= cursor (1- scroll-top)) 853 | ;; Go to previous page 854 | (cl-decf scroll-top))) 855 | (setf (popup-cursor popup) cursor 856 | (popup-scroll-top popup) scroll-top) 857 | (popup-draw popup))) 858 | 859 | (defun popup-page-next (popup) 860 | "Select next item of POPUP per `popup-height' range. 861 | Pages down through POPUP." 862 | (dotimes (counter (1- (popup-height popup))) 863 | (popup-next popup))) 864 | 865 | (defun popup-page-previous (popup) 866 | "Select previous item of POPUP per `popup-height' range. 867 | Pages up through POPUP." 868 | (dotimes (counter (1- (popup-height popup))) 869 | (popup-previous popup))) 870 | 871 | (defun popup-scroll-down (popup &optional n) 872 | "Scroll down N of POPUP and draw." 873 | (let ((scroll-top (min (+ (popup-scroll-top popup) (or n 1)) 874 | (- (length (popup-list popup)) (popup-height popup))))) 875 | (setf (popup-cursor popup) scroll-top 876 | (popup-scroll-top popup) scroll-top) 877 | (popup-draw popup))) 878 | 879 | (defun popup-scroll-up (popup &optional n) 880 | "Scroll up N of POPUP and draw." 881 | (let ((scroll-top (max (- (popup-scroll-top popup) (or n 1)) 882 | 0))) 883 | (setf (popup-cursor popup) scroll-top 884 | (popup-scroll-top popup) scroll-top) 885 | (popup-draw popup))) 886 | 887 | 888 | 889 | ;;; Popup Incremental Search 890 | 891 | (defface popup-isearch-match 892 | '((t (:inherit default :background "sky blue"))) 893 | "Popup isearch match face." 894 | :group 'popup) 895 | 896 | (defvar popup-isearch-cursor-color "blue") 897 | 898 | (defvar popup-isearch-keymap 899 | (let ((map (make-sparse-keymap))) 900 | ;;(define-key map "\r" 'popup-isearch-done) 901 | (define-key map "\C-g" 'popup-isearch-cancel) 902 | (define-key map "\C-b" 'popup-isearch-close) 903 | (define-key map [left] 'popup-isearch-close) 904 | (define-key map "\C-h" 'popup-isearch-delete) 905 | (define-key map (kbd "DEL") 'popup-isearch-delete) 906 | (define-key map (kbd "C-y") 'popup-isearch-yank) 907 | map)) 908 | 909 | (defvar popup-menu-show-quick-help-function 'popup-menu-show-quick-help 910 | "Function used for showing quick help by `popup-menu*'.") 911 | 912 | (defcustom popup-isearch-regexp-builder-function #'regexp-quote 913 | "Function used to construct a regexp from a pattern. You may for instance 914 | provide a function that replaces spaces by '.+' if you like helm or ivy style 915 | of completion." 916 | :type 'function) 917 | 918 | (defsubst popup-isearch-char-p (char) 919 | (and (integerp char) 920 | (<= 32 char) 921 | (<= char 126))) 922 | 923 | (defun popup-isearch-filter-list (pattern list) 924 | (cl-loop with regexp = (funcall popup-isearch-regexp-builder-function pattern) 925 | for item in list 926 | do 927 | (unless (stringp item) 928 | (setq item (popup-item-propertize (popup-x-to-string item) 929 | 'value item))) 930 | if (string-match regexp item) 931 | collect 932 | (let ((beg (match-beginning 0)) 933 | (end (match-end 0))) 934 | (alter-text-property 0 (length item) 'face 935 | (lambda (prop) 936 | (unless (eq prop 'popup-isearch-match) 937 | prop)) 938 | item) 939 | (put-text-property beg end 940 | 'face 'popup-isearch-match 941 | item) 942 | item))) 943 | 944 | (defun popup-isearch-prompt (popup pattern) 945 | (format "Pattern: %s" (if (= (length (popup-list popup)) 0) 946 | (propertize pattern 'face 'isearch-fail) 947 | pattern))) 948 | 949 | (defun popup-isearch-update (popup filter pattern &optional callback) 950 | (setf (popup-cursor popup) 0 951 | (popup-scroll-top popup) 0 952 | (popup-pattern popup) pattern) 953 | (let ((list (funcall filter pattern (popup-original-list popup)))) 954 | (popup-set-filtered-list popup list) 955 | (if callback 956 | (funcall callback list))) 957 | (popup-draw popup)) 958 | 959 | (cl-defun popup-isearch (popup 960 | &key 961 | (filter 'popup-isearch-filter-list) 962 | (cursor-color popup-isearch-cursor-color) 963 | (keymap popup-isearch-keymap) 964 | callback 965 | help-delay) 966 | "Start isearch on POPUP. This function is synchronized, meaning 967 | event loop waits for quiting of isearch. 968 | 969 | FILTER is function with two argumenst to perform popup items filtering. 970 | 971 | CURSOR-COLOR is a cursor color during isearch. The default value 972 | is `popup-isearch-cursor-color'. 973 | 974 | KEYMAP is a keymap which is used when processing events during 975 | event loop. The default value is `popup-isearch-keymap'. 976 | 977 | CALLBACK is a function taking one argument. `popup-isearch' calls 978 | CALLBACK, if specified, after isearch finished or isearch 979 | canceled. The arguments is whole filtered list of items. 980 | 981 | HELP-DELAY is a delay of displaying helps." 982 | (let ((list (popup-original-list popup)) 983 | (pattern (or (popup-pattern popup) "")) 984 | (old-cursor-color (frame-parameter (selected-frame) 'cursor-color)) 985 | prompt key binding) 986 | (unwind-protect 987 | (cl-block nil 988 | (if cursor-color 989 | (set-cursor-color cursor-color)) 990 | (while t 991 | (setq prompt (popup-isearch-prompt popup pattern)) 992 | (setq key (popup-menu-read-key-sequence keymap prompt help-delay)) 993 | (if (null key) 994 | (unless (funcall popup-menu-show-quick-help-function popup nil :prompt prompt) 995 | (clear-this-command-keys) 996 | (push (read-event prompt) unread-command-events)) 997 | (setq binding (lookup-key keymap key)) 998 | (cond 999 | ((and (stringp key) 1000 | (popup-isearch-char-p (aref key 0))) 1001 | (setq pattern (concat pattern key))) 1002 | ((eq binding 'popup-isearch-done) 1003 | (cl-return nil)) 1004 | ((eq binding 'popup-isearch-cancel) 1005 | (popup-isearch-update popup filter "" callback) 1006 | (cl-return t)) 1007 | ((eq binding 'popup-isearch-close) 1008 | (popup-isearch-update popup filter "" callback) 1009 | (setq unread-command-events 1010 | (append (listify-key-sequence key) unread-command-events)) 1011 | (cl-return nil)) 1012 | ((eq binding 'popup-isearch-delete) 1013 | (if (> (length pattern) 0) 1014 | (setq pattern (substring pattern 0 (1- (length pattern)))))) 1015 | ((eq binding 'popup-isearch-yank) 1016 | (popup-isearch-update popup filter (car kill-ring) callback) 1017 | (cl-return nil)) 1018 | (t 1019 | (setq unread-command-events 1020 | (append (listify-key-sequence key) unread-command-events)) 1021 | (cl-return nil))) 1022 | (popup-isearch-update popup filter pattern callback)))) 1023 | (if old-cursor-color 1024 | (set-cursor-color old-cursor-color))))) 1025 | 1026 | 1027 | 1028 | ;;; Popup Tip 1029 | 1030 | (defface popup-tip-face 1031 | '((t (:background "khaki1" :foreground "black"))) 1032 | "Face for popup tip." 1033 | :group 'popup) 1034 | 1035 | (defvar popup-tip-max-width 80) 1036 | 1037 | (cl-defun popup-tip (string 1038 | &key 1039 | point 1040 | (around t) 1041 | width 1042 | (height 15) 1043 | min-height 1044 | max-width 1045 | truncate 1046 | margin 1047 | margin-left 1048 | margin-right 1049 | scroll-bar 1050 | parent 1051 | parent-offset 1052 | nowait 1053 | nostrip 1054 | prompt 1055 | face 1056 | &allow-other-keys 1057 | &aux tip lines) 1058 | "Show a tooltip of STRING at POINT. This function is 1059 | synchronized unless NOWAIT specified. Almost all arguments are 1060 | the same as in `popup-create', except for TRUNCATE, NOWAIT, and 1061 | PROMPT. 1062 | 1063 | If TRUNCATE is non-nil, the tooltip can be truncated. 1064 | 1065 | If NOWAIT is non-nil, this function immediately returns the 1066 | tooltip instance without entering event loop. 1067 | 1068 | If `NOSTRIP` is non-nil, `STRING` properties are not stripped. 1069 | 1070 | PROMPT is a prompt string when reading events during event loop. 1071 | 1072 | If FACE is non-nil, it will be used instead of face `popup-tip-face'." 1073 | (if (bufferp string) 1074 | (setq string (with-current-buffer string (buffer-string)))) 1075 | 1076 | (unless nostrip 1077 | ;; TODO strip text (mainly face) properties 1078 | (setq string (substring-no-properties string))) 1079 | 1080 | (setq string (popup-replace-displayable string)) 1081 | 1082 | (and (eq margin t) (setq margin 1)) 1083 | (or margin-left (setq margin-left margin)) 1084 | (or margin-right (setq margin-right margin)) 1085 | 1086 | (let ((it (popup-fill-string string width popup-tip-max-width))) 1087 | (setq width (car it) 1088 | lines (cdr it))) 1089 | 1090 | (setq tip (popup-create point width height 1091 | :min-height min-height 1092 | :max-width max-width 1093 | :around around 1094 | :margin-left margin-left 1095 | :margin-right margin-right 1096 | :scroll-bar scroll-bar 1097 | :face (or face 'popup-tip-face) 1098 | :parent parent 1099 | :parent-offset parent-offset)) 1100 | 1101 | (unwind-protect 1102 | (when (> (popup-width tip) 0) ; not to be corrupted 1103 | (when (and (not (eq width (popup-width tip))) ; truncated 1104 | (not truncate)) 1105 | ;; Refill once again to lines be fitted to popup width 1106 | (setq width (popup-width tip)) 1107 | (setq lines (cdr (popup-fill-string string width width)))) 1108 | 1109 | (popup-set-list tip lines) 1110 | (popup-draw tip) 1111 | (if nowait 1112 | tip 1113 | (clear-this-command-keys) 1114 | (push (read-event prompt) unread-command-events) 1115 | t)) 1116 | (unless nowait 1117 | (popup-delete tip)))) 1118 | 1119 | 1120 | 1121 | ;;; Popup Menu 1122 | 1123 | (defface popup-menu-face 1124 | '((t (:inherit popup-face))) 1125 | "Face for popup menu." 1126 | :group 'popup) 1127 | 1128 | (defface popup-menu-mouse-face 1129 | '((t (:background "blue" :foreground "white"))) 1130 | "Face for popup menu." 1131 | :group 'popup) 1132 | 1133 | (defface popup-menu-selection-face 1134 | '((t (:inherit default :background "steelblue" :foreground "white"))) 1135 | "Face for popup menu selection." 1136 | :group 'popup) 1137 | 1138 | (defface popup-menu-summary-face 1139 | '((t (:inherit popup-summary-face))) 1140 | "Face for popup summary." 1141 | :group 'popup) 1142 | 1143 | (defvar popup-menu-show-tip-function 'popup-tip 1144 | "Function used for showing tooltip by `popup-menu-show-quick-help'.") 1145 | 1146 | (defun popup-menu-show-help (menu &optional persist item) 1147 | (popup-item-show-help (or item (popup-selected-item menu)) persist)) 1148 | 1149 | (defun popup-menu-documentation (menu &optional item) 1150 | (popup-item-documentation (or item (popup-selected-item menu)))) 1151 | 1152 | (defun popup-menu-show-quick-help (menu &optional item &rest args) 1153 | (let* ((point (plist-get args :point)) 1154 | (height (or (plist-get args :height) (popup-height menu))) 1155 | (min-height (min height (popup-current-height menu))) 1156 | (around nil) 1157 | (parent-offset (popup-offset menu)) 1158 | (doc (popup-menu-documentation menu item))) 1159 | (when (stringp doc) 1160 | (if (popup-hidden-p menu) 1161 | (setq around t 1162 | menu nil 1163 | parent-offset nil) 1164 | (setq point nil)) 1165 | (let ((popup-use-optimized-column-computation nil)) ; To avoid wrong positioning 1166 | (apply popup-menu-show-tip-function 1167 | doc 1168 | :point point 1169 | :height height 1170 | :min-height min-height 1171 | :around around 1172 | :parent menu 1173 | :parent-offset parent-offset 1174 | args))))) 1175 | 1176 | (defun popup-menu-item-of-mouse-event (event) 1177 | (when (and (consp event) 1178 | (memq (cl-first event) '(mouse-1 mouse-2 mouse-3 mouse-4 mouse-5))) 1179 | (let* ((position (cl-second event)) 1180 | (object (elt position 4))) 1181 | (when (consp object) 1182 | (get-text-property (cdr object) 'popup-item (car object)))))) 1183 | 1184 | (defun popup-menu-read-key-sequence (keymap &optional prompt timeout) 1185 | (catch 'timeout 1186 | (let ((timer (and timeout 1187 | (run-with-timer timeout nil 1188 | (lambda () 1189 | (if (zerop (length (this-command-keys))) 1190 | (throw 'timeout nil)))))) 1191 | (old-global-map (current-global-map)) 1192 | (temp-global-map (make-sparse-keymap)) 1193 | (overriding-terminal-local-map (make-sparse-keymap))) 1194 | (substitute-key-definition 'keyboard-quit 'keyboard-quit 1195 | temp-global-map old-global-map) 1196 | (define-key temp-global-map [menu-bar] (lookup-key old-global-map [menu-bar])) 1197 | (define-key temp-global-map [tool-bar] (lookup-key old-global-map [tool-bar])) 1198 | (set-keymap-parent overriding-terminal-local-map keymap) 1199 | (if (current-local-map) 1200 | (define-key overriding-terminal-local-map [menu-bar] 1201 | (lookup-key (current-local-map) [menu-bar]))) 1202 | (unwind-protect 1203 | (progn 1204 | (use-global-map temp-global-map) 1205 | (clear-this-command-keys) 1206 | (with-temp-message prompt 1207 | (read-key-sequence nil))) 1208 | (use-global-map old-global-map) 1209 | (if timer (cancel-timer timer)))))) 1210 | 1211 | (defun popup-menu-fallback (event default)) 1212 | 1213 | (cl-defun popup-menu-event-loop (menu 1214 | keymap 1215 | fallback 1216 | &key 1217 | prompt 1218 | help-delay 1219 | isearch 1220 | isearch-filter 1221 | isearch-cursor-color 1222 | isearch-keymap 1223 | isearch-callback 1224 | &aux key binding) 1225 | (cl-block nil 1226 | (while (popup-live-p menu) 1227 | (and isearch 1228 | (popup-isearch menu 1229 | :filter isearch-filter 1230 | :cursor-color isearch-cursor-color 1231 | :keymap isearch-keymap 1232 | :callback isearch-callback 1233 | :help-delay help-delay) 1234 | (keyboard-quit)) 1235 | (setq key (popup-menu-read-key-sequence keymap prompt help-delay)) 1236 | (setq binding (and key (lookup-key keymap key))) 1237 | (cond 1238 | ((or (null key) (zerop (length key))) 1239 | (unless (funcall popup-menu-show-quick-help-function menu nil :prompt prompt) 1240 | (clear-this-command-keys) 1241 | (push (read-event prompt) unread-command-events))) 1242 | ((eq (lookup-key (current-global-map) key) 'keyboard-quit) 1243 | (keyboard-quit) 1244 | (cl-return)) 1245 | ((eq binding 'popup-close) 1246 | (if (popup-parent menu) 1247 | (cl-return))) 1248 | ((memq binding '(popup-select popup-open)) 1249 | (let* ((item (or (popup-menu-item-of-mouse-event (elt key 0)) 1250 | (popup-selected-item menu))) 1251 | (index (cl-position item (popup-list menu))) 1252 | (sublist (popup-item-sublist item))) 1253 | (unless index (cl-return)) 1254 | (if sublist 1255 | (popup-aif (let (popup-use-optimized-column-computation) 1256 | (popup-cascade-menu sublist 1257 | :around nil 1258 | :margin-left (popup-margin-left menu) 1259 | :margin-right (popup-margin-right menu) 1260 | :scroll-bar (popup-scroll-bar menu) 1261 | :parent menu 1262 | :parent-offset index 1263 | :help-delay help-delay 1264 | :isearch isearch 1265 | :isearch-filter isearch-filter 1266 | :isearch-cursor-color isearch-cursor-color 1267 | :isearch-keymap isearch-keymap 1268 | :isearch-callback isearch-callback)) 1269 | (and it (cl-return it))) 1270 | (if (eq binding 'popup-select) 1271 | (cl-return (popup-item-value-or-self item)))))) 1272 | ((eq binding 'popup-next) 1273 | (popup-next menu)) 1274 | ((eq binding 'popup-previous) 1275 | (popup-previous menu)) 1276 | ((eq binding 'popup-page-next) 1277 | (popup-page-next menu)) 1278 | ((eq binding 'popup-page-previous) 1279 | (popup-page-previous menu)) 1280 | ((eq binding 'popup-help) 1281 | (popup-menu-show-help menu)) 1282 | ((eq binding 'popup-isearch) 1283 | (popup-isearch menu 1284 | :filter isearch-filter 1285 | :cursor-color isearch-cursor-color 1286 | :keymap isearch-keymap 1287 | :callback isearch-callback 1288 | :help-delay help-delay)) 1289 | ((commandp binding) 1290 | (call-interactively binding)) 1291 | (t 1292 | (funcall fallback key (key-binding key))))))) 1293 | 1294 | (defun popup-preferred-width (list) 1295 | "Return the preferred width to show LIST beautifully." 1296 | (cl-loop with tab-width = 4 1297 | for item in list 1298 | for summary = (popup-item-summary item) 1299 | maximize (string-width (popup-x-to-string item)) into width 1300 | if (stringp summary) 1301 | maximize (+ (string-width summary) 2) into summary-width 1302 | finally return 1303 | (let ((total (+ (or width 0) (or summary-width 0)))) 1304 | (* (ceiling (/ total 10.0)) 10)))) 1305 | 1306 | (defvar popup-menu-keymap 1307 | (let ((map (make-sparse-keymap))) 1308 | (define-key map "\r" 'popup-select) 1309 | (define-key map "\C-f" 'popup-open) 1310 | (define-key map [right] 'popup-open) 1311 | (define-key map "\C-b" 'popup-close) 1312 | (define-key map [left] 'popup-close) 1313 | 1314 | (define-key map "\C-n" 'popup-next) 1315 | (define-key map [down] 'popup-next) 1316 | (define-key map "\C-p" 'popup-previous) 1317 | (define-key map [up] 'popup-previous) 1318 | 1319 | (define-key map [next] 'popup-page-next) 1320 | (define-key map [prior] 'popup-page-previous) 1321 | 1322 | (define-key map [f1] 'popup-help) 1323 | (define-key map (kbd "\C-?") 'popup-help) 1324 | 1325 | (define-key map "\C-s" 'popup-isearch) 1326 | 1327 | (define-key map [mouse-1] 'popup-select) 1328 | (define-key map [mouse-4] 'popup-previous) 1329 | (define-key map [mouse-5] 'popup-next) 1330 | map)) 1331 | 1332 | (cl-defun popup-menu* (list 1333 | &key 1334 | point 1335 | (around t) 1336 | (width (popup-preferred-width list)) 1337 | (height 15) 1338 | max-width 1339 | margin 1340 | margin-left 1341 | margin-right 1342 | scroll-bar 1343 | symbol 1344 | parent 1345 | parent-offset 1346 | cursor 1347 | (keymap popup-menu-keymap) 1348 | (fallback 'popup-menu-fallback) 1349 | help-delay 1350 | nowait 1351 | prompt 1352 | isearch 1353 | (isearch-filter 'popup-isearch-filter-list) 1354 | (isearch-cursor-color popup-isearch-cursor-color) 1355 | (isearch-keymap popup-isearch-keymap) 1356 | isearch-callback 1357 | initial-index 1358 | &allow-other-keys 1359 | &aux menu event) 1360 | "Show a popup menu of LIST at POINT. This function returns a 1361 | value of the selected item. Almost all arguments are the same as in 1362 | `popup-create', except for KEYMAP, FALLBACK, HELP-DELAY, PROMPT, 1363 | ISEARCH, ISEARCH-FILTER, ISEARCH-CURSOR-COLOR, ISEARCH-KEYMAP, and 1364 | ISEARCH-CALLBACK. 1365 | 1366 | If KEYMAP is a keymap which is used when processing events during 1367 | event loop. 1368 | 1369 | If FALLBACK is a function taking two arguments; a key and a 1370 | command. FALLBACK is called when no special operation is found on 1371 | the key. The default value is `popup-menu-fallback', which does 1372 | nothing. 1373 | 1374 | HELP-DELAY is a delay of displaying helps. 1375 | 1376 | If NOWAIT is non-nil, this function immediately returns the menu 1377 | instance without entering event loop. 1378 | 1379 | PROMPT is a prompt string when reading events during event loop. 1380 | 1381 | If ISEARCH is non-nil, do isearch as soon as displaying the popup 1382 | menu. 1383 | 1384 | ISEARCH-FILTER is a filtering function taking two arguments: 1385 | search pattern and list of items. Returns a list of matching items. 1386 | 1387 | ISEARCH-CURSOR-COLOR is a cursor color during isearch. The 1388 | default value is `popup-isearch-cursor-color'. 1389 | 1390 | ISEARCH-KEYMAP is a keymap which is used when processing events 1391 | during event loop. The default value is `popup-isearch-keymap'. 1392 | 1393 | ISEARCH-CALLBACK is a function taking one argument. `popup-menu' 1394 | calls ISEARCH-CALLBACK, if specified, after isearch finished or 1395 | isearch canceled. The arguments is whole filtered list of items. 1396 | 1397 | If `INITIAL-INDEX' is non-nil, this is an initial index value for 1398 | `popup-select'. Only positive integer is valid." 1399 | (and (eq margin t) (setq margin 1)) 1400 | (or margin-left (setq margin-left margin)) 1401 | (or margin-right (setq margin-right margin)) 1402 | (if (and scroll-bar 1403 | (integerp margin-right) 1404 | (> margin-right 0)) 1405 | ;; Make scroll-bar space as margin-right 1406 | (cl-decf margin-right)) 1407 | (setq menu (popup-create point width height 1408 | :max-width max-width 1409 | :around around 1410 | :face 'popup-menu-face 1411 | :mouse-face 'popup-menu-mouse-face 1412 | :selection-face 'popup-menu-selection-face 1413 | :summary-face 'popup-menu-summary-face 1414 | :margin-left margin-left 1415 | :margin-right margin-right 1416 | :scroll-bar scroll-bar 1417 | :symbol symbol 1418 | :parent parent 1419 | :parent-offset parent-offset)) 1420 | (unwind-protect 1421 | (progn 1422 | (popup-set-list menu list) 1423 | (if cursor 1424 | (popup-jump menu cursor) 1425 | (popup-draw menu)) 1426 | (when initial-index 1427 | (dotimes (_i (min (- (length list) 1) initial-index)) 1428 | (popup-next menu))) 1429 | (if nowait 1430 | menu 1431 | (popup-menu-event-loop menu keymap fallback 1432 | :prompt prompt 1433 | :help-delay help-delay 1434 | :isearch isearch 1435 | :isearch-filter isearch-filter 1436 | :isearch-cursor-color isearch-cursor-color 1437 | :isearch-keymap isearch-keymap 1438 | :isearch-callback isearch-callback))) 1439 | (unless nowait 1440 | (popup-delete menu)))) 1441 | 1442 | (defun popup-cascade-menu (list &rest args) 1443 | "Same as `popup-menu' except that an element of LIST can be 1444 | also a sub-menu if the element is a cons cell formed (ITEM 1445 | . SUBLIST) where ITEM is an usual item and SUBLIST is a list of 1446 | the sub menu." 1447 | (apply 'popup-menu* 1448 | (mapcar (lambda (item) 1449 | (if (consp item) 1450 | (popup-make-item (car item) 1451 | :sublist (cdr item) 1452 | :symbol ">") 1453 | item)) 1454 | list) 1455 | :symbol t 1456 | args)) 1457 | 1458 | (provide 'popup) 1459 | ;;; popup.el ends here 1460 | --------------------------------------------------------------------------------