ido-ubiquitous.el 51 KB

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