ido-ubiquitous.el 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221
  1. ;;; ido-ubiquitous.el --- Use ido (nearly) everywhere. -*- lexical-binding: t -*-
  2. ;; Copyright (C) 2011-2015 Ryan C. Thompson
  3. ;; Author: Ryan C. Thompson
  4. ;; URL: https://github.com/DarwinAwardWinner/ido-ubiquitous
  5. ;; Version: 3.10
  6. ;; Created: 2011-09-01
  7. ;; Keywords: convenience, completion, ido
  8. ;; EmacsWiki: InteractivelyDoThings
  9. ;; Package-Requires: ((emacs "24.1") (ido-completing-read+ "3.10") (cl-lib "0.5"))
  10. ;; Filename: ido-ubiquitous.el
  11. ;; This file is NOT part of GNU Emacs.
  12. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  13. ;;
  14. ;;; Commentary:
  15. ;; If you use the excellent `ido-mode' for efficient completion of
  16. ;; file names and buffers, you might wonder if you can get ido-style
  17. ;; completion everywhere else too. Well, that's what this package
  18. ;; does! ido-ubiquitous is here to enable ido-style completion for
  19. ;; (almost) every function that uses the standard completion function
  20. ;; `completing-read'.
  21. ;; To use this package, call `ido-ubiquitous-mode' to enable the mode,
  22. ;; or use `M-x customize-variable ido-ubiquitous-mode' it to enable it
  23. ;; permanently. Once the mode is enabled, most functions that use
  24. ;; `completing-read' will now have ido completion. If you decide in
  25. ;; the middle of a command that you would rather not use ido, just C-f
  26. ;; or C-b at the end/beginning of the input to fall back to non-ido
  27. ;; completion (this is the same shortcut as when using ido for buffers
  28. ;; or files).
  29. ;; Note that `completing-read' has some quirks and complex behavior
  30. ;; that ido cannot emulate. Ido-ubiquitous attempts to detect some of
  31. ;; these quirks and avoid using ido when it sees them. So some
  32. ;; functions will not have ido completion even when this mode is
  33. ;; enabled. Some other functions have ido disabled in them because
  34. ;; their packages already provide support for ido via other means (for
  35. ;; example, org-mode and magit). See `M-x customize-group
  36. ;; ido-ubiquitous' and read about the override variables for more
  37. ;; information.
  38. ;; ido-ubiquitous version 3.0 is a major update, including a split
  39. ;; into two packages, and some of the configuration options have
  40. ;; changed in non-backwards-compatible ways. If you have customized
  41. ;; ido-ubiquitous, be sure to check out `M-x customize-group
  42. ;; ido-ubiquitous' and `M-x customize-group ido-completing-read+'
  43. ;; after updating to 3.0 and make sure the new settings are to your
  44. ;; liking.
  45. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  46. ;;
  47. ;; This program is free software: you can redistribute it and/or modify
  48. ;; it under the terms of the GNU General Public License as published by
  49. ;; the Free Software Foundation, either version 3 of the License, or (at
  50. ;; your option) any later version.
  51. ;;
  52. ;; This program is distributed in the hope that it will be useful, but
  53. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  54. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  55. ;; General Public License for more details.
  56. ;;
  57. ;; You should have received a copy of the GNU General Public License
  58. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  59. ;;
  60. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  61. ;;
  62. ;;; Code:
  63. (defconst ido-ubiquitous-version "3.10"
  64. "Currently running version of ido-ubiquitous.
  65. Note that when you update ido-ubiquitous, this variable may not
  66. be updated until you restart Emacs.")
  67. (eval-when-compile
  68. (when (or (not (boundp 'completing-read-function))
  69. (< emacs-major-version 24))
  70. (error "Could not find required variable `completing-read-function'. Are you using Emacs version 24 or higher? If you have Emacs 23 or lower, please downgrade to ido-ubiquitous version 1.7 (or upgrade Emacs).")))
  71. (require 'ido)
  72. (require 'advice)
  73. (require 'cl-lib)
  74. (require 'cus-edit)
  75. (require 'ido-completing-read+)
  76. ;; Only exists in emacs 24.4 and up; we don't use this library
  77. ;; directly, but we load it here so we can test if it's available,
  78. ;; because if it isn't we need enable a workaround.
  79. (require 'nadvice nil 'noerror)
  80. ;;; Debug messages
  81. ;;;###autoload
  82. (define-minor-mode ido-ubiquitous-debug-mode
  83. "If non-nil, ido-ubiquitous will print debug info.
  84. Debug info is printed to the *Messages* buffer."
  85. nil
  86. :global t
  87. :group 'ido-ubiquitous)
  88. ;; Defined as a macro for efficiency (args are not evaluated unless
  89. ;; debug mode is on)
  90. (defmacro ido-ubiquitous--debug-message (format-string &rest args)
  91. `(when ido-ubiquitous-debug-mode
  92. (message (concat "ido-ubiquitous: " ,format-string) ,@args)))
  93. (defun ido-ubiquitous--explain-fallback (arg)
  94. ;; This function accepts a string, or an ido-ubiquitous-fallback
  95. ;; signal.
  96. (when ido-ubiquitous-debug-mode
  97. (when (and (listp arg)
  98. (eq (car arg) 'ido-ubiquitous-fallback))
  99. (setq arg (cdr arg)))
  100. (ido-ubiquitous--debug-message "Falling back to `%s' because %s."
  101. ido-cr+-fallback-function arg)))
  102. ;;; Internal utility functions
  103. (defun ido-ubiquitous--as-string (sym-or-str)
  104. "Return name of symbol, return string as is."
  105. (if (symbolp sym-or-str)
  106. (symbol-name sym-or-str)
  107. sym-or-str))
  108. (defun ido-ubiquitous--as-symbol (sym-or-str)
  109. "Return string as symbol, return symbol as is."
  110. (if (symbolp sym-or-str)
  111. sym-or-str
  112. (intern sym-or-str)))
  113. ;;; Custom widget definitions
  114. ;; We need to define some custom widget types for use in the override
  115. ;; variables.
  116. (define-widget 'lazy-notag 'lazy
  117. "Like lazy widget, but does not display its tag, only its value."
  118. :format "%v")
  119. ;; Define matcher functions and widgets for match specifications
  120. (defvar ido-ubiquitous-match-spec-widget-types nil
  121. "List of widget names for match specs.")
  122. (defvar ido-ubiquitous-spec-matchers nil
  123. "Alist of functions for matching function specs against function names.")
  124. (cl-loop for (widget-name widget-tag key field-type matcher) in
  125. '((exact-match "Exact match" exact string string=)
  126. (prefix-match "Prefix match" prefix string string-prefix-p)
  127. (regexp-match "Regexp match" regexp regexp string-match-p))
  128. do (define-widget (ido-ubiquitous--as-symbol widget-name) 'lazy-notag widget-tag
  129. :menu-tag widget-tag
  130. :type `(list :tag ,widget-tag :format "%v"
  131. (const :format ""
  132. :tag ,widget-tag
  133. ,key)
  134. (,field-type :tag ,widget-tag)))
  135. do (add-to-list 'ido-ubiquitous-match-spec-widget-types
  136. widget-name 'append)
  137. do (add-to-list 'ido-ubiquitous-spec-matchers
  138. (cons key matcher) 'append))
  139. (define-widget 'ido-ubiquitous-match-spec 'lazy-notag
  140. "Choice of exact, prefix, or regexp match."
  141. :type `(choice :tag "Match type"
  142. ,@ido-ubiquitous-match-spec-widget-types))
  143. (define-widget 'ido-ubiquitous-command-override-spec 'lazy-notag
  144. "Choice of override action plus match specification."
  145. :type '(cons :tag "Override rule"
  146. (choice :tag "For matching commands"
  147. (const :menu-tag "Disable"
  148. :tag "Disable ido-ubiquitous"
  149. disable)
  150. (const :menu-tag "Enable"
  151. :tag "Enable ido-ubiquitous in normal default mode"
  152. enable)
  153. (const :menu-tag "Enable old-style default"
  154. :tag "Enable ido-ubiquitous in old-style default mode"
  155. enable-old))
  156. ido-ubiquitous-match-spec))
  157. (define-widget 'ido-ubiquitous-function-override-spec 'lazy-notag
  158. "Choice of override action and function name. (Exact match only.)"
  159. :type '(list :tag "Override rule"
  160. (choice :tag "Do the following"
  161. (const :menu-tag "Disable"
  162. :tag "Disable ido-ubiquitous"
  163. disable)
  164. (const :menu-tag "Enable"
  165. :tag "Enable ido-ubiquitous in normal default mode"
  166. enable)
  167. (const :menu-tag "Enable old-style default"
  168. :tag "Enable ido-ubiquitous in old-style default mode"
  169. enable-old))
  170. (const :format "" exact)
  171. (string :tag "For function")))
  172. ;;; Custom Declarations
  173. (defgroup ido-ubiquitous nil
  174. "Use ido for (almost) all completion."
  175. :group 'ido
  176. :group 'ido-completing-read-plus)
  177. ;;;###autoload
  178. (define-obsolete-variable-alias 'ido-ubiquitous
  179. 'ido-ubiquitous-mode "ido-ubiquitous 0.8")
  180. ;;;###autoload
  181. (define-obsolete-function-alias 'ido-ubiquitous
  182. 'ido-ubiquitous-mode "ido-ubiquitous 0.8")
  183. ;;;###autoload
  184. (define-minor-mode ido-ubiquitous-mode
  185. "Use `ido-completing-read' instead of `completing-read' almost everywhere.
  186. If this mode causes problems for a function, you can customize
  187. when ido completion is or is not used by customizing
  188. `ido-ubiquitous-command-overrides' or
  189. `ido-ubiquitous-function-overrides'."
  190. nil
  191. :global t
  192. :group 'ido-ubiquitous
  193. ;; Actually enable/disable the mode by setting
  194. ;; `completing-read-function'.
  195. (setq completing-read-function
  196. (if ido-ubiquitous-mode
  197. #'completing-read-ido-ubiquitous
  198. ido-cr+-fallback-function)))
  199. ;; Variables for functionality that has moved to ido-completing-read+
  200. (define-obsolete-variable-alias
  201. 'ido-ubiquitous-max-items
  202. 'ido-cr+-max-items
  203. "ido-ubiquitous 3.0")
  204. (define-obsolete-variable-alias
  205. 'ido-ubiquitous-fallback-completing-read-function
  206. 'ido-cr+-fallback-function
  207. "ido-ubiquitous 3.0")
  208. (define-obsolete-variable-alias
  209. 'ido-ubiquitous-enable-compatibility-globally
  210. 'ido-ubiquitous-enable-old-style-default
  211. "ido-ubiquitous 2.0")
  212. (defcustom ido-ubiquitous-default-state 'enable
  213. "Default ido-ubiquitous mode of operation for commands with no override.
  214. This can be set to one of three options:
  215. * `enable': use normal ido completion;
  216. * `enable-old': use ido completion, but with emulation of the
  217. old-style default selection of `completing-read';
  218. * `disable': use non-ido completion.
  219. Command-specific and function-specific overrides are available to
  220. override this default for specific commands/functions. See
  221. `ido-ubiquitous-command-overrides' and
  222. `ido-ubiquitous-function-overrides'.
  223. The `enable-old' option swaps the behavior of RET and C-j but
  224. only for the first keypress after beginning completion.
  225. Specifically, on the first keypress, RET will return an empty
  226. string and C-j will return the first item on the list. The
  227. purpose of this is to emulate a legacy compatibility quirk of
  228. `completing-read'. From the `completing-read' docstring:
  229. > If the input is null, `completing-read' returns DEF, or the
  230. > first element of the list of default values, or an empty string
  231. > if DEF is nil, regardless of the value of REQUIRE-MATCH.
  232. This odd behavior is required for compatibility with an old-style
  233. usage pattern whereby the default was requested by returning an
  234. empty string. In this mode, the caller receives the empty string
  235. and handles the default case manually, while `completing-read'
  236. never has any knowledge of the default. This is a problem for
  237. ido, which normally returns the first element in the list, not an
  238. empty string, when the input is empty and you press RET. Without
  239. knowledge of the default, it cannot ensure that the default is
  240. first on the list, so returning the first item is not the correct
  241. behavior. Instead, it must return an empty string like
  242. `completing-read'.
  243. The `disable' mode is available as a default, which seems
  244. counterintuitive. But this allows you, if you so desire, to
  245. enable ido-ubiquitous selectively for only a few specific commands
  246. using overrides and disable it for everything else."
  247. :type '(choice :tag "Default mode"
  248. (const :menu-tag "Disable"
  249. :tag "Disable ido-ubiquitous"
  250. disable)
  251. (const :menu-tag "Enable"
  252. :tag "Enable ido-ubiquitous in normal default mode"
  253. enable)
  254. (const :menu-tag "Enable old-style default"
  255. :tag "Enable ido-ubiquitous in old-style default mode"
  256. enable-old))
  257. :group 'ido-ubiquitous)
  258. (make-obsolete-variable
  259. 'ido-ubiquitous-enable-old-style-default
  260. "This variable no longer has any effect. Set
  261. `ido-ubiquitous-default-state' to `enable-old' instead."
  262. "ido-ubiquitous 3.0")
  263. (defconst ido-ubiquitous-default-command-overrides
  264. '(;; If you want ido for M-x, install smex
  265. (disable exact "execute-extended-command")
  266. ;; Wanderlust uses new-style default
  267. (enable prefix "wl-")
  268. ;; Info functions use old-style default selection
  269. (enable-old prefix "Info-")
  270. ;; https://github.com/DarwinAwardWinner/ido-ubiquitous/issues/4
  271. (enable exact "webjump")
  272. ;; https://github.com/DarwinAwardWinner/ido-ubiquitous/issues/28
  273. (enable regexp "\\`\\(find\\|load\\|locate\\)-library\\'")
  274. ;; https://github.com/DarwinAwardWinner/ido-ubiquitous/issues/37
  275. ;; Org already supports ido natively
  276. (disable prefix "org-")
  277. ;; https://github.com/bbatsov/prelude/issues/488
  278. ;; https://github.com/DarwinAwardWinner/ido-ubiquitous/issues/44
  279. ;; tmm implements its own non-standard completion mechanics
  280. (disable prefix "tmm-")
  281. ;; https://github.com/DarwinAwardWinner/ido-ubiquitous/issues/47
  282. ;; theme functions don't need old-style compatibility
  283. (enable regexp "\\`\\(load\\|enable\\|disable\\|describe\\|custom-theme-visit\\)-theme\\'")
  284. ;; https://github.com/DarwinAwardWinner/ido-ubiquitous/issues/79
  285. (enable-old prefix "bbdb-")
  286. ;; https://github.com/DarwinAwardWinner/ido-ubiquitous/issues/83
  287. (enable-old exact "where-is")
  288. ;; https://github.com/DarwinAwardWinner/ido-ubiquitous/issues/85
  289. (enable prefix "xref-")
  290. ;; https://github.com/DarwinAwardWinner/ido-ubiquitous/issues/60
  291. (disable exact "todo-add-category")
  292. ;; https://github.com/DarwinAwardWinner/ido-ubiquitous/issues/51
  293. (enable exact "find-tag")
  294. ;; https://github.com/DarwinAwardWinner/ido-ubiquitous/issues/89
  295. (enable prefix "etags-select-")
  296. ) ; Close paren on separate line for better VC diffs
  297. "Default value of `ido-ubiquitous-command-overrides'.
  298. You can restore these using the command `ido-ubiquitous-restore-default-overrides'.")
  299. (defconst ido-ubiquitous-default-function-overrides
  300. '((disable exact "read-file-name")
  301. (disable exact "read-file-name-internal")
  302. (disable exact "read-buffer")
  303. (disable exact "gnus-emacs-completing-read")
  304. (disable exact "gnus-iswitchb-completing-read")
  305. (disable exact "grep-read-files")
  306. (disable exact "magit-builtin-completing-read")
  307. ;; https://github.com/DarwinAwardWinner/ido-ubiquitous/issues/36
  308. (enable exact "bookmark-completing-read")
  309. ;; https://github.com/DarwinAwardWinner/ido-ubiquitous/issues/4
  310. (enable-old exact "webjump-read-choice")
  311. (enable-old exact "webjump-read-url-choice")
  312. ;; https://github.com/DarwinAwardWinner/ido-ubiquitous/issues/9
  313. (disable exact "isearchp-read-unicode-char")
  314. ;; https://github.com/DarwinAwardWinner/ido-ubiquitous/issues/37
  315. (disable exact "org-completing-read")
  316. (disable exact "org-completing-read-no-i")
  317. (disable exact "org-iswitchb-completing-read")
  318. (disable exact "org-icompleting-read")
  319. ;; https://github.com/DarwinAwardWinner/ido-ubiquitous/issues/38
  320. (enable exact "read-char-by-name")
  321. ;; https://github.com/DarwinAwardWinner/ido-ubiquitous/issues/39
  322. (disable exact "Info-read-node-name")
  323. ;; https://github.com/purcell/emacs.d/issues/182#issuecomment-44212927
  324. (disable exact "tmm-menubar")
  325. ;; https://github.com/DarwinAwardWinner/ido-ubiquitous/issues/58
  326. ;; https://github.com/mooz/js2-mode/issues/181
  327. (enable exact "imenu--completion-buffer")
  328. ;; https://github.com/DarwinAwardWinner/ido-ubiquitous/issues/74
  329. (enable-old exact "auto-insert"))
  330. "Default value of `ido-ubiquitous-function-overrides'.
  331. You can restore these using the command `ido-ubiquitous-restore-default-overrides'.")
  332. (defcustom ido-ubiquitous-command-overrides ido-ubiquitous-default-command-overrides
  333. "List of command override specifications for ido-ubiquitous
  334. Each override specification describes how ido-ubiquitous should
  335. behave one or many commands. A specification has the
  336. form `(BEHAVIOR MATCH-TYPE MATCH-TEXT)'. BEHAVIOR is one of the
  337. following:
  338. * `disable': ido-ubiquitous should not be used at all for the
  339. specified commands;
  340. * `enable': ido-ubiquitous may be used with the specified
  341. commands, without emulating the old-style default selection
  342. of `completing-read';
  343. * `enable-old': ido-ubiquitous may be used with the specified
  344. commands, and should emulate the old-style default selection
  345. of `completing-read'.
  346. MATCH-TYPE affects how MATCH-TEXT is interpreted, as follows:
  347. * `exact': the specification only affects the one command
  348. whose name is MATCH-TEXT;
  349. * `prefix': the specification affects any command whose name
  350. starts with MATCH-TEXT (This is useful for specifying a
  351. certain behavior for an entire package);
  352. * `regexp': the specification affects any command whose name
  353. matches MATCH-TEXT (with MATCH-TEXT being interpreted as a
  354. regular expression)
  355. MATCH-TEXT should be a string.
  356. Since this variable's has a somewhat complex structure, it is
  357. recommended that you set this variable through Customize.
  358. Note that this variable only affects *commands*, which are
  359. functions marked as interactive. See
  360. `ido-ubiquitous-function-overrides' for how to modify the
  361. behavior of ido-ubiquitous for arbitrary functions.
  362. Note: If multiple overrides match the same commmand, the first
  363. one in the list will take precedence.
  364. If you need to add a new specification to this list, please also
  365. file a bug report at https://github.com/DarwinAwardWinner/ido-ubiquitous/issues"
  366. :type '(repeat ido-ubiquitous-command-override-spec)
  367. :group 'ido-ubiquitous)
  368. (defmacro ido-ubiquitous-with-override (override &rest body)
  369. "Eval BODY with specified OVERRIDE in place.
  370. The OVERRIDE argument is evaluated normally, so if it is a
  371. literal symbol, it must be quoted.
  372. See `ido-ubiquitous-command-overrides' for valid override types."
  373. (declare (indent 1))
  374. ;; Eval override
  375. `(let ((ido-ubiquitous-next-override ,override))
  376. ,@body))
  377. (defun ido-ubiquitous-apply-function-override (func override)
  378. "Set the override property on FUNC to OVERRIDE and set up advice to apply the override."
  379. (setq func (ido-ubiquitous--as-symbol func)
  380. override (ido-ubiquitous--as-symbol override))
  381. (if (memq override '(disable enable enable-old nil))
  382. (progn
  383. (put func 'ido-ubiquitous-override override)
  384. (when override
  385. (let ((docstring
  386. (format "Override ido-ubiquitous behavior in %s if its `ido-ubiquitous-override' property is non-nil." func)))
  387. (eval
  388. `(defadvice ,func (around ido-ubiquitous-override activate)
  389. ,docstring
  390. (let* ((func ',func)
  391. (override (get func 'ido-ubiquitous-override)))
  392. (when override
  393. (ido-ubiquitous--debug-message
  394. "Using override `%s' for function `%s'"
  395. override func))
  396. (ido-ubiquitous-with-override override
  397. ad-do-it)))))))
  398. (display-warning
  399. 'ido-ubiquitous
  400. (format "Ignoring invalid override action `%s' for function `%s' found in `ido-ubiquitous-function-overrides'."
  401. override func)
  402. :warning)))
  403. (defun ido-ubiquitous-set-function-overrides (sym newval)
  404. "Custom setter function for `ido-ubiquitous-function-overrides'.
  405. In addition to setting the variable, this also sets up advice on
  406. each function to apply the appropriate override."
  407. ;; Unset all previous overrides
  408. (when (boundp sym)
  409. (let ((oldval (eval sym)))
  410. (cl-loop for (_action _match-type func) in oldval
  411. do (ido-ubiquitous-apply-function-override func nil))))
  412. ;; Ensure that function names are strings, not symbols
  413. (setq newval
  414. (cl-loop for (action match-type func) in newval
  415. collect (list action match-type
  416. (ido-ubiquitous--as-string func))))
  417. ;; set new overrides
  418. (cl-loop for override in newval
  419. for (action match-type func) = override
  420. ;; Remove duplicate overrides
  421. if (member func overridden-functions)
  422. do (display-warning
  423. 'ido-ubiquitous
  424. (format
  425. "Removing duplicate override for function `%s'" func))
  426. ;; Apply valid overrides
  427. else if (eq match-type 'exact)
  428. do (ido-ubiquitous-apply-function-override func action)
  429. and collect func into overridden-functions
  430. and collect override into final-value
  431. ;; Remove invalid overrides
  432. else
  433. do (display-warning
  434. 'ido-ubiquitous
  435. (format
  436. "Removing invalid function override match-type `%s' for function `%s'; only match-type `exact' is supported in `ido-ubiquitous-function-overrides'."
  437. match-type func))
  438. ;; Set the value to only the overrides that were actually
  439. ;; applied.
  440. finally return
  441. (set-default sym final-value)))
  442. (defcustom ido-ubiquitous-auto-update-overrides 'notify
  443. "Whether to add new overrides when updating ido-ubiquitous.
  444. Ido-ubiquitous comes with a default set of overrides for commands
  445. that are known to require them. New versions of ido-ubiquitous
  446. may come with updates to the default overrides as more commands
  447. are discovered to require them. However, customizing your own
  448. overrides would normally prevent you from receiving these
  449. updates, since Emacs will not overwrite your customizations.
  450. To resolve this problem, you can set this variable to `t', and
  451. then ido-ubiquitous can automatically add any new built-in
  452. overrides whenever it is updated. (Actually, the update will
  453. happen the next time Emacs is restarted after the update.) This
  454. allows you to add your own overrides but still receive updates to
  455. the default set. The default overrides will always be added with
  456. lower precedence than user-added ones.
  457. If you want ido-ubiquitous to just notify you about new default
  458. overrides instead of adding them itself, set this variable to
  459. `notify'. If you don't want this auto-update behavior at all, set
  460. it to `nil'.
  461. (Note that having this option enabled effectively prevents you
  462. from removing any of the built-in default overrides, since they
  463. will simply be re-added the next time Emacs starts. However, your
  464. custom overrides will still take precedence, so this shouldn't be
  465. a problem.)"
  466. :type '(choice :tag "When new overrides are available:"
  467. (const :menu-tag "Auto-add"
  468. :tag "Add them automatically"
  469. t)
  470. (const :menu-tag "Notify"
  471. :tag "Notify the user about them"
  472. notify)
  473. (const :menu-tag "Ignore"
  474. :tag "Ignore them"
  475. nil))
  476. :group 'ido-ubiquitous)
  477. (defcustom ido-ubiquitous-function-overrides ido-ubiquitous-default-function-overrides
  478. "List of function override specifications for ido-ubiquitous
  479. Function override specifications have a similar structure to
  480. command override specifications (see
  481. `ido-ubiquitous-command-overrides'). A function override
  482. specification has the form `(BEHAVIOR MATCH-TYPE MATCH-TEXT)'.
  483. However, `MATCH-TYPE' may ONLY be `exact'; No other match type is
  484. supported.
  485. Note: If multiple overrides are set for the same function, the
  486. first one in the list will take precedence, and the rest will be
  487. ignored and deleted from the override list, with a warning.
  488. Invalid override specifications will also be deleted with a
  489. warning.
  490. If you need to add a new specification to this list, please also file a
  491. bug report at https://github.com/DarwinAwardWinner/ido-ubiquitous/issues
  492. Setting this variable directly has no effect. You must set it
  493. through Customize."
  494. :type '(repeat ido-ubiquitous-function-override-spec)
  495. :set 'ido-ubiquitous-set-function-overrides
  496. :group 'ido-ubiquitous)
  497. (defcustom ido-ubiquitous-allow-on-functional-collection nil
  498. "Allow ido completion when COLLECTION is a function.
  499. The `completing-read' function allows its COLLECTION argument to
  500. be a function instead of a list of choices. Some such functions
  501. simply return a list of completions and are suitable for use with
  502. ido, but others implement more complex behavior and will result
  503. in incorrect behavior if used with ido. Since there is no way to
  504. tell the difference, this preference defaults to nil, which means
  505. that ido-ubiquitous will not work when COLLECTION is a function
  506. unless there is a specific override in effect. To disable this
  507. safeguard and GUARANTEE ERRORS on some functions, you may set
  508. this to non-nil, but this is not recommended."
  509. :type 'boolean
  510. :group 'ido-ubiquitous)
  511. ;;; ido-ubiquitous core
  512. ;; These variable are used to make ido-ubiquitous work properly in the
  513. ;; case that `completing-read' is called recursively (which is
  514. ;; possible when `enable-recursive-minibuffers' is non-nil.)
  515. (defvar ido-ubiquitous-enable-next-call nil
  516. "If non-nil, then the next call to `ido-completing-read' is by ido-ubiquitous.")
  517. (defvar ido-ubiquitous-enable-this-call nil
  518. "If non-nil, then the current call to `ido-completing-read' is by ido-ubiquitous.")
  519. (defvar ido-ubiquitous-next-override nil
  520. "This holds the override to be applied on the next call to `completing-read'.
  521. It's value can be nil or one of the symbols `disable', `enable', or
  522. `enable-old'.
  523. You should not modify this variable directly. Instead use the
  524. macro `ido-ubiquitous-with-override'.")
  525. (defvar ido-ubiquitous-active-override nil
  526. "This holds the override being applied to the current call to `completing-read'.
  527. It's value can be nil or one of the symbols `disable', `enable', or
  528. `enable-old'.
  529. You should not modify this variable directly. Instead use the
  530. macro `ido-ubiquitous-with-override'.")
  531. (defvar ido-ubiquitous-active-state nil
  532. "This holds the ido-ubiquitous mode of operation for the current call to `completing-read'.
  533. It's value can be nil or one of the symbols `disable', `enable', or
  534. `enable-old'.
  535. You should not modify this variable directly.")
  536. (defadvice ido-completing-read (around ido-ubiquitous activate)
  537. "Enable ido-ubiquitous features if this call was done through ido-ubiquitous.
  538. This advice ensures that `ido-ubiquitous-enable-this-call' is set
  539. properly while `ido-completing-read' is executing. This variable
  540. is used to determine whether to enable certain behaviors only for
  541. ido-ubiquitous, not for ordinary ido completion."
  542. ;; Set "this" and clear "next" so it doesn't apply to nested calls.
  543. (let* ((ido-ubiquitous-enable-this-call ido-ubiquitous-enable-next-call)
  544. (ido-ubiquitous-enable-next-call nil)
  545. (ido-ubiquitous-initial-item nil))
  546. ad-do-it))
  547. ;; Signal used to trigger fallback
  548. (put 'ido-ubiquitous-fallback 'error-conditions '(ido-ubiquitous-fallback error))
  549. (put 'ido-ubiquitous-fallback 'error-message "ido-ubiquitous-fallback")
  550. (defun completing-read-ido-ubiquitous
  551. (prompt collection &optional predicate
  552. require-match initial-input
  553. hist def inherit-input-method)
  554. "ido-based method for reading from the minibuffer with completion.
  555. See `completing-read' for the meaning of the arguments.
  556. This function is a wrapper for `ido-completing-read' designed to
  557. be used as the value of `completing-read-function'. Importantly,
  558. it detects edge cases that ido cannot handle and uses normal
  559. completion for them."
  560. (let (;; Save the original arguments in case we need to do the
  561. ;; fallback
  562. (orig-args
  563. (list prompt collection predicate require-match
  564. initial-input hist def inherit-input-method)))
  565. (condition-case sig
  566. (let* (;; Set the active override and clear the "next" one so it
  567. ;; doesn't apply to nested calls.
  568. (ido-ubiquitous-active-override ido-ubiquitous-next-override)
  569. (ido-ubiquitous-next-override nil)
  570. ;; Apply the active override, if any
  571. (ido-ubiquitous-active-state
  572. (or ido-ubiquitous-active-override
  573. ido-ubiquitous-default-state
  574. 'enable)))
  575. ;; If ido-ubiquitous is disabled this time, fall back
  576. (when (eq ido-ubiquitous-active-state 'disable)
  577. (signal 'ido-ubiquitous-fallback
  578. "`ido-ubiquitous-active-state' is `disable'"))
  579. ;; Handle a collection that is a function: either expand
  580. ;; completion list now or fall back
  581. (when (functionp collection)
  582. (if (or ido-ubiquitous-allow-on-functional-collection
  583. (memq ido-ubiquitous-active-override
  584. '(enable enable-old)))
  585. (setq collection (all-completions "" collection predicate))
  586. (signal 'ido-ubiquitous-fallback
  587. "COLLECTION is a function and there is no override")))
  588. (let ((ido-ubiquitous-enable-next-call t))
  589. (ido-completing-read+
  590. prompt collection predicate require-match
  591. initial-input hist def inherit-input-method)))
  592. ;; Handler for ido-ubiquitous-fallback signal
  593. (ido-ubiquitous-fallback
  594. (ido-ubiquitous--explain-fallback sig)
  595. (apply ido-cr+-fallback-function orig-args)))))
  596. (define-obsolete-function-alias 'completing-read-ido 'completing-read-ido-ubiquitous
  597. "ido-ubiquitous 3.0")
  598. ;;; Old-style default support
  599. (defvar ido-ubiquitous-initial-item nil
  600. "The first item selected when ido starts.
  601. This is initialized to the first item in the list of completions
  602. when ido starts, and is cleared when any character is entered
  603. into the prompt or the list is cycled. If it is non-nil and still
  604. equal to the first item in the completion list when ido exits,
  605. then if `ido-ubiquitous-active-state' is `enable-old', ido
  606. returns an empty string instead of the first item on the list.")
  607. (defmacro ido-ubiquitous-set-initial-item (item)
  608. "Wrapper for `(setq ido-ubiquitous-initial-item ITEM)'.
  609. This wrapper differs from simply doing `(setq
  610. ido-ubiquitous-initial-item ITEM)' in several ways. First, it has
  611. no effect (and does not evaluate ITEM) unless
  612. `ido-ubiquitous-active-state' is `enable-old'. Second, it emits
  613. appropriate debug messages."
  614. `(when (eq ido-ubiquitous-active-state 'enable-old)
  615. (let ((item ,item))
  616. (cond
  617. (item
  618. (ido-ubiquitous--debug-message
  619. "Setting `ido-ubiquitous-initial-item' to `%S'."
  620. item))
  621. (ido-ubiquitous-initial-item
  622. (ido-ubiquitous--debug-message "Clearing `ido-ubiquitous-initial-item'.")))
  623. (setq ido-ubiquitous-initial-item item))))
  624. (defadvice ido-read-internal (before ido-ubiquitous-clear-initial-item activate)
  625. (ido-ubiquitous-set-initial-item nil))
  626. (defadvice ido-make-choice-list (after ido-ubiquitous-set-initial-item activate)
  627. (ido-ubiquitous-set-initial-item
  628. (when (and ad-return-value (listp ad-return-value))
  629. (car ad-return-value))))
  630. (defadvice ido-next-match (after ido-ubiquitous-clear-initial-item activate)
  631. (ido-ubiquitous-set-initial-item nil))
  632. (defadvice ido-prev-match (after ido-ubiquitous-clear-initial-item activate)
  633. (ido-ubiquitous-set-initial-item nil))
  634. ;; Clear initial item after `self-insert-command'
  635. (defun ido-ubiquitous-post-insert-hook ()
  636. (eval '(ido-ubiquitous-set-initial-item nil)))
  637. (defun ido-ubiquitous-ido-minibuffer-setup-hook ()
  638. (add-hook
  639. 'post-self-insert-hook
  640. #'ido-ubiquitous-post-insert-hook
  641. nil
  642. 'local))
  643. (add-hook 'ido-minibuffer-setup-hook
  644. #'ido-ubiquitous-ido-minibuffer-setup-hook)
  645. (defun ido-ubiquitous-should-use-old-style-default ()
  646. "Returns non nil if ido-ubiquitous should emulate old-style default.
  647. This function simply encapsulates all the checks that need to be
  648. done in order to decide whether to swap RET and C-j. See
  649. `ido-ubiquitous-default-state' for more information."
  650. ;; These checks outside the loop don't produce debug messages
  651. (and
  652. ;; Only if old-style default enabled
  653. (eq ido-ubiquitous-active-state 'enable-old)
  654. ;; This loop is just an implementation of `and' that reports which
  655. ;; arg was nil for debugging purposes.
  656. (cl-loop
  657. for test in
  658. '((bound-and-true-p ido-cur-item)
  659. ;; Only if completing a list, not a buffer or file
  660. (eq ido-cur-item 'list)
  661. ;; Only if this call was done through ido-ubiquitous
  662. ido-ubiquitous-enable-this-call
  663. ;; Only if default is nil
  664. (null ido-default-item)
  665. ;; Only if input is empty
  666. (string= ido-text "")
  667. ;; Only if `ido-ubiquitous-initial-item' hasn't been cleared
  668. ido-ubiquitous-initial-item
  669. ;; Only if initial item hasn't changed
  670. (string= (car ido-cur-list)
  671. ido-ubiquitous-initial-item))
  672. for test-result = (eval test)
  673. if (not test-result)
  674. do (ido-ubiquitous--debug-message
  675. "Not enabling old-style default selection because `%S' is nil"
  676. test)
  677. and return nil
  678. finally do (ido-ubiquitous--debug-message
  679. "Enabling old-style default selection")
  680. finally return t)))
  681. (defadvice ido-exit-minibuffer (around ido-ubiquitous-old-style-default-compat activate)
  682. "Emulate a quirk of `completing-read'.
  683. > If the input is null, `completing-read' returns DEF, or the
  684. > first element of the list of default values, or an empty string
  685. > if DEF is nil, regardless of the value of REQUIRE-MATCH.
  686. See `ido-ubiquitous-default-state', which controls whether this
  687. advice has any effect."
  688. (condition-case nil
  689. (if (ido-ubiquitous-should-use-old-style-default)
  690. (let ((ido-ubiquitous-active-state 'enable))
  691. (ido-select-text))
  692. ad-do-it)
  693. (error
  694. (display-warning 'ido-ubiquitous "Advice on `ido-exit-minibuffer' failed." :warning)
  695. ad-do-it))
  696. (ido-ubiquitous-set-initial-item nil))
  697. (defadvice ido-select-text (around ido-ubiquitous-old-style-default-compat activate)
  698. "Emulate a quirk of `completing-read'.
  699. > If the input is null, `completing-read' returns DEF, or the
  700. > first element of the list of default values, or an empty string
  701. > if DEF is nil, regardless of the value of REQUIRE-MATCH.
  702. See `ido-ubiquitous-default-state', which controls whether this
  703. advice has any effect."
  704. (condition-case nil
  705. (if (ido-ubiquitous-should-use-old-style-default)
  706. (let ((ido-ubiquitous-active-state 'enable))
  707. (ido-exit-minibuffer))
  708. ad-do-it)
  709. (error
  710. (display-warning 'ido-ubiquitous "Advice on `ido-select-text' failed." :warning)
  711. ad-do-it))
  712. (ido-ubiquitous-set-initial-item nil))
  713. ;;; Overrides
  714. (defun ido-ubiquitous--overrides-have-same-target-p (o1 o2)
  715. (cl-destructuring-bind (oride1 type1 text1) o1
  716. (cl-destructuring-bind(oride2 type2 text2) o2
  717. ;; Avoid warnings about unused vars
  718. oride1 oride2
  719. (and (string= text1 text2)
  720. (eq type1 type2)))))
  721. (defun ido-ubiquitous--combine-override-lists (olist1 olist2)
  722. "Append OLIST2 to OLIST1, but remove redundant elements.
  723. Redundancy is determined using
  724. `ido-ubiquitous--overrides-have-same-target-p'."
  725. (let ((olist2
  726. (cl-remove-if
  727. (lambda (o2) (cl-member
  728. o2 olist1
  729. :test #'ido-ubiquitous--overrides-have-same-target-p))
  730. olist2)))
  731. (append olist1 olist2)))
  732. (defun ido-ubiquitous-update-overrides (&optional save quiet)
  733. "Re-add the default overrides without erasing custom overrides.
  734. This is useful after an update of ido-ubiquitous that adds new
  735. default overrides. See `ido-ubiquitous-auto-update-overrides' for
  736. more information.
  737. If SAVE is non-nil, also save the overrides to the user's custom
  738. file (but only if they were already customized). When called
  739. interactively, a prefix argument triggers a save.
  740. When called from Lisp code, this function returns the list of
  741. variables whose values were modified. In particular, it returns
  742. non-nil if any variables were modified, and nil if no modifications
  743. were made."
  744. (interactive "P")
  745. (let ((unmodified-vars nil)
  746. (updated-vars nil)
  747. (final-message-lines nil)
  748. (final-message-is-warning nil))
  749. (cl-loop
  750. for (var def) in
  751. '((ido-ubiquitous-command-overrides
  752. ido-ubiquitous-default-command-overrides)
  753. (ido-ubiquitous-function-overrides
  754. ido-ubiquitous-default-function-overrides))
  755. do (let* ((var-state (custom-variable-state var (eval var)))
  756. (curval (eval var))
  757. (defval (eval def))
  758. (newval (ido-ubiquitous--combine-override-lists
  759. curval defval)))
  760. (cond
  761. ;; Nothing to add to var, do nothing
  762. ((and (equal curval newval)
  763. (eq var-state 'saved))
  764. (ido-ubiquitous--debug-message
  765. "No need to modify value of option `%s'"
  766. var)
  767. (push var unmodified-vars))
  768. ;; Var is not customized, just set the new default
  769. ((eq var-state 'standard)
  770. (ido-ubiquitous--debug-message
  771. "Setting uncustomized option `%s' to its default value"
  772. var)
  773. (push var unmodified-vars)
  774. (set var defval))
  775. ;; Var is saved to custom.el, set and save new value (if
  776. ;; SAVE is t)
  777. ((and save
  778. (eq var-state 'saved))
  779. (ido-ubiquitous--debug-message
  780. "Updating option `%s' with new overrides and saving it."
  781. var)
  782. (push var updated-vars)
  783. (customize-save-variable var newval))
  784. ;; Var is modified but not saved (or SAVE is nil), update it
  785. ;; but don't save it
  786. (t
  787. (ido-ubiquitous--debug-message
  788. "Updating option `%s' with new overrides but not saving it for future sessions."
  789. var)
  790. (push var updated-vars)
  791. (customize-set-variable var newval)))))
  792. (unless quiet
  793. ;; Now compose a single message that summarizes what was done
  794. (if (null updated-vars)
  795. (push "No updates to ido-ubiquitous override variables were needed."
  796. final-message-lines)
  797. (push
  798. (format "Updated the following ido-ubiquitous override variables: %S"
  799. (sort updated-vars #'string<))
  800. final-message-lines)
  801. (if save
  802. (push
  803. "All updated variables were successfully saved."
  804. final-message-lines)
  805. (push
  806. "However, they have not been saved for future sessions. To save them, re-run this command with a prefix argument: `C-u M-x ido-ubiquitous-update-overrides'; or else manually inspect and save their values using `M-x customize-group ido-ubiquitous'."
  807. final-message-lines)
  808. (setq final-message-is-warning t)))
  809. (if final-message-is-warning
  810. (display-warning 'ido-ubiquitous
  811. (mapconcat 'identity (nreverse final-message-lines) "\n"))
  812. (message (mapconcat 'identity (nreverse final-message-lines) "\n"))))
  813. updated-vars))
  814. (defun ido-ubiquitous--find-override-updates (current-value available-updates)
  815. (cl-set-difference (ido-ubiquitous--combine-override-lists
  816. current-value available-updates)
  817. current-value))
  818. (defun ido-ubiquitous--maybe-update-overrides ()
  819. "Maybe call `ido-ubiquitous-update-overrides.
  820. See `ido-ubiquitous-auto-update-overrides."
  821. (if ido-ubiquitous-auto-update-overrides
  822. (let* ((command-override-updates
  823. (ido-ubiquitous--find-override-updates
  824. ido-ubiquitous-command-overrides
  825. ido-ubiquitous-default-command-overrides))
  826. (function-override-updates
  827. (ido-ubiquitous--find-override-updates
  828. ido-ubiquitous-function-overrides
  829. ido-ubiquitous-default-function-overrides))
  830. (update-count
  831. (+ (length command-override-updates)
  832. (length function-override-updates))))
  833. (if (> update-count 0)
  834. (if (eq ido-ubiquitous-auto-update-overrides 'notify)
  835. (display-warning
  836. 'ido-ubiquitous
  837. (format "There are %s new overrides available. Use `M-x ido-ubiquitous-update-overrides' to enable them. (See `ido-ubiquitous-auto-update-overrides' for more information.)"
  838. update-count))
  839. (ido-ubiquitous--debug-message "Applying override updates.")
  840. (ido-ubiquitous-update-overrides t))
  841. (ido-ubiquitous--debug-message "No override updates availble.")))
  842. (ido-ubiquitous--debug-message "Skipping override updates by user preference.")))
  843. (define-obsolete-function-alias
  844. 'ido-ubiquitous-restore-default-overrides
  845. 'ido-ubiquitous-update-overrides
  846. "ido-ubiquitous 3.9")
  847. (defun ido-ubiquitous-spec-match (spec symbol)
  848. "Returns t if SPEC matches SYMBOL (which should be a function name).
  849. See `ido-ubiquitous-command-overrides'. If the match spec is
  850. invalid or any other error occurs, the error is demoted to a
  851. warning and the function returns nil."
  852. (condition-case err
  853. (when (and symbol (symbolp symbol))
  854. (cl-destructuring-bind (type text) spec
  855. (let ((matcher (cdr (assoc type ido-ubiquitous-spec-matchers)))
  856. (text (ido-ubiquitous--as-string text))
  857. (symname (ido-ubiquitous--as-string symbol)))
  858. (if matcher
  859. (funcall matcher text symname)
  860. ;; If the matcher is invalid, issue a warning and return
  861. ;; nil.
  862. (error "Unknown match spec type \"%s\". See `ido-ubiquitous-spec-matchers' for valid types." type)
  863. nil))))
  864. (error
  865. (display-warning 'ido-ubiquitous "Error during ido-ubiquitous spec matching: %S" err)
  866. nil)))
  867. (defun ido-ubiquitous-get-command-override (cmd)
  868. "Return the override associated with the command CMD.
  869. If there is no override set for CMD in
  870. `ido-ubiquitous-command-overrides', return nil."
  871. (when (and cmd (symbolp cmd))
  872. (cl-loop for override in ido-ubiquitous-command-overrides
  873. for (action . spec) = override
  874. for valid-action = (and (memq action '(disable enable enable-old nil))
  875. (assoc (car spec) ido-ubiquitous-spec-matchers))
  876. unless valid-action
  877. do (progn
  878. (display-warning
  879. 'ido-ubiquitous
  880. (format "Removing invalid override `%S' from `ido-ubiquitous-command-overrides'"
  881. (cons action spec))
  882. :warning)
  883. (setq ido-ubiquitous-command-overrides
  884. (remove override ido-ubiquitous-command-overrides)))
  885. when (and valid-action (ido-ubiquitous-spec-match spec cmd))
  886. return action
  887. finally return nil)))
  888. ;;; Workaround for https://github.com/DarwinAwardWinner/ido-ubiquitous/issues/24
  889. ;; When `call-interactively' is advised, `called-interactively-p'
  890. ;; always returns nil. So we redefine it (and `interactive-p') to test
  891. ;; the correct condition.
  892. (defsubst ido-ubiquitous--looks-like-advised-orig (func)
  893. "Returns t if FUNC is a symbol starting with \"ad-Orig-\".
  894. Such symbols are used to store the original definitions of
  895. functions that have been advised by `defadvice' or similar."
  896. (and (symbolp func)
  897. (string-prefix-p "ad-Orig-" (symbol-name func))))
  898. (defsubst ido-ubiquitous--looks-like-call-interactively (func)
  899. "Returns t if FUNC looks like the function `call-interactively'.
  900. FUNC \"looks like\" `call-interactively' if it is the literal
  901. symbol `call-interactively', or the value of `(symbol-function
  902. 'call-interactively)', or a symbol whose `symbol-function' is the
  903. same as that of `call-interactively'.
  904. This function is used to determine whether a given function was
  905. \"called by\" `call-interactively' and therefore was called
  906. interactively."
  907. (when func
  908. (eq (symbol-function 'call-interactively)
  909. (if (symbolp func)
  910. (symbol-function func)
  911. func))))
  912. (defun ido-ubiquitous--backtrace-from (fun)
  913. "Return all backtrace frames, starting with the one for FUN.
  914. FUN may be a list of functions, in which case the first one found
  915. on the stack will be used."
  916. (let ((stack
  917. (cl-loop for i upfrom 0
  918. for frame = (backtrace-frame i)
  919. while frame
  920. collect frame))
  921. (funcs (if (functionp fun)
  922. (list fun)
  923. fun)))
  924. (while (and stack
  925. (not (memq (cl-cadar stack) funcs)))
  926. (setq stack (cdr stack)))
  927. stack))
  928. (defun ido-ubiquitous--clean-advice-from-backtrace (stack)
  929. "Takes a stack trace and cleans all evidence of advice.
  930. Specifically, for each call to a function starting with
  931. \"ad-Orig-\", that call and all prior calls up to but not
  932. including the advised function's original name are deleted from
  933. the stack."
  934. (let ((skipping-until nil))
  935. (cl-loop for frame in stack
  936. for func = (cadr frame)
  937. ;; Check if we found the frame we we're skipping to
  938. if (and skipping-until
  939. (eq func skipping-until))
  940. do (setq skipping-until nil)
  941. ;; If we're looking at an the original form of an advised
  942. ;; function, skip until the real name of that function.
  943. if (and (not skipping-until)
  944. (ido-ubiquitous--looks-like-advised-orig func))
  945. do (setq skipping-until
  946. (intern
  947. (substring (symbol-name func)
  948. (eval-when-compile (length "ad-Orig-")))))
  949. unless skipping-until collect frame)))
  950. (defsubst ido-ubiquitous--interactive-internal ()
  951. "Equivalent of the INTERACTIVE macro in the Emacs C source.
  952. This is an internal function that should never be called
  953. directly.
  954. See the C source for the logic behind this function."
  955. (and (not executing-kbd-macro)
  956. (not noninteractive)))
  957. (defun ido-ubiquitous--interactive-p-internal ()
  958. "Equivalent of C function \"interactive_p\".
  959. This is an internal function that should never be called
  960. directly.
  961. See the C source for the logic behind this function."
  962. (let ((stack
  963. ;; We clean advice from the backtrace. This ensures that we
  964. ;; get the right answer even if `call-interactively' has been
  965. ;; advised.
  966. (ido-ubiquitous--clean-advice-from-backtrace
  967. (cdr
  968. (ido-ubiquitous--backtrace-from
  969. '(called-interactively-p interactive-p))))))
  970. ;; See comments in the C function for the logic here.
  971. (while (and stack
  972. (or (eq (cl-cadar stack) 'bytecode)
  973. (null (caar stack))))
  974. (setq stack (cdr stack)))
  975. ;; Top of stack is now the function that we want to know
  976. ;; about. Pop it, then check if the next function is
  977. ;; `call-interactively', using a more permissive test than the default.
  978. (ido-ubiquitous--looks-like-call-interactively (cl-cadadr stack))))
  979. (defadvice call-interactively (around ido-ubiquitous activate)
  980. "Implements the behavior specified in `ido-ubiquitous-command-overrides'."
  981. (let* ((cmd (ad-get-arg 0))
  982. (override (ido-ubiquitous-get-command-override cmd)))
  983. (when override
  984. (ido-ubiquitous--debug-message "Using override `%s' for command `%s'"
  985. override cmd))
  986. (ido-ubiquitous-with-override override
  987. ad-do-it)))
  988. ;; Work around `called-interactively-p' in Emacs 24.3 and earlier,
  989. ;; which always returns nil when `call-interactively' is advised.
  990. (when (not (and (featurep 'nadvice)
  991. (boundp 'called-interactively-p-functions)))
  992. (defadvice interactive-p (around ido-ubiquitous activate)
  993. "Return the correct result when `call-interactively' is advised.
  994. This advice completely overrides the original definition."
  995. (condition-case nil
  996. (setq ad-return-value
  997. (and (ido-ubiquitous--interactive-internal)
  998. (ido-ubiquitous--interactive-p-internal)))
  999. ;; In case of error in the advice, fall back to the default
  1000. ;; implementation
  1001. (error ad-do-it)))
  1002. (defadvice called-interactively-p (around ido-ubiquitous activate)
  1003. "Return the correct result when `call-interactively' is advised.
  1004. This advice completely overrides the original definition."
  1005. (condition-case nil
  1006. (setq ad-return-value
  1007. (and (or (ido-ubiquitous--interactive-internal)
  1008. (not (eq kind 'interactive)))
  1009. (ido-ubiquitous--interactive-p-internal)))
  1010. ;; In case of error in the advice, fall back to the default
  1011. ;; implementation
  1012. (error ad-do-it))))
  1013. ;;; Other
  1014. (defsubst ido-ubiquitous--fixup-old-advice ()
  1015. ;; Clean up old versions of ido-ubiquitous advice if they exist
  1016. (ignore-errors (ad-remove-advice 'completing-read 'around 'ido-ubiquitous))
  1017. (ignore-errors (ad-remove-advice 'ido-completing-read 'around 'detect-replacing-cr))
  1018. (ignore-errors (ad-remove-advice 'ido-magic-forward-char 'before 'ido-ubiquitous-fallback))
  1019. (ignore-errors (ad-remove-advice 'ido-magic-backward-char 'before 'ido-ubiquitous-fallback))
  1020. (ignore-errors (ad-remove-advice 'ido-exit-minibuffer 'around 'compatibility))
  1021. (ad-activate-all))
  1022. (defsubst ido-ubiquitous--fixup-old-magit-overrides ()
  1023. (let ((old-override '(disable prefix "magit-"))
  1024. (new-override '(disable exact "magit-builtin-completing-read")))
  1025. (when (member old-override
  1026. ido-ubiquitous-command-overrides)
  1027. (customize-set-variable
  1028. 'ido-ubiquitous-command-overrides
  1029. (remove old-override ido-ubiquitous-command-overrides))
  1030. (unless (member new-override ido-ubiquitous-function-overrides)
  1031. (customize-set-variable 'ido-ubiquitous-function-overrides
  1032. (append ido-ubiquitous-function-overrides
  1033. (list new-override))))
  1034. (display-warning
  1035. 'ido-ubiquitous
  1036. "Fixing obsolete magit overrides.
  1037. Magit has changed recently such that the old override that
  1038. ido-ubiquitous defined for it now causes problems. This old
  1039. override has been automatically removed and the new one added.
  1040. Please use `M-x customize-group ido-ubiquitous' and review the
  1041. override variables and save them to your customization file."
  1042. :warning))))
  1043. (defun ido-ubiquitous-initialize ()
  1044. "Do initial setup for ido-ubiquitous.
  1045. This only needs to be called once when the file is first loaded.
  1046. It cleans up any traces of old versions of ido-ubiquitous and
  1047. then sets up the mode."
  1048. (ido-ubiquitous--fixup-old-advice)
  1049. (ido-ubiquitous--fixup-old-magit-overrides)
  1050. (ido-ubiquitous--maybe-update-overrides)
  1051. ;; Make sure the mode is turned on/off as specified by the value of
  1052. ;; the mode variable
  1053. (ido-ubiquitous-mode (if ido-ubiquitous-mode 1 0)))
  1054. (ido-ubiquitous-initialize)
  1055. (provide 'ido-ubiquitous)
  1056. ;; Local Variables:
  1057. ;; indent-tabs-mode: nil
  1058. ;; End:
  1059. ;;; ido-ubiquitous.el ends here