← Back to team overview

kicad-developers team mailing list archive

Re: [PATCH] Implement auto annotation on component/symbol placement.

 

Hi,
I'm sorry for the delay but I finally got everything sorted out. I
formatted the code and fixed a few bugs I found.
Regarding `git format-patch`, I used it originally but my commits contain
many TODOs and temporary code so I was told to just squash everything
together and send in a single patch. I will send both this time and let you
see which one you actually want to use. I tried my best to clean up my
commit history but it's still not really perfect.

Thank you.

On Tue, Oct 15, 2019 at 10:41 PM Wayne Stambaugh <stambaughw@xxxxxxxxx>
wrote:

> Use the clang-format git commit hook[1] to only format the changes in
> your git commits rather than entire files.
>
> [1]:
>
> http://docs.kicad-pcb.org/doxygen/md_Documentation_development_coding-style-policy.html#tools
>
> On 10/15/19 4:22 PM, Zficani Zficani wrote:
> > HI,
> > thank you so much for the feedback.
> > I agree with your first point, will do that very soon.
> > Regarding the code formatting, I tried using both uncrustify and
> > clang-format with the supplied configs but none of them really worked
> > because they would end up changing lots of other code outside of the
> > scope of this feature so I'm not sure what to do about that (even just
> > other parts of files I edited). I could always do this by hand but
> > automagic code formatting is much nicer.
> > I suspected there may be some issues with multi-unit symbols so I will
> > look into that.
> > I will also use `format-patch` when sending in new patches. Thank you
> > for letting me know about it.
> >
> > On Tue, Oct 15, 2019 at 8:41 PM Seth Hillbrand <seth@xxxxxxxxxxxxx
> > <mailto:seth@xxxxxxxxxxxxx>> wrote:
> >
> >     On 2019-10-14 14:42, Zficani Zficani wrote:
> >
> >     > Hi,
> >     > No problem, I just wanted to make sure I sent the message properly.
> >     > Here's a single squashed patch with all previous changes and these
> >     > comments about copying selection.
> >     >
> >     > Thank you so much for your review.
> >     >
> >
> >     Hi Zficani-
> >
> >     The functionality feels correct and I really like it.  Here are a few
> >     comments on the current patch:
> >
> >     1) I would prefer that the disabled options in the Annotation page
> are
> >     grey (disabled) and not hidden when the option is unchecked.  This
> >     reserves the correct space for them when we add options in the
> future.
> >
> >     2) Please double-check your code formatting.  Spaces inside the
> >     parentheses are missing in a few spots.
> >
> >     3) Don't use C-style casts.  C++ static_cast<type>() is preferred.
> >
> >     4) Single-line statements after if/else don't get brackets {}
> >
> >     5) I think that pasting Unit B of a component should paste as the
> first
> >     missing Unit B in the schematic and not the next open annotation
> >     number.
> >       See the attached image for the result of duplicating a quad op-amp
> >     for
> >     an example of this problem.
> >
> >     This will be a great addition to KiCad.  Thank you for taking this
> one
> >     on!
> >
> >     Best-
> >     Seth
> >
> >     Seth Hillbrand
> >     KiCad Services Corporation
> >     +1 530 302 5483 | +1 212 603 9372
> >     www.kipro-pcb.com <http://www.kipro-pcb.com>
> >     Davis, CA
> >
> >
> > _______________________________________________
> > Mailing list: https://launchpad.net/~kicad-developers
> > Post to     : kicad-developers@xxxxxxxxxxxxxxxxxxx
> > Unsubscribe : https://launchpad.net/~kicad-developers
> > More help   : https://help.launchpad.net/ListHelp
> >
>
> _______________________________________________
> Mailing list: https://launchpad.net/~kicad-developers
> Post to     : kicad-developers@xxxxxxxxxxxxxxxxxxx
> Unsubscribe : https://launchpad.net/~kicad-developers
> More help   : https://help.launchpad.net/ListHelp
>
From 50445a1db879106c2bafcfd01771d6db33492e8b Mon Sep 17 00:00:00 2001
From: Nufflee <zifcani@xxxxxxxxx>
Date: Mon, 23 Sep 2019 19:47:02 +0200
Subject: [PATCH 03/16] Annotate components on placement (in
 SCH_DRAWING_TOOLS::PlaceComponent).

---
 eeschema/tools/sch_drawing_tools.cpp | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/eeschema/tools/sch_drawing_tools.cpp b/eeschema/tools/sch_drawing_tools.cpp
index e2945d54a..b39ee999e 100644
--- a/eeschema/tools/sch_drawing_tools.cpp
+++ b/eeschema/tools/sch_drawing_tools.cpp
@@ -188,6 +188,12 @@ int SCH_DRAWING_TOOLS::PlaceComponent(  const TOOL_EVENT& aEvent  )
             }
             else
             {
+                // TODO(nufflee): settings
+                if ( true )
+                {
+                    component->Annotate( g_CurrentSheet );
+                }
+
                 m_frame->AddItemToScreenAndUndoList( component );
                 component = nullptr;
 
-- 
2.20.1

From 4f9ac4f41b8f0bb70bac04c34ed9ffb6469beb2b Mon Sep 17 00:00:00 2001
From: Nufflee <zifcani@xxxxxxxxx>
Date: Mon, 23 Sep 2019 20:01:39 +0200
Subject: [PATCH 05/16] Annotate components when pasted.

---
 eeschema/tools/sch_editor_control.cpp | 43 +++++++++++++++++++++++++--
 1 file changed, 41 insertions(+), 2 deletions(-)

diff --git a/eeschema/tools/sch_editor_control.cpp b/eeschema/tools/sch_editor_control.cpp
index 23cb07db3..584c0e418 100644
--- a/eeschema/tools/sch_editor_control.cpp
+++ b/eeschema/tools/sch_editor_control.cpp
@@ -52,6 +52,7 @@
 #include <invoke_sch_dialog.h>
 #include <dialogs/dialog_paste_special.h>
 #include <netlist_exporters/netlist_exporter_pspice.h>
+#include <refdes_utils.h>
 
 int SCH_EDITOR_CONTROL::New( const TOOL_EVENT& aEvent )
 {
@@ -871,7 +872,7 @@ int SCH_EDITOR_CONTROL::Redo( const TOOL_EVENT& aEvent )
 bool SCH_EDITOR_CONTROL::doCopy()
 {
     EE_SELECTION_TOOL* selTool = m_toolMgr->GetTool<EE_SELECTION_TOOL>();
-    EE_SELECTION&      selection = selTool->RequestSelection();
+    EE_SELECTION       selection = selTool->RequestSelection();
 
     if( !selection.GetSize() )
         return false;
@@ -890,6 +891,38 @@ bool SCH_EDITOR_CONTROL::doCopy()
     STRING_FORMATTER formatter;
     SCH_LEGACY_PLUGIN plugin;
 
+    // TODO(nufflee): settings
+    if ( true )
+    {
+        std::sort( selection.begin(), selection.end(), []( const EDA_ITEM* left, const EDA_ITEM* right ) 
+        {
+            // TODO(nufflee): Possibly extract this from SCH_REFERENCE_LIST::sortByReferenceOnly
+            if ( left->Type() == SCH_COMPONENT_T && right->Type() == SCH_COMPONENT_T )
+            {
+                SCH_COMPONENT* leftComponent  = (SCH_COMPONENT*) left;
+                SCH_COMPONENT* rightComponent = (SCH_COMPONENT*) right;
+                int ii;
+
+                ii = UTIL::RefDesStringCompare( leftComponent->GetRef( g_CurrentSheet ), rightComponent->GetRef( g_CurrentSheet ) );
+
+                if( ii == 0 )
+                {
+                    ii = leftComponent->GetField( VALUE )->GetText().CmpNoCase( rightComponent->GetField( VALUE )->GetText() );
+                }
+
+                if( ii == 0 )
+                {
+                    ii = leftComponent->GetUnit() - rightComponent->GetUnit();
+                }
+
+                return ii < 0;
+            }
+
+            return false;
+        });
+    }
+
+    // TODO(nufflee): Not a reference this function might be looking for?
     plugin.Format( &selection, &formatter );
 
     return m_toolMgr->SaveClipboard( formatter.GetString() );
@@ -1013,7 +1046,8 @@ int SCH_EDITOR_CONTROL::Paste( const TOOL_EVENT& aEvent )
 
         if( item->Type() == SCH_COMPONENT_T )
         {
-            if( !dropAnnotations && !forceKeepAnnotations )
+            // TODO(nufflee): settings
+            if( !true && !dropAnnotations && !forceKeepAnnotations )
             {
                 for( SCH_ITEM* temp = dlist.GetFirst(); temp != lastExisting; temp = temp->Next() )
                 {
@@ -1066,6 +1100,11 @@ int SCH_EDITOR_CONTROL::Paste( const TOOL_EVENT& aEvent )
                 component->ClearAnnotation( nullptr );
                 component->SetUnit( unit );
             }
+            // TODO(nufflee): settings
+            else if ( true )
+            {
+                component->Annotate( g_CurrentSheet );
+            }
 
             component->Resolve( *symLibTable, partLib );
             component->UpdatePins();
-- 
2.20.1

From 8a83b6825237cce2018d85c7a4ea7b7e8810e6ac Mon Sep 17 00:00:00 2001
From: Nufflee <zifcani@xxxxxxxxx>
Date: Mon, 23 Sep 2019 19:48:18 +0200
Subject: [PATCH 04/16] Annotate components when duplicated.

---
 eeschema/tools/sch_edit_tool.cpp | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)

diff --git a/eeschema/tools/sch_edit_tool.cpp b/eeschema/tools/sch_edit_tool.cpp
index c512240a1..de975301b 100644
--- a/eeschema/tools/sch_edit_tool.cpp
+++ b/eeschema/tools/sch_edit_tool.cpp
@@ -777,7 +777,18 @@ int SCH_EDIT_TOOL::Duplicate( const TOOL_EVENT& aEvent )
             SCH_COMPONENT* component = (SCH_COMPONENT*) newItem;
 
             component->SetTimeStamp( GetNewTimeStamp() );
-            component->ClearAnnotation( NULL );
+
+            // TODO(nufflee): sort components like when placed
+
+            // TODO(nufflee): settings
+            if ( true ) 
+            {
+                component->Annotate( g_CurrentSheet );
+            }
+            else
+            {
+                component->ClearAnnotation( NULL );
+            }
 
             component->SetParent( m_frame->GetScreen() );
             m_frame->AddToScreen( component );
-- 
2.20.1

From 75749c97be6fca84baab5f353f4d9aea084f3932 Mon Sep 17 00:00:00 2001
From: Nufflee <zifcani@xxxxxxxxx>
Date: Mon, 23 Sep 2019 19:08:49 +0200
Subject: [PATCH 01/16] Add .vscode to .gitignore.

---
 .gitignore | 1 +
 1 file changed, 1 insertion(+)

diff --git a/.gitignore b/.gitignore
index 619c7f963..f62e05e15 100644
--- a/.gitignore
+++ b/.gitignore
@@ -74,6 +74,7 @@ pcbnew/Info.plist
 .*.swp
 *~
 .idea
+.vscode
 .DS_Store
 *.png
 *.kiface
-- 
2.20.1

From 953261d5a6cc702c6a4e704c36cbd4b7c6709134 Mon Sep 17 00:00:00 2001
From: Nufflee <zifcani@xxxxxxxxx>
Date: Mon, 23 Sep 2019 19:19:10 +0200
Subject: [PATCH 02/16] Add SCH_COMPONENT::Annotate.

This function annotates a component with next free reference number on a given sheet.
---
 eeschema/sch_component.cpp | 39 ++++++++++++++++++++++++++++++++++++++
 eeschema/sch_component.h   |  6 ++++++
 2 files changed, 45 insertions(+)

diff --git a/eeschema/sch_component.cpp b/eeschema/sch_component.cpp
index c4dc51085..2b7ac56bc 100644
--- a/eeschema/sch_component.cpp
+++ b/eeschema/sch_component.cpp
@@ -44,6 +44,7 @@
 #include <netlist_object.h>
 #include <lib_item.h>
 #include <symbol_lib_table.h>
+#include <sch_reference_list.h>
 
 #include <dialogs/dialog_schematic_find.h>
 
@@ -718,6 +719,44 @@ void SCH_COMPONENT::SetRef( const SCH_SHEET_PATH* sheet, const wxString& ref )
     m_isInNetlist = ! ref.StartsWith( wxT( "#" ) );
 }
 
+void SCH_COMPONENT::Annotate( SCH_SHEET_PATH* aSheet )
+{
+    SCH_REFERENCE_LIST references;
+    wxString prefix = GetPrefix();
+
+    aSheet->GetComponents( references );
+
+    int lastRefNum = 0;
+
+    references.SortByReferenceOnly();
+
+    for ( unsigned i = 0; i < references.GetCount(); i++ ) 
+    {
+        if ( references[i].GetComp()->GetPrefix() == prefix ) 
+        {
+            long refNum = -1;
+
+            references[i].Split();
+
+            if ( !references[i].GetRefNumber().ToLong( &refNum ) )
+            {
+                continue;
+            }
+
+            if ( refNum - lastRefNum > 1 )
+            {
+                break;
+            }
+
+            if ( refNum > lastRefNum ) 
+            {
+                lastRefNum = refNum;
+            }
+        }
+    }
+
+    SetRef( aSheet, prefix << ( lastRefNum + 1 ) );
+}
 
 bool SCH_COMPONENT::IsAnnotated( const SCH_SHEET_PATH* aSheet )
 {
diff --git a/eeschema/sch_component.h b/eeschema/sch_component.h
index b64938a34..9b2b77150 100644
--- a/eeschema/sch_component.h
+++ b/eeschema/sch_component.h
@@ -508,6 +508,12 @@ public:
      */
     void SetRef( const SCH_SHEET_PATH* aSheet, const wxString& aReference );
 
+    /**
+     * Annotates the component with first available reference number for its reference prefix on a given sheet.
+     * @param aSheet is the sheet path to annotate component on
+     */
+    void Annotate( SCH_SHEET_PATH* aSheet );
+
     /**
      * Checks if the component has a valid annotation (reference) for the given sheet path
      * @param aSheet is the sheet path to test
-- 
2.20.1

From 125c2fbb0231b2d9b91764930184567916f08566 Mon Sep 17 00:00:00 2001
From: Nufflee <zifcani@xxxxxxxxx>
Date: Fri, 27 Sep 2019 21:02:46 +0200
Subject: [PATCH 06/16] Introduce Eeschema Annotation Options and add them to
 preferences.

Disable Annotation Options instead of hiding them.

Annotation options are grayed out now instead of hidden.
Make 'current page' default scope.

Rename AutoAnnotationNumbering -> AutoAnnotationAlgo.
---
 eeschema/CMakeLists.txt                       |   2 +
 .../panel_eeschema_annotation_options.cpp     |  98 +++
 .../panel_eeschema_annotation_options.h       |  20 +
 ...panel_eeschema_annotation_options_base.cpp |  83 +++
 ...panel_eeschema_annotation_options_base.fbp | 579 ++++++++++++++++++
 .../panel_eeschema_annotation_options_base.h  |  59 ++
 eeschema/eeschema_config.cpp                  |   7 +
 eeschema/sch_component.cpp                    |   4 +
 eeschema/sch_component.h                      |  20 +
 eeschema/tools/ee_selection.cpp               |  31 +
 eeschema/tools/ee_selection.h                 |   5 +
 eeschema/tools/sch_edit_tool.cpp              |  12 +-
 eeschema/tools/sch_editor_control.cpp         |  30 +-
 13 files changed, 920 insertions(+), 30 deletions(-)
 create mode 100644 eeschema/dialogs/panel_eeschema_annotation_options.cpp
 create mode 100644 eeschema/dialogs/panel_eeschema_annotation_options.h
 create mode 100644 eeschema/dialogs/panel_eeschema_annotation_options_base.cpp
 create mode 100644 eeschema/dialogs/panel_eeschema_annotation_options_base.fbp
 create mode 100644 eeschema/dialogs/panel_eeschema_annotation_options_base.h

diff --git a/eeschema/CMakeLists.txt b/eeschema/CMakeLists.txt
index f22886414..86569a39d 100644
--- a/eeschema/CMakeLists.txt
+++ b/eeschema/CMakeLists.txt
@@ -97,6 +97,8 @@ set( EESCHEMA_DLGS
     dialogs/panel_eeschema_display_options_base.cpp
     dialogs/panel_eeschema_settings.cpp
     dialogs/panel_eeschema_settings_base.cpp
+    dialogs/panel_eeschema_annotation_options.cpp
+    dialogs/panel_eeschema_annotation_options_base.cpp
     dialogs/panel_libedit_settings.cpp
     dialogs/panel_libedit_settings_base.cpp
     dialogs/panel_sym_lib_table.cpp
diff --git a/eeschema/dialogs/panel_eeschema_annotation_options.cpp b/eeschema/dialogs/panel_eeschema_annotation_options.cpp
new file mode 100644
index 000000000..46e391ab1
--- /dev/null
+++ b/eeschema/dialogs/panel_eeschema_annotation_options.cpp
@@ -0,0 +1,98 @@
+/*
+ * This program source code file is part of KiCad, a free EDA CAD application.
+ *
+ * Copyright (C) 2019 Kicad Developers, see AUTHORS.txt for contributors.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, you may find one here:
+ * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
+ * or you may search the http://www.gnu.org website for the version 2 license,
+ * or you may write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
+ */
+
+#include <panel_eeschema_annotation_options.h>
+#include <sch_component.h>
+
+PANEL_EESCHEMA_ANNOTATION_OPTIONS::PANEL_EESCHEMA_ANNOTATION_OPTIONS( SCH_EDIT_FRAME* aFrame, wxWindow* aWindow )
+    : PANEL_EESCHEMA_ANNOTATION_OPTIONS_BASE( aWindow ),
+      m_frame( aFrame )
+{
+}
+
+bool PANEL_EESCHEMA_ANNOTATION_OPTIONS::TransferDataToWindow()
+{
+    bool enabled = m_frame->IsAutoAnnotationEnabled();
+
+    m_cbEnabled->SetValue( enabled );
+
+    SetContentEnabled(enabled);
+
+    m_rbScope->SetSelection( SCH_COMPONENT::GetAutoAnnotationScopeOption() );
+    
+    switch ( SCH_COMPONENT::GetAutoAnnotationAlgoOption() )
+    {
+        case INCREMENTAL_BY_REF:  m_rbFirstFree->SetValue( true );  break;
+        case SHEET_NUMBER_X_100:  m_rbSheetX100->SetValue( true );  break;
+        case SHEET_NUMBER_X_1000: m_rbSheetX1000->SetValue( true ); break;
+    }
+
+    m_textNumberAfter->SetValue( wxString::Format( "%d", m_frame->GetAutoAnnotationFirstFreeNumOption() ) );
+
+    return true;
+}
+
+bool PANEL_EESCHEMA_ANNOTATION_OPTIONS::TransferDataFromWindow()
+{
+    m_frame->SetIsAutoAnnotationEnabled( m_cbEnabled->GetValue() );
+
+    m_frame->SetAutoAnnotationScopeOption( m_rbScope->GetSelection() );
+
+    if ( m_rbFirstFree->GetValue() )
+    {
+        SCH_COMPONENT::SetAutoAnnotationAlgoOption( 0 );
+    }
+    else if ( m_rbSheetX100->GetValue() )
+    {
+        SCH_COMPONENT::SetAutoAnnotationAlgoOption( 1 );
+    }
+    else if ( m_rbSheetX1000->GetValue() )
+    {
+        SCH_COMPONENT::SetAutoAnnotationAlgoOption( 2 );
+    }
+
+    long numberAfter;
+
+    if ( m_textNumberAfter->GetValue().ToLong( &numberAfter ) )
+    {
+        m_frame->SetAutoAnnotationFirstFreeNumOption( numberAfter );
+    }
+
+    m_frame->SaveProjectSettings( false );
+
+    return true;
+}
+
+void PANEL_EESCHEMA_ANNOTATION_OPTIONS::OnEnableClick( wxCommandEvent& event )
+{
+    SetContentEnabled( m_cbEnabled->IsChecked() );
+}
+
+void PANEL_EESCHEMA_ANNOTATION_OPTIONS::SetContentEnabled(bool enabled)
+{
+    m_rbScope->Enable( enabled );
+    m_rbFirstFree->Enable( enabled );
+    m_rbSheetX100->Enable( enabled );
+    m_rbSheetX1000->Enable( enabled );
+    m_textNumberAfter->Enable( enabled );
+}
diff --git a/eeschema/dialogs/panel_eeschema_annotation_options.h b/eeschema/dialogs/panel_eeschema_annotation_options.h
new file mode 100644
index 000000000..98b64c38d
--- /dev/null
+++ b/eeschema/dialogs/panel_eeschema_annotation_options.h
@@ -0,0 +1,20 @@
+#ifndef KICAD_PANEL_EESCHEMA_ANNOTATION_OPTIONS_H
+#define KICAD_PANEL_EESCHEMA_ANNOTATION_OPTIONS_H
+
+#include "panel_eeschema_annotation_options_base.h"
+
+class PANEL_EESCHEMA_ANNOTATION_OPTIONS : public PANEL_EESCHEMA_ANNOTATION_OPTIONS_BASE
+{
+public:
+    PANEL_EESCHEMA_ANNOTATION_OPTIONS( wxWindow* aWindow );
+
+    void OnEnableClick( wxCommandEvent& event ) override;
+
+private:
+    bool TransferDataFromWindow() override;
+    bool TransferDataToWindow() override;
+
+    void SetContentEnabled(bool enabled = true);
+};
+
+#endif //KICAD_PANEL_EESCHEMA_ANNOTATION_OPTIONS_H
diff --git a/eeschema/dialogs/panel_eeschema_annotation_options_base.cpp b/eeschema/dialogs/panel_eeschema_annotation_options_base.cpp
new file mode 100644
index 000000000..cd1363092
--- /dev/null
+++ b/eeschema/dialogs/panel_eeschema_annotation_options_base.cpp
@@ -0,0 +1,83 @@
+///////////////////////////////////////////////////////////////////////////
+// C++ code generated with wxFormBuilder (version Sep 10 2019)
+// http://www.wxformbuilder.org/
+//
+// PLEASE DO *NOT* EDIT THIS FILE!
+///////////////////////////////////////////////////////////////////////////
+
+#include "panel_eeschema_annotation_options_base.h"
+
+///////////////////////////////////////////////////////////////////////////
+
+PANEL_EESCHEMA_ANNOTATION_OPTIONS_BASE::PANEL_EESCHEMA_ANNOTATION_OPTIONS_BASE( wxWindow* parent, wxWindowID id, const wxPoint& pos, const wxSize& size, long style, const wxString& name ) : wxPanel( parent, id, pos, size, style, name )
+{
+	wxBoxSizer* bmainSizer;
+	bmainSizer = new wxBoxSizer( wxVERTICAL );
+
+	wxBoxSizer* bupperSizer;
+	bupperSizer = new wxBoxSizer( wxVERTICAL );
+
+	m_cbEnabled = new wxCheckBox( this, wxID_ANY, _("Annotate components on placement"), wxDefaultPosition, wxDefaultSize, 0 );
+	bupperSizer->Add( m_cbEnabled, 0, wxALL, 5 );
+
+	m_separator = new wxStaticLine( this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxLI_HORIZONTAL );
+	bupperSizer->Add( m_separator, 0, wxEXPAND | wxALL, 5 );
+
+	m_contentSizer = new wxFlexGridSizer( 1, 2, 0, 0 );
+	m_contentSizer->AddGrowableCol( 0 );
+	m_contentSizer->AddGrowableCol( 1 );
+	m_contentSizer->SetFlexibleDirection( wxBOTH );
+	m_contentSizer->SetNonFlexibleGrowMode( wxFLEX_GROWMODE_SPECIFIED );
+
+	wxString m_rbScopeChoices[] = { _("Use the current page only"), _("Use the entire schematic") };
+	int m_rbScopeNChoices = sizeof( m_rbScopeChoices ) / sizeof( wxString );
+	m_rbScope = new wxRadioBox( this, wxID_ANY, _("Scope:"), wxDefaultPosition, wxDefaultSize, m_rbScopeNChoices, m_rbScopeChoices, 1, wxRA_SPECIFY_COLS );
+	m_rbScope->SetSelection( 0 );
+	m_contentSizer->Add( m_rbScope, 0, wxALL|wxEXPAND, 5 );
+
+	wxStaticBoxSizer* sbSizer2;
+	sbSizer2 = new wxStaticBoxSizer( new wxStaticBox( this, wxID_ANY, _("Numbering:") ), wxVERTICAL );
+
+	wxGridBagSizer* gbSizer1;
+	gbSizer1 = new wxGridBagSizer( 0, 0 );
+	gbSizer1->SetFlexibleDirection( wxBOTH );
+	gbSizer1->SetNonFlexibleGrowMode( wxFLEX_GROWMODE_SPECIFIED );
+
+	m_rbFirstFree = new wxRadioButton( sbSizer2->GetStaticBox(), wxID_FIRST_FREE, _("Use first free number after:"), wxDefaultPosition, wxDefaultSize, wxRB_GROUP );
+	gbSizer1->Add( m_rbFirstFree, wxGBPosition( 0, 0 ), wxGBSpan( 1, 1 ), wxTOP, 2 );
+
+	m_textNumberAfter = new wxTextCtrl( sbSizer2->GetStaticBox(), wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize( 60,-1 ), 0 );
+	gbSizer1->Add( m_textNumberAfter, wxGBPosition( 0, 1 ), wxGBSpan( 1, 1 ), wxLEFT, 2 );
+
+	m_rbSheetX100 = new wxRadioButton( sbSizer2->GetStaticBox(), wxID_SHEET_X_100, _("First free after sheet number X 100"), wxDefaultPosition, wxDefaultSize, 0 );
+	gbSizer1->Add( m_rbSheetX100, wxGBPosition( 1, 0 ), wxGBSpan( 1, 2 ), wxBOTTOM|wxTOP, 4 );
+
+	m_rbSheetX1000 = new wxRadioButton( sbSizer2->GetStaticBox(), wxID_SHEET_X_1000, _("First free after sheet number X 1000"), wxDefaultPosition, wxDefaultSize, 0 );
+	gbSizer1->Add( m_rbSheetX1000, wxGBPosition( 2, 0 ), wxGBSpan( 1, 2 ), wxBOTTOM|wxTOP, 2 );
+
+
+	sbSizer2->Add( gbSizer1, 1, wxEXPAND, 5 );
+
+
+	m_contentSizer->Add( sbSizer2, 1, wxALL|wxEXPAND, 5 );
+
+
+	bupperSizer->Add( m_contentSizer, 0, wxBOTTOM|wxEXPAND, 5 );
+
+
+	bmainSizer->Add( bupperSizer, 1, wxEXPAND|wxTOP|wxRIGHT|wxLEFT, 6 );
+
+
+	this->SetSizer( bmainSizer );
+	this->Layout();
+
+	// Connect Events
+	m_cbEnabled->Connect( wxEVT_COMMAND_CHECKBOX_CLICKED, wxCommandEventHandler( PANEL_EESCHEMA_ANNOTATION_OPTIONS_BASE::OnEnableClick ), NULL, this );
+}
+
+PANEL_EESCHEMA_ANNOTATION_OPTIONS_BASE::~PANEL_EESCHEMA_ANNOTATION_OPTIONS_BASE()
+{
+	// Disconnect Events
+	m_cbEnabled->Disconnect( wxEVT_COMMAND_CHECKBOX_CLICKED, wxCommandEventHandler( PANEL_EESCHEMA_ANNOTATION_OPTIONS_BASE::OnEnableClick ), NULL, this );
+
+}
diff --git a/eeschema/dialogs/panel_eeschema_annotation_options_base.fbp b/eeschema/dialogs/panel_eeschema_annotation_options_base.fbp
new file mode 100644
index 000000000..53c664cb8
--- /dev/null
+++ b/eeschema/dialogs/panel_eeschema_annotation_options_base.fbp
@@ -0,0 +1,579 @@
+<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
+<wxFormBuilder_Project>
+    <FileVersion major="1" minor="15" />
+    <object class="Project" expanded="1">
+        <property name="class_decoration"></property>
+        <property name="code_generation">C++</property>
+        <property name="disconnect_events">1</property>
+        <property name="disconnect_mode">source_name</property>
+        <property name="disconnect_php_events">0</property>
+        <property name="disconnect_python_events">0</property>
+        <property name="embedded_files_path">res</property>
+        <property name="encoding">UTF-8</property>
+        <property name="event_generation">connect</property>
+        <property name="file">panel_eeschema_annotation_options_base</property>
+        <property name="first_id">1000</property>
+        <property name="help_provider">none</property>
+        <property name="image_path_wrapper_function_name"></property>
+        <property name="indent_with_spaces"></property>
+        <property name="internationalize">1</property>
+        <property name="name">PanelEeschemaAnnotationOptionsBase</property>
+        <property name="namespace"></property>
+        <property name="path">.</property>
+        <property name="precompiled_header"></property>
+        <property name="relative_path">1</property>
+        <property name="skip_lua_events">1</property>
+        <property name="skip_php_events">1</property>
+        <property name="skip_python_events">1</property>
+        <property name="ui_table">UI</property>
+        <property name="use_array_enum">0</property>
+        <property name="use_enum">0</property>
+        <property name="use_microsoft_bom">0</property>
+        <object class="Panel" expanded="1">
+            <property name="aui_managed">0</property>
+            <property name="aui_manager_style">wxAUI_MGR_DEFAULT</property>
+            <property name="bg"></property>
+            <property name="context_help"></property>
+            <property name="context_menu">1</property>
+            <property name="enabled">1</property>
+            <property name="event_handler">impl_virtual</property>
+            <property name="fg"></property>
+            <property name="font"></property>
+            <property name="hidden">0</property>
+            <property name="id">wxID_ANY</property>
+            <property name="maximum_size"></property>
+            <property name="minimum_size"></property>
+            <property name="name">PANEL_EESCHEMA_ANNOTATION_OPTIONS_BASE</property>
+            <property name="pos"></property>
+            <property name="size">500,156</property>
+            <property name="subclass">; ; forward_declare</property>
+            <property name="tooltip"></property>
+            <property name="window_extra_style"></property>
+            <property name="window_name"></property>
+            <property name="window_style">wxTAB_TRAVERSAL</property>
+            <object class="wxBoxSizer" expanded="0">
+                <property name="minimum_size"></property>
+                <property name="name">bmainSizer</property>
+                <property name="orient">wxVERTICAL</property>
+                <property name="permission">none</property>
+                <object class="sizeritem" expanded="0">
+                    <property name="border">6</property>
+                    <property name="flag">wxEXPAND|wxTOP|wxRIGHT|wxLEFT</property>
+                    <property name="proportion">1</property>
+                    <object class="wxBoxSizer" expanded="0">
+                        <property name="minimum_size"></property>
+                        <property name="name">bupperSizer</property>
+                        <property name="orient">wxVERTICAL</property>
+                        <property name="permission">none</property>
+                        <object class="sizeritem" expanded="0">
+                            <property name="border">5</property>
+                            <property name="flag">wxALL</property>
+                            <property name="proportion">0</property>
+                            <object class="wxCheckBox" expanded="0">
+                                <property name="BottomDockable">1</property>
+                                <property name="LeftDockable">1</property>
+                                <property name="RightDockable">1</property>
+                                <property name="TopDockable">1</property>
+                                <property name="aui_layer"></property>
+                                <property name="aui_name"></property>
+                                <property name="aui_position"></property>
+                                <property name="aui_row"></property>
+                                <property name="best_size"></property>
+                                <property name="bg"></property>
+                                <property name="caption"></property>
+                                <property name="caption_visible">1</property>
+                                <property name="center_pane">0</property>
+                                <property name="checked">0</property>
+                                <property name="close_button">1</property>
+                                <property name="context_help"></property>
+                                <property name="context_menu">1</property>
+                                <property name="default_pane">0</property>
+                                <property name="dock">Dock</property>
+                                <property name="dock_fixed">0</property>
+                                <property name="docking">Left</property>
+                                <property name="enabled">1</property>
+                                <property name="fg"></property>
+                                <property name="floatable">1</property>
+                                <property name="font"></property>
+                                <property name="gripper">0</property>
+                                <property name="hidden">0</property>
+                                <property name="id">wxID_ANY</property>
+                                <property name="label">Annotate components on placement</property>
+                                <property name="max_size"></property>
+                                <property name="maximize_button">0</property>
+                                <property name="maximum_size"></property>
+                                <property name="min_size"></property>
+                                <property name="minimize_button">0</property>
+                                <property name="minimum_size"></property>
+                                <property name="moveable">1</property>
+                                <property name="name">m_cbEnabled</property>
+                                <property name="pane_border">1</property>
+                                <property name="pane_position"></property>
+                                <property name="pane_size"></property>
+                                <property name="permission">protected</property>
+                                <property name="pin_button">1</property>
+                                <property name="pos"></property>
+                                <property name="resize">Resizable</property>
+                                <property name="show">1</property>
+                                <property name="size"></property>
+                                <property name="style"></property>
+                                <property name="subclass">; ; forward_declare</property>
+                                <property name="toolbar_pane">0</property>
+                                <property name="tooltip"></property>
+                                <property name="validator_data_type"></property>
+                                <property name="validator_style">wxFILTER_NONE</property>
+                                <property name="validator_type">wxDefaultValidator</property>
+                                <property name="validator_variable"></property>
+                                <property name="window_extra_style"></property>
+                                <property name="window_name"></property>
+                                <property name="window_style"></property>
+                                <event name="OnCheckBox">OnEnableClick</event>
+                            </object>
+                        </object>
+                        <object class="sizeritem" expanded="0">
+                            <property name="border">5</property>
+                            <property name="flag">wxEXPAND | wxALL</property>
+                            <property name="proportion">0</property>
+                            <object class="wxStaticLine" expanded="0">
+                                <property name="BottomDockable">1</property>
+                                <property name="LeftDockable">1</property>
+                                <property name="RightDockable">1</property>
+                                <property name="TopDockable">1</property>
+                                <property name="aui_layer"></property>
+                                <property name="aui_name"></property>
+                                <property name="aui_position"></property>
+                                <property name="aui_row"></property>
+                                <property name="best_size"></property>
+                                <property name="bg"></property>
+                                <property name="caption"></property>
+                                <property name="caption_visible">1</property>
+                                <property name="center_pane">0</property>
+                                <property name="close_button">1</property>
+                                <property name="context_help"></property>
+                                <property name="context_menu">1</property>
+                                <property name="default_pane">0</property>
+                                <property name="dock">Dock</property>
+                                <property name="dock_fixed">0</property>
+                                <property name="docking">Left</property>
+                                <property name="enabled">1</property>
+                                <property name="fg"></property>
+                                <property name="floatable">1</property>
+                                <property name="font"></property>
+                                <property name="gripper">0</property>
+                                <property name="hidden">0</property>
+                                <property name="id">wxID_ANY</property>
+                                <property name="max_size"></property>
+                                <property name="maximize_button">0</property>
+                                <property name="maximum_size"></property>
+                                <property name="min_size"></property>
+                                <property name="minimize_button">0</property>
+                                <property name="minimum_size"></property>
+                                <property name="moveable">1</property>
+                                <property name="name">m_separator</property>
+                                <property name="pane_border">1</property>
+                                <property name="pane_position"></property>
+                                <property name="pane_size"></property>
+                                <property name="permission">protected</property>
+                                <property name="pin_button">1</property>
+                                <property name="pos"></property>
+                                <property name="resize">Resizable</property>
+                                <property name="show">1</property>
+                                <property name="size"></property>
+                                <property name="style">wxLI_HORIZONTAL</property>
+                                <property name="subclass">; ; forward_declare</property>
+                                <property name="toolbar_pane">0</property>
+                                <property name="tooltip"></property>
+                                <property name="window_extra_style"></property>
+                                <property name="window_name"></property>
+                                <property name="window_style"></property>
+                            </object>
+                        </object>
+                        <object class="sizeritem" expanded="0">
+                            <property name="border">5</property>
+                            <property name="flag">wxBOTTOM|wxEXPAND</property>
+                            <property name="proportion">0</property>
+                            <object class="wxFlexGridSizer" expanded="0">
+                                <property name="cols">2</property>
+                                <property name="flexible_direction">wxBOTH</property>
+                                <property name="growablecols">0,1</property>
+                                <property name="growablerows"></property>
+                                <property name="hgap">0</property>
+                                <property name="minimum_size"></property>
+                                <property name="name">m_contentSizer</property>
+                                <property name="non_flexible_grow_mode">wxFLEX_GROWMODE_SPECIFIED</property>
+                                <property name="permission">protected</property>
+                                <property name="rows">1</property>
+                                <property name="vgap">0</property>
+                                <object class="sizeritem" expanded="0">
+                                    <property name="border">5</property>
+                                    <property name="flag">wxALL|wxEXPAND</property>
+                                    <property name="proportion">0</property>
+                                    <object class="wxRadioBox" expanded="0">
+                                        <property name="BottomDockable">1</property>
+                                        <property name="LeftDockable">1</property>
+                                        <property name="RightDockable">1</property>
+                                        <property name="TopDockable">1</property>
+                                        <property name="aui_layer"></property>
+                                        <property name="aui_name"></property>
+                                        <property name="aui_position"></property>
+                                        <property name="aui_row"></property>
+                                        <property name="best_size"></property>
+                                        <property name="bg"></property>
+                                        <property name="caption"></property>
+                                        <property name="caption_visible">1</property>
+                                        <property name="center_pane">0</property>
+                                        <property name="choices">&quot;Use the entire schematic&quot; &quot;Use the current page only&quot;</property>
+                                        <property name="close_button">1</property>
+                                        <property name="context_help"></property>
+                                        <property name="context_menu">1</property>
+                                        <property name="default_pane">0</property>
+                                        <property name="dock">Dock</property>
+                                        <property name="dock_fixed">0</property>
+                                        <property name="docking">Left</property>
+                                        <property name="enabled">1</property>
+                                        <property name="fg"></property>
+                                        <property name="floatable">1</property>
+                                        <property name="font"></property>
+                                        <property name="gripper">0</property>
+                                        <property name="hidden">0</property>
+                                        <property name="id">wxID_ANY</property>
+                                        <property name="label">Scope:</property>
+                                        <property name="majorDimension">1</property>
+                                        <property name="max_size"></property>
+                                        <property name="maximize_button">0</property>
+                                        <property name="maximum_size"></property>
+                                        <property name="min_size"></property>
+                                        <property name="minimize_button">0</property>
+                                        <property name="minimum_size"></property>
+                                        <property name="moveable">1</property>
+                                        <property name="name">m_rbScope</property>
+                                        <property name="pane_border">1</property>
+                                        <property name="pane_position"></property>
+                                        <property name="pane_size"></property>
+                                        <property name="permission">protected</property>
+                                        <property name="pin_button">1</property>
+                                        <property name="pos"></property>
+                                        <property name="resize">Resizable</property>
+                                        <property name="selection">0</property>
+                                        <property name="show">1</property>
+                                        <property name="size"></property>
+                                        <property name="style">wxRA_SPECIFY_COLS</property>
+                                        <property name="subclass">; forward_declare</property>
+                                        <property name="toolbar_pane">0</property>
+                                        <property name="tooltip"></property>
+                                        <property name="validator_data_type"></property>
+                                        <property name="validator_style">wxFILTER_NONE</property>
+                                        <property name="validator_type">wxDefaultValidator</property>
+                                        <property name="validator_variable"></property>
+                                        <property name="window_extra_style"></property>
+                                        <property name="window_name"></property>
+                                        <property name="window_style"></property>
+                                    </object>
+                                </object>
+                                <object class="sizeritem" expanded="0">
+                                    <property name="border">5</property>
+                                    <property name="flag">wxALL|wxEXPAND</property>
+                                    <property name="proportion">1</property>
+                                    <object class="wxStaticBoxSizer" expanded="0">
+                                        <property name="id">wxID_ANY</property>
+                                        <property name="label">Numbering:</property>
+                                        <property name="minimum_size"></property>
+                                        <property name="name">sbSizer2</property>
+                                        <property name="orient">wxVERTICAL</property>
+                                        <property name="parent">1</property>
+                                        <property name="permission">none</property>
+                                        <object class="sizeritem" expanded="0">
+                                            <property name="border">5</property>
+                                            <property name="flag">wxEXPAND</property>
+                                            <property name="proportion">1</property>
+                                            <object class="wxGridBagSizer" expanded="0">
+                                                <property name="empty_cell_size"></property>
+                                                <property name="flexible_direction">wxBOTH</property>
+                                                <property name="growablecols"></property>
+                                                <property name="growablerows"></property>
+                                                <property name="hgap">0</property>
+                                                <property name="minimum_size"></property>
+                                                <property name="name">gbSizer1</property>
+                                                <property name="non_flexible_grow_mode">wxFLEX_GROWMODE_SPECIFIED</property>
+                                                <property name="permission">none</property>
+                                                <property name="vgap">0</property>
+                                                <object class="gbsizeritem" expanded="0">
+                                                    <property name="border">2</property>
+                                                    <property name="colspan">1</property>
+                                                    <property name="column">0</property>
+                                                    <property name="flag">wxTOP</property>
+                                                    <property name="row">0</property>
+                                                    <property name="rowspan">1</property>
+                                                    <object class="wxRadioButton" expanded="0">
+                                                        <property name="BottomDockable">1</property>
+                                                        <property name="LeftDockable">1</property>
+                                                        <property name="RightDockable">1</property>
+                                                        <property name="TopDockable">1</property>
+                                                        <property name="aui_layer"></property>
+                                                        <property name="aui_name"></property>
+                                                        <property name="aui_position"></property>
+                                                        <property name="aui_row"></property>
+                                                        <property name="best_size"></property>
+                                                        <property name="bg"></property>
+                                                        <property name="caption"></property>
+                                                        <property name="caption_visible">1</property>
+                                                        <property name="center_pane">0</property>
+                                                        <property name="close_button">1</property>
+                                                        <property name="context_help"></property>
+                                                        <property name="context_menu">1</property>
+                                                        <property name="default_pane">0</property>
+                                                        <property name="dock">Dock</property>
+                                                        <property name="dock_fixed">0</property>
+                                                        <property name="docking">Left</property>
+                                                        <property name="enabled">1</property>
+                                                        <property name="fg"></property>
+                                                        <property name="floatable">1</property>
+                                                        <property name="font"></property>
+                                                        <property name="gripper">0</property>
+                                                        <property name="hidden">0</property>
+                                                        <property name="id">wxID_FIRST_FREE</property>
+                                                        <property name="label">Use first free number after:</property>
+                                                        <property name="max_size"></property>
+                                                        <property name="maximize_button">0</property>
+                                                        <property name="maximum_size"></property>
+                                                        <property name="min_size"></property>
+                                                        <property name="minimize_button">0</property>
+                                                        <property name="minimum_size"></property>
+                                                        <property name="moveable">1</property>
+                                                        <property name="name">m_rbFirstFree</property>
+                                                        <property name="pane_border">1</property>
+                                                        <property name="pane_position"></property>
+                                                        <property name="pane_size"></property>
+                                                        <property name="permission">protected</property>
+                                                        <property name="pin_button">1</property>
+                                                        <property name="pos"></property>
+                                                        <property name="resize">Resizable</property>
+                                                        <property name="show">1</property>
+                                                        <property name="size"></property>
+                                                        <property name="style">wxRB_GROUP</property>
+                                                        <property name="subclass">; forward_declare</property>
+                                                        <property name="toolbar_pane">0</property>
+                                                        <property name="tooltip"></property>
+                                                        <property name="validator_data_type"></property>
+                                                        <property name="validator_style">wxFILTER_NONE</property>
+                                                        <property name="validator_type">wxDefaultValidator</property>
+                                                        <property name="validator_variable"></property>
+                                                        <property name="value">0</property>
+                                                        <property name="window_extra_style"></property>
+                                                        <property name="window_name"></property>
+                                                        <property name="window_style"></property>
+                                                    </object>
+                                                </object>
+                                                <object class="gbsizeritem" expanded="0">
+                                                    <property name="border">2</property>
+                                                    <property name="colspan">1</property>
+                                                    <property name="column">1</property>
+                                                    <property name="flag">wxLEFT</property>
+                                                    <property name="row">0</property>
+                                                    <property name="rowspan">1</property>
+                                                    <object class="wxTextCtrl" expanded="0">
+                                                        <property name="BottomDockable">1</property>
+                                                        <property name="LeftDockable">1</property>
+                                                        <property name="RightDockable">1</property>
+                                                        <property name="TopDockable">1</property>
+                                                        <property name="aui_layer"></property>
+                                                        <property name="aui_name"></property>
+                                                        <property name="aui_position"></property>
+                                                        <property name="aui_row"></property>
+                                                        <property name="best_size"></property>
+                                                        <property name="bg"></property>
+                                                        <property name="caption"></property>
+                                                        <property name="caption_visible">1</property>
+                                                        <property name="center_pane">0</property>
+                                                        <property name="close_button">1</property>
+                                                        <property name="context_help"></property>
+                                                        <property name="context_menu">1</property>
+                                                        <property name="default_pane">0</property>
+                                                        <property name="dock">Dock</property>
+                                                        <property name="dock_fixed">0</property>
+                                                        <property name="docking">Left</property>
+                                                        <property name="enabled">1</property>
+                                                        <property name="fg"></property>
+                                                        <property name="floatable">1</property>
+                                                        <property name="font"></property>
+                                                        <property name="gripper">0</property>
+                                                        <property name="hidden">0</property>
+                                                        <property name="id">wxID_ANY</property>
+                                                        <property name="max_size"></property>
+                                                        <property name="maximize_button">0</property>
+                                                        <property name="maximum_size"></property>
+                                                        <property name="maxlength"></property>
+                                                        <property name="min_size"></property>
+                                                        <property name="minimize_button">0</property>
+                                                        <property name="minimum_size"></property>
+                                                        <property name="moveable">1</property>
+                                                        <property name="name">m_textNumberAfter</property>
+                                                        <property name="pane_border">1</property>
+                                                        <property name="pane_position"></property>
+                                                        <property name="pane_size"></property>
+                                                        <property name="permission">protected</property>
+                                                        <property name="pin_button">1</property>
+                                                        <property name="pos"></property>
+                                                        <property name="resize">Resizable</property>
+                                                        <property name="show">1</property>
+                                                        <property name="size">60,-1</property>
+                                                        <property name="style"></property>
+                                                        <property name="subclass">; forward_declare</property>
+                                                        <property name="toolbar_pane">0</property>
+                                                        <property name="tooltip"></property>
+                                                        <property name="validator_data_type"></property>
+                                                        <property name="validator_style">wxFILTER_NONE</property>
+                                                        <property name="validator_type">wxDefaultValidator</property>
+                                                        <property name="validator_variable"></property>
+                                                        <property name="value"></property>
+                                                        <property name="window_extra_style"></property>
+                                                        <property name="window_name"></property>
+                                                        <property name="window_style"></property>
+                                                    </object>
+                                                </object>
+                                                <object class="gbsizeritem" expanded="0">
+                                                    <property name="border">4</property>
+                                                    <property name="colspan">2</property>
+                                                    <property name="column">0</property>
+                                                    <property name="flag">wxBOTTOM|wxTOP</property>
+                                                    <property name="row">1</property>
+                                                    <property name="rowspan">1</property>
+                                                    <object class="wxRadioButton" expanded="0">
+                                                        <property name="BottomDockable">1</property>
+                                                        <property name="LeftDockable">1</property>
+                                                        <property name="RightDockable">1</property>
+                                                        <property name="TopDockable">1</property>
+                                                        <property name="aui_layer"></property>
+                                                        <property name="aui_name"></property>
+                                                        <property name="aui_position"></property>
+                                                        <property name="aui_row"></property>
+                                                        <property name="best_size"></property>
+                                                        <property name="bg"></property>
+                                                        <property name="caption"></property>
+                                                        <property name="caption_visible">1</property>
+                                                        <property name="center_pane">0</property>
+                                                        <property name="close_button">1</property>
+                                                        <property name="context_help"></property>
+                                                        <property name="context_menu">1</property>
+                                                        <property name="default_pane">0</property>
+                                                        <property name="dock">Dock</property>
+                                                        <property name="dock_fixed">0</property>
+                                                        <property name="docking">Left</property>
+                                                        <property name="enabled">1</property>
+                                                        <property name="fg"></property>
+                                                        <property name="floatable">1</property>
+                                                        <property name="font"></property>
+                                                        <property name="gripper">0</property>
+                                                        <property name="hidden">0</property>
+                                                        <property name="id">wxID_SHEET_X_100</property>
+                                                        <property name="label">First free after sheet number X 100</property>
+                                                        <property name="max_size"></property>
+                                                        <property name="maximize_button">0</property>
+                                                        <property name="maximum_size"></property>
+                                                        <property name="min_size"></property>
+                                                        <property name="minimize_button">0</property>
+                                                        <property name="minimum_size"></property>
+                                                        <property name="moveable">1</property>
+                                                        <property name="name">m_rbSheetX100</property>
+                                                        <property name="pane_border">1</property>
+                                                        <property name="pane_position"></property>
+                                                        <property name="pane_size"></property>
+                                                        <property name="permission">protected</property>
+                                                        <property name="pin_button">1</property>
+                                                        <property name="pos"></property>
+                                                        <property name="resize">Resizable</property>
+                                                        <property name="show">1</property>
+                                                        <property name="size"></property>
+                                                        <property name="style"></property>
+                                                        <property name="subclass">; forward_declare</property>
+                                                        <property name="toolbar_pane">0</property>
+                                                        <property name="tooltip"></property>
+                                                        <property name="validator_data_type"></property>
+                                                        <property name="validator_style">wxFILTER_NONE</property>
+                                                        <property name="validator_type">wxDefaultValidator</property>
+                                                        <property name="validator_variable"></property>
+                                                        <property name="value">0</property>
+                                                        <property name="window_extra_style"></property>
+                                                        <property name="window_name"></property>
+                                                        <property name="window_style"></property>
+                                                    </object>
+                                                </object>
+                                                <object class="gbsizeritem" expanded="0">
+                                                    <property name="border">2</property>
+                                                    <property name="colspan">2</property>
+                                                    <property name="column">0</property>
+                                                    <property name="flag">wxBOTTOM|wxTOP</property>
+                                                    <property name="row">2</property>
+                                                    <property name="rowspan">1</property>
+                                                    <object class="wxRadioButton" expanded="0">
+                                                        <property name="BottomDockable">1</property>
+                                                        <property name="LeftDockable">1</property>
+                                                        <property name="RightDockable">1</property>
+                                                        <property name="TopDockable">1</property>
+                                                        <property name="aui_layer"></property>
+                                                        <property name="aui_name"></property>
+                                                        <property name="aui_position"></property>
+                                                        <property name="aui_row"></property>
+                                                        <property name="best_size"></property>
+                                                        <property name="bg"></property>
+                                                        <property name="caption"></property>
+                                                        <property name="caption_visible">1</property>
+                                                        <property name="center_pane">0</property>
+                                                        <property name="close_button">1</property>
+                                                        <property name="context_help"></property>
+                                                        <property name="context_menu">1</property>
+                                                        <property name="default_pane">0</property>
+                                                        <property name="dock">Dock</property>
+                                                        <property name="dock_fixed">0</property>
+                                                        <property name="docking">Left</property>
+                                                        <property name="enabled">1</property>
+                                                        <property name="fg"></property>
+                                                        <property name="floatable">1</property>
+                                                        <property name="font"></property>
+                                                        <property name="gripper">0</property>
+                                                        <property name="hidden">0</property>
+                                                        <property name="id">wxID_SHEET_X_1000</property>
+                                                        <property name="label">First free after sheet number X 1000</property>
+                                                        <property name="max_size"></property>
+                                                        <property name="maximize_button">0</property>
+                                                        <property name="maximum_size"></property>
+                                                        <property name="min_size"></property>
+                                                        <property name="minimize_button">0</property>
+                                                        <property name="minimum_size"></property>
+                                                        <property name="moveable">1</property>
+                                                        <property name="name">m_rbSheetX1000</property>
+                                                        <property name="pane_border">1</property>
+                                                        <property name="pane_position"></property>
+                                                        <property name="pane_size"></property>
+                                                        <property name="permission">protected</property>
+                                                        <property name="pin_button">1</property>
+                                                        <property name="pos"></property>
+                                                        <property name="resize">Resizable</property>
+                                                        <property name="show">1</property>
+                                                        <property name="size"></property>
+                                                        <property name="style"></property>
+                                                        <property name="subclass">; forward_declare</property>
+                                                        <property name="toolbar_pane">0</property>
+                                                        <property name="tooltip"></property>
+                                                        <property name="validator_data_type"></property>
+                                                        <property name="validator_style">wxFILTER_NONE</property>
+                                                        <property name="validator_type">wxDefaultValidator</property>
+                                                        <property name="validator_variable"></property>
+                                                        <property name="value">0</property>
+                                                        <property name="window_extra_style"></property>
+                                                        <property name="window_name"></property>
+                                                        <property name="window_style"></property>
+                                                    </object>
+                                                </object>
+                                            </object>
+                                        </object>
+                                    </object>
+                                </object>
+                            </object>
+                        </object>
+                    </object>
+                </object>
+            </object>
+        </object>
+    </object>
+</wxFormBuilder_Project>
diff --git a/eeschema/dialogs/panel_eeschema_annotation_options_base.h b/eeschema/dialogs/panel_eeschema_annotation_options_base.h
new file mode 100644
index 000000000..9d4e4a4dd
--- /dev/null
+++ b/eeschema/dialogs/panel_eeschema_annotation_options_base.h
@@ -0,0 +1,59 @@
+///////////////////////////////////////////////////////////////////////////
+// C++ code generated with wxFormBuilder (version Sep 10 2019)
+// http://www.wxformbuilder.org/
+//
+// PLEASE DO *NOT* EDIT THIS FILE!
+///////////////////////////////////////////////////////////////////////////
+
+#pragma once
+
+#include <wx/artprov.h>
+#include <wx/xrc/xmlres.h>
+#include <wx/intl.h>
+#include <wx/string.h>
+#include <wx/checkbox.h>
+#include <wx/gdicmn.h>
+#include <wx/font.h>
+#include <wx/colour.h>
+#include <wx/settings.h>
+#include <wx/statline.h>
+#include <wx/radiobox.h>
+#include <wx/radiobut.h>
+#include <wx/textctrl.h>
+#include <wx/gbsizer.h>
+#include <wx/sizer.h>
+#include <wx/statbox.h>
+#include <wx/panel.h>
+
+///////////////////////////////////////////////////////////////////////////
+
+#define wxID_FIRST_FREE 1000
+#define wxID_SHEET_X_100 1001
+#define wxID_SHEET_X_1000 1002
+
+///////////////////////////////////////////////////////////////////////////////
+/// Class PANEL_EESCHEMA_ANNOTATION_OPTIONS_BASE
+///////////////////////////////////////////////////////////////////////////////
+class PANEL_EESCHEMA_ANNOTATION_OPTIONS_BASE : public wxPanel
+{
+	private:
+
+	protected:
+		wxCheckBox* m_cbEnabled;
+		wxStaticLine* m_separator;
+		wxFlexGridSizer* m_contentSizer;
+		wxRadioBox* m_rbScope;
+		wxRadioButton* m_rbFirstFree;
+		wxTextCtrl* m_textNumberAfter;
+		wxRadioButton* m_rbSheetX100;
+		wxRadioButton* m_rbSheetX1000;
+
+		// Virtual event handlers, overide them in your derived class
+		virtual void OnEnableClick( wxCommandEvent& event ) { event.Skip(); }
+
+
+	public:
+
+		PANEL_EESCHEMA_ANNOTATION_OPTIONS_BASE( wxWindow* parent, wxWindowID id = wxID_ANY, const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxSize( 500,156 ), long style = wxTAB_TRAVERSAL, const wxString& name = wxEmptyString );
+		~PANEL_EESCHEMA_ANNOTATION_OPTIONS_BASE();
+};
diff --git a/eeschema/eeschema_config.cpp b/eeschema/eeschema_config.cpp
index 527a40262..9b1189056 100644
--- a/eeschema/eeschema_config.cpp
+++ b/eeschema/eeschema_config.cpp
@@ -43,6 +43,7 @@
 #include <widgets/widget_eeschema_color_config.h>
 #include <widgets/symbol_tree_pane.h>
 #include <dialogs/panel_libedit_settings.h>
+#include <dialogs/panel_eeschema_annotation_options.h>
 #include <sch_painter.h>
 #include "sch_junction.h"
 #include "eeschema_id.h"
@@ -126,6 +127,7 @@ void SCH_EDIT_FRAME::InstallPreferences( PAGED_DIALOG* aParent,
     book->AddSubPage( new PANEL_EESCHEMA_COLOR_CONFIG( this, book ), _( "Colors" ) );
     book->AddSubPage( new PANEL_EESCHEMA_TEMPLATE_FIELDNAMES( this, book ),
                       _( "Field Name Templates" ) );
+    book->AddSubPage( new PANEL_EESCHEMA_ANNOTATION_OPTIONS( book ), _( "Annotation Options" ) );
 
     aHotkeysPanel->AddHotKeys( GetToolManager() );
 }
@@ -153,6 +155,11 @@ PARAM_CFG_ARRAY& SCH_EDIT_FRAME::GetProjectFileParameters()
     m_projectFileParams.push_back( new PARAM_CFG_BOOL( wxT( "SpiceAjustPassiveValues" ),
                                             &m_spiceAjustPassiveValues, false ) );
 
+    m_projectFileParams.push_back( new PARAM_CFG_BOOL( wxT( "AutoAnnotationEnabled" ),     SCH_COMPONENT::AutoAnnotationEnabledPtr() ) );
+    m_projectFileParams.push_back( new PARAM_CFG_INT( wxT( "AutoAnnotationScope" ),        SCH_COMPONENT::AutoAnnotationScopeOptionPtr(), 0, 0, 1 ) );
+    m_projectFileParams.push_back( new PARAM_CFG_INT( wxT( "AutoAnnotationAlgo" ),   SCH_COMPONENT::AutoAnnotationAlgoOptionPtr(), 0, 0, 2 ) );
+    m_projectFileParams.push_back( new PARAM_CFG_INT( wxT( "AutoAnnotationFirstFreeNum" ), SCH_COMPONENT::AutoAnnotationFirstFreeNumOptionPtr(), 0 ) );
+ 
     m_projectFileParams.push_back( new PARAM_CFG_INT( wxT( "LabSize" ),
                                             &s_defaultTextSize, DEFAULT_SIZE_TEXT, 5, 1000 ) );
 
diff --git a/eeschema/sch_component.cpp b/eeschema/sch_component.cpp
index 2b7ac56bc..60fe95b6b 100644
--- a/eeschema/sch_component.cpp
+++ b/eeschema/sch_component.cpp
@@ -56,6 +56,10 @@
 
 #include <trace_helpers.h>
 
+bool SCH_COMPONENT::s_autoAnnotateEnabled;
+int  SCH_COMPONENT::s_autoAnnotateScopeOption;
+int  SCH_COMPONENT::s_autoAnnotateAlgoOption;
+int  SCH_COMPONENT::s_autoAnnotateFirstFreeNumOption;
 
 /**
  * Function toUTFTildaText
diff --git a/eeschema/sch_component.h b/eeschema/sch_component.h
index 9b2b77150..f0d20b8ee 100644
--- a/eeschema/sch_component.h
+++ b/eeschema/sch_component.h
@@ -102,6 +102,11 @@ private:
 
     bool        m_isInNetlist;  ///< True if the component should appear in the netlist
 
+    static bool s_autoAnnotateEnabled;
+    static int  s_autoAnnotateScopeOption;
+    static int  s_autoAnnotateAlgoOption;
+    static int  s_autoAnnotateFirstFreeNumOption;
+
     /**
      * Defines the hierarchical path and reference of the component.  This allows support
      * for hierarchical sheets that reference the same schematic.  The format for the path
@@ -648,6 +653,21 @@ public:
     bool HasHighlightedPins();
 
     void HighlightPin( LIB_PIN* aPin );
+    
+    static bool IsAutoAnnotationEnabled() { return s_autoAnnotateEnabled; }
+    static int GetAutoAnnotationScopeOption() { return s_autoAnnotateScopeOption; }
+    static int GetAutoAnnotationAlgoOption() { return s_autoAnnotateAlgoOption; }
+    static int GetAutoAnnotationFirstFreeNumOption() { return s_autoAnnotateFirstFreeNumOption; }
+
+    static void SetIsAutoAnnotationEnabled(bool value) { s_autoAnnotateEnabled = value; }
+    static void SetAutoAnnotationScopeOption(int value) { s_autoAnnotateScopeOption = value; }
+    static void SetAutoAnnotationAlgoOption(int value) { s_autoAnnotateAlgoOption = value; }
+    static void SetAutoAnnotationFirstFreeNumOption(int value) { s_autoAnnotateFirstFreeNumOption = value; }
+
+    static bool* AutoAnnotationEnabledPtr() { return &s_autoAnnotateEnabled; }
+    static int* AutoAnnotationScopeOptionPtr() { return &s_autoAnnotateScopeOption; }
+    static int* AutoAnnotationAlgoOptionPtr() { return &s_autoAnnotateAlgoOption; }
+    static int* AutoAnnotationFirstFreeNumOptionPtr() { return &s_autoAnnotateFirstFreeNumOption; }
 
 private:
     bool doIsConnected( const wxPoint& aPosition ) const override;
diff --git a/eeschema/tools/ee_selection.cpp b/eeschema/tools/ee_selection.cpp
index 84f418afb..1c44431cf 100644
--- a/eeschema/tools/ee_selection.cpp
+++ b/eeschema/tools/ee_selection.cpp
@@ -27,6 +27,8 @@
 #include <tools/ee_selection.h>
 #include <sch_item.h>
 #include <lib_item.h>
+#include <sch_component.h>
+#include <refdes_utils.h>
 
 EDA_ITEM* EE_SELECTION::GetTopLeftItem( bool onlyModules ) const
 {
@@ -56,3 +58,32 @@ EDA_ITEM* EE_SELECTION::GetTopLeftItem( bool onlyModules ) const
 
     return static_cast<EDA_ITEM*>( topLeftItem );
 }
+
+void EE_SELECTION::SortComponentsByRef()
+{
+    std::sort( begin(), end(), []( const EDA_ITEM* left, const EDA_ITEM* right ) 
+    {
+        if ( left->Type() == SCH_COMPONENT_T && right->Type() == SCH_COMPONENT_T )
+        {
+            SCH_COMPONENT* leftComponent  = (SCH_COMPONENT*) left;
+            SCH_COMPONENT* rightComponent = (SCH_COMPONENT*) right;
+            int ii;
+
+            ii = UTIL::RefDesStringCompare( leftComponent->GetRef( g_CurrentSheet ), rightComponent->GetRef( g_CurrentSheet ) );
+
+            if( ii == 0 )
+            {
+                ii = leftComponent->GetField( VALUE )->GetText().CmpNoCase( rightComponent->GetField( VALUE )->GetText() );
+            }
+
+            if( ii == 0 )
+            {
+                ii = leftComponent->GetUnit() - rightComponent->GetUnit();
+            }
+
+            return ii < 0;
+        }
+
+        return false;
+    });
+}
\ No newline at end of file
diff --git a/eeschema/tools/ee_selection.h b/eeschema/tools/ee_selection.h
index 3b8d2f59d..da4a71c9a 100644
--- a/eeschema/tools/ee_selection.h
+++ b/eeschema/tools/ee_selection.h
@@ -32,6 +32,11 @@ class EE_SELECTION : public SELECTION
 {
 public:
     EDA_ITEM* GetTopLeftItem( bool onlyModules = false ) const override;
+
+    /**
+     * Sorts this selection by reference number of each SCH_COMPONENT (if any are present).
+     */
+    void SortComponentsByRef();
 };
 
 #endif  //  EE_SELECTION_H
\ No newline at end of file
diff --git a/eeschema/tools/sch_edit_tool.cpp b/eeschema/tools/sch_edit_tool.cpp
index de975301b..85e0880d9 100644
--- a/eeschema/tools/sch_edit_tool.cpp
+++ b/eeschema/tools/sch_edit_tool.cpp
@@ -716,7 +716,9 @@ static KICAD_T duplicatableItems[] =
 
 int SCH_EDIT_TOOL::Duplicate( const TOOL_EVENT& aEvent )
 {
-    EE_SELECTION& selection = m_selectionTool->RequestSelection( duplicatableItems );
+    // Selection has to be copied here because `SortComponentsByRef` invalidates
+    // some iterators in another object.
+    EE_SELECTION selection = m_selectionTool->RequestSelection( duplicatableItems );
 
     if( selection.GetSize() == 0 )
         return 0;
@@ -732,6 +734,12 @@ int SCH_EDIT_TOOL::Duplicate( const TOOL_EVENT& aEvent )
     bool copiedSheets = false;
     SCH_SHEET_LIST initial_sheetpathList( g_RootSheet );
 
+    // TODO(nufflee): settings
+    if ( true ) 
+    {
+        selection.SortComponentsByRef();
+    }
+
     for( unsigned ii = 0; ii < selection.GetSize(); ++ii )
     {
         SCH_ITEM* oldItem = static_cast<SCH_ITEM*>( selection.GetItem( ii ) );
@@ -778,8 +786,6 @@ int SCH_EDIT_TOOL::Duplicate( const TOOL_EVENT& aEvent )
 
             component->SetTimeStamp( GetNewTimeStamp() );
 
-            // TODO(nufflee): sort components like when placed
-
             // TODO(nufflee): settings
             if ( true ) 
             {
diff --git a/eeschema/tools/sch_editor_control.cpp b/eeschema/tools/sch_editor_control.cpp
index 584c0e418..719b91592 100644
--- a/eeschema/tools/sch_editor_control.cpp
+++ b/eeschema/tools/sch_editor_control.cpp
@@ -872,6 +872,8 @@ int SCH_EDITOR_CONTROL::Redo( const TOOL_EVENT& aEvent )
 bool SCH_EDITOR_CONTROL::doCopy()
 {
     EE_SELECTION_TOOL* selTool = m_toolMgr->GetTool<EE_SELECTION_TOOL>();
+    // Selection has to be copied here because `SortComponentsByRef` invalidates
+    // some iterators in another object.
     EE_SELECTION       selection = selTool->RequestSelection();
 
     if( !selection.GetSize() )
@@ -894,35 +896,9 @@ bool SCH_EDITOR_CONTROL::doCopy()
     // TODO(nufflee): settings
     if ( true )
     {
-        std::sort( selection.begin(), selection.end(), []( const EDA_ITEM* left, const EDA_ITEM* right ) 
-        {
-            // TODO(nufflee): Possibly extract this from SCH_REFERENCE_LIST::sortByReferenceOnly
-            if ( left->Type() == SCH_COMPONENT_T && right->Type() == SCH_COMPONENT_T )
-            {
-                SCH_COMPONENT* leftComponent  = (SCH_COMPONENT*) left;
-                SCH_COMPONENT* rightComponent = (SCH_COMPONENT*) right;
-                int ii;
-
-                ii = UTIL::RefDesStringCompare( leftComponent->GetRef( g_CurrentSheet ), rightComponent->GetRef( g_CurrentSheet ) );
-
-                if( ii == 0 )
-                {
-                    ii = leftComponent->GetField( VALUE )->GetText().CmpNoCase( rightComponent->GetField( VALUE )->GetText() );
-                }
-
-                if( ii == 0 )
-                {
-                    ii = leftComponent->GetUnit() - rightComponent->GetUnit();
-                }
-
-                return ii < 0;
-            }
-
-            return false;
-        });
+        selection.SortComponentsByRef();
     }
 
-    // TODO(nufflee): Not a reference this function might be looking for?
     plugin.Format( &selection, &formatter );
 
     return m_toolMgr->SaveClipboard( formatter.GetString() );
-- 
2.20.1

From 9e73ad123999a9f9f949a22105ac01e365964a8b Mon Sep 17 00:00:00 2001
From: Nufflee <zifcani@xxxxxxxxx>
Date: Wed, 25 Sep 2019 20:56:02 +0200
Subject: [PATCH 08/16] Check for `IsAutoAnnotationEnabled` before enabling
 auto annotation.

---
 eeschema/tools/sch_drawing_tools.cpp  | 3 +--
 eeschema/tools/sch_edit_tool.cpp      | 6 ++----
 eeschema/tools/sch_editor_control.cpp | 9 +++------
 3 files changed, 6 insertions(+), 12 deletions(-)

diff --git a/eeschema/tools/sch_drawing_tools.cpp b/eeschema/tools/sch_drawing_tools.cpp
index b39ee999e..90e3e55e9 100644
--- a/eeschema/tools/sch_drawing_tools.cpp
+++ b/eeschema/tools/sch_drawing_tools.cpp
@@ -188,8 +188,7 @@ int SCH_DRAWING_TOOLS::PlaceComponent(  const TOOL_EVENT& aEvent  )
             }
             else
             {
-                // TODO(nufflee): settings
-                if ( true )
+                if ( SCH_COMPONENT::IsAutoAnnotationEnabled() )
                 {
                     component->Annotate( g_CurrentSheet );
                 }
diff --git a/eeschema/tools/sch_edit_tool.cpp b/eeschema/tools/sch_edit_tool.cpp
index 85e0880d9..41e52060a 100644
--- a/eeschema/tools/sch_edit_tool.cpp
+++ b/eeschema/tools/sch_edit_tool.cpp
@@ -734,8 +734,7 @@ int SCH_EDIT_TOOL::Duplicate( const TOOL_EVENT& aEvent )
     bool copiedSheets = false;
     SCH_SHEET_LIST initial_sheetpathList( g_RootSheet );
 
-    // TODO(nufflee): settings
-    if ( true ) 
+    if ( SCH_COMPONENT::IsAutoAnnotationEnabled() ) 
     {
         selection.SortComponentsByRef();
     }
@@ -786,8 +785,7 @@ int SCH_EDIT_TOOL::Duplicate( const TOOL_EVENT& aEvent )
 
             component->SetTimeStamp( GetNewTimeStamp() );
 
-            // TODO(nufflee): settings
-            if ( true ) 
+            if ( SCH_COMPONENT::IsAutoAnnotationEnabled() ) 
             {
                 component->Annotate( g_CurrentSheet );
             }
diff --git a/eeschema/tools/sch_editor_control.cpp b/eeschema/tools/sch_editor_control.cpp
index 719b91592..cc83a8367 100644
--- a/eeschema/tools/sch_editor_control.cpp
+++ b/eeschema/tools/sch_editor_control.cpp
@@ -893,8 +893,7 @@ bool SCH_EDITOR_CONTROL::doCopy()
     STRING_FORMATTER formatter;
     SCH_LEGACY_PLUGIN plugin;
 
-    // TODO(nufflee): settings
-    if ( true )
+    if ( SCH_COMPONENT::IsAutoAnnotationEnabled() )
     {
         selection.SortComponentsByRef();
     }
@@ -1022,8 +1021,7 @@ int SCH_EDITOR_CONTROL::Paste( const TOOL_EVENT& aEvent )
 
         if( item->Type() == SCH_COMPONENT_T )
         {
-            // TODO(nufflee): settings
-            if( !true && !dropAnnotations && !forceKeepAnnotations )
+            if( !SCH_COMPONENT::IsAutoAnnotationEnabled() && !dropAnnotations && !forceKeepAnnotations )
             {
                 for( SCH_ITEM* temp = dlist.GetFirst(); temp != lastExisting; temp = temp->Next() )
                 {
@@ -1076,8 +1074,7 @@ int SCH_EDITOR_CONTROL::Paste( const TOOL_EVENT& aEvent )
                 component->ClearAnnotation( nullptr );
                 component->SetUnit( unit );
             }
-            // TODO(nufflee): settings
-            else if ( true )
+            else if ( SCH_COMPONENT::IsAutoAnnotationEnabled() )
             {
                 component->Annotate( g_CurrentSheet );
             }
-- 
2.20.1

From f20d2a96485d4e61dfb4ee2d61d0bb282d5ba97e Mon Sep 17 00:00:00 2001
From: Nufflee <zifcani@xxxxxxxxx>
Date: Wed, 25 Sep 2019 20:16:49 +0200
Subject: [PATCH 07/16] Save Annotation Options to Project Settings.

---
 eeschema/dialogs/panel_eeschema_annotation_options.h | 6 +++++-
 eeschema/eeschema_config.cpp                         | 3 +--
 2 files changed, 6 insertions(+), 3 deletions(-)

diff --git a/eeschema/dialogs/panel_eeschema_annotation_options.h b/eeschema/dialogs/panel_eeschema_annotation_options.h
index 98b64c38d..507700977 100644
--- a/eeschema/dialogs/panel_eeschema_annotation_options.h
+++ b/eeschema/dialogs/panel_eeschema_annotation_options.h
@@ -2,11 +2,15 @@
 #define KICAD_PANEL_EESCHEMA_ANNOTATION_OPTIONS_H
 
 #include "panel_eeschema_annotation_options_base.h"
+#include <sch_edit_frame.h>
 
 class PANEL_EESCHEMA_ANNOTATION_OPTIONS : public PANEL_EESCHEMA_ANNOTATION_OPTIONS_BASE
 {
+private:
+    SCH_EDIT_FRAME* m_frame;
+
 public:
-    PANEL_EESCHEMA_ANNOTATION_OPTIONS( wxWindow* aWindow );
+    PANEL_EESCHEMA_ANNOTATION_OPTIONS( SCH_EDIT_FRAME* aFrame, wxWindow* aWindow );
 
     void OnEnableClick( wxCommandEvent& event ) override;
 
diff --git a/eeschema/eeschema_config.cpp b/eeschema/eeschema_config.cpp
index 9b1189056..da3719036 100644
--- a/eeschema/eeschema_config.cpp
+++ b/eeschema/eeschema_config.cpp
@@ -127,14 +127,13 @@ void SCH_EDIT_FRAME::InstallPreferences( PAGED_DIALOG* aParent,
     book->AddSubPage( new PANEL_EESCHEMA_COLOR_CONFIG( this, book ), _( "Colors" ) );
     book->AddSubPage( new PANEL_EESCHEMA_TEMPLATE_FIELDNAMES( this, book ),
                       _( "Field Name Templates" ) );
-    book->AddSubPage( new PANEL_EESCHEMA_ANNOTATION_OPTIONS( book ), _( "Annotation Options" ) );
+    book->AddSubPage( new PANEL_EESCHEMA_ANNOTATION_OPTIONS( this, book ), _( "Annotation Options" ) );
 
     aHotkeysPanel->AddHotKeys( GetToolManager() );
 }
 
 
 PARAM_CFG_ARRAY& SCH_EDIT_FRAME::GetProjectFileParameters()
-
 {
     if( !m_projectFileParams.empty() )
         return m_projectFileParams;
-- 
2.20.1

From c280175f4c2d787756577f9dbd168e717e2e8b7c Mon Sep 17 00:00:00 2001
From: Nufflee <zifcani@xxxxxxxxx>
Date: Sat, 28 Sep 2019 01:36:52 +0200
Subject: [PATCH 10/16] Fix duplicate timestamp issues when pasting.

---
 eeschema/tools/sch_editor_control.cpp | 19 ++++++++++---------
 1 file changed, 10 insertions(+), 9 deletions(-)

diff --git a/eeschema/tools/sch_editor_control.cpp b/eeschema/tools/sch_editor_control.cpp
index fa9a3eab7..10d8c9f19 100644
--- a/eeschema/tools/sch_editor_control.cpp
+++ b/eeschema/tools/sch_editor_control.cpp
@@ -1021,7 +1021,7 @@ int SCH_EDITOR_CONTROL::Paste( const TOOL_EVENT& aEvent )
 
         if( item->Type() == SCH_COMPONENT_T )
         {
-            if( !SCH_COMPONENT::IsAutoAnnotationEnabled() && !dropAnnotations && !forceKeepAnnotations )
+            if( !dropAnnotations && !forceKeepAnnotations )
             {
                 for( SCH_ITEM* temp = dlist.GetFirst(); temp != lastExisting; temp = temp->Next() )
                 {
@@ -1065,19 +1065,20 @@ int SCH_EDITOR_CONTROL::Paste( const TOOL_EVENT& aEvent )
         {
             SCH_COMPONENT* component = (SCH_COMPONENT*) item;
 
-            if( dropAnnotations )
+            if( dropAnnotations || SCH_COMPONENT::IsAutoAnnotationEnabled() )
             {
+                // preserve the selected unit
+                int unit = component->GetUnit();
+
+                if ( SCH_COMPONENT::IsAutoAnnotationEnabled() )
+                    component->Annotate( g_CurrentSheet, SCH_COMPONENT::GetAutoAnnotationScopeOption(), (ANNOTATE_OPTION_T) SCH_COMPONENT::GetAutoAnnotationAlgoOption() );
+                else
+                    component->ClearAnnotation( nullptr );
+
                 component->SetTimeStamp( GetNewTimeStamp() );
 
-                // clear the annotation, but preserve the selected unit
-                int unit = component->GetUnit();
-                component->ClearAnnotation( nullptr );
                 component->SetUnit( unit );
             }
-            else if ( SCH_COMPONENT::IsAutoAnnotationEnabled() )
-            {
-                component->Annotate( g_CurrentSheet, SCH_COMPONENT::GetAutoAnnotationScopeOption(), (ANNOTATE_OPTION_T) SCH_COMPONENT::GetAutoAnnotationAlgoOption() );
-            }
 
             component->Resolve( *symLibTable, partLib );
             component->UpdatePins();
-- 
2.20.1

From db3f65a1bc88dfd15993ac5e116d61d12f66ea5e Mon Sep 17 00:00:00 2001
From: Nufflee <zifcani@xxxxxxxxx>
Date: Fri, 27 Sep 2019 21:11:58 +0200
Subject: [PATCH 09/16] Implement scope and numbering options.

---
 eeschema/sch_component.cpp            | 41 +++++++++++++++++++++++----
 eeschema/sch_component.h              |  7 +++--
 eeschema/tools/sch_drawing_tools.cpp  |  2 +-
 eeschema/tools/sch_edit_tool.cpp      |  2 +-
 eeschema/tools/sch_editor_control.cpp |  2 +-
 5 files changed, 44 insertions(+), 10 deletions(-)

diff --git a/eeschema/sch_component.cpp b/eeschema/sch_component.cpp
index 60fe95b6b..4fd482646 100644
--- a/eeschema/sch_component.cpp
+++ b/eeschema/sch_component.cpp
@@ -723,14 +723,30 @@ void SCH_COMPONENT::SetRef( const SCH_SHEET_PATH* sheet, const wxString& ref )
     m_isInNetlist = ! ref.StartsWith( wxT( "#" ) );
 }
 
-void SCH_COMPONENT::Annotate( SCH_SHEET_PATH* aSheet )
+void SCH_COMPONENT::Annotate( SCH_SHEET_PATH* aSheet, bool aEntireSchematic, ANNOTATE_OPTION_T aAlgoOption )
 {
     SCH_REFERENCE_LIST references;
     wxString prefix = GetPrefix();
 
-    aSheet->GetComponents( references );
+    // Build the sheet list.
+    SCH_SHEET_LIST sheets( g_RootSheet );
 
-    int lastRefNum = 0;
+    if ( aEntireSchematic && aAlgoOption == INCREMENTAL_BY_REF )
+    {
+        sheets.GetComponents( references, false );
+    }
+    else
+    {
+        aSheet->GetComponents( references );
+    }
+
+    int lastRefNum = 1;
+    int interval = 0;
+    
+    if (aAlgoOption != INCREMENTAL_BY_REF)
+    {
+        interval = ( aAlgoOption == SHEET_NUMBER_X_100 ? 100 : 1000 ) * aSheet->GetPageNumber() + 1;
+    }
 
     references.SortByReferenceOnly();
 
@@ -747,7 +763,13 @@ void SCH_COMPONENT::Annotate( SCH_SHEET_PATH* aSheet )
                 continue;
             }
 
-            if ( refNum - lastRefNum > 1 )
+            if ( refNum < interval )
+            {
+                continue;
+            }
+
+            // Check for a gap.
+            if ( ( refNum - interval ) - lastRefNum > 1 )
             {
                 break;
             }
@@ -759,7 +781,16 @@ void SCH_COMPONENT::Annotate( SCH_SHEET_PATH* aSheet )
         }
     }
 
-    SetRef( aSheet, prefix << ( lastRefNum + 1 ) );
+    if (lastRefNum < interval)
+    {
+        lastRefNum = interval;
+    }
+    else
+    {
+        lastRefNum++;
+    }
+
+    SetRef( aSheet, prefix << lastRefNum );
 }
 
 bool SCH_COMPONENT::IsAnnotated( const SCH_SHEET_PATH* aSheet )
diff --git a/eeschema/sch_component.h b/eeschema/sch_component.h
index f0d20b8ee..ed823e111 100644
--- a/eeschema/sch_component.h
+++ b/eeschema/sch_component.h
@@ -37,6 +37,7 @@
 #include <lib_item.h>
 #include <sch_pin.h>
 #include <sch_base_frame.h>
+#include <sch_edit_frame.h>
 
 class SCH_SCREEN;
 class SCH_SHEET_PATH;
@@ -515,9 +516,11 @@ public:
 
     /**
      * Annotates the component with first available reference number for its reference prefix on a given sheet.
-     * @param aSheet is the sheet path to annotate component on
+     * @param aSheet is the sheet path to annotate the symbol on
+     * @param aEntireSchematic defines whether the scope of annotation is just the current page or the whole schematic 
+     * @param aAlgoOption is the algorithm option used to annotate the symbol
      */
-    void Annotate( SCH_SHEET_PATH* aSheet );
+    void Annotate( SCH_SHEET_PATH* aSheet, bool aEntireSchematic, ANNOTATE_OPTION_T aAlgoOption );
 
     /**
      * Checks if the component has a valid annotation (reference) for the given sheet path
diff --git a/eeschema/tools/sch_drawing_tools.cpp b/eeschema/tools/sch_drawing_tools.cpp
index 90e3e55e9..b88de1907 100644
--- a/eeschema/tools/sch_drawing_tools.cpp
+++ b/eeschema/tools/sch_drawing_tools.cpp
@@ -190,7 +190,7 @@ int SCH_DRAWING_TOOLS::PlaceComponent(  const TOOL_EVENT& aEvent  )
             {
                 if ( SCH_COMPONENT::IsAutoAnnotationEnabled() )
                 {
-                    component->Annotate( g_CurrentSheet );
+                    component->Annotate( g_CurrentSheet, SCH_COMPONENT::GetAutoAnnotationScopeOption(), (ANNOTATE_OPTION_T) SCH_COMPONENT::GetAutoAnnotationAlgoOption() );
                 }
 
                 m_frame->AddItemToScreenAndUndoList( component );
diff --git a/eeschema/tools/sch_edit_tool.cpp b/eeschema/tools/sch_edit_tool.cpp
index 41e52060a..c3dc45b70 100644
--- a/eeschema/tools/sch_edit_tool.cpp
+++ b/eeschema/tools/sch_edit_tool.cpp
@@ -787,7 +787,7 @@ int SCH_EDIT_TOOL::Duplicate( const TOOL_EVENT& aEvent )
 
             if ( SCH_COMPONENT::IsAutoAnnotationEnabled() ) 
             {
-                component->Annotate( g_CurrentSheet );
+                component->Annotate( g_CurrentSheet, SCH_COMPONENT::GetAutoAnnotationScopeOption(), (ANNOTATE_OPTION_T) SCH_COMPONENT::GetAutoAnnotationAlgoOption() );
             }
             else
             {
diff --git a/eeschema/tools/sch_editor_control.cpp b/eeschema/tools/sch_editor_control.cpp
index cc83a8367..fa9a3eab7 100644
--- a/eeschema/tools/sch_editor_control.cpp
+++ b/eeschema/tools/sch_editor_control.cpp
@@ -1076,7 +1076,7 @@ int SCH_EDITOR_CONTROL::Paste( const TOOL_EVENT& aEvent )
             }
             else if ( SCH_COMPONENT::IsAutoAnnotationEnabled() )
             {
-                component->Annotate( g_CurrentSheet );
+                component->Annotate( g_CurrentSheet, SCH_COMPONENT::GetAutoAnnotationScopeOption(), (ANNOTATE_OPTION_T) SCH_COMPONENT::GetAutoAnnotationAlgoOption() );
             }
 
             component->Resolve( *symLibTable, partLib );
-- 
2.20.1

From 16e73d5a29a47fa8f2d558765c22dfb1e4b442c9 Mon Sep 17 00:00:00 2001
From: Nufflee <zifcani@xxxxxxxxx>
Date: Sun, 29 Sep 2019 22:27:46 +0200
Subject: [PATCH 11/16] Fix off-by-one error when auto annotating components
 incrementally.

Add missing copyright notices.


Minor: codestyle.


Fix trailing whitespaces.


Change copyright to just 2019.
---
 .../panel_eeschema_annotation_options.h       | 23 +++++++++++++++++++
 eeschema/sch_component.cpp                    | 18 ++++++++-------
 eeschema/sch_component.h                      |  4 ++--
 eeschema/tools/ee_selection.cpp               |  2 +-
 eeschema/tools/sch_edit_tool.cpp              |  4 ++--
 5 files changed, 38 insertions(+), 13 deletions(-)

diff --git a/eeschema/dialogs/panel_eeschema_annotation_options.h b/eeschema/dialogs/panel_eeschema_annotation_options.h
index 507700977..46a7203b4 100644
--- a/eeschema/dialogs/panel_eeschema_annotation_options.h
+++ b/eeschema/dialogs/panel_eeschema_annotation_options.h
@@ -1,3 +1,26 @@
+/*
+ * This program source code file is part of KiCad, a free EDA CAD application.
+ *
+ * Copyright (C) 2019 Kicad Developers, see AUTHORS.txt for contributors.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, you may find one here:
+ * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
+ * or you may search the http://www.gnu.org website for the version 2 license,
+ * or you may write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
+ */
+
 #ifndef KICAD_PANEL_EESCHEMA_ANNOTATION_OPTIONS_H
 #define KICAD_PANEL_EESCHEMA_ANNOTATION_OPTIONS_H
 
diff --git a/eeschema/sch_component.cpp b/eeschema/sch_component.cpp
index 4fd482646..19edaed8e 100644
--- a/eeschema/sch_component.cpp
+++ b/eeschema/sch_component.cpp
@@ -740,21 +740,23 @@ void SCH_COMPONENT::Annotate( SCH_SHEET_PATH* aSheet, bool aEntireSchematic, ANN
         aSheet->GetComponents( references );
     }
 
-    int lastRefNum = 1;
+    int lastRefNum = 0;
     int interval = 0;
-    
-    if (aAlgoOption != INCREMENTAL_BY_REF)
+
+    if ( aAlgoOption != INCREMENTAL_BY_REF )
     {
         interval = ( aAlgoOption == SHEET_NUMBER_X_100 ? 100 : 1000 ) * aSheet->GetPageNumber() + 1;
+
+        lastRefNum++;
     }
 
     references.SortByReferenceOnly();
 
-    for ( unsigned i = 0; i < references.GetCount(); i++ ) 
+    for ( unsigned i = 0; i < references.GetCount(); i++ )
     {
-        if ( references[i].GetComp()->GetPrefix() == prefix ) 
+        if ( references[i].GetComp()->GetPrefix() == prefix )
         {
-            long refNum = -1;
+            long refNum;
 
             references[i].Split();
 
@@ -774,14 +776,14 @@ void SCH_COMPONENT::Annotate( SCH_SHEET_PATH* aSheet, bool aEntireSchematic, ANN
                 break;
             }
 
-            if ( refNum > lastRefNum ) 
+            if ( refNum > lastRefNum )
             {
                 lastRefNum = refNum;
             }
         }
     }
 
-    if (lastRefNum < interval)
+    if ( lastRefNum < interval )
     {
         lastRefNum = interval;
     }
diff --git a/eeschema/sch_component.h b/eeschema/sch_component.h
index ed823e111..bfd071ead 100644
--- a/eeschema/sch_component.h
+++ b/eeschema/sch_component.h
@@ -517,7 +517,7 @@ public:
     /**
      * Annotates the component with first available reference number for its reference prefix on a given sheet.
      * @param aSheet is the sheet path to annotate the symbol on
-     * @param aEntireSchematic defines whether the scope of annotation is just the current page or the whole schematic 
+     * @param aEntireSchematic defines whether the scope of annotation is just the current page or the whole schematic
      * @param aAlgoOption is the algorithm option used to annotate the symbol
      */
     void Annotate( SCH_SHEET_PATH* aSheet, bool aEntireSchematic, ANNOTATE_OPTION_T aAlgoOption );
@@ -656,7 +656,7 @@ public:
     bool HasHighlightedPins();
 
     void HighlightPin( LIB_PIN* aPin );
-    
+
     static bool IsAutoAnnotationEnabled() { return s_autoAnnotateEnabled; }
     static int GetAutoAnnotationScopeOption() { return s_autoAnnotateScopeOption; }
     static int GetAutoAnnotationAlgoOption() { return s_autoAnnotateAlgoOption; }
diff --git a/eeschema/tools/ee_selection.cpp b/eeschema/tools/ee_selection.cpp
index 1c44431cf..1c3054657 100644
--- a/eeschema/tools/ee_selection.cpp
+++ b/eeschema/tools/ee_selection.cpp
@@ -61,7 +61,7 @@ EDA_ITEM* EE_SELECTION::GetTopLeftItem( bool onlyModules ) const
 
 void EE_SELECTION::SortComponentsByRef()
 {
-    std::sort( begin(), end(), []( const EDA_ITEM* left, const EDA_ITEM* right ) 
+    std::sort( begin(), end(), []( const EDA_ITEM* left, const EDA_ITEM* right )
     {
         if ( left->Type() == SCH_COMPONENT_T && right->Type() == SCH_COMPONENT_T )
         {
diff --git a/eeschema/tools/sch_edit_tool.cpp b/eeschema/tools/sch_edit_tool.cpp
index c3dc45b70..abd327d0f 100644
--- a/eeschema/tools/sch_edit_tool.cpp
+++ b/eeschema/tools/sch_edit_tool.cpp
@@ -734,7 +734,7 @@ int SCH_EDIT_TOOL::Duplicate( const TOOL_EVENT& aEvent )
     bool copiedSheets = false;
     SCH_SHEET_LIST initial_sheetpathList( g_RootSheet );
 
-    if ( SCH_COMPONENT::IsAutoAnnotationEnabled() ) 
+    if ( SCH_COMPONENT::IsAutoAnnotationEnabled() )
     {
         selection.SortComponentsByRef();
     }
@@ -785,7 +785,7 @@ int SCH_EDIT_TOOL::Duplicate( const TOOL_EVENT& aEvent )
 
             component->SetTimeStamp( GetNewTimeStamp() );
 
-            if ( SCH_COMPONENT::IsAutoAnnotationEnabled() ) 
+            if ( SCH_COMPONENT::IsAutoAnnotationEnabled() )
             {
                 component->Annotate( g_CurrentSheet, SCH_COMPONENT::GetAutoAnnotationScopeOption(), (ANNOTATE_OPTION_T) SCH_COMPONENT::GetAutoAnnotationAlgoOption() );
             }
-- 
2.20.1

From c52dfad1a575961826f29a7126d37a1494c1f8d2 Mon Sep 17 00:00:00 2001
From: Nufflee <zifcani@xxxxxxxxx>
Date: Fri, 25 Oct 2019 20:40:18 +0200
Subject: [PATCH 15/16] Add QtCreator project files to .gitignore.

---
 .gitignore | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/.gitignore b/.gitignore
index f62e05e15..ad5fad1c9 100644
--- a/.gitignore
+++ b/.gitignore
@@ -26,7 +26,7 @@ pcb_calculator/attenuators/bridget_tee_formula.h
 pcb_calculator/attenuators/pi_formula.h
 pcb_calculator/attenuators/splitter_formula.h
 pcb_calculator/attenuators/tee_formula.h
-pcb_calculator/tracks_width_versus_current_formula.h
+pcb_calculator/tracks_width_versus_current_formula.h
 Makefile
 CMakeCache.txt
 auto_renamed_to_cpp
@@ -88,6 +88,12 @@ pcbnew/Info.plist
 *.gch
 *.orig
 *.patch
+kicad.cflags
+kicad.config
+kicad.creator*
+kicad.cxxflags
+kicad.files
+kicad.includes
 
 # These CMake files are wanted
 !CMakeModules/*.cmake
-- 
2.20.1

From 46808cd87b3ab0d30f279addc7da9129ad37bf5e Mon Sep 17 00:00:00 2001
From: Nufflee <zifcani@xxxxxxxxx>
Date: Sun, 29 Sep 2019 21:16:36 +0200
Subject: [PATCH 12/16] Move auto annotate options from SCH_COMPONENT to
 SCH_EDIT_FRAME.

---
 .../panel_eeschema_annotation_options.cpp     | 12 +++++------
 eeschema/eeschema_config.cpp                  | 10 +++++-----
 eeschema/sch_component.cpp                    |  5 -----
 eeschema/sch_component.h                      | 20 -------------------
 eeschema/sch_edit_frame.h                     | 17 ++++++++++++++++
 eeschema/tools/sch_drawing_tools.cpp          |  6 +++---
 eeschema/tools/sch_edit_tool.cpp              |  6 +++---
 eeschema/tools/sch_editor_control.cpp         |  8 ++++----
 8 files changed, 38 insertions(+), 46 deletions(-)

diff --git a/eeschema/dialogs/panel_eeschema_annotation_options.cpp b/eeschema/dialogs/panel_eeschema_annotation_options.cpp
index 46e391ab1..fae709b6a 100644
--- a/eeschema/dialogs/panel_eeschema_annotation_options.cpp
+++ b/eeschema/dialogs/panel_eeschema_annotation_options.cpp
@@ -38,9 +38,9 @@ bool PANEL_EESCHEMA_ANNOTATION_OPTIONS::TransferDataToWindow()
 
     SetContentEnabled(enabled);
 
-    m_rbScope->SetSelection( SCH_COMPONENT::GetAutoAnnotationScopeOption() );
-    
-    switch ( SCH_COMPONENT::GetAutoAnnotationAlgoOption() )
+    m_rbScope->SetSelection( m_frame->GetAutoAnnotationScopeOption() );
+
+    switch ( m_frame->GetAutoAnnotationAlgoOption() )
     {
         case INCREMENTAL_BY_REF:  m_rbFirstFree->SetValue( true );  break;
         case SHEET_NUMBER_X_100:  m_rbSheetX100->SetValue( true );  break;
@@ -60,15 +60,15 @@ bool PANEL_EESCHEMA_ANNOTATION_OPTIONS::TransferDataFromWindow()
 
     if ( m_rbFirstFree->GetValue() )
     {
-        SCH_COMPONENT::SetAutoAnnotationAlgoOption( 0 );
+        m_frame->SetAutoAnnotationAlgoOption( INCREMENTAL_BY_REF );
     }
     else if ( m_rbSheetX100->GetValue() )
     {
-        SCH_COMPONENT::SetAutoAnnotationAlgoOption( 1 );
+        m_frame->SetAutoAnnotationAlgoOption( SHEET_NUMBER_X_100 );
     }
     else if ( m_rbSheetX1000->GetValue() )
     {
-        SCH_COMPONENT::SetAutoAnnotationAlgoOption( 2 );
+        m_frame->SetAutoAnnotationAlgoOption( SHEET_NUMBER_X_1000 );
     }
 
     long numberAfter;
diff --git a/eeschema/eeschema_config.cpp b/eeschema/eeschema_config.cpp
index da3719036..316643fe5 100644
--- a/eeschema/eeschema_config.cpp
+++ b/eeschema/eeschema_config.cpp
@@ -154,11 +154,11 @@ PARAM_CFG_ARRAY& SCH_EDIT_FRAME::GetProjectFileParameters()
     m_projectFileParams.push_back( new PARAM_CFG_BOOL( wxT( "SpiceAjustPassiveValues" ),
                                             &m_spiceAjustPassiveValues, false ) );
 
-    m_projectFileParams.push_back( new PARAM_CFG_BOOL( wxT( "AutoAnnotationEnabled" ),     SCH_COMPONENT::AutoAnnotationEnabledPtr() ) );
-    m_projectFileParams.push_back( new PARAM_CFG_INT( wxT( "AutoAnnotationScope" ),        SCH_COMPONENT::AutoAnnotationScopeOptionPtr(), 0, 0, 1 ) );
-    m_projectFileParams.push_back( new PARAM_CFG_INT( wxT( "AutoAnnotationAlgo" ),   SCH_COMPONENT::AutoAnnotationAlgoOptionPtr(), 0, 0, 2 ) );
-    m_projectFileParams.push_back( new PARAM_CFG_INT( wxT( "AutoAnnotationFirstFreeNum" ), SCH_COMPONENT::AutoAnnotationFirstFreeNumOptionPtr(), 0 ) );
- 
+    m_projectFileParams.push_back( new PARAM_CFG_BOOL( wxT( "AutoAnnotationEnabled" ),      &m_autoAnnotateEnabled ) );
+    m_projectFileParams.push_back( new PARAM_CFG_INT(  wxT( "AutoAnnotationScope" ),        &m_autoAnnotateScopeOption, 0, 0, 1 ) );
+    m_projectFileParams.push_back( new PARAM_CFG_INT(  wxT( "AutoAnnotationAlgo" ),         (int*)&m_autoAnnotateAlgoOption, 0, 0, 2 ) );
+    m_projectFileParams.push_back( new PARAM_CFG_INT(  wxT( "AutoAnnotationFirstFreeNum" ), &m_autoAnnotateFirstFreeNumOption, 0 ) );
+
     m_projectFileParams.push_back( new PARAM_CFG_INT( wxT( "LabSize" ),
                                             &s_defaultTextSize, DEFAULT_SIZE_TEXT, 5, 1000 ) );
 
diff --git a/eeschema/sch_component.cpp b/eeschema/sch_component.cpp
index 19edaed8e..3e1f78beb 100644
--- a/eeschema/sch_component.cpp
+++ b/eeschema/sch_component.cpp
@@ -56,11 +56,6 @@
 
 #include <trace_helpers.h>
 
-bool SCH_COMPONENT::s_autoAnnotateEnabled;
-int  SCH_COMPONENT::s_autoAnnotateScopeOption;
-int  SCH_COMPONENT::s_autoAnnotateAlgoOption;
-int  SCH_COMPONENT::s_autoAnnotateFirstFreeNumOption;
-
 /**
  * Function toUTFTildaText
  * convert a wxString to UTF8 and replace any control characters with a ~,
diff --git a/eeschema/sch_component.h b/eeschema/sch_component.h
index bfd071ead..8777e24b0 100644
--- a/eeschema/sch_component.h
+++ b/eeschema/sch_component.h
@@ -103,11 +103,6 @@ private:
 
     bool        m_isInNetlist;  ///< True if the component should appear in the netlist
 
-    static bool s_autoAnnotateEnabled;
-    static int  s_autoAnnotateScopeOption;
-    static int  s_autoAnnotateAlgoOption;
-    static int  s_autoAnnotateFirstFreeNumOption;
-
     /**
      * Defines the hierarchical path and reference of the component.  This allows support
      * for hierarchical sheets that reference the same schematic.  The format for the path
@@ -657,21 +652,6 @@ public:
 
     void HighlightPin( LIB_PIN* aPin );
 
-    static bool IsAutoAnnotationEnabled() { return s_autoAnnotateEnabled; }
-    static int GetAutoAnnotationScopeOption() { return s_autoAnnotateScopeOption; }
-    static int GetAutoAnnotationAlgoOption() { return s_autoAnnotateAlgoOption; }
-    static int GetAutoAnnotationFirstFreeNumOption() { return s_autoAnnotateFirstFreeNumOption; }
-
-    static void SetIsAutoAnnotationEnabled(bool value) { s_autoAnnotateEnabled = value; }
-    static void SetAutoAnnotationScopeOption(int value) { s_autoAnnotateScopeOption = value; }
-    static void SetAutoAnnotationAlgoOption(int value) { s_autoAnnotateAlgoOption = value; }
-    static void SetAutoAnnotationFirstFreeNumOption(int value) { s_autoAnnotateFirstFreeNumOption = value; }
-
-    static bool* AutoAnnotationEnabledPtr() { return &s_autoAnnotateEnabled; }
-    static int* AutoAnnotationScopeOptionPtr() { return &s_autoAnnotateScopeOption; }
-    static int* AutoAnnotationAlgoOptionPtr() { return &s_autoAnnotateAlgoOption; }
-    static int* AutoAnnotationFirstFreeNumOptionPtr() { return &s_autoAnnotateFirstFreeNumOption; }
-
 private:
     bool doIsConnected( const wxPoint& aPosition ) const override;
 };
diff --git a/eeschema/sch_edit_frame.h b/eeschema/sch_edit_frame.h
index c7982050c..761abbc18 100644
--- a/eeschema/sch_edit_frame.h
+++ b/eeschema/sch_edit_frame.h
@@ -103,6 +103,11 @@ private:
     wxString                m_DefaultSchematicFileName;
     wxString                m_SelectedNetName;
 
+    bool                    m_autoAnnotateEnabled;
+    int                     m_autoAnnotateScopeOption;
+    ANNOTATE_OPTION_T       m_autoAnnotateAlgoOption;
+    int                     m_autoAnnotateFirstFreeNumOption;
+
     PARAM_CFG_ARRAY         m_projectFileParams;
     PARAM_CFG_ARRAY         m_configSettings;
     ERC_SETTINGS            m_ercSettings;
@@ -214,6 +219,18 @@ public:
     bool GetSpiceAjustPassiveValues() const { return m_spiceAjustPassiveValues; }
     void SetSpiceAdjustPassiveValues( bool aEnable ) { m_spiceAjustPassiveValues = aEnable; }
 
+    bool IsAutoAnnotationEnabled() { return m_autoAnnotateEnabled; }
+    void SetIsAutoAnnotationEnabled(bool value) { m_autoAnnotateEnabled = value; }
+
+    int GetAutoAnnotationScopeOption() { return m_autoAnnotateScopeOption; }
+    void SetAutoAnnotationScopeOption(int value) { m_autoAnnotateScopeOption = value; }
+
+    ANNOTATE_OPTION_T GetAutoAnnotationAlgoOption() { return m_autoAnnotateAlgoOption; }
+    void SetAutoAnnotationAlgoOption(ANNOTATE_OPTION_T value) { m_autoAnnotateAlgoOption = value; }
+
+    int GetAutoAnnotationFirstFreeNumOption() { return m_autoAnnotateFirstFreeNumOption; }
+    void SetAutoAnnotationFirstFreeNumOption(int value) { m_autoAnnotateFirstFreeNumOption = value; }
+
     /// accessor to the destination directory to use when generating plot files.
     const wxString& GetPlotDirectoryName() const { return m_plotDirectoryName; }
     void SetPlotDirectoryName( const wxString& aDirName ) { m_plotDirectoryName = aDirName; }
diff --git a/eeschema/tools/sch_drawing_tools.cpp b/eeschema/tools/sch_drawing_tools.cpp
index b88de1907..b3d8234fc 100644
--- a/eeschema/tools/sch_drawing_tools.cpp
+++ b/eeschema/tools/sch_drawing_tools.cpp
@@ -188,9 +188,9 @@ int SCH_DRAWING_TOOLS::PlaceComponent(  const TOOL_EVENT& aEvent  )
             }
             else
             {
-                if ( SCH_COMPONENT::IsAutoAnnotationEnabled() )
+                if ( m_frame->IsAutoAnnotationEnabled() )
                 {
-                    component->Annotate( g_CurrentSheet, SCH_COMPONENT::GetAutoAnnotationScopeOption(), (ANNOTATE_OPTION_T) SCH_COMPONENT::GetAutoAnnotationAlgoOption() );
+                    component->Annotate( g_CurrentSheet, m_frame->GetAutoAnnotationScopeOption(), m_frame->GetAutoAnnotationAlgoOption() );
                 }
 
                 m_frame->AddItemToScreenAndUndoList( component );
@@ -791,7 +791,7 @@ int SCH_DRAWING_TOOLS::DrawSheet( const TOOL_EVENT& aEvent )
                 m_frame->AddItemToScreenAndUndoList( sheet );
                 m_selectionTool->AddItemToSel( sheet );
             }
-            else 
+            else
             {
                 delete sheet;
             }
diff --git a/eeschema/tools/sch_edit_tool.cpp b/eeschema/tools/sch_edit_tool.cpp
index abd327d0f..498bc46eb 100644
--- a/eeschema/tools/sch_edit_tool.cpp
+++ b/eeschema/tools/sch_edit_tool.cpp
@@ -734,7 +734,7 @@ int SCH_EDIT_TOOL::Duplicate( const TOOL_EVENT& aEvent )
     bool copiedSheets = false;
     SCH_SHEET_LIST initial_sheetpathList( g_RootSheet );
 
-    if ( SCH_COMPONENT::IsAutoAnnotationEnabled() )
+    if ( m_frame->IsAutoAnnotationEnabled() )
     {
         selection.SortComponentsByRef();
     }
@@ -785,9 +785,9 @@ int SCH_EDIT_TOOL::Duplicate( const TOOL_EVENT& aEvent )
 
             component->SetTimeStamp( GetNewTimeStamp() );
 
-            if ( SCH_COMPONENT::IsAutoAnnotationEnabled() )
+            if ( m_frame->IsAutoAnnotationEnabled() )
             {
-                component->Annotate( g_CurrentSheet, SCH_COMPONENT::GetAutoAnnotationScopeOption(), (ANNOTATE_OPTION_T) SCH_COMPONENT::GetAutoAnnotationAlgoOption() );
+                component->Annotate( g_CurrentSheet, m_frame->GetAutoAnnotationScopeOption(), m_frame->GetAutoAnnotationAlgoOption() );
             }
             else
             {
diff --git a/eeschema/tools/sch_editor_control.cpp b/eeschema/tools/sch_editor_control.cpp
index 10d8c9f19..53760bb59 100644
--- a/eeschema/tools/sch_editor_control.cpp
+++ b/eeschema/tools/sch_editor_control.cpp
@@ -893,7 +893,7 @@ bool SCH_EDITOR_CONTROL::doCopy()
     STRING_FORMATTER formatter;
     SCH_LEGACY_PLUGIN plugin;
 
-    if ( SCH_COMPONENT::IsAutoAnnotationEnabled() )
+    if ( m_frame->IsAutoAnnotationEnabled() )
     {
         selection.SortComponentsByRef();
     }
@@ -1065,13 +1065,13 @@ int SCH_EDITOR_CONTROL::Paste( const TOOL_EVENT& aEvent )
         {
             SCH_COMPONENT* component = (SCH_COMPONENT*) item;
 
-            if( dropAnnotations || SCH_COMPONENT::IsAutoAnnotationEnabled() )
+            if( dropAnnotations || m_frame->IsAutoAnnotationEnabled() )
             {
                 // preserve the selected unit
                 int unit = component->GetUnit();
 
-                if ( SCH_COMPONENT::IsAutoAnnotationEnabled() )
-                    component->Annotate( g_CurrentSheet, SCH_COMPONENT::GetAutoAnnotationScopeOption(), (ANNOTATE_OPTION_T) SCH_COMPONENT::GetAutoAnnotationAlgoOption() );
+                if ( m_frame->IsAutoAnnotationEnabled() )
+                    component->Annotate( g_CurrentSheet, m_frame->GetAutoAnnotationScopeOption(), m_frame->GetAutoAnnotationAlgoOption() );
                 else
                     component->ClearAnnotation( nullptr );
 
-- 
2.20.1

From bbe1866263fb3446b7124a342d6f52250daf0ea7 Mon Sep 17 00:00:00 2001
From: Nufflee <zifcani@xxxxxxxxx>
Date: Sun, 29 Sep 2019 22:11:46 +0200
Subject: [PATCH 13/16] Introduce SCH_COMPONENT::CompareRefDes.

This function is introduced to reduce code duplication when comparing SCH_COMPONENTs and SCH_REFERENCEs.
---
 eeschema/component_references_lister.cpp | 16 +---------------
 eeschema/sch_component.cpp               | 18 ++++++++++++++++++
 eeschema/sch_component.h                 |  2 ++
 eeschema/tools/ee_selection.cpp          | 15 +--------------
 4 files changed, 22 insertions(+), 29 deletions(-)

diff --git a/eeschema/component_references_lister.cpp b/eeschema/component_references_lister.cpp
index 867fd18e3..503202ea3 100644
--- a/eeschema/component_references_lister.cpp
+++ b/eeschema/component_references_lister.cpp
@@ -112,21 +112,7 @@ bool SCH_REFERENCE_LIST::sortByRefAndValue( const SCH_REFERENCE& item1,
 bool SCH_REFERENCE_LIST::sortByReferenceOnly( const SCH_REFERENCE& item1,
                                               const SCH_REFERENCE& item2 )
 {
-    int             ii;
-
-    ii = UTIL::RefDesStringCompare( item1.GetRef(), item2.GetRef() );
-
-    if( ii == 0 )
-    {
-        ii = item1.m_RootCmp->GetField( VALUE )->GetText().CmpNoCase( item2.m_RootCmp->GetField( VALUE )->GetText() );
-    }
-
-    if( ii == 0 )
-    {
-        ii = item1.m_Unit - item2.m_Unit;
-    }
-
-    return ii < 0;
+    return item1.m_RootCmp->CompareRefDes(item2.m_RootCmp);
 }
 
 
diff --git a/eeschema/sch_component.cpp b/eeschema/sch_component.cpp
index 3e1f78beb..5a4f6834c 100644
--- a/eeschema/sch_component.cpp
+++ b/eeschema/sch_component.cpp
@@ -45,6 +45,7 @@
 #include <lib_item.h>
 #include <symbol_lib_table.h>
 #include <sch_reference_list.h>
+#include <refdes_utils.h>
 
 #include <dialogs/dialog_schematic_find.h>
 
@@ -2044,3 +2045,20 @@ bool SCH_COMPONENT::ClearAllHighlightFlags()
 
     return retVal;
 }
+
+bool SCH_COMPONENT::CompareRefDes( SCH_COMPONENT* aOther )
+{
+    int ii = UTIL::RefDesStringCompare( GetRef( g_CurrentSheet ), aOther->GetRef( g_CurrentSheet ) );
+
+    if( ii == 0 )
+    {
+        ii = GetField( VALUE )->GetText().CmpNoCase( aOther->GetField( VALUE )->GetText() );
+    }
+
+    if( ii == 0 )
+    {
+        ii = GetUnit() - aOther->GetUnit();
+    }
+
+    return ii < 0;
+}
diff --git a/eeschema/sch_component.h b/eeschema/sch_component.h
index 8777e24b0..a08ce00e8 100644
--- a/eeschema/sch_component.h
+++ b/eeschema/sch_component.h
@@ -652,6 +652,8 @@ public:
 
     void HighlightPin( LIB_PIN* aPin );
 
+    bool CompareRefDes( SCH_COMPONENT* aOther );
+
 private:
     bool doIsConnected( const wxPoint& aPosition ) const override;
 };
diff --git a/eeschema/tools/ee_selection.cpp b/eeschema/tools/ee_selection.cpp
index 1c3054657..5619670af 100644
--- a/eeschema/tools/ee_selection.cpp
+++ b/eeschema/tools/ee_selection.cpp
@@ -67,21 +67,8 @@ void EE_SELECTION::SortComponentsByRef()
         {
             SCH_COMPONENT* leftComponent  = (SCH_COMPONENT*) left;
             SCH_COMPONENT* rightComponent = (SCH_COMPONENT*) right;
-            int ii;
 
-            ii = UTIL::RefDesStringCompare( leftComponent->GetRef( g_CurrentSheet ), rightComponent->GetRef( g_CurrentSheet ) );
-
-            if( ii == 0 )
-            {
-                ii = leftComponent->GetField( VALUE )->GetText().CmpNoCase( rightComponent->GetField( VALUE )->GetText() );
-            }
-
-            if( ii == 0 )
-            {
-                ii = leftComponent->GetUnit() - rightComponent->GetUnit();
-            }
-
-            return ii < 0;
+            return leftComponent->CompareRefDes(rightComponent);
         }
 
         return false;
-- 
2.20.1

From 19a546cce2f338b33bdac5b4c215b6e0d4078ae7 Mon Sep 17 00:00:00 2001
From: Nufflee <zifcani@xxxxxxxxx>
Date: Fri, 18 Oct 2019 20:54:30 +0200
Subject: [PATCH 14/16] Fix auto annotation for multi-part symbols.

---
 eeschema/sch_component.cpp | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/eeschema/sch_component.cpp b/eeschema/sch_component.cpp
index 5a4f6834c..a4f702f5e 100644
--- a/eeschema/sch_component.cpp
+++ b/eeschema/sch_component.cpp
@@ -741,16 +741,16 @@ void SCH_COMPONENT::Annotate( SCH_SHEET_PATH* aSheet, bool aEntireSchematic, ANN
 
     if ( aAlgoOption != INCREMENTAL_BY_REF )
     {
-        interval = ( aAlgoOption == SHEET_NUMBER_X_100 ? 100 : 1000 ) * aSheet->GetPageNumber() + 1;
-
-        lastRefNum++;
+        interval = ( aAlgoOption == SHEET_NUMBER_X_100 ? 100 : 1000 ) * aSheet->GetPageNumber();
     }
 
+    lastRefNum = interval;
+
     references.SortByReferenceOnly();
 
     for ( unsigned i = 0; i < references.GetCount(); i++ )
     {
-        if ( references[i].GetComp()->GetPrefix() == prefix )
+        if ( references[i].GetComp()->GetPrefix() == prefix && references[i].GetComp()->m_unit == m_unit )
         {
             long refNum;
 
@@ -767,7 +767,7 @@ void SCH_COMPONENT::Annotate( SCH_SHEET_PATH* aSheet, bool aEntireSchematic, ANN
             }
 
             // Check for a gap.
-            if ( ( refNum - interval ) - lastRefNum > 1 )
+            if ( (refNum - interval) - (lastRefNum - interval) > 1 )
             {
                 break;
             }
-- 
2.20.1

From 3f81bc83830d2b165d5400e6fe8f62f7029fe00c Mon Sep 17 00:00:00 2001
From: Nufflee <zifcani@xxxxxxxxx>
Date: Sat, 26 Oct 2019 00:10:40 +0200
Subject: [PATCH 16/16] Code formatting and cleanup.

---
 eeschema/component_references_lister.cpp      |  2 +-
 .../panel_eeschema_annotation_options.cpp     | 43 +++++++-------
 .../panel_eeschema_annotation_options.h       |  2 +-
 eeschema/eeschema_config.cpp                  | 53 +++++++++--------
 eeschema/sch_component.cpp                    | 59 +++++++------------
 eeschema/sch_component.h                      | 10 ++--
 eeschema/sch_edit_frame.h                     | 48 +++++++++++----
 eeschema/tools/ee_selection.cpp               | 23 ++++----
 eeschema/tools/sch_drawing_tools.cpp          |  7 +--
 eeschema/tools/sch_edit_tool.cpp              |  9 ++-
 eeschema/tools/sch_editor_control.cpp         | 55 +++++++++--------
 11 files changed, 159 insertions(+), 152 deletions(-)

diff --git a/eeschema/component_references_lister.cpp b/eeschema/component_references_lister.cpp
index 503202ea3..b0feb3122 100644
--- a/eeschema/component_references_lister.cpp
+++ b/eeschema/component_references_lister.cpp
@@ -112,7 +112,7 @@ bool SCH_REFERENCE_LIST::sortByRefAndValue( const SCH_REFERENCE& item1,
 bool SCH_REFERENCE_LIST::sortByReferenceOnly( const SCH_REFERENCE& item1,
                                               const SCH_REFERENCE& item2 )
 {
-    return item1.m_RootCmp->CompareRefDes(item2.m_RootCmp);
+    return item1.m_RootCmp->CompareRefDes( item2.m_RootCmp );
 }
 
 
diff --git a/eeschema/dialogs/panel_eeschema_annotation_options.cpp b/eeschema/dialogs/panel_eeschema_annotation_options.cpp
index fae709b6a..3e7040f3b 100644
--- a/eeschema/dialogs/panel_eeschema_annotation_options.cpp
+++ b/eeschema/dialogs/panel_eeschema_annotation_options.cpp
@@ -24,9 +24,9 @@
 #include <panel_eeschema_annotation_options.h>
 #include <sch_component.h>
 
-PANEL_EESCHEMA_ANNOTATION_OPTIONS::PANEL_EESCHEMA_ANNOTATION_OPTIONS( SCH_EDIT_FRAME* aFrame, wxWindow* aWindow )
-    : PANEL_EESCHEMA_ANNOTATION_OPTIONS_BASE( aWindow ),
-      m_frame( aFrame )
+PANEL_EESCHEMA_ANNOTATION_OPTIONS::PANEL_EESCHEMA_ANNOTATION_OPTIONS(
+        SCH_EDIT_FRAME* aFrame, wxWindow* aWindow )
+        : PANEL_EESCHEMA_ANNOTATION_OPTIONS_BASE( aWindow ), m_frame( aFrame )
 {
 }
 
@@ -36,18 +36,25 @@ bool PANEL_EESCHEMA_ANNOTATION_OPTIONS::TransferDataToWindow()
 
     m_cbEnabled->SetValue( enabled );
 
-    SetContentEnabled(enabled);
+    SetContentEnabled( enabled );
 
     m_rbScope->SetSelection( m_frame->GetAutoAnnotationScopeOption() );
 
-    switch ( m_frame->GetAutoAnnotationAlgoOption() )
+    switch( m_frame->GetAutoAnnotationAlgoOption() )
     {
-        case INCREMENTAL_BY_REF:  m_rbFirstFree->SetValue( true );  break;
-        case SHEET_NUMBER_X_100:  m_rbSheetX100->SetValue( true );  break;
-        case SHEET_NUMBER_X_1000: m_rbSheetX1000->SetValue( true ); break;
+    case INCREMENTAL_BY_REF:
+        m_rbFirstFree->SetValue( true );
+        break;
+    case SHEET_NUMBER_X_100:
+        m_rbSheetX100->SetValue( true );
+        break;
+    case SHEET_NUMBER_X_1000:
+        m_rbSheetX1000->SetValue( true );
+        break;
     }
 
-    m_textNumberAfter->SetValue( wxString::Format( "%d", m_frame->GetAutoAnnotationFirstFreeNumOption() ) );
+    m_textNumberAfter->SetValue(
+            wxString::Format( "%d", m_frame->GetAutoAnnotationFirstFreeNumOption() ) );
 
     return true;
 }
@@ -58,25 +65,17 @@ bool PANEL_EESCHEMA_ANNOTATION_OPTIONS::TransferDataFromWindow()
 
     m_frame->SetAutoAnnotationScopeOption( m_rbScope->GetSelection() );
 
-    if ( m_rbFirstFree->GetValue() )
-    {
+    if( m_rbFirstFree->GetValue() )
         m_frame->SetAutoAnnotationAlgoOption( INCREMENTAL_BY_REF );
-    }
-    else if ( m_rbSheetX100->GetValue() )
-    {
+    else if( m_rbSheetX100->GetValue() )
         m_frame->SetAutoAnnotationAlgoOption( SHEET_NUMBER_X_100 );
-    }
-    else if ( m_rbSheetX1000->GetValue() )
-    {
+    else if( m_rbSheetX1000->GetValue() )
         m_frame->SetAutoAnnotationAlgoOption( SHEET_NUMBER_X_1000 );
-    }
 
     long numberAfter;
 
-    if ( m_textNumberAfter->GetValue().ToLong( &numberAfter ) )
-    {
+    if( m_textNumberAfter->GetValue().ToLong( &numberAfter ) )
         m_frame->SetAutoAnnotationFirstFreeNumOption( numberAfter );
-    }
 
     m_frame->SaveProjectSettings( false );
 
@@ -88,7 +87,7 @@ void PANEL_EESCHEMA_ANNOTATION_OPTIONS::OnEnableClick( wxCommandEvent& event )
     SetContentEnabled( m_cbEnabled->IsChecked() );
 }
 
-void PANEL_EESCHEMA_ANNOTATION_OPTIONS::SetContentEnabled(bool enabled)
+void PANEL_EESCHEMA_ANNOTATION_OPTIONS::SetContentEnabled( bool enabled )
 {
     m_rbScope->Enable( enabled );
     m_rbFirstFree->Enable( enabled );
diff --git a/eeschema/dialogs/panel_eeschema_annotation_options.h b/eeschema/dialogs/panel_eeschema_annotation_options.h
index 46a7203b4..94c596773 100644
--- a/eeschema/dialogs/panel_eeschema_annotation_options.h
+++ b/eeschema/dialogs/panel_eeschema_annotation_options.h
@@ -41,7 +41,7 @@ private:
     bool TransferDataFromWindow() override;
     bool TransferDataToWindow() override;
 
-    void SetContentEnabled(bool enabled = true);
+    void SetContentEnabled( bool enabled = true );
 };
 
 #endif //KICAD_PANEL_EESCHEMA_ANNOTATION_OPTIONS_H
diff --git a/eeschema/eeschema_config.cpp b/eeschema/eeschema_config.cpp
index 316643fe5..f17c58b5c 100644
--- a/eeschema/eeschema_config.cpp
+++ b/eeschema/eeschema_config.cpp
@@ -21,32 +21,32 @@
  * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
  */
 
-#include <fctsys.h>
-#include <pgm_base.h>
-#include <kiface_i.h>
+#include "eeschema_id.h"
+#include "sch_junction.h"
+#include <class_library.h>
 #include <confirm.h>
+#include <dialogs/panel_eeschema_annotation_options.h>
+#include <dialogs/panel_eeschema_display_options.h>
+#include <dialogs/panel_eeschema_settings.h>
+#include <dialogs/panel_eeschema_template_fieldnames.h>
+#include <dialogs/panel_libedit_settings.h>
+#include <eeschema_config.h>
+#include <fctsys.h>
 #include <gestfich.h>
+#include <kiface_i.h>
+#include <lib_edit_frame.h>
+#include <panel_display_options.h>
+#include <panel_hotkeys_editor.h>
+#include <pgm_base.h>
 #include <sch_edit_frame.h>
+#include <sch_painter.h>
 #include <sch_sheet.h>
-#include <lib_edit_frame.h>
-#include <eeschema_config.h>
-#include <ws_data_model.h>
-#include <class_library.h>
 #include <symbol_lib_table.h>
-#include <wildcards_and_files_ext.h>
 #include <widgets/paged_dialog.h>
-#include <dialogs/panel_eeschema_template_fieldnames.h>
-#include <dialogs/panel_eeschema_settings.h>
-#include <dialogs/panel_eeschema_display_options.h>
-#include <panel_display_options.h>
-#include <panel_hotkeys_editor.h>
-#include <widgets/widget_eeschema_color_config.h>
 #include <widgets/symbol_tree_pane.h>
-#include <dialogs/panel_libedit_settings.h>
-#include <dialogs/panel_eeschema_annotation_options.h>
-#include <sch_painter.h>
-#include "sch_junction.h"
-#include "eeschema_id.h"
+#include <widgets/widget_eeschema_color_config.h>
+#include <wildcards_and_files_ext.h>
+#include <ws_data_model.h>
 
 static int s_defaultBusThickness = DEFAULTBUSTHICKNESS;
 static int s_defaultWireThickness  = DEFAULTDRAWLINETHICKNESS;
@@ -127,7 +127,8 @@ void SCH_EDIT_FRAME::InstallPreferences( PAGED_DIALOG* aParent,
     book->AddSubPage( new PANEL_EESCHEMA_COLOR_CONFIG( this, book ), _( "Colors" ) );
     book->AddSubPage( new PANEL_EESCHEMA_TEMPLATE_FIELDNAMES( this, book ),
                       _( "Field Name Templates" ) );
-    book->AddSubPage( new PANEL_EESCHEMA_ANNOTATION_OPTIONS( this, book ), _( "Annotation Options" ) );
+    book->AddSubPage(
+            new PANEL_EESCHEMA_ANNOTATION_OPTIONS( this, book ), _( "Annotation Options" ) );
 
     aHotkeysPanel->AddHotKeys( GetToolManager() );
 }
@@ -154,10 +155,14 @@ PARAM_CFG_ARRAY& SCH_EDIT_FRAME::GetProjectFileParameters()
     m_projectFileParams.push_back( new PARAM_CFG_BOOL( wxT( "SpiceAjustPassiveValues" ),
                                             &m_spiceAjustPassiveValues, false ) );
 
-    m_projectFileParams.push_back( new PARAM_CFG_BOOL( wxT( "AutoAnnotationEnabled" ),      &m_autoAnnotateEnabled ) );
-    m_projectFileParams.push_back( new PARAM_CFG_INT(  wxT( "AutoAnnotationScope" ),        &m_autoAnnotateScopeOption, 0, 0, 1 ) );
-    m_projectFileParams.push_back( new PARAM_CFG_INT(  wxT( "AutoAnnotationAlgo" ),         (int*)&m_autoAnnotateAlgoOption, 0, 0, 2 ) );
-    m_projectFileParams.push_back( new PARAM_CFG_INT(  wxT( "AutoAnnotationFirstFreeNum" ), &m_autoAnnotateFirstFreeNumOption, 0 ) );
+    m_projectFileParams.push_back(
+            new PARAM_CFG_BOOL( wxT( "AutoAnnotationEnabled" ), &m_autoAnnotateEnabled ) );
+    m_projectFileParams.push_back( new PARAM_CFG_INT(
+            wxT( "AutoAnnotationScope" ), &m_autoAnnotateScopeOption, 0, 0, 1 ) );
+    m_projectFileParams.push_back( new PARAM_CFG_INT( wxT( "AutoAnnotationAlgo" ),
+            reinterpret_cast<int*>( &m_autoAnnotateAlgoOption ), 0, 0, 2 ) );
+    m_projectFileParams.push_back( new PARAM_CFG_INT(
+            wxT( "AutoAnnotationFirstFreeNum" ), &m_autoAnnotateFirstFreeNumOption, 0 ) );
 
     m_projectFileParams.push_back( new PARAM_CFG_INT( wxT( "LabSize" ),
                                             &s_defaultTextSize, DEFAULT_SIZE_TEXT, 5, 1000 ) );
diff --git a/eeschema/sch_component.cpp b/eeschema/sch_component.cpp
index a4f702f5e..12890c2b0 100644
--- a/eeschema/sch_component.cpp
+++ b/eeschema/sch_component.cpp
@@ -33,19 +33,19 @@
 #include <msgpanel.h>
 #include <bitmaps.h>
 
-#include <general.h>
 #include <class_library.h>
-#include <lib_rectangle.h>
+#include <general.h>
+#include <lib_item.h>
 #include <lib_pin.h>
+#include <lib_rectangle.h>
 #include <lib_text.h>
+#include <netlist_object.h>
+#include <refdes_utils.h>
 #include <sch_component.h>
+#include <sch_reference_list.h>
 #include <sch_sheet.h>
 #include <sch_sheet_path.h>
-#include <netlist_object.h>
-#include <lib_item.h>
 #include <symbol_lib_table.h>
-#include <sch_reference_list.h>
-#include <refdes_utils.h>
 
 #include <dialogs/dialog_schematic_find.h>
 
@@ -719,74 +719,60 @@ void SCH_COMPONENT::SetRef( const SCH_SHEET_PATH* sheet, const wxString& ref )
     m_isInNetlist = ! ref.StartsWith( wxT( "#" ) );
 }
 
-void SCH_COMPONENT::Annotate( SCH_SHEET_PATH* aSheet, bool aEntireSchematic, ANNOTATE_OPTION_T aAlgoOption )
+void SCH_COMPONENT::Annotate(
+        SCH_SHEET_PATH* aSheet, bool aEntireSchematic, ANNOTATE_OPTION_T aAlgoOption )
 {
     SCH_REFERENCE_LIST references;
-    wxString prefix = GetPrefix();
+    wxString           prefix = GetPrefix();
 
     // Build the sheet list.
     SCH_SHEET_LIST sheets( g_RootSheet );
 
-    if ( aEntireSchematic && aAlgoOption == INCREMENTAL_BY_REF )
-    {
+    if( aEntireSchematic && aAlgoOption == INCREMENTAL_BY_REF )
         sheets.GetComponents( references, false );
-    }
     else
-    {
         aSheet->GetComponents( references );
-    }
 
     int lastRefNum = 0;
     int interval = 0;
 
-    if ( aAlgoOption != INCREMENTAL_BY_REF )
-    {
+    if( aAlgoOption != INCREMENTAL_BY_REF )
         interval = ( aAlgoOption == SHEET_NUMBER_X_100 ? 100 : 1000 ) * aSheet->GetPageNumber();
-    }
 
     lastRefNum = interval;
 
     references.SortByReferenceOnly();
 
-    for ( unsigned i = 0; i < references.GetCount(); i++ )
+    for( unsigned i = 0; i < references.GetCount(); i++ )
     {
-        if ( references[i].GetComp()->GetPrefix() == prefix && references[i].GetComp()->m_unit == m_unit )
+        if( references[i].GetComp()->GetPrefix() == prefix
+                && references[i].GetComp()->m_unit == m_unit )
         {
             long refNum;
 
             references[i].Split();
 
-            if ( !references[i].GetRefNumber().ToLong( &refNum ) )
-            {
+            if( !references[i].GetRefNumber().ToLong( &refNum ) )
                 continue;
-            }
 
-            if ( refNum < interval )
-            {
+            if( refNum < interval )
                 continue;
-            }
 
             // Check for a gap.
-            if ( (refNum - interval) - (lastRefNum - interval) > 1 )
-            {
+            if( ( refNum - interval ) - ( lastRefNum - interval ) > 1 )
                 break;
-            }
 
-            if ( refNum > lastRefNum )
+            if( refNum > lastRefNum )
             {
                 lastRefNum = refNum;
             }
         }
     }
 
-    if ( lastRefNum < interval )
-    {
+    if( lastRefNum < interval )
         lastRefNum = interval;
-    }
     else
-    {
         lastRefNum++;
-    }
 
     SetRef( aSheet, prefix << lastRefNum );
 }
@@ -2048,17 +2034,14 @@ bool SCH_COMPONENT::ClearAllHighlightFlags()
 
 bool SCH_COMPONENT::CompareRefDes( SCH_COMPONENT* aOther )
 {
-    int ii = UTIL::RefDesStringCompare( GetRef( g_CurrentSheet ), aOther->GetRef( g_CurrentSheet ) );
+    int ii =
+            UTIL::RefDesStringCompare( GetRef( g_CurrentSheet ), aOther->GetRef( g_CurrentSheet ) );
 
     if( ii == 0 )
-    {
         ii = GetField( VALUE )->GetText().CmpNoCase( aOther->GetField( VALUE )->GetText() );
-    }
 
     if( ii == 0 )
-    {
         ii = GetUnit() - aOther->GetUnit();
-    }
 
     return ii < 0;
 }
diff --git a/eeschema/sch_component.h b/eeschema/sch_component.h
index a08ce00e8..bf5c501db 100644
--- a/eeschema/sch_component.h
+++ b/eeschema/sch_component.h
@@ -29,15 +29,15 @@
 
 #include <lib_id.h>
 
-#include <sch_field.h>
-#include <transform.h>
 #include <general.h>
-#include <vector>
-#include <set>
 #include <lib_item.h>
-#include <sch_pin.h>
 #include <sch_base_frame.h>
 #include <sch_edit_frame.h>
+#include <sch_field.h>
+#include <sch_pin.h>
+#include <set>
+#include <transform.h>
+#include <vector>
 
 class SCH_SCREEN;
 class SCH_SHEET_PATH;
diff --git a/eeschema/sch_edit_frame.h b/eeschema/sch_edit_frame.h
index 761abbc18..7ed8a8206 100644
--- a/eeschema/sch_edit_frame.h
+++ b/eeschema/sch_edit_frame.h
@@ -103,10 +103,10 @@ private:
     wxString                m_DefaultSchematicFileName;
     wxString                m_SelectedNetName;
 
-    bool                    m_autoAnnotateEnabled;
-    int                     m_autoAnnotateScopeOption;
-    ANNOTATE_OPTION_T       m_autoAnnotateAlgoOption;
-    int                     m_autoAnnotateFirstFreeNumOption;
+    bool              m_autoAnnotateEnabled;
+    int               m_autoAnnotateScopeOption;
+    ANNOTATE_OPTION_T m_autoAnnotateAlgoOption;
+    int               m_autoAnnotateFirstFreeNumOption;
 
     PARAM_CFG_ARRAY         m_projectFileParams;
     PARAM_CFG_ARRAY         m_configSettings;
@@ -219,17 +219,41 @@ public:
     bool GetSpiceAjustPassiveValues() const { return m_spiceAjustPassiveValues; }
     void SetSpiceAdjustPassiveValues( bool aEnable ) { m_spiceAjustPassiveValues = aEnable; }
 
-    bool IsAutoAnnotationEnabled() { return m_autoAnnotateEnabled; }
-    void SetIsAutoAnnotationEnabled(bool value) { m_autoAnnotateEnabled = value; }
+    bool IsAutoAnnotationEnabled()
+    {
+        return m_autoAnnotateEnabled;
+    }
+    void SetIsAutoAnnotationEnabled( bool value )
+    {
+        m_autoAnnotateEnabled = value;
+    }
 
-    int GetAutoAnnotationScopeOption() { return m_autoAnnotateScopeOption; }
-    void SetAutoAnnotationScopeOption(int value) { m_autoAnnotateScopeOption = value; }
+    int GetAutoAnnotationScopeOption()
+    {
+        return m_autoAnnotateScopeOption;
+    }
+    void SetAutoAnnotationScopeOption( int value )
+    {
+        m_autoAnnotateScopeOption = value;
+    }
 
-    ANNOTATE_OPTION_T GetAutoAnnotationAlgoOption() { return m_autoAnnotateAlgoOption; }
-    void SetAutoAnnotationAlgoOption(ANNOTATE_OPTION_T value) { m_autoAnnotateAlgoOption = value; }
+    ANNOTATE_OPTION_T GetAutoAnnotationAlgoOption()
+    {
+        return m_autoAnnotateAlgoOption;
+    }
+    void SetAutoAnnotationAlgoOption( ANNOTATE_OPTION_T value )
+    {
+        m_autoAnnotateAlgoOption = value;
+    }
 
-    int GetAutoAnnotationFirstFreeNumOption() { return m_autoAnnotateFirstFreeNumOption; }
-    void SetAutoAnnotationFirstFreeNumOption(int value) { m_autoAnnotateFirstFreeNumOption = value; }
+    int GetAutoAnnotationFirstFreeNumOption()
+    {
+        return m_autoAnnotateFirstFreeNumOption;
+    }
+    void SetAutoAnnotationFirstFreeNumOption( int value )
+    {
+        m_autoAnnotateFirstFreeNumOption = value;
+    }
 
     /// accessor to the destination directory to use when generating plot files.
     const wxString& GetPlotDirectoryName() const { return m_plotDirectoryName; }
diff --git a/eeschema/tools/ee_selection.cpp b/eeschema/tools/ee_selection.cpp
index 5619670af..7efa4f5a8 100644
--- a/eeschema/tools/ee_selection.cpp
+++ b/eeschema/tools/ee_selection.cpp
@@ -22,13 +22,13 @@
  * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
  */
 
-#include <limits>
 #include <functional>
-#include <tools/ee_selection.h>
-#include <sch_item.h>
 #include <lib_item.h>
-#include <sch_component.h>
+#include <limits>
 #include <refdes_utils.h>
+#include <sch_component.h>
+#include <sch_item.h>
+#include <tools/ee_selection.h>
 
 EDA_ITEM* EE_SELECTION::GetTopLeftItem( bool onlyModules ) const
 {
@@ -61,16 +61,15 @@ EDA_ITEM* EE_SELECTION::GetTopLeftItem( bool onlyModules ) const
 
 void EE_SELECTION::SortComponentsByRef()
 {
-    std::sort( begin(), end(), []( const EDA_ITEM* left, const EDA_ITEM* right )
-    {
-        if ( left->Type() == SCH_COMPONENT_T && right->Type() == SCH_COMPONENT_T )
+    std::sort( begin(), end(), []( EDA_ITEM* left, EDA_ITEM* right ) {
+        if( left->Type() == SCH_COMPONENT_T && right->Type() == SCH_COMPONENT_T )
         {
-            SCH_COMPONENT* leftComponent  = (SCH_COMPONENT*) left;
-            SCH_COMPONENT* rightComponent = (SCH_COMPONENT*) right;
+            SCH_COMPONENT* leftComponent = static_cast<SCH_COMPONENT*>( left );
+            SCH_COMPONENT* rightComponent = static_cast<SCH_COMPONENT*>( right );
 
-            return leftComponent->CompareRefDes(rightComponent);
+            return leftComponent->CompareRefDes( rightComponent );
         }
 
         return false;
-    });
-}
\ No newline at end of file
+    } );
+}
diff --git a/eeschema/tools/sch_drawing_tools.cpp b/eeschema/tools/sch_drawing_tools.cpp
index b3d8234fc..234c00c58 100644
--- a/eeschema/tools/sch_drawing_tools.cpp
+++ b/eeschema/tools/sch_drawing_tools.cpp
@@ -188,10 +188,9 @@ int SCH_DRAWING_TOOLS::PlaceComponent(  const TOOL_EVENT& aEvent  )
             }
             else
             {
-                if ( m_frame->IsAutoAnnotationEnabled() )
-                {
-                    component->Annotate( g_CurrentSheet, m_frame->GetAutoAnnotationScopeOption(), m_frame->GetAutoAnnotationAlgoOption() );
-                }
+                if( m_frame->IsAutoAnnotationEnabled() )
+                    component->Annotate( g_CurrentSheet, m_frame->GetAutoAnnotationScopeOption(),
+                            m_frame->GetAutoAnnotationAlgoOption() );
 
                 m_frame->AddItemToScreenAndUndoList( component );
                 component = nullptr;
diff --git a/eeschema/tools/sch_edit_tool.cpp b/eeschema/tools/sch_edit_tool.cpp
index 498bc46eb..9d91d5a4a 100644
--- a/eeschema/tools/sch_edit_tool.cpp
+++ b/eeschema/tools/sch_edit_tool.cpp
@@ -734,10 +734,8 @@ int SCH_EDIT_TOOL::Duplicate( const TOOL_EVENT& aEvent )
     bool copiedSheets = false;
     SCH_SHEET_LIST initial_sheetpathList( g_RootSheet );
 
-    if ( m_frame->IsAutoAnnotationEnabled() )
-    {
+    if( m_frame->IsAutoAnnotationEnabled() )
         selection.SortComponentsByRef();
-    }
 
     for( unsigned ii = 0; ii < selection.GetSize(); ++ii )
     {
@@ -785,9 +783,10 @@ int SCH_EDIT_TOOL::Duplicate( const TOOL_EVENT& aEvent )
 
             component->SetTimeStamp( GetNewTimeStamp() );
 
-            if ( m_frame->IsAutoAnnotationEnabled() )
+            if( m_frame->IsAutoAnnotationEnabled() )
             {
-                component->Annotate( g_CurrentSheet, m_frame->GetAutoAnnotationScopeOption(), m_frame->GetAutoAnnotationAlgoOption() );
+                component->Annotate( g_CurrentSheet, m_frame->GetAutoAnnotationScopeOption(),
+                        m_frame->GetAutoAnnotationAlgoOption() );
             }
             else
             {
diff --git a/eeschema/tools/sch_editor_control.cpp b/eeschema/tools/sch_editor_control.cpp
index 53760bb59..ad2148679 100644
--- a/eeschema/tools/sch_editor_control.cpp
+++ b/eeschema/tools/sch_editor_control.cpp
@@ -22,37 +22,37 @@
  * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
  */
 
+#include <advanced_config.h>
+#include <class_library.h>
+#include <confirm.h>
+#include <connection_graph.h>
+#include <dialogs/dialog_fields_editor_global.h>
+#include <dialogs/dialog_page_settings.h>
+#include <dialogs/dialog_paste_special.h>
+#include <eeschema_id.h>
+#include <erc.h>
 #include <fctsys.h>
+#include <invoke_sch_dialog.h>
 #include <kiway.h>
-#include <sch_view.h>
+#include <netlist_exporters/netlist_exporter_pspice.h>
+#include <netlist_object.h>
+#include <refdes_utils.h>
 #include <sch_edit_frame.h>
-#include <sch_sheet.h>
+#include <sch_legacy_plugin.h>
 #include <sch_line.h>
-#include <connection_graph.h>
-#include <erc.h>
-#include <eeschema_id.h>
-#include <netlist_object.h>
-#include <tool/tool_manager.h>
+#include <sch_painter.h>
+#include <sch_sheet.h>
+#include <sch_view.h>
+#include <sim/sim_plot_frame.h>
+#include <simulation_cursors.h>
+#include <status_popup.h>
 #include <tool/picker_tool.h>
+#include <tool/tool_manager.h>
 #include <tools/ee_actions.h>
-#include <tools/sch_editor_control.h>
 #include <tools/ee_selection.h>
 #include <tools/ee_selection_tool.h>
-#include <advanced_config.h>
-#include <simulation_cursors.h>
-#include <sim/sim_plot_frame.h>
-#include <sch_legacy_plugin.h>
-#include <class_library.h>
-#include <confirm.h>
-#include <sch_painter.h>
-#include <status_popup.h>
+#include <tools/sch_editor_control.h>
 #include <ws_proxy_undo_item.h>
-#include <dialogs/dialog_page_settings.h>
-#include <dialogs/dialog_fields_editor_global.h>
-#include <invoke_sch_dialog.h>
-#include <dialogs/dialog_paste_special.h>
-#include <netlist_exporters/netlist_exporter_pspice.h>
-#include <refdes_utils.h>
 
 int SCH_EDITOR_CONTROL::New( const TOOL_EVENT& aEvent )
 {
@@ -874,7 +874,7 @@ bool SCH_EDITOR_CONTROL::doCopy()
     EE_SELECTION_TOOL* selTool = m_toolMgr->GetTool<EE_SELECTION_TOOL>();
     // Selection has to be copied here because `SortComponentsByRef` invalidates
     // some iterators in another object.
-    EE_SELECTION       selection = selTool->RequestSelection();
+    EE_SELECTION selection = selTool->RequestSelection();
 
     if( !selection.GetSize() )
         return false;
@@ -893,10 +893,8 @@ bool SCH_EDITOR_CONTROL::doCopy()
     STRING_FORMATTER formatter;
     SCH_LEGACY_PLUGIN plugin;
 
-    if ( m_frame->IsAutoAnnotationEnabled() )
-    {
+    if( m_frame->IsAutoAnnotationEnabled() )
         selection.SortComponentsByRef();
-    }
 
     plugin.Format( &selection, &formatter );
 
@@ -1070,8 +1068,9 @@ int SCH_EDITOR_CONTROL::Paste( const TOOL_EVENT& aEvent )
                 // preserve the selected unit
                 int unit = component->GetUnit();
 
-                if ( m_frame->IsAutoAnnotationEnabled() )
-                    component->Annotate( g_CurrentSheet, m_frame->GetAutoAnnotationScopeOption(), m_frame->GetAutoAnnotationAlgoOption() );
+                if( m_frame->IsAutoAnnotationEnabled() )
+                    component->Annotate( g_CurrentSheet, m_frame->GetAutoAnnotationScopeOption(),
+                            m_frame->GetAutoAnnotationAlgoOption() );
                 else
                     component->ClearAnnotation( nullptr );
 
-- 
2.20.1

diff --git a/.gitignore b/.gitignore
index 619c7f963..ad5fad1c9 100644
--- a/.gitignore
+++ b/.gitignore
@@ -26,7 +26,7 @@ pcb_calculator/attenuators/bridget_tee_formula.h
 pcb_calculator/attenuators/pi_formula.h
 pcb_calculator/attenuators/splitter_formula.h
 pcb_calculator/attenuators/tee_formula.h
-pcb_calculator/tracks_width_versus_current_formula.h
+pcb_calculator/tracks_width_versus_current_formula.h
 Makefile
 CMakeCache.txt
 auto_renamed_to_cpp
@@ -74,6 +74,7 @@ pcbnew/Info.plist
 .*.swp
 *~
 .idea
+.vscode
 .DS_Store
 *.png
 *.kiface
@@ -87,6 +88,12 @@ pcbnew/Info.plist
 *.gch
 *.orig
 *.patch
+kicad.cflags
+kicad.config
+kicad.creator*
+kicad.cxxflags
+kicad.files
+kicad.includes
 
 # These CMake files are wanted
 !CMakeModules/*.cmake
diff --git a/eeschema/CMakeLists.txt b/eeschema/CMakeLists.txt
index f22886414..86569a39d 100644
--- a/eeschema/CMakeLists.txt
+++ b/eeschema/CMakeLists.txt
@@ -97,6 +97,8 @@ set( EESCHEMA_DLGS
     dialogs/panel_eeschema_display_options_base.cpp
     dialogs/panel_eeschema_settings.cpp
     dialogs/panel_eeschema_settings_base.cpp
+    dialogs/panel_eeschema_annotation_options.cpp
+    dialogs/panel_eeschema_annotation_options_base.cpp
     dialogs/panel_libedit_settings.cpp
     dialogs/panel_libedit_settings_base.cpp
     dialogs/panel_sym_lib_table.cpp
diff --git a/eeschema/component_references_lister.cpp b/eeschema/component_references_lister.cpp
index 867fd18e3..b0feb3122 100644
--- a/eeschema/component_references_lister.cpp
+++ b/eeschema/component_references_lister.cpp
@@ -112,21 +112,7 @@ bool SCH_REFERENCE_LIST::sortByRefAndValue( const SCH_REFERENCE& item1,
 bool SCH_REFERENCE_LIST::sortByReferenceOnly( const SCH_REFERENCE& item1,
                                               const SCH_REFERENCE& item2 )
 {
-    int             ii;
-
-    ii = UTIL::RefDesStringCompare( item1.GetRef(), item2.GetRef() );
-
-    if( ii == 0 )
-    {
-        ii = item1.m_RootCmp->GetField( VALUE )->GetText().CmpNoCase( item2.m_RootCmp->GetField( VALUE )->GetText() );
-    }
-
-    if( ii == 0 )
-    {
-        ii = item1.m_Unit - item2.m_Unit;
-    }
-
-    return ii < 0;
+    return item1.m_RootCmp->CompareRefDes( item2.m_RootCmp );
 }
 
 
diff --git a/eeschema/dialogs/panel_eeschema_annotation_options.cpp b/eeschema/dialogs/panel_eeschema_annotation_options.cpp
new file mode 100644
index 000000000..3e7040f3b
--- /dev/null
+++ b/eeschema/dialogs/panel_eeschema_annotation_options.cpp
@@ -0,0 +1,97 @@
+/*
+ * This program source code file is part of KiCad, a free EDA CAD application.
+ *
+ * Copyright (C) 2019 Kicad Developers, see AUTHORS.txt for contributors.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, you may find one here:
+ * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
+ * or you may search the http://www.gnu.org website for the version 2 license,
+ * or you may write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
+ */
+
+#include <panel_eeschema_annotation_options.h>
+#include <sch_component.h>
+
+PANEL_EESCHEMA_ANNOTATION_OPTIONS::PANEL_EESCHEMA_ANNOTATION_OPTIONS(
+        SCH_EDIT_FRAME* aFrame, wxWindow* aWindow )
+        : PANEL_EESCHEMA_ANNOTATION_OPTIONS_BASE( aWindow ), m_frame( aFrame )
+{
+}
+
+bool PANEL_EESCHEMA_ANNOTATION_OPTIONS::TransferDataToWindow()
+{
+    bool enabled = m_frame->IsAutoAnnotationEnabled();
+
+    m_cbEnabled->SetValue( enabled );
+
+    SetContentEnabled( enabled );
+
+    m_rbScope->SetSelection( m_frame->GetAutoAnnotationScopeOption() );
+
+    switch( m_frame->GetAutoAnnotationAlgoOption() )
+    {
+    case INCREMENTAL_BY_REF:
+        m_rbFirstFree->SetValue( true );
+        break;
+    case SHEET_NUMBER_X_100:
+        m_rbSheetX100->SetValue( true );
+        break;
+    case SHEET_NUMBER_X_1000:
+        m_rbSheetX1000->SetValue( true );
+        break;
+    }
+
+    m_textNumberAfter->SetValue(
+            wxString::Format( "%d", m_frame->GetAutoAnnotationFirstFreeNumOption() ) );
+
+    return true;
+}
+
+bool PANEL_EESCHEMA_ANNOTATION_OPTIONS::TransferDataFromWindow()
+{
+    m_frame->SetIsAutoAnnotationEnabled( m_cbEnabled->GetValue() );
+
+    m_frame->SetAutoAnnotationScopeOption( m_rbScope->GetSelection() );
+
+    if( m_rbFirstFree->GetValue() )
+        m_frame->SetAutoAnnotationAlgoOption( INCREMENTAL_BY_REF );
+    else if( m_rbSheetX100->GetValue() )
+        m_frame->SetAutoAnnotationAlgoOption( SHEET_NUMBER_X_100 );
+    else if( m_rbSheetX1000->GetValue() )
+        m_frame->SetAutoAnnotationAlgoOption( SHEET_NUMBER_X_1000 );
+
+    long numberAfter;
+
+    if( m_textNumberAfter->GetValue().ToLong( &numberAfter ) )
+        m_frame->SetAutoAnnotationFirstFreeNumOption( numberAfter );
+
+    m_frame->SaveProjectSettings( false );
+
+    return true;
+}
+
+void PANEL_EESCHEMA_ANNOTATION_OPTIONS::OnEnableClick( wxCommandEvent& event )
+{
+    SetContentEnabled( m_cbEnabled->IsChecked() );
+}
+
+void PANEL_EESCHEMA_ANNOTATION_OPTIONS::SetContentEnabled( bool enabled )
+{
+    m_rbScope->Enable( enabled );
+    m_rbFirstFree->Enable( enabled );
+    m_rbSheetX100->Enable( enabled );
+    m_rbSheetX1000->Enable( enabled );
+    m_textNumberAfter->Enable( enabled );
+}
diff --git a/eeschema/dialogs/panel_eeschema_annotation_options.h b/eeschema/dialogs/panel_eeschema_annotation_options.h
new file mode 100644
index 000000000..94c596773
--- /dev/null
+++ b/eeschema/dialogs/panel_eeschema_annotation_options.h
@@ -0,0 +1,47 @@
+/*
+ * This program source code file is part of KiCad, a free EDA CAD application.
+ *
+ * Copyright (C) 2019 Kicad Developers, see AUTHORS.txt for contributors.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, you may find one here:
+ * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
+ * or you may search the http://www.gnu.org website for the version 2 license,
+ * or you may write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
+ */
+
+#ifndef KICAD_PANEL_EESCHEMA_ANNOTATION_OPTIONS_H
+#define KICAD_PANEL_EESCHEMA_ANNOTATION_OPTIONS_H
+
+#include "panel_eeschema_annotation_options_base.h"
+#include <sch_edit_frame.h>
+
+class PANEL_EESCHEMA_ANNOTATION_OPTIONS : public PANEL_EESCHEMA_ANNOTATION_OPTIONS_BASE
+{
+private:
+    SCH_EDIT_FRAME* m_frame;
+
+public:
+    PANEL_EESCHEMA_ANNOTATION_OPTIONS( SCH_EDIT_FRAME* aFrame, wxWindow* aWindow );
+
+    void OnEnableClick( wxCommandEvent& event ) override;
+
+private:
+    bool TransferDataFromWindow() override;
+    bool TransferDataToWindow() override;
+
+    void SetContentEnabled( bool enabled = true );
+};
+
+#endif //KICAD_PANEL_EESCHEMA_ANNOTATION_OPTIONS_H
diff --git a/eeschema/dialogs/panel_eeschema_annotation_options_base.cpp b/eeschema/dialogs/panel_eeschema_annotation_options_base.cpp
new file mode 100644
index 000000000..cd1363092
--- /dev/null
+++ b/eeschema/dialogs/panel_eeschema_annotation_options_base.cpp
@@ -0,0 +1,83 @@
+///////////////////////////////////////////////////////////////////////////
+// C++ code generated with wxFormBuilder (version Sep 10 2019)
+// http://www.wxformbuilder.org/
+//
+// PLEASE DO *NOT* EDIT THIS FILE!
+///////////////////////////////////////////////////////////////////////////
+
+#include "panel_eeschema_annotation_options_base.h"
+
+///////////////////////////////////////////////////////////////////////////
+
+PANEL_EESCHEMA_ANNOTATION_OPTIONS_BASE::PANEL_EESCHEMA_ANNOTATION_OPTIONS_BASE( wxWindow* parent, wxWindowID id, const wxPoint& pos, const wxSize& size, long style, const wxString& name ) : wxPanel( parent, id, pos, size, style, name )
+{
+	wxBoxSizer* bmainSizer;
+	bmainSizer = new wxBoxSizer( wxVERTICAL );
+
+	wxBoxSizer* bupperSizer;
+	bupperSizer = new wxBoxSizer( wxVERTICAL );
+
+	m_cbEnabled = new wxCheckBox( this, wxID_ANY, _("Annotate components on placement"), wxDefaultPosition, wxDefaultSize, 0 );
+	bupperSizer->Add( m_cbEnabled, 0, wxALL, 5 );
+
+	m_separator = new wxStaticLine( this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxLI_HORIZONTAL );
+	bupperSizer->Add( m_separator, 0, wxEXPAND | wxALL, 5 );
+
+	m_contentSizer = new wxFlexGridSizer( 1, 2, 0, 0 );
+	m_contentSizer->AddGrowableCol( 0 );
+	m_contentSizer->AddGrowableCol( 1 );
+	m_contentSizer->SetFlexibleDirection( wxBOTH );
+	m_contentSizer->SetNonFlexibleGrowMode( wxFLEX_GROWMODE_SPECIFIED );
+
+	wxString m_rbScopeChoices[] = { _("Use the current page only"), _("Use the entire schematic") };
+	int m_rbScopeNChoices = sizeof( m_rbScopeChoices ) / sizeof( wxString );
+	m_rbScope = new wxRadioBox( this, wxID_ANY, _("Scope:"), wxDefaultPosition, wxDefaultSize, m_rbScopeNChoices, m_rbScopeChoices, 1, wxRA_SPECIFY_COLS );
+	m_rbScope->SetSelection( 0 );
+	m_contentSizer->Add( m_rbScope, 0, wxALL|wxEXPAND, 5 );
+
+	wxStaticBoxSizer* sbSizer2;
+	sbSizer2 = new wxStaticBoxSizer( new wxStaticBox( this, wxID_ANY, _("Numbering:") ), wxVERTICAL );
+
+	wxGridBagSizer* gbSizer1;
+	gbSizer1 = new wxGridBagSizer( 0, 0 );
+	gbSizer1->SetFlexibleDirection( wxBOTH );
+	gbSizer1->SetNonFlexibleGrowMode( wxFLEX_GROWMODE_SPECIFIED );
+
+	m_rbFirstFree = new wxRadioButton( sbSizer2->GetStaticBox(), wxID_FIRST_FREE, _("Use first free number after:"), wxDefaultPosition, wxDefaultSize, wxRB_GROUP );
+	gbSizer1->Add( m_rbFirstFree, wxGBPosition( 0, 0 ), wxGBSpan( 1, 1 ), wxTOP, 2 );
+
+	m_textNumberAfter = new wxTextCtrl( sbSizer2->GetStaticBox(), wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize( 60,-1 ), 0 );
+	gbSizer1->Add( m_textNumberAfter, wxGBPosition( 0, 1 ), wxGBSpan( 1, 1 ), wxLEFT, 2 );
+
+	m_rbSheetX100 = new wxRadioButton( sbSizer2->GetStaticBox(), wxID_SHEET_X_100, _("First free after sheet number X 100"), wxDefaultPosition, wxDefaultSize, 0 );
+	gbSizer1->Add( m_rbSheetX100, wxGBPosition( 1, 0 ), wxGBSpan( 1, 2 ), wxBOTTOM|wxTOP, 4 );
+
+	m_rbSheetX1000 = new wxRadioButton( sbSizer2->GetStaticBox(), wxID_SHEET_X_1000, _("First free after sheet number X 1000"), wxDefaultPosition, wxDefaultSize, 0 );
+	gbSizer1->Add( m_rbSheetX1000, wxGBPosition( 2, 0 ), wxGBSpan( 1, 2 ), wxBOTTOM|wxTOP, 2 );
+
+
+	sbSizer2->Add( gbSizer1, 1, wxEXPAND, 5 );
+
+
+	m_contentSizer->Add( sbSizer2, 1, wxALL|wxEXPAND, 5 );
+
+
+	bupperSizer->Add( m_contentSizer, 0, wxBOTTOM|wxEXPAND, 5 );
+
+
+	bmainSizer->Add( bupperSizer, 1, wxEXPAND|wxTOP|wxRIGHT|wxLEFT, 6 );
+
+
+	this->SetSizer( bmainSizer );
+	this->Layout();
+
+	// Connect Events
+	m_cbEnabled->Connect( wxEVT_COMMAND_CHECKBOX_CLICKED, wxCommandEventHandler( PANEL_EESCHEMA_ANNOTATION_OPTIONS_BASE::OnEnableClick ), NULL, this );
+}
+
+PANEL_EESCHEMA_ANNOTATION_OPTIONS_BASE::~PANEL_EESCHEMA_ANNOTATION_OPTIONS_BASE()
+{
+	// Disconnect Events
+	m_cbEnabled->Disconnect( wxEVT_COMMAND_CHECKBOX_CLICKED, wxCommandEventHandler( PANEL_EESCHEMA_ANNOTATION_OPTIONS_BASE::OnEnableClick ), NULL, this );
+
+}
diff --git a/eeschema/dialogs/panel_eeschema_annotation_options_base.fbp b/eeschema/dialogs/panel_eeschema_annotation_options_base.fbp
new file mode 100644
index 000000000..53c664cb8
--- /dev/null
+++ b/eeschema/dialogs/panel_eeschema_annotation_options_base.fbp
@@ -0,0 +1,579 @@
+<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
+<wxFormBuilder_Project>
+    <FileVersion major="1" minor="15" />
+    <object class="Project" expanded="1">
+        <property name="class_decoration"></property>
+        <property name="code_generation">C++</property>
+        <property name="disconnect_events">1</property>
+        <property name="disconnect_mode">source_name</property>
+        <property name="disconnect_php_events">0</property>
+        <property name="disconnect_python_events">0</property>
+        <property name="embedded_files_path">res</property>
+        <property name="encoding">UTF-8</property>
+        <property name="event_generation">connect</property>
+        <property name="file">panel_eeschema_annotation_options_base</property>
+        <property name="first_id">1000</property>
+        <property name="help_provider">none</property>
+        <property name="image_path_wrapper_function_name"></property>
+        <property name="indent_with_spaces"></property>
+        <property name="internationalize">1</property>
+        <property name="name">PanelEeschemaAnnotationOptionsBase</property>
+        <property name="namespace"></property>
+        <property name="path">.</property>
+        <property name="precompiled_header"></property>
+        <property name="relative_path">1</property>
+        <property name="skip_lua_events">1</property>
+        <property name="skip_php_events">1</property>
+        <property name="skip_python_events">1</property>
+        <property name="ui_table">UI</property>
+        <property name="use_array_enum">0</property>
+        <property name="use_enum">0</property>
+        <property name="use_microsoft_bom">0</property>
+        <object class="Panel" expanded="1">
+            <property name="aui_managed">0</property>
+            <property name="aui_manager_style">wxAUI_MGR_DEFAULT</property>
+            <property name="bg"></property>
+            <property name="context_help"></property>
+            <property name="context_menu">1</property>
+            <property name="enabled">1</property>
+            <property name="event_handler">impl_virtual</property>
+            <property name="fg"></property>
+            <property name="font"></property>
+            <property name="hidden">0</property>
+            <property name="id">wxID_ANY</property>
+            <property name="maximum_size"></property>
+            <property name="minimum_size"></property>
+            <property name="name">PANEL_EESCHEMA_ANNOTATION_OPTIONS_BASE</property>
+            <property name="pos"></property>
+            <property name="size">500,156</property>
+            <property name="subclass">; ; forward_declare</property>
+            <property name="tooltip"></property>
+            <property name="window_extra_style"></property>
+            <property name="window_name"></property>
+            <property name="window_style">wxTAB_TRAVERSAL</property>
+            <object class="wxBoxSizer" expanded="0">
+                <property name="minimum_size"></property>
+                <property name="name">bmainSizer</property>
+                <property name="orient">wxVERTICAL</property>
+                <property name="permission">none</property>
+                <object class="sizeritem" expanded="0">
+                    <property name="border">6</property>
+                    <property name="flag">wxEXPAND|wxTOP|wxRIGHT|wxLEFT</property>
+                    <property name="proportion">1</property>
+                    <object class="wxBoxSizer" expanded="0">
+                        <property name="minimum_size"></property>
+                        <property name="name">bupperSizer</property>
+                        <property name="orient">wxVERTICAL</property>
+                        <property name="permission">none</property>
+                        <object class="sizeritem" expanded="0">
+                            <property name="border">5</property>
+                            <property name="flag">wxALL</property>
+                            <property name="proportion">0</property>
+                            <object class="wxCheckBox" expanded="0">
+                                <property name="BottomDockable">1</property>
+                                <property name="LeftDockable">1</property>
+                                <property name="RightDockable">1</property>
+                                <property name="TopDockable">1</property>
+                                <property name="aui_layer"></property>
+                                <property name="aui_name"></property>
+                                <property name="aui_position"></property>
+                                <property name="aui_row"></property>
+                                <property name="best_size"></property>
+                                <property name="bg"></property>
+                                <property name="caption"></property>
+                                <property name="caption_visible">1</property>
+                                <property name="center_pane">0</property>
+                                <property name="checked">0</property>
+                                <property name="close_button">1</property>
+                                <property name="context_help"></property>
+                                <property name="context_menu">1</property>
+                                <property name="default_pane">0</property>
+                                <property name="dock">Dock</property>
+                                <property name="dock_fixed">0</property>
+                                <property name="docking">Left</property>
+                                <property name="enabled">1</property>
+                                <property name="fg"></property>
+                                <property name="floatable">1</property>
+                                <property name="font"></property>
+                                <property name="gripper">0</property>
+                                <property name="hidden">0</property>
+                                <property name="id">wxID_ANY</property>
+                                <property name="label">Annotate components on placement</property>
+                                <property name="max_size"></property>
+                                <property name="maximize_button">0</property>
+                                <property name="maximum_size"></property>
+                                <property name="min_size"></property>
+                                <property name="minimize_button">0</property>
+                                <property name="minimum_size"></property>
+                                <property name="moveable">1</property>
+                                <property name="name">m_cbEnabled</property>
+                                <property name="pane_border">1</property>
+                                <property name="pane_position"></property>
+                                <property name="pane_size"></property>
+                                <property name="permission">protected</property>
+                                <property name="pin_button">1</property>
+                                <property name="pos"></property>
+                                <property name="resize">Resizable</property>
+                                <property name="show">1</property>
+                                <property name="size"></property>
+                                <property name="style"></property>
+                                <property name="subclass">; ; forward_declare</property>
+                                <property name="toolbar_pane">0</property>
+                                <property name="tooltip"></property>
+                                <property name="validator_data_type"></property>
+                                <property name="validator_style">wxFILTER_NONE</property>
+                                <property name="validator_type">wxDefaultValidator</property>
+                                <property name="validator_variable"></property>
+                                <property name="window_extra_style"></property>
+                                <property name="window_name"></property>
+                                <property name="window_style"></property>
+                                <event name="OnCheckBox">OnEnableClick</event>
+                            </object>
+                        </object>
+                        <object class="sizeritem" expanded="0">
+                            <property name="border">5</property>
+                            <property name="flag">wxEXPAND | wxALL</property>
+                            <property name="proportion">0</property>
+                            <object class="wxStaticLine" expanded="0">
+                                <property name="BottomDockable">1</property>
+                                <property name="LeftDockable">1</property>
+                                <property name="RightDockable">1</property>
+                                <property name="TopDockable">1</property>
+                                <property name="aui_layer"></property>
+                                <property name="aui_name"></property>
+                                <property name="aui_position"></property>
+                                <property name="aui_row"></property>
+                                <property name="best_size"></property>
+                                <property name="bg"></property>
+                                <property name="caption"></property>
+                                <property name="caption_visible">1</property>
+                                <property name="center_pane">0</property>
+                                <property name="close_button">1</property>
+                                <property name="context_help"></property>
+                                <property name="context_menu">1</property>
+                                <property name="default_pane">0</property>
+                                <property name="dock">Dock</property>
+                                <property name="dock_fixed">0</property>
+                                <property name="docking">Left</property>
+                                <property name="enabled">1</property>
+                                <property name="fg"></property>
+                                <property name="floatable">1</property>
+                                <property name="font"></property>
+                                <property name="gripper">0</property>
+                                <property name="hidden">0</property>
+                                <property name="id">wxID_ANY</property>
+                                <property name="max_size"></property>
+                                <property name="maximize_button">0</property>
+                                <property name="maximum_size"></property>
+                                <property name="min_size"></property>
+                                <property name="minimize_button">0</property>
+                                <property name="minimum_size"></property>
+                                <property name="moveable">1</property>
+                                <property name="name">m_separator</property>
+                                <property name="pane_border">1</property>
+                                <property name="pane_position"></property>
+                                <property name="pane_size"></property>
+                                <property name="permission">protected</property>
+                                <property name="pin_button">1</property>
+                                <property name="pos"></property>
+                                <property name="resize">Resizable</property>
+                                <property name="show">1</property>
+                                <property name="size"></property>
+                                <property name="style">wxLI_HORIZONTAL</property>
+                                <property name="subclass">; ; forward_declare</property>
+                                <property name="toolbar_pane">0</property>
+                                <property name="tooltip"></property>
+                                <property name="window_extra_style"></property>
+                                <property name="window_name"></property>
+                                <property name="window_style"></property>
+                            </object>
+                        </object>
+                        <object class="sizeritem" expanded="0">
+                            <property name="border">5</property>
+                            <property name="flag">wxBOTTOM|wxEXPAND</property>
+                            <property name="proportion">0</property>
+                            <object class="wxFlexGridSizer" expanded="0">
+                                <property name="cols">2</property>
+                                <property name="flexible_direction">wxBOTH</property>
+                                <property name="growablecols">0,1</property>
+                                <property name="growablerows"></property>
+                                <property name="hgap">0</property>
+                                <property name="minimum_size"></property>
+                                <property name="name">m_contentSizer</property>
+                                <property name="non_flexible_grow_mode">wxFLEX_GROWMODE_SPECIFIED</property>
+                                <property name="permission">protected</property>
+                                <property name="rows">1</property>
+                                <property name="vgap">0</property>
+                                <object class="sizeritem" expanded="0">
+                                    <property name="border">5</property>
+                                    <property name="flag">wxALL|wxEXPAND</property>
+                                    <property name="proportion">0</property>
+                                    <object class="wxRadioBox" expanded="0">
+                                        <property name="BottomDockable">1</property>
+                                        <property name="LeftDockable">1</property>
+                                        <property name="RightDockable">1</property>
+                                        <property name="TopDockable">1</property>
+                                        <property name="aui_layer"></property>
+                                        <property name="aui_name"></property>
+                                        <property name="aui_position"></property>
+                                        <property name="aui_row"></property>
+                                        <property name="best_size"></property>
+                                        <property name="bg"></property>
+                                        <property name="caption"></property>
+                                        <property name="caption_visible">1</property>
+                                        <property name="center_pane">0</property>
+                                        <property name="choices">&quot;Use the entire schematic&quot; &quot;Use the current page only&quot;</property>
+                                        <property name="close_button">1</property>
+                                        <property name="context_help"></property>
+                                        <property name="context_menu">1</property>
+                                        <property name="default_pane">0</property>
+                                        <property name="dock">Dock</property>
+                                        <property name="dock_fixed">0</property>
+                                        <property name="docking">Left</property>
+                                        <property name="enabled">1</property>
+                                        <property name="fg"></property>
+                                        <property name="floatable">1</property>
+                                        <property name="font"></property>
+                                        <property name="gripper">0</property>
+                                        <property name="hidden">0</property>
+                                        <property name="id">wxID_ANY</property>
+                                        <property name="label">Scope:</property>
+                                        <property name="majorDimension">1</property>
+                                        <property name="max_size"></property>
+                                        <property name="maximize_button">0</property>
+                                        <property name="maximum_size"></property>
+                                        <property name="min_size"></property>
+                                        <property name="minimize_button">0</property>
+                                        <property name="minimum_size"></property>
+                                        <property name="moveable">1</property>
+                                        <property name="name">m_rbScope</property>
+                                        <property name="pane_border">1</property>
+                                        <property name="pane_position"></property>
+                                        <property name="pane_size"></property>
+                                        <property name="permission">protected</property>
+                                        <property name="pin_button">1</property>
+                                        <property name="pos"></property>
+                                        <property name="resize">Resizable</property>
+                                        <property name="selection">0</property>
+                                        <property name="show">1</property>
+                                        <property name="size"></property>
+                                        <property name="style">wxRA_SPECIFY_COLS</property>
+                                        <property name="subclass">; forward_declare</property>
+                                        <property name="toolbar_pane">0</property>
+                                        <property name="tooltip"></property>
+                                        <property name="validator_data_type"></property>
+                                        <property name="validator_style">wxFILTER_NONE</property>
+                                        <property name="validator_type">wxDefaultValidator</property>
+                                        <property name="validator_variable"></property>
+                                        <property name="window_extra_style"></property>
+                                        <property name="window_name"></property>
+                                        <property name="window_style"></property>
+                                    </object>
+                                </object>
+                                <object class="sizeritem" expanded="0">
+                                    <property name="border">5</property>
+                                    <property name="flag">wxALL|wxEXPAND</property>
+                                    <property name="proportion">1</property>
+                                    <object class="wxStaticBoxSizer" expanded="0">
+                                        <property name="id">wxID_ANY</property>
+                                        <property name="label">Numbering:</property>
+                                        <property name="minimum_size"></property>
+                                        <property name="name">sbSizer2</property>
+                                        <property name="orient">wxVERTICAL</property>
+                                        <property name="parent">1</property>
+                                        <property name="permission">none</property>
+                                        <object class="sizeritem" expanded="0">
+                                            <property name="border">5</property>
+                                            <property name="flag">wxEXPAND</property>
+                                            <property name="proportion">1</property>
+                                            <object class="wxGridBagSizer" expanded="0">
+                                                <property name="empty_cell_size"></property>
+                                                <property name="flexible_direction">wxBOTH</property>
+                                                <property name="growablecols"></property>
+                                                <property name="growablerows"></property>
+                                                <property name="hgap">0</property>
+                                                <property name="minimum_size"></property>
+                                                <property name="name">gbSizer1</property>
+                                                <property name="non_flexible_grow_mode">wxFLEX_GROWMODE_SPECIFIED</property>
+                                                <property name="permission">none</property>
+                                                <property name="vgap">0</property>
+                                                <object class="gbsizeritem" expanded="0">
+                                                    <property name="border">2</property>
+                                                    <property name="colspan">1</property>
+                                                    <property name="column">0</property>
+                                                    <property name="flag">wxTOP</property>
+                                                    <property name="row">0</property>
+                                                    <property name="rowspan">1</property>
+                                                    <object class="wxRadioButton" expanded="0">
+                                                        <property name="BottomDockable">1</property>
+                                                        <property name="LeftDockable">1</property>
+                                                        <property name="RightDockable">1</property>
+                                                        <property name="TopDockable">1</property>
+                                                        <property name="aui_layer"></property>
+                                                        <property name="aui_name"></property>
+                                                        <property name="aui_position"></property>
+                                                        <property name="aui_row"></property>
+                                                        <property name="best_size"></property>
+                                                        <property name="bg"></property>
+                                                        <property name="caption"></property>
+                                                        <property name="caption_visible">1</property>
+                                                        <property name="center_pane">0</property>
+                                                        <property name="close_button">1</property>
+                                                        <property name="context_help"></property>
+                                                        <property name="context_menu">1</property>
+                                                        <property name="default_pane">0</property>
+                                                        <property name="dock">Dock</property>
+                                                        <property name="dock_fixed">0</property>
+                                                        <property name="docking">Left</property>
+                                                        <property name="enabled">1</property>
+                                                        <property name="fg"></property>
+                                                        <property name="floatable">1</property>
+                                                        <property name="font"></property>
+                                                        <property name="gripper">0</property>
+                                                        <property name="hidden">0</property>
+                                                        <property name="id">wxID_FIRST_FREE</property>
+                                                        <property name="label">Use first free number after:</property>
+                                                        <property name="max_size"></property>
+                                                        <property name="maximize_button">0</property>
+                                                        <property name="maximum_size"></property>
+                                                        <property name="min_size"></property>
+                                                        <property name="minimize_button">0</property>
+                                                        <property name="minimum_size"></property>
+                                                        <property name="moveable">1</property>
+                                                        <property name="name">m_rbFirstFree</property>
+                                                        <property name="pane_border">1</property>
+                                                        <property name="pane_position"></property>
+                                                        <property name="pane_size"></property>
+                                                        <property name="permission">protected</property>
+                                                        <property name="pin_button">1</property>
+                                                        <property name="pos"></property>
+                                                        <property name="resize">Resizable</property>
+                                                        <property name="show">1</property>
+                                                        <property name="size"></property>
+                                                        <property name="style">wxRB_GROUP</property>
+                                                        <property name="subclass">; forward_declare</property>
+                                                        <property name="toolbar_pane">0</property>
+                                                        <property name="tooltip"></property>
+                                                        <property name="validator_data_type"></property>
+                                                        <property name="validator_style">wxFILTER_NONE</property>
+                                                        <property name="validator_type">wxDefaultValidator</property>
+                                                        <property name="validator_variable"></property>
+                                                        <property name="value">0</property>
+                                                        <property name="window_extra_style"></property>
+                                                        <property name="window_name"></property>
+                                                        <property name="window_style"></property>
+                                                    </object>
+                                                </object>
+                                                <object class="gbsizeritem" expanded="0">
+                                                    <property name="border">2</property>
+                                                    <property name="colspan">1</property>
+                                                    <property name="column">1</property>
+                                                    <property name="flag">wxLEFT</property>
+                                                    <property name="row">0</property>
+                                                    <property name="rowspan">1</property>
+                                                    <object class="wxTextCtrl" expanded="0">
+                                                        <property name="BottomDockable">1</property>
+                                                        <property name="LeftDockable">1</property>
+                                                        <property name="RightDockable">1</property>
+                                                        <property name="TopDockable">1</property>
+                                                        <property name="aui_layer"></property>
+                                                        <property name="aui_name"></property>
+                                                        <property name="aui_position"></property>
+                                                        <property name="aui_row"></property>
+                                                        <property name="best_size"></property>
+                                                        <property name="bg"></property>
+                                                        <property name="caption"></property>
+                                                        <property name="caption_visible">1</property>
+                                                        <property name="center_pane">0</property>
+                                                        <property name="close_button">1</property>
+                                                        <property name="context_help"></property>
+                                                        <property name="context_menu">1</property>
+                                                        <property name="default_pane">0</property>
+                                                        <property name="dock">Dock</property>
+                                                        <property name="dock_fixed">0</property>
+                                                        <property name="docking">Left</property>
+                                                        <property name="enabled">1</property>
+                                                        <property name="fg"></property>
+                                                        <property name="floatable">1</property>
+                                                        <property name="font"></property>
+                                                        <property name="gripper">0</property>
+                                                        <property name="hidden">0</property>
+                                                        <property name="id">wxID_ANY</property>
+                                                        <property name="max_size"></property>
+                                                        <property name="maximize_button">0</property>
+                                                        <property name="maximum_size"></property>
+                                                        <property name="maxlength"></property>
+                                                        <property name="min_size"></property>
+                                                        <property name="minimize_button">0</property>
+                                                        <property name="minimum_size"></property>
+                                                        <property name="moveable">1</property>
+                                                        <property name="name">m_textNumberAfter</property>
+                                                        <property name="pane_border">1</property>
+                                                        <property name="pane_position"></property>
+                                                        <property name="pane_size"></property>
+                                                        <property name="permission">protected</property>
+                                                        <property name="pin_button">1</property>
+                                                        <property name="pos"></property>
+                                                        <property name="resize">Resizable</property>
+                                                        <property name="show">1</property>
+                                                        <property name="size">60,-1</property>
+                                                        <property name="style"></property>
+                                                        <property name="subclass">; forward_declare</property>
+                                                        <property name="toolbar_pane">0</property>
+                                                        <property name="tooltip"></property>
+                                                        <property name="validator_data_type"></property>
+                                                        <property name="validator_style">wxFILTER_NONE</property>
+                                                        <property name="validator_type">wxDefaultValidator</property>
+                                                        <property name="validator_variable"></property>
+                                                        <property name="value"></property>
+                                                        <property name="window_extra_style"></property>
+                                                        <property name="window_name"></property>
+                                                        <property name="window_style"></property>
+                                                    </object>
+                                                </object>
+                                                <object class="gbsizeritem" expanded="0">
+                                                    <property name="border">4</property>
+                                                    <property name="colspan">2</property>
+                                                    <property name="column">0</property>
+                                                    <property name="flag">wxBOTTOM|wxTOP</property>
+                                                    <property name="row">1</property>
+                                                    <property name="rowspan">1</property>
+                                                    <object class="wxRadioButton" expanded="0">
+                                                        <property name="BottomDockable">1</property>
+                                                        <property name="LeftDockable">1</property>
+                                                        <property name="RightDockable">1</property>
+                                                        <property name="TopDockable">1</property>
+                                                        <property name="aui_layer"></property>
+                                                        <property name="aui_name"></property>
+                                                        <property name="aui_position"></property>
+                                                        <property name="aui_row"></property>
+                                                        <property name="best_size"></property>
+                                                        <property name="bg"></property>
+                                                        <property name="caption"></property>
+                                                        <property name="caption_visible">1</property>
+                                                        <property name="center_pane">0</property>
+                                                        <property name="close_button">1</property>
+                                                        <property name="context_help"></property>
+                                                        <property name="context_menu">1</property>
+                                                        <property name="default_pane">0</property>
+                                                        <property name="dock">Dock</property>
+                                                        <property name="dock_fixed">0</property>
+                                                        <property name="docking">Left</property>
+                                                        <property name="enabled">1</property>
+                                                        <property name="fg"></property>
+                                                        <property name="floatable">1</property>
+                                                        <property name="font"></property>
+                                                        <property name="gripper">0</property>
+                                                        <property name="hidden">0</property>
+                                                        <property name="id">wxID_SHEET_X_100</property>
+                                                        <property name="label">First free after sheet number X 100</property>
+                                                        <property name="max_size"></property>
+                                                        <property name="maximize_button">0</property>
+                                                        <property name="maximum_size"></property>
+                                                        <property name="min_size"></property>
+                                                        <property name="minimize_button">0</property>
+                                                        <property name="minimum_size"></property>
+                                                        <property name="moveable">1</property>
+                                                        <property name="name">m_rbSheetX100</property>
+                                                        <property name="pane_border">1</property>
+                                                        <property name="pane_position"></property>
+                                                        <property name="pane_size"></property>
+                                                        <property name="permission">protected</property>
+                                                        <property name="pin_button">1</property>
+                                                        <property name="pos"></property>
+                                                        <property name="resize">Resizable</property>
+                                                        <property name="show">1</property>
+                                                        <property name="size"></property>
+                                                        <property name="style"></property>
+                                                        <property name="subclass">; forward_declare</property>
+                                                        <property name="toolbar_pane">0</property>
+                                                        <property name="tooltip"></property>
+                                                        <property name="validator_data_type"></property>
+                                                        <property name="validator_style">wxFILTER_NONE</property>
+                                                        <property name="validator_type">wxDefaultValidator</property>
+                                                        <property name="validator_variable"></property>
+                                                        <property name="value">0</property>
+                                                        <property name="window_extra_style"></property>
+                                                        <property name="window_name"></property>
+                                                        <property name="window_style"></property>
+                                                    </object>
+                                                </object>
+                                                <object class="gbsizeritem" expanded="0">
+                                                    <property name="border">2</property>
+                                                    <property name="colspan">2</property>
+                                                    <property name="column">0</property>
+                                                    <property name="flag">wxBOTTOM|wxTOP</property>
+                                                    <property name="row">2</property>
+                                                    <property name="rowspan">1</property>
+                                                    <object class="wxRadioButton" expanded="0">
+                                                        <property name="BottomDockable">1</property>
+                                                        <property name="LeftDockable">1</property>
+                                                        <property name="RightDockable">1</property>
+                                                        <property name="TopDockable">1</property>
+                                                        <property name="aui_layer"></property>
+                                                        <property name="aui_name"></property>
+                                                        <property name="aui_position"></property>
+                                                        <property name="aui_row"></property>
+                                                        <property name="best_size"></property>
+                                                        <property name="bg"></property>
+                                                        <property name="caption"></property>
+                                                        <property name="caption_visible">1</property>
+                                                        <property name="center_pane">0</property>
+                                                        <property name="close_button">1</property>
+                                                        <property name="context_help"></property>
+                                                        <property name="context_menu">1</property>
+                                                        <property name="default_pane">0</property>
+                                                        <property name="dock">Dock</property>
+                                                        <property name="dock_fixed">0</property>
+                                                        <property name="docking">Left</property>
+                                                        <property name="enabled">1</property>
+                                                        <property name="fg"></property>
+                                                        <property name="floatable">1</property>
+                                                        <property name="font"></property>
+                                                        <property name="gripper">0</property>
+                                                        <property name="hidden">0</property>
+                                                        <property name="id">wxID_SHEET_X_1000</property>
+                                                        <property name="label">First free after sheet number X 1000</property>
+                                                        <property name="max_size"></property>
+                                                        <property name="maximize_button">0</property>
+                                                        <property name="maximum_size"></property>
+                                                        <property name="min_size"></property>
+                                                        <property name="minimize_button">0</property>
+                                                        <property name="minimum_size"></property>
+                                                        <property name="moveable">1</property>
+                                                        <property name="name">m_rbSheetX1000</property>
+                                                        <property name="pane_border">1</property>
+                                                        <property name="pane_position"></property>
+                                                        <property name="pane_size"></property>
+                                                        <property name="permission">protected</property>
+                                                        <property name="pin_button">1</property>
+                                                        <property name="pos"></property>
+                                                        <property name="resize">Resizable</property>
+                                                        <property name="show">1</property>
+                                                        <property name="size"></property>
+                                                        <property name="style"></property>
+                                                        <property name="subclass">; forward_declare</property>
+                                                        <property name="toolbar_pane">0</property>
+                                                        <property name="tooltip"></property>
+                                                        <property name="validator_data_type"></property>
+                                                        <property name="validator_style">wxFILTER_NONE</property>
+                                                        <property name="validator_type">wxDefaultValidator</property>
+                                                        <property name="validator_variable"></property>
+                                                        <property name="value">0</property>
+                                                        <property name="window_extra_style"></property>
+                                                        <property name="window_name"></property>
+                                                        <property name="window_style"></property>
+                                                    </object>
+                                                </object>
+                                            </object>
+                                        </object>
+                                    </object>
+                                </object>
+                            </object>
+                        </object>
+                    </object>
+                </object>
+            </object>
+        </object>
+    </object>
+</wxFormBuilder_Project>
diff --git a/eeschema/dialogs/panel_eeschema_annotation_options_base.h b/eeschema/dialogs/panel_eeschema_annotation_options_base.h
new file mode 100644
index 000000000..9d4e4a4dd
--- /dev/null
+++ b/eeschema/dialogs/panel_eeschema_annotation_options_base.h
@@ -0,0 +1,59 @@
+///////////////////////////////////////////////////////////////////////////
+// C++ code generated with wxFormBuilder (version Sep 10 2019)
+// http://www.wxformbuilder.org/
+//
+// PLEASE DO *NOT* EDIT THIS FILE!
+///////////////////////////////////////////////////////////////////////////
+
+#pragma once
+
+#include <wx/artprov.h>
+#include <wx/xrc/xmlres.h>
+#include <wx/intl.h>
+#include <wx/string.h>
+#include <wx/checkbox.h>
+#include <wx/gdicmn.h>
+#include <wx/font.h>
+#include <wx/colour.h>
+#include <wx/settings.h>
+#include <wx/statline.h>
+#include <wx/radiobox.h>
+#include <wx/radiobut.h>
+#include <wx/textctrl.h>
+#include <wx/gbsizer.h>
+#include <wx/sizer.h>
+#include <wx/statbox.h>
+#include <wx/panel.h>
+
+///////////////////////////////////////////////////////////////////////////
+
+#define wxID_FIRST_FREE 1000
+#define wxID_SHEET_X_100 1001
+#define wxID_SHEET_X_1000 1002
+
+///////////////////////////////////////////////////////////////////////////////
+/// Class PANEL_EESCHEMA_ANNOTATION_OPTIONS_BASE
+///////////////////////////////////////////////////////////////////////////////
+class PANEL_EESCHEMA_ANNOTATION_OPTIONS_BASE : public wxPanel
+{
+	private:
+
+	protected:
+		wxCheckBox* m_cbEnabled;
+		wxStaticLine* m_separator;
+		wxFlexGridSizer* m_contentSizer;
+		wxRadioBox* m_rbScope;
+		wxRadioButton* m_rbFirstFree;
+		wxTextCtrl* m_textNumberAfter;
+		wxRadioButton* m_rbSheetX100;
+		wxRadioButton* m_rbSheetX1000;
+
+		// Virtual event handlers, overide them in your derived class
+		virtual void OnEnableClick( wxCommandEvent& event ) { event.Skip(); }
+
+
+	public:
+
+		PANEL_EESCHEMA_ANNOTATION_OPTIONS_BASE( wxWindow* parent, wxWindowID id = wxID_ANY, const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxSize( 500,156 ), long style = wxTAB_TRAVERSAL, const wxString& name = wxEmptyString );
+		~PANEL_EESCHEMA_ANNOTATION_OPTIONS_BASE();
+};
diff --git a/eeschema/eeschema_config.cpp b/eeschema/eeschema_config.cpp
index 527a40262..f17c58b5c 100644
--- a/eeschema/eeschema_config.cpp
+++ b/eeschema/eeschema_config.cpp
@@ -21,31 +21,32 @@
  * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
  */
 
-#include <fctsys.h>
-#include <pgm_base.h>
-#include <kiface_i.h>
+#include "eeschema_id.h"
+#include "sch_junction.h"
+#include <class_library.h>
 #include <confirm.h>
+#include <dialogs/panel_eeschema_annotation_options.h>
+#include <dialogs/panel_eeschema_display_options.h>
+#include <dialogs/panel_eeschema_settings.h>
+#include <dialogs/panel_eeschema_template_fieldnames.h>
+#include <dialogs/panel_libedit_settings.h>
+#include <eeschema_config.h>
+#include <fctsys.h>
 #include <gestfich.h>
+#include <kiface_i.h>
+#include <lib_edit_frame.h>
+#include <panel_display_options.h>
+#include <panel_hotkeys_editor.h>
+#include <pgm_base.h>
 #include <sch_edit_frame.h>
+#include <sch_painter.h>
 #include <sch_sheet.h>
-#include <lib_edit_frame.h>
-#include <eeschema_config.h>
-#include <ws_data_model.h>
-#include <class_library.h>
 #include <symbol_lib_table.h>
-#include <wildcards_and_files_ext.h>
 #include <widgets/paged_dialog.h>
-#include <dialogs/panel_eeschema_template_fieldnames.h>
-#include <dialogs/panel_eeschema_settings.h>
-#include <dialogs/panel_eeschema_display_options.h>
-#include <panel_display_options.h>
-#include <panel_hotkeys_editor.h>
-#include <widgets/widget_eeschema_color_config.h>
 #include <widgets/symbol_tree_pane.h>
-#include <dialogs/panel_libedit_settings.h>
-#include <sch_painter.h>
-#include "sch_junction.h"
-#include "eeschema_id.h"
+#include <widgets/widget_eeschema_color_config.h>
+#include <wildcards_and_files_ext.h>
+#include <ws_data_model.h>
 
 static int s_defaultBusThickness = DEFAULTBUSTHICKNESS;
 static int s_defaultWireThickness  = DEFAULTDRAWLINETHICKNESS;
@@ -126,13 +127,14 @@ void SCH_EDIT_FRAME::InstallPreferences( PAGED_DIALOG* aParent,
     book->AddSubPage( new PANEL_EESCHEMA_COLOR_CONFIG( this, book ), _( "Colors" ) );
     book->AddSubPage( new PANEL_EESCHEMA_TEMPLATE_FIELDNAMES( this, book ),
                       _( "Field Name Templates" ) );
+    book->AddSubPage(
+            new PANEL_EESCHEMA_ANNOTATION_OPTIONS( this, book ), _( "Annotation Options" ) );
 
     aHotkeysPanel->AddHotKeys( GetToolManager() );
 }
 
 
 PARAM_CFG_ARRAY& SCH_EDIT_FRAME::GetProjectFileParameters()
-
 {
     if( !m_projectFileParams.empty() )
         return m_projectFileParams;
@@ -153,6 +155,15 @@ PARAM_CFG_ARRAY& SCH_EDIT_FRAME::GetProjectFileParameters()
     m_projectFileParams.push_back( new PARAM_CFG_BOOL( wxT( "SpiceAjustPassiveValues" ),
                                             &m_spiceAjustPassiveValues, false ) );
 
+    m_projectFileParams.push_back(
+            new PARAM_CFG_BOOL( wxT( "AutoAnnotationEnabled" ), &m_autoAnnotateEnabled ) );
+    m_projectFileParams.push_back( new PARAM_CFG_INT(
+            wxT( "AutoAnnotationScope" ), &m_autoAnnotateScopeOption, 0, 0, 1 ) );
+    m_projectFileParams.push_back( new PARAM_CFG_INT( wxT( "AutoAnnotationAlgo" ),
+            reinterpret_cast<int*>( &m_autoAnnotateAlgoOption ), 0, 0, 2 ) );
+    m_projectFileParams.push_back( new PARAM_CFG_INT(
+            wxT( "AutoAnnotationFirstFreeNum" ), &m_autoAnnotateFirstFreeNumOption, 0 ) );
+
     m_projectFileParams.push_back( new PARAM_CFG_INT( wxT( "LabSize" ),
                                             &s_defaultTextSize, DEFAULT_SIZE_TEXT, 5, 1000 ) );
 
diff --git a/eeschema/sch_component.cpp b/eeschema/sch_component.cpp
index c4dc51085..12890c2b0 100644
--- a/eeschema/sch_component.cpp
+++ b/eeschema/sch_component.cpp
@@ -33,16 +33,18 @@
 #include <msgpanel.h>
 #include <bitmaps.h>
 
-#include <general.h>
 #include <class_library.h>
-#include <lib_rectangle.h>
+#include <general.h>
+#include <lib_item.h>
 #include <lib_pin.h>
+#include <lib_rectangle.h>
 #include <lib_text.h>
+#include <netlist_object.h>
+#include <refdes_utils.h>
 #include <sch_component.h>
+#include <sch_reference_list.h>
 #include <sch_sheet.h>
 #include <sch_sheet_path.h>
-#include <netlist_object.h>
-#include <lib_item.h>
 #include <symbol_lib_table.h>
 
 #include <dialogs/dialog_schematic_find.h>
@@ -55,7 +57,6 @@
 
 #include <trace_helpers.h>
 
-
 /**
  * Function toUTFTildaText
  * convert a wxString to UTF8 and replace any control characters with a ~,
@@ -718,6 +719,63 @@ void SCH_COMPONENT::SetRef( const SCH_SHEET_PATH* sheet, const wxString& ref )
     m_isInNetlist = ! ref.StartsWith( wxT( "#" ) );
 }
 
+void SCH_COMPONENT::Annotate(
+        SCH_SHEET_PATH* aSheet, bool aEntireSchematic, ANNOTATE_OPTION_T aAlgoOption )
+{
+    SCH_REFERENCE_LIST references;
+    wxString           prefix = GetPrefix();
+
+    // Build the sheet list.
+    SCH_SHEET_LIST sheets( g_RootSheet );
+
+    if( aEntireSchematic && aAlgoOption == INCREMENTAL_BY_REF )
+        sheets.GetComponents( references, false );
+    else
+        aSheet->GetComponents( references );
+
+    int lastRefNum = 0;
+    int interval = 0;
+
+    if( aAlgoOption != INCREMENTAL_BY_REF )
+        interval = ( aAlgoOption == SHEET_NUMBER_X_100 ? 100 : 1000 ) * aSheet->GetPageNumber();
+
+    lastRefNum = interval;
+
+    references.SortByReferenceOnly();
+
+    for( unsigned i = 0; i < references.GetCount(); i++ )
+    {
+        if( references[i].GetComp()->GetPrefix() == prefix
+                && references[i].GetComp()->m_unit == m_unit )
+        {
+            long refNum;
+
+            references[i].Split();
+
+            if( !references[i].GetRefNumber().ToLong( &refNum ) )
+                continue;
+
+            if( refNum < interval )
+                continue;
+
+            // Check for a gap.
+            if( ( refNum - interval ) - ( lastRefNum - interval ) > 1 )
+                break;
+
+            if( refNum > lastRefNum )
+            {
+                lastRefNum = refNum;
+            }
+        }
+    }
+
+    if( lastRefNum < interval )
+        lastRefNum = interval;
+    else
+        lastRefNum++;
+
+    SetRef( aSheet, prefix << lastRefNum );
+}
 
 bool SCH_COMPONENT::IsAnnotated( const SCH_SHEET_PATH* aSheet )
 {
@@ -1973,3 +2031,17 @@ bool SCH_COMPONENT::ClearAllHighlightFlags()
 
     return retVal;
 }
+
+bool SCH_COMPONENT::CompareRefDes( SCH_COMPONENT* aOther )
+{
+    int ii =
+            UTIL::RefDesStringCompare( GetRef( g_CurrentSheet ), aOther->GetRef( g_CurrentSheet ) );
+
+    if( ii == 0 )
+        ii = GetField( VALUE )->GetText().CmpNoCase( aOther->GetField( VALUE )->GetText() );
+
+    if( ii == 0 )
+        ii = GetUnit() - aOther->GetUnit();
+
+    return ii < 0;
+}
diff --git a/eeschema/sch_component.h b/eeschema/sch_component.h
index b64938a34..bf5c501db 100644
--- a/eeschema/sch_component.h
+++ b/eeschema/sch_component.h
@@ -29,14 +29,15 @@
 
 #include <lib_id.h>
 
-#include <sch_field.h>
-#include <transform.h>
 #include <general.h>
-#include <vector>
-#include <set>
 #include <lib_item.h>
-#include <sch_pin.h>
 #include <sch_base_frame.h>
+#include <sch_edit_frame.h>
+#include <sch_field.h>
+#include <sch_pin.h>
+#include <set>
+#include <transform.h>
+#include <vector>
 
 class SCH_SCREEN;
 class SCH_SHEET_PATH;
@@ -508,6 +509,14 @@ public:
      */
     void SetRef( const SCH_SHEET_PATH* aSheet, const wxString& aReference );
 
+    /**
+     * Annotates the component with first available reference number for its reference prefix on a given sheet.
+     * @param aSheet is the sheet path to annotate the symbol on
+     * @param aEntireSchematic defines whether the scope of annotation is just the current page or the whole schematic
+     * @param aAlgoOption is the algorithm option used to annotate the symbol
+     */
+    void Annotate( SCH_SHEET_PATH* aSheet, bool aEntireSchematic, ANNOTATE_OPTION_T aAlgoOption );
+
     /**
      * Checks if the component has a valid annotation (reference) for the given sheet path
      * @param aSheet is the sheet path to test
@@ -643,6 +652,8 @@ public:
 
     void HighlightPin( LIB_PIN* aPin );
 
+    bool CompareRefDes( SCH_COMPONENT* aOther );
+
 private:
     bool doIsConnected( const wxPoint& aPosition ) const override;
 };
diff --git a/eeschema/sch_edit_frame.h b/eeschema/sch_edit_frame.h
index c7982050c..7ed8a8206 100644
--- a/eeschema/sch_edit_frame.h
+++ b/eeschema/sch_edit_frame.h
@@ -103,6 +103,11 @@ private:
     wxString                m_DefaultSchematicFileName;
     wxString                m_SelectedNetName;
 
+    bool              m_autoAnnotateEnabled;
+    int               m_autoAnnotateScopeOption;
+    ANNOTATE_OPTION_T m_autoAnnotateAlgoOption;
+    int               m_autoAnnotateFirstFreeNumOption;
+
     PARAM_CFG_ARRAY         m_projectFileParams;
     PARAM_CFG_ARRAY         m_configSettings;
     ERC_SETTINGS            m_ercSettings;
@@ -214,6 +219,42 @@ public:
     bool GetSpiceAjustPassiveValues() const { return m_spiceAjustPassiveValues; }
     void SetSpiceAdjustPassiveValues( bool aEnable ) { m_spiceAjustPassiveValues = aEnable; }
 
+    bool IsAutoAnnotationEnabled()
+    {
+        return m_autoAnnotateEnabled;
+    }
+    void SetIsAutoAnnotationEnabled( bool value )
+    {
+        m_autoAnnotateEnabled = value;
+    }
+
+    int GetAutoAnnotationScopeOption()
+    {
+        return m_autoAnnotateScopeOption;
+    }
+    void SetAutoAnnotationScopeOption( int value )
+    {
+        m_autoAnnotateScopeOption = value;
+    }
+
+    ANNOTATE_OPTION_T GetAutoAnnotationAlgoOption()
+    {
+        return m_autoAnnotateAlgoOption;
+    }
+    void SetAutoAnnotationAlgoOption( ANNOTATE_OPTION_T value )
+    {
+        m_autoAnnotateAlgoOption = value;
+    }
+
+    int GetAutoAnnotationFirstFreeNumOption()
+    {
+        return m_autoAnnotateFirstFreeNumOption;
+    }
+    void SetAutoAnnotationFirstFreeNumOption( int value )
+    {
+        m_autoAnnotateFirstFreeNumOption = value;
+    }
+
     /// accessor to the destination directory to use when generating plot files.
     const wxString& GetPlotDirectoryName() const { return m_plotDirectoryName; }
     void SetPlotDirectoryName( const wxString& aDirName ) { m_plotDirectoryName = aDirName; }
diff --git a/eeschema/tools/ee_selection.cpp b/eeschema/tools/ee_selection.cpp
index 84f418afb..7efa4f5a8 100644
--- a/eeschema/tools/ee_selection.cpp
+++ b/eeschema/tools/ee_selection.cpp
@@ -22,11 +22,13 @@
  * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
  */
 
-#include <limits>
 #include <functional>
-#include <tools/ee_selection.h>
-#include <sch_item.h>
 #include <lib_item.h>
+#include <limits>
+#include <refdes_utils.h>
+#include <sch_component.h>
+#include <sch_item.h>
+#include <tools/ee_selection.h>
 
 EDA_ITEM* EE_SELECTION::GetTopLeftItem( bool onlyModules ) const
 {
@@ -56,3 +58,18 @@ EDA_ITEM* EE_SELECTION::GetTopLeftItem( bool onlyModules ) const
 
     return static_cast<EDA_ITEM*>( topLeftItem );
 }
+
+void EE_SELECTION::SortComponentsByRef()
+{
+    std::sort( begin(), end(), []( EDA_ITEM* left, EDA_ITEM* right ) {
+        if( left->Type() == SCH_COMPONENT_T && right->Type() == SCH_COMPONENT_T )
+        {
+            SCH_COMPONENT* leftComponent = static_cast<SCH_COMPONENT*>( left );
+            SCH_COMPONENT* rightComponent = static_cast<SCH_COMPONENT*>( right );
+
+            return leftComponent->CompareRefDes( rightComponent );
+        }
+
+        return false;
+    } );
+}
diff --git a/eeschema/tools/ee_selection.h b/eeschema/tools/ee_selection.h
index 3b8d2f59d..da4a71c9a 100644
--- a/eeschema/tools/ee_selection.h
+++ b/eeschema/tools/ee_selection.h
@@ -32,6 +32,11 @@ class EE_SELECTION : public SELECTION
 {
 public:
     EDA_ITEM* GetTopLeftItem( bool onlyModules = false ) const override;
+
+    /**
+     * Sorts this selection by reference number of each SCH_COMPONENT (if any are present).
+     */
+    void SortComponentsByRef();
 };
 
 #endif  //  EE_SELECTION_H
\ No newline at end of file
diff --git a/eeschema/tools/sch_drawing_tools.cpp b/eeschema/tools/sch_drawing_tools.cpp
index e2945d54a..234c00c58 100644
--- a/eeschema/tools/sch_drawing_tools.cpp
+++ b/eeschema/tools/sch_drawing_tools.cpp
@@ -188,6 +188,10 @@ int SCH_DRAWING_TOOLS::PlaceComponent(  const TOOL_EVENT& aEvent  )
             }
             else
             {
+                if( m_frame->IsAutoAnnotationEnabled() )
+                    component->Annotate( g_CurrentSheet, m_frame->GetAutoAnnotationScopeOption(),
+                            m_frame->GetAutoAnnotationAlgoOption() );
+
                 m_frame->AddItemToScreenAndUndoList( component );
                 component = nullptr;
 
@@ -786,7 +790,7 @@ int SCH_DRAWING_TOOLS::DrawSheet( const TOOL_EVENT& aEvent )
                 m_frame->AddItemToScreenAndUndoList( sheet );
                 m_selectionTool->AddItemToSel( sheet );
             }
-            else 
+            else
             {
                 delete sheet;
             }
diff --git a/eeschema/tools/sch_edit_tool.cpp b/eeschema/tools/sch_edit_tool.cpp
index c512240a1..9d91d5a4a 100644
--- a/eeschema/tools/sch_edit_tool.cpp
+++ b/eeschema/tools/sch_edit_tool.cpp
@@ -716,7 +716,9 @@ static KICAD_T duplicatableItems[] =
 
 int SCH_EDIT_TOOL::Duplicate( const TOOL_EVENT& aEvent )
 {
-    EE_SELECTION& selection = m_selectionTool->RequestSelection( duplicatableItems );
+    // Selection has to be copied here because `SortComponentsByRef` invalidates
+    // some iterators in another object.
+    EE_SELECTION selection = m_selectionTool->RequestSelection( duplicatableItems );
 
     if( selection.GetSize() == 0 )
         return 0;
@@ -732,6 +734,9 @@ int SCH_EDIT_TOOL::Duplicate( const TOOL_EVENT& aEvent )
     bool copiedSheets = false;
     SCH_SHEET_LIST initial_sheetpathList( g_RootSheet );
 
+    if( m_frame->IsAutoAnnotationEnabled() )
+        selection.SortComponentsByRef();
+
     for( unsigned ii = 0; ii < selection.GetSize(); ++ii )
     {
         SCH_ITEM* oldItem = static_cast<SCH_ITEM*>( selection.GetItem( ii ) );
@@ -777,7 +782,16 @@ int SCH_EDIT_TOOL::Duplicate( const TOOL_EVENT& aEvent )
             SCH_COMPONENT* component = (SCH_COMPONENT*) newItem;
 
             component->SetTimeStamp( GetNewTimeStamp() );
-            component->ClearAnnotation( NULL );
+
+            if( m_frame->IsAutoAnnotationEnabled() )
+            {
+                component->Annotate( g_CurrentSheet, m_frame->GetAutoAnnotationScopeOption(),
+                        m_frame->GetAutoAnnotationAlgoOption() );
+            }
+            else
+            {
+                component->ClearAnnotation( NULL );
+            }
 
             component->SetParent( m_frame->GetScreen() );
             m_frame->AddToScreen( component );
diff --git a/eeschema/tools/sch_editor_control.cpp b/eeschema/tools/sch_editor_control.cpp
index 23cb07db3..ad2148679 100644
--- a/eeschema/tools/sch_editor_control.cpp
+++ b/eeschema/tools/sch_editor_control.cpp
@@ -22,36 +22,37 @@
  * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
  */
 
+#include <advanced_config.h>
+#include <class_library.h>
+#include <confirm.h>
+#include <connection_graph.h>
+#include <dialogs/dialog_fields_editor_global.h>
+#include <dialogs/dialog_page_settings.h>
+#include <dialogs/dialog_paste_special.h>
+#include <eeschema_id.h>
+#include <erc.h>
 #include <fctsys.h>
+#include <invoke_sch_dialog.h>
 #include <kiway.h>
-#include <sch_view.h>
+#include <netlist_exporters/netlist_exporter_pspice.h>
+#include <netlist_object.h>
+#include <refdes_utils.h>
 #include <sch_edit_frame.h>
-#include <sch_sheet.h>
+#include <sch_legacy_plugin.h>
 #include <sch_line.h>
-#include <connection_graph.h>
-#include <erc.h>
-#include <eeschema_id.h>
-#include <netlist_object.h>
-#include <tool/tool_manager.h>
+#include <sch_painter.h>
+#include <sch_sheet.h>
+#include <sch_view.h>
+#include <sim/sim_plot_frame.h>
+#include <simulation_cursors.h>
+#include <status_popup.h>
 #include <tool/picker_tool.h>
+#include <tool/tool_manager.h>
 #include <tools/ee_actions.h>
-#include <tools/sch_editor_control.h>
 #include <tools/ee_selection.h>
 #include <tools/ee_selection_tool.h>
-#include <advanced_config.h>
-#include <simulation_cursors.h>
-#include <sim/sim_plot_frame.h>
-#include <sch_legacy_plugin.h>
-#include <class_library.h>
-#include <confirm.h>
-#include <sch_painter.h>
-#include <status_popup.h>
+#include <tools/sch_editor_control.h>
 #include <ws_proxy_undo_item.h>
-#include <dialogs/dialog_page_settings.h>
-#include <dialogs/dialog_fields_editor_global.h>
-#include <invoke_sch_dialog.h>
-#include <dialogs/dialog_paste_special.h>
-#include <netlist_exporters/netlist_exporter_pspice.h>
 
 int SCH_EDITOR_CONTROL::New( const TOOL_EVENT& aEvent )
 {
@@ -871,7 +872,9 @@ int SCH_EDITOR_CONTROL::Redo( const TOOL_EVENT& aEvent )
 bool SCH_EDITOR_CONTROL::doCopy()
 {
     EE_SELECTION_TOOL* selTool = m_toolMgr->GetTool<EE_SELECTION_TOOL>();
-    EE_SELECTION&      selection = selTool->RequestSelection();
+    // Selection has to be copied here because `SortComponentsByRef` invalidates
+    // some iterators in another object.
+    EE_SELECTION selection = selTool->RequestSelection();
 
     if( !selection.GetSize() )
         return false;
@@ -890,6 +893,9 @@ bool SCH_EDITOR_CONTROL::doCopy()
     STRING_FORMATTER formatter;
     SCH_LEGACY_PLUGIN plugin;
 
+    if( m_frame->IsAutoAnnotationEnabled() )
+        selection.SortComponentsByRef();
+
     plugin.Format( &selection, &formatter );
 
     return m_toolMgr->SaveClipboard( formatter.GetString() );
@@ -1057,13 +1063,19 @@ int SCH_EDITOR_CONTROL::Paste( const TOOL_EVENT& aEvent )
         {
             SCH_COMPONENT* component = (SCH_COMPONENT*) item;
 
-            if( dropAnnotations )
+            if( dropAnnotations || m_frame->IsAutoAnnotationEnabled() )
             {
+                // preserve the selected unit
+                int unit = component->GetUnit();
+
+                if( m_frame->IsAutoAnnotationEnabled() )
+                    component->Annotate( g_CurrentSheet, m_frame->GetAutoAnnotationScopeOption(),
+                            m_frame->GetAutoAnnotationAlgoOption() );
+                else
+                    component->ClearAnnotation( nullptr );
+
                 component->SetTimeStamp( GetNewTimeStamp() );
 
-                // clear the annotation, but preserve the selected unit
-                int unit = component->GetUnit();
-                component->ClearAnnotation( nullptr );
                 component->SetUnit( unit );
             }
 

Follow ups

References