1;; ------------------------------ COPYRIGHT NOTICE ------------------------------
2;; klaralv.el version 1.4
3;; Copyright Klaralvdalens Datakonsult AB.
4;;
5;; This program is free software; you can redistribute it and/or modify it
6;; under the terms of the GNU General Public License as published by the Free
7;; Software Foundation; either version 2 of the License, or (at your option)
8;; any later version.
9;;
10;; This program is distributed in the hope that it will be useful, but
11;; WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12;; or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
13;; for more details.
14;;
15;; You should have received a copy of the GNU General Public License along
16;; with GNU Emacs.  If you did not, write to the Free Software Foundation,
17;; Inc., 51 Franklin Street, Fifth Floor., Boston, MA 02110-1301, USA.
18
19
20;; ------------------------------ INSTALLATION ------------------------------
21;; To use this file, put this file in your emacs config directory.
22;; If you do not have such a directory, create one and add
23;; something like the following to your .emacs:
24;; (setq load-path (cons "/home/joe/Emacs/" load-path))
25;;
26;; Next insert the following line into your .emacs
27;; (require 'klaralv)
28;; (global-set-key [(f5)] 'kdab-insert-header)
29;; (global-set-key [(shift f5)] 'kdab-insert-forward-decl)
30;; (global-set-key [(control f5)] 'kdab-lookup-qt-documentation)
31;;
32;; If you use Qtopia, insert the following line to activate Qtopia headers
33;; (setq kdab-include-qpe 't)
34;; If you do not want include files to be prefixed with qtopia/,
35;; as in qtopia/qpeapplication, then insert the following code in your setup
36;; (setq kdab-prefix-qpe nil)
37;;
38;; If you are using Qt 3 rather than Qt 4, insert the following line into you setup:
39;; (setq kdab-qt-version 3)
40;; or in a .emacs-dirvars file:
41;; kdab-qt-version: 3
42;;
43;; Finally, if you do not want header files to be inserted lower-cased when
44;; not found locally, insert (kdab-lowercase-header-files nil)
45
46(require 'kdab-qstring)
47
48;; ------------------------------ CONFIGURATION ------------------------------
49(defvar kdab-qt-version 4
50  "Specify which header files to use")
51
52(defvar kdab-include-qpe nil
53  "Specifies whether Qtopia headers should be included")
54
55(defvar kdab-prefix-qpe 't
56  "set this to nil if you do not want QTopia header files prefixed with qtopia/")
57
58(defvar kdab-lowercase-header-files 't
59  "Should header files be all lowercase, or would you prefer to use same case as class?")
60
61(defvar kdab-private-special-includes '()
62  "Override variable for special include specifications")
63
64(defvar kdab-suffix-for-private-headers "_p\\|_priv"
65  "Regular expression specifying the possible suffix to a file name for private includes.
66The default regular expression allows that a file named test.cpp may include test_p.h or test_priv.h.
67This is used when inserting headers, to ensure that no includes are inserted before the private header")
68
69;; ------------------------------ Include Specifications ------------------------------
70;; special case for include files
71;; Please notify blackie@klaralvdalens-datakonsult.se with any modification to this variable!
72(defvar kdab-special-includes
73  '(
74    ; KDE
75    (kdebug.h kDebug kWarning kError kFatal kBacktrace kdDebug kdWarning kdError kdFatal kdBacktrace)
76    (kicondialog.h KIconCanvas KIconButton)
77    (knuminput.h KDoubleNumInput KIntNumInput)
78
79    ; KDGear - http://www.klaralvdalens-datakonsult.se
80    (KDCheckableGroupBox.h KDCheckableGroupBox)
81    (KDCheckableHGroupBox.h KDCheckableHGroupBox)
82    (KDCheckableVGroupBox.h KDCheckableVGroupBox)
83    (KDCloseableWidget.h KDCloseableWidget)
84    (KDConfigDialog.h KDConfigDialog)
85    (KDConfigWidget.h KDConfigWidget)
86    (KDDateWidget.h KDDateWidget KDDateTimeWidget)
87    (KDDirMonitor.h KDDirMonitor)
88    (KDGridWidget.h KDGridWidget)
89    (KDListBoxPair.h KDListBoxPair)
90    (KDMinimizeSplitter.h KDMinimizeSplitter)
91    (KDSearchableListBox.h KDSearchableListBox)
92    (KDSemiSizingControl.h KDSemiSizingControl)
93    (KDShowHideTableControl.h KDShowHideTableControl)
94    (KDSimpleSizingControl.h KDSimpleSizingControl)
95    (KDSizingControl.h KDSizingControl)
96    (KDStream.h KDStream)
97    (KDTimeWidget.h KDTimeWidget)
98
99    ; KDChart - http://www.klaralvdalens-datakonsult.se
100    (KDChart.h KDChart)
101    (KDChartAxisParams.h KDChartAxisParams)
102    (KDChartBaseSeries.h KDChartBaseSeries)
103    (KDChartCustomBox.h KDChartCustomBox)
104    (KDChartData.h KDChartData)
105    (KDChartEnums.h KDChartEnums)
106    (KDChartListTable.h KDChartListTableData KDChartListTablePrivate)
107    (KDChartNotEnoughSpaceException.h KDChartNotEnoughSpaceException)
108    (KDChartPainter.h KDChartPainter)
109    (KDChartParams.h KDChartFrameSettings KDChartParams ModeAndChart)
110    (KDChartPlaneSeries.h KDChartPlaneSeries)
111    (KDChartPropertySet.h KDChartPropertySet)
112    (KDChartSeriesCollection.h  KDChartSeriesCollection)
113    (KDChartTable.h KDChartTableData)
114    (KDChartTableBase.h KDChartTableDataBase)
115    (KDChartTextPiece.h KDChartTextPiece)
116    (KDChartUnknownTypeException.h KDChartUnknownTypeException)
117    (KDChartVectorSeries.h KDChartVectorSeries)
118    (KDChartVectorTable.h KDChartVectorTableData KDChartVectorTablePrivate)
119    (KDChartWidget.h KDChartWidget)
120    (KDFrame.h KDFrame KDFrameCorner)
121    (KDFrameProfileSection.h KDFrameProfileSection)
122
123    ; Useful fake entries
124    (kapplication.h kapp)
125    (klocale.h i18n i18nc i18ncp i18np I18N_NOOP)
126    (kstandarddirs.h locate locateLocal)
127    (stdlib.h getenv)
128    (unistd.h unlink sleep usleep)
129    (iostream cout cerr)
130    (ctype.h isalnum isalpha isascii isblank iscntrl isdigit isgraph islower isprint ispunct isspace isupper isxdigit)
131    (string.h strcpy strlen memcpy strerror)
132    (stdlib.h atoi atol)
133    )
134    "List of special include files which do not follow the normal scheme")
135
136(defvar kdab-qt3-special-includes
137  '((qlayout.h QHBoxLayout QVBoxLayout QGridLayout QBoxLayout)
138    (qlistview.h QListViewItem QCheckListItem QListViewItemIterator)
139    (qiconview.h QIconViewItem QIconDragItem QIconDrag)
140    (qdragobject.h QTextDrag QStoredDrag QUriDag QColorDrag QImageDrag QDragManager)
141    (qmime.h QMimeSource QMimeSourceFactory QWindowsMime)
142    (qptrlist.h QPtrListIterator)
143    (qevent.h QTimerEvent QMouseEvent QWheelEvent QTabletEvent QKeyEvent
144              QFocusEvent QPaintEvent QMoveEvent QResizeEvent QCloseEvent
145              QShowEvent QHideEvent QContextMenuEvent QIMEvent QDropEvent
146              QDragMoveEvent QDragEnterEvent QDragResponseEvent QDragLeaveEvent
147              QChildEvent QCustomEvent)
148    (qdatetime.h QTime QDateTime QDate)
149    (qdatetimeedit.h QTimeEdit QDateTimeEditBase QDateEdit QDateTimeEdit)
150    (qcstring.h QByteArray)
151    (qobjectlist.h QObjectListIt QObjectListIterator)
152    (qwidgetlist.h QWidgetListIt)
153    (qtabbar.h QTab)
154    (qpalette.h QColorGroup)
155    (qaction.h QActionGroup)
156    (qvalidator.h QIntValidator QDoubleValidator QRegExpValidator)
157    (qlistbox.h QListBoxItem QListBoxText QListBoxPixmap)
158    (qstring.h QChar QCharRef QConstString)
159    (qcanvas.h QCanvasSprite QCanvasPolygonalItem QCanvasRectangle
160               QCanvasPolygon QCanvasEllipse QCanvasText QCanvasLine
161               QCanvasChunk QCanvas QCanvasItem QCanvasView QCanvasPixmap)
162    (qgl.h QGLFormat QGL QGLContext QGLWidget QGLColormap)
163    (qtable.h QTableSelection QTableItem QComboTableItem QCheckTableItem)
164    (qsqlfield.h QSqlField QSqlFieldInfo)
165    (qsqlrecord.h QSqlRecord QSqlRecordInfo)
166
167    ; Qt/Embedded
168    (qcopchannel_qws.h QCopChannel)
169    (qdirectpainter_qws.h QDirectPainter)
170    (qfontfactorybdf_qws.h QFontFactoryBDF)
171    (qfontfactoryttf_qws.h QFontFactoryFT)
172    (qfontmanager_qws.h QGlyphMetrics QGlyph QRenderedFont QDiskFont QFontManager QFontFactory)
173    (qgfx_qws.h QScreenCursor QPoolEntry QScreen QGfx)
174    (qgfxlinuxfb_qws.h QLinuxFbScreen)
175    (qgfxmatroxdefs_qws.h QQnxFbGfx QQnxScreen)
176    (qgfxraster_qws.h QGfxRasterBase QGfxRaster)
177    (qgfxvnc_qws.h QRfbRect QRfbPixelFormat QRfbServerInit QRfbSetEncodings
178                   QRfbFrameBufferUpdateRequest QRfbKeyEvent QRfbPointerEvent QRfbClientCutText QVNCServer)
179    (qkeyboard_qws.h QWSKeyboardHandler)
180    (qlock_qws.h QLock QLockHolder)
181    (qmemorymanager_qws.h QMemoryManagerPixmap QMemoryManager)
182    (qsoundqss_qws.h QWSSoundServer QWSSoundClient QWSSoundServerClient QWSSoundServerSocket)
183    (qwindowsystem_qws.h QWSInternalWindowInfo QWSScreenSaver QWSWindow QWSSoundServer
184                         QWSServer qwsServer KeyboardFilter QWSClient)
185    (qwsbeosdecoration_qws.h QWSBeOSDecoration)
186    (qwscursor_qws.h QWSCursor)
187    (qwsdecoration_qws.h QWSDecoration)
188    (qwsdefaultdecoration_qws.h QWSDefaultDecoration)
189    (qwsdisplay_qws.h QWSWindowInfo QWSDisplay)
190    (qwshydrodecoration_qws.h QWSHydroDecoration)
191    (qwskde2decoration_qws.h QWSKDE2Decoration)
192    (qwskdedecoration_qws.h QWSKDEDecoration)
193    (qwsmanager_qws.h QWSManager QWSButton)
194    (qwsmouse_qws.h QWSPointerCalibrationData QWSMouseHandler QCalibratedMouseHandler
195                    QAutoMouseHandlerPrivate QWSMouseHandlerPrivate QVrTPanelHandlerPrivate
196                    QTPanelHandlerPrivate QYopyTPanelHandlerPrivate QCustomTPanelHandlerPrivate
197                    QVFbMouseHandlerPrivate)
198    (qwsproperty_qws.h QWSPropertyManager)
199    (qwsregionmanager_qws.h QWSRegionManager)
200    (qwssocket_qws.h QWSSocket QWSServerSocket)
201    (qwswindowsdecoration_qws.h QWSWindowsDecoration)
202    (qstatusbar.h statusBar())
203
204    ; Useful fake entries
205    (qapplication.h qApp)
206    (qglobal.h qDebug qWarning)
207    (qeventloop.h eventloop)
208  ))
209
210(defvar kdab-qt4-special-includes
211  '(
212    ; Useful fake entries
213    (QApplication qApp)
214    (QDebug qDebug qWarning)
215    (QEventLoop eventloop)
216    ))
217
218(defvar kdab-qpe-includes
219  '(
220    (alarmserver.h AlarmServer)
221    (applnk.h AppLnk DocLnk AppLnkSet DocLnkSet)
222    (calendar.h Calendar)
223    (categories.h CategoryGroup CategoryGroup Categories CheckedListView)
224    (categorymenu.h CategoryMenu)
225    (categoryselect.h CategoryCombo CategorySelect CategoryEdit CategoryWidget)
226    (config.h Config)
227    (contact.h Contact)
228    (database.h QWSDatabase DatabaseDefaultView Database DatabaseView DatabaseDefaultView)
229    (datebookdb.h DateBookDB)
230    (datebookmonth.h DateBookMonthHeader DayItemMonth DateBookMonthTable DateBookMonth DateButton)
231    (event.h Event EffectiveEvent EffectiveEventSizeSorter EffectiveEventTimeSorter)
232    (filemanager.h FileManager)
233    (fileselector.h FileSelectorItem FileSelector)
234    (finddialog.h FindDialog)
235    (fontdatabase.h FontDatabase)
236    (fontmanager.h FontManager)
237    (global.h Global)
238    (imageedit.h ImageEdit)
239    (inputmethodinterface.h InputMethodInterface)
240    (ir.h Ir)
241    (lightstyle.h LightStyle)
242    (lnkproperties.h LnkProperties)
243    (mediaplayerplugininterface.h MediaPlayerDecoder)
244    (menubutton.h MenuButton)
245    (mimetype.h MimeType)
246    (network.h Network)
247    (palmtoprecord.h Record)
248    (palmtopuidgen.h UidGen)
249    (password.h Password)
250    (power.h PowerStatus PowerStatusManager )
251    (process.h Process)
252    (qcopenvelope_qws.h QCopEnvelope)
253    (qdawg.h QDawg)
254    (qlibrary.h QLibrary)
255    (qpeapplication.h QPEApplication)
256    (qpedecoration_qws.h QPEDecoration QPEManager)
257    (qpedialog.h QPEDialogListener)
258    (qpemenubar.h  QPEMenuToolFocusManager QPEMenuBar)
259    (qpemessagebox.h QPEMessageBox)
260    (qpestyle.h QPEStyle : public QWindowsStyle)
261    (qpetoolbar.h QPEToolBar)
262    (record.h Record)
263    (resource.h Resource)
264    (sound.h Sound)
265    (storage.h StorageInfo FileSystem)
266    (task.h Task)
267    (timeconversion.h TimeConversion)
268    (timestring.h DateFormat TimeString)
269    (tzselect.h TZCombo TimeZoneSelector)
270    ))
271
272; List of classes in Qt4 that don't start with a Q.
273(defvar kdab-qt4-classes
274  '(ActiveQt) )
275
276;; ------------------------------ SOURCE CODE ------------------------------
277
278;; build Qt4 special include list
279(defun kdab-build-qt4-special-includes ()
280  (let ( elm (res kdab-qt4-special-includes) (list kdab-qt4-classes))
281    (while list
282      (progn
283        (setq elm (car list))
284        (setq list (cdr list))
285        (setq res (cons (list elm elm ) res ))))
286    res))
287
288(defun kdab-build-qpe-special-incldues ()
289  (let (elm header classes (list kdab-qpe-includes) filename (result '()))
290    (while list
291      (setq elm (car list))
292      (setq list (cdr list))
293      (setq filename (concat (if kdab-prefix-qpe "qtopia/" "") (symbol-name (car elm))))
294      (setq result (cons (cons (intern filename) (cdr elm)) result)))
295    result))
296
297(defun kdab-join-lists (list1 list2)
298  (let ((res list1)
299        (list list2)
300        elm)
301    (while list
302      (progn
303        (setq elm (car list))
304        (setq list (cdr list))
305        (setq res (cons elm res))))
306    res))
307
308(defun kdab-get-special-include-list ()
309  (kdab-join-lists kdab-private-special-includes
310                   (kdab-join-lists kdab-special-includes
311                                    (kdab-join-lists (if (eq kdab-qt-version 3) kdab-qt3-special-includes
312                                                       (if (eq kdab-qt-version 4) (kdab-build-qt4-special-includes) '()))
313                                                     (if kdab-include-qpe (kdab-build-qpe-special-incldues) '())))))
314
315;; Lookup class `cls' in kdab-special-includes and return the associate include file name
316(defun kdab-map-special (cls)
317  (let ((list (kdab-get-special-include-list))
318        (found nil))
319    (while (and list (not found))
320      (let* ( (elm (car list))
321              (include-file (car elm))
322              (classes (cdr elm)))
323        ( while (and classes (not found))
324          (if (string= (downcase cls) (downcase (symbol-name (car classes))))
325              (setq found include-file)
326            (setq classes (cdr classes)))))
327      (setq list (cdr list)))
328    (if found
329        (symbol-name found)
330         ; not found: check if starts with 'Q'
331      (if (string-startsWith cls "Q")
332	  cls ; assume a Qt-4 forwarding header exists for the class
333	nil))  ; return value
334    ))
335
336
337
338;--------------------------------------------------------------------------------
339; Insert include file for Qt program.
340; Place point anywhere on a Qt class, and invoke this function. A result of
341; this is that an include line is added (if it does not already exists) for
342; the given class.
343;--------------------------------------------------------------------------------
344(defun kdab-insert-header ( prefix )
345  "Insert include file for class at point"
346  (interactive "P")
347  (save-excursion
348    (let* ((word-at-point (if prefix
349                              (read-from-minibuffer "Class: ")
350                            (current-word))))
351      (kdab-insert-header-non-interactive word-at-point))))
352
353;--------------------------------------------------------------------------------
354; insert include file for `word-with-case' non-interactively.
355; for an interactive version see kdab-insert-header
356;--------------------------------------------------------------------------------
357(defun kdab-insert-header-non-interactive (word-with-case)
358  (save-excursion
359    (let* ((word (downcase word-with-case))
360           (special-header (cond
361                    ((kdab-map-special word) (kdab-map-special word))
362                    ((and (string-match "^qdom" word) (eq kdab-qt-version 3)) "qdom.h")
363                    ((and (string-match "^qxml" word) (eq kdab-qt-version 3)) "qxml.h")
364                    ((and (string-match "^q" word) (eq kdab-qt-version 3)) (concat word ".h") )
365                    ((and (string-match "^q" word) (eq kdab-qt-version 4)) word-with-case )
366                    (kdab-lowercase-header-files (concat word ".h" ))
367                    (t (concat word-with-case ".h"))))
368           header is-local override)
369
370      ;; decide on the header file.
371      (if (functionp 'kdab-name-include-file)
372          (setq override (kdab-name-include-file word-with-case)))
373
374      (if override
375          (progn ;; Users hook solved it for us.
376            (setq header (car override))
377            (setq is-local (cdr override)))
378        (if (file-exists-p (concat word-with-case ".h"))
379            (progn ; file exists in given case in pwd.
380              (setq header (concat word-with-case ".h"))
381              (setq is-local 't))
382          (if  (file-exists-p (concat word ".h")) ; file exists in lowercase in pwd
383              (progn
384                (setq header (concat word ".h"))
385                (setq is-local 't))
386            (if (file-exists-p word-with-case)
387                (progn
388                  (setq header word-with-case)
389                  (setq is-local 't))
390              (if (file-exists-p word)
391                  (progn
392                    (setq header word)
393                    (setq is-local 't))
394                (progn ; header in <..> path
395                  (setq header special-header)
396                  (setq is-local nil)))))))
397
398      (kdab-insert-include-file header is-local t))))
399
400;--------------------------------------------------------------------------------
401; Insert header file for header. If is-local insert it with ""
402; otherwise insert it with <>
403;--------------------------------------------------------------------------------
404(defun kdab-insert-include-file (header is-local show-message)
405  (let ((include-file (if is-local
406                          (concat "#include \"" header "\"")
407                        (concat "#include <" header ">"))))
408
409    (beginning-of-buffer)
410    ; first look for //#include "foo.h" being already there
411    (if (re-search-forward (concat "^ *// *\\(#include *[<\"][ \t]*" header "[ \t]*[>\"]\\)") nil t)
412        (progn
413          (replace-match "\\1")
414          (when show-message
415            (message (concat "commented in #include for " header))))
416
417      (if (not (re-search-forward (concat "#include *[\"<][ \t]*" header "[ \t]*[\">]") nil t))
418          (kdab-insert-header-from-end include-file show-message) ;; No include existed
419        (when show-message
420          (message (concat "header file \"" header "\" is already included")))))))
421
422;--------------------------------------------------------------------------------
423; Insert header file starting the search from behind. This is a helper function for
424; kdab-insert-include-file
425; Historically kdab-insert-include-file added the include file at the end of the list
426; of include files, which had the unfortunate effect that it sometimes inserted the
427; include into #ifdef'ed sections.
428; So now we insert it at the top of the list. We need to go after the include for
429; the source files own header.
430;--------------------------------------------------------------------------------
431(defun kdab-insert-header-from-end (include-file show-message)
432  (let ((basename (file-name-nondirectory (file-name-sans-extension (buffer-file-name))))
433        found-own-include found-any-include)
434    (goto-char (point-max)) ; Using end-of-buffer makes point move, despite save-excursion
435    (setq found-own-include
436          (re-search-backward (concat "^[ \t]*#[ \t]*include[ \t]*[\"<]" basename "\\("
437                                      kdab-suffix-for-private-headers "\\)?\\(.h\\)?[\">]") nil 't))
438
439    (if found-own-include
440        (progn ; Found an include for my own header file
441          (next-line 1)
442          (beginning-of-line))
443      (progn ; Did not find a local include - we will then insert before first include
444        (beginning-of-buffer)
445        (setq found-any-include (re-search-forward "^[ \s]*#include[ \t]*[\"<]\\([^\">]+\\)[\">]" nil t))
446        (if found-any-include
447            (beginning-of-line) ;; OK we found some include
448          (beginning-of-buffer)))) ;; None found at all.
449
450    ;; Now insert the header
451    (insert (concat include-file "\n"))
452    (when show-message
453      (message (concat "inserted " include-file)))))
454
455
456;----------------------------------------------------------------------------
457; Insert a forward declaration for a Qt class.
458; Place point anywhere on a Qt class, and invoke this function. A
459; result of this is that a forward declaration line is added (if it does
460; not already exist) for the given class.
461;----------------------------------------------------------------------------
462(defun kdab-insert-forward-decl ( prefix )
463  (interactive "P")
464  (save-excursion
465    (let* ((word (if prefix (read-from-minibuffer "Class: ")
466                   (current-word))))
467      (beginning-of-buffer)
468      (if (re-search-forward (concat "^ *// *\\(class *" word ";\\)") nil t)
469          (progn
470            (replace-match "\\1")
471            (message (concat "commented in forward declaration for " word)))
472
473        (if (not (re-search-forward (concat "class *" word ";") nil t))
474            (progn
475                                        ; No forward decl existed
476                                        ; Look for other forward declarations and insert this one before them
477                                        ; (this avoids finding class Private; inside a class, or other stuff in the middle of the file)
478              (if (re-search-forward "^[ \t]*class .*;" nil t)
479                  (progn
480					; Exit namespace foo { class bar; } if necessary
481					; This is a modified version of (backward-up-list) which doesn't
482					; throw an error when not found.
483		    (goto-char (or (scan-lists (point) -1 1 nil t) (point))) ; ### do multiple times if necessary
484		    (re-search-backward "^[ \t]*namespace " nil t) ; in case of namespace foo\n{
485		    (beginning-of-line))
486                                        ; No forward declarations found, lets search for include lines.
487                                        ; For those we start from the end, because we want to leave file.h first.
488                (progn (goto-char (point-max))
489		       (if (re-search-backward "#include" nil t)
490			   (progn (end-of-line) (forward-char 1))
491			 (beginning-of-buffer))))
492
493              (progn
494                (insert "class " word ";\n")
495                (message (concat "inserted class " word ";"))))
496          (message (concat "forward decl for \"" word "\" already exists")))))))
497
498
499(defun is-qpe-class (class)
500  (let ((list kdab-qpe-includes) classes (found nil))
501    (while (and (not found) list)
502      (setq classes (cdr (car list)))
503      (while classes
504        (if (string= (downcase (symbol-name (car classes))) (downcase class))
505            (setq found 't))
506        (setq classes (cdr classes)))
507      (setq list (cdr list)))
508    found))
509
510
511(provide 'klaralv)
512